1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.googlejavaformat.intellij; 18 19 import com.google.googlejavaformat.java.JavaFormatterOptions; 20 import com.intellij.openapi.components.PersistentStateComponent; 21 import com.intellij.openapi.components.ServiceManager; 22 import com.intellij.openapi.components.State; 23 import com.intellij.openapi.components.Storage; 24 import com.intellij.openapi.project.Project; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26 import org.jetbrains.annotations.NotNull; 27 28 @State( 29 name = "GoogleJavaFormatSettings", 30 storages = {@Storage("google-java-format.xml")}) 31 class GoogleJavaFormatSettings implements PersistentStateComponent<GoogleJavaFormatSettings.State> { 32 33 private State state = new State(); 34 getInstance(Project project)35 static GoogleJavaFormatSettings getInstance(Project project) { 36 return ServiceManager.getService(project, GoogleJavaFormatSettings.class); 37 } 38 39 @Nullable 40 @Override getState()41 public State getState() { 42 return state; 43 } 44 45 @Override loadState(@otNull State state)46 public void loadState(@NotNull State state) { 47 this.state = state; 48 } 49 isEnabled()50 boolean isEnabled() { 51 return state.enabled.equals(EnabledState.ENABLED); 52 } 53 setEnabled(boolean enabled)54 void setEnabled(boolean enabled) { 55 setEnabled(enabled ? EnabledState.ENABLED : EnabledState.DISABLED); 56 } 57 setEnabled(EnabledState enabled)58 void setEnabled(EnabledState enabled) { 59 state.enabled = enabled; 60 } 61 isUninitialized()62 boolean isUninitialized() { 63 return state.enabled.equals(EnabledState.UNKNOWN); 64 } 65 getStyle()66 JavaFormatterOptions.Style getStyle() { 67 return state.style; 68 } 69 setStyle(JavaFormatterOptions.Style style)70 void setStyle(JavaFormatterOptions.Style style) { 71 state.style = style; 72 } 73 74 enum EnabledState { 75 UNKNOWN, 76 ENABLED, 77 DISABLED 78 } 79 80 static class State { 81 82 private EnabledState enabled = EnabledState.UNKNOWN; 83 public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE; 84 85 // enabled used to be a boolean so we use bean property methods for backwards compatibility setEnabled(@ullable String enabledStr)86 public void setEnabled(@Nullable String enabledStr) { 87 if (enabledStr == null) { 88 enabled = EnabledState.UNKNOWN; 89 } else if (Boolean.parseBoolean(enabledStr)) { 90 enabled = EnabledState.ENABLED; 91 } else { 92 enabled = EnabledState.DISABLED; 93 } 94 } 95 getEnabled()96 public String getEnabled() { 97 switch (enabled) { 98 case ENABLED: 99 return "true"; 100 case DISABLED: 101 return "false"; 102 default: 103 return null; 104 } 105 } 106 } 107 } 108