55
66package com .microsoft .intellij .ui ;
77
8-
98import com .azure .core .management .AzureEnvironment ;
109import com .intellij .icons .AllIcons ;
1110import com .intellij .openapi .project .Project ;
1413import com .intellij .ui .SimpleListCellRenderer ;
1514import com .intellij .util .ui .UIUtil ;
1615import com .microsoft .azure .toolkit .ide .common .store .AzureConfigInitializer ;
17- import com .microsoft .azure .toolkit .intellij .common .AzureComboBox ;
1816import com .microsoft .azure .toolkit .intellij .connector .Password ;
19- import com .microsoft .azure .toolkit .intellij .connector .database .component .PasswordSaveComboBox ;
2017import com .microsoft .azure .toolkit .lib .Azure ;
2118import com .microsoft .azure .toolkit .lib .AzureConfiguration ;
2219import com .microsoft .azure .toolkit .lib .auth .AzureCloud ;
2926import lombok .extern .slf4j .Slf4j ;
3027import org .apache .commons .lang3 .ObjectUtils ;
3128import org .apache .commons .lang3 .StringUtils ;
32- import org .jetbrains .annotations .NotNull ;
3329
3430import javax .annotation .Nonnull ;
3531import javax .swing .*;
3632import java .awt .*;
3733import java .awt .event .ItemEvent ;
3834import java .util .Objects ;
35+ import java .util .Optional ;
3936
4037import static com .microsoft .azure .toolkit .intellij .common .AzureBundle .message ;
4138import static com .microsoft .azuretools .telemetry .TelemetryConstants .ACCOUNT ;
4239import static com .microsoft .azuretools .telemetry .TelemetryConstants .SIGNOUT ;
4340
44-
4541@ Slf4j
4642public class AzurePanel implements AzureAbstractConfigurablePanel {
4743 private static final String DISPLAY_NAME = "Azure" ;
4844 private JPanel contentPane ;
4945 private JCheckBox allowTelemetryCheckBox ;
5046 private JTextPane allowTelemetryComment ;
5147 private JComboBox <AzureEnvironment > azureEnvironmentComboBox ;
52- private PasswordSaveComboBox savePasswordComboBox ;
48+ private JComboBox < Password . SaveType > savePasswordComboBox ;
5349 private FunctionCoreToolsCombobox funcCoreToolsPath ;
5450 private JLabel azureEnvDesc ;
5551
56- private AzureConfiguration originalData ;
57- private Project project ;
52+ private AzureConfiguration originalConfig ;
53+ private final Project project ;
5854
5955 public AzurePanel (Project project ) {
6056 this .project = project ;
@@ -67,14 +63,22 @@ public void init() {
6763 }
6864 Messages .configureMessagePaneUi (allowTelemetryComment , message ("settings.root.telemetry.notice" ));
6965 allowTelemetryComment .setForeground (UIUtil .getContextHelpForeground ());
70- final ComboBoxModel <AzureEnvironment > model = new DefaultComboBoxModel <>(Azure .az (AzureCloud .class ).list ().toArray (new AzureEnvironment [0 ]));
71- azureEnvironmentComboBox .setModel (model );
66+ final ComboBoxModel <AzureEnvironment > envModel = new DefaultComboBoxModel <>(Azure .az (AzureCloud .class ).list ().toArray (new AzureEnvironment [0 ]));
67+ azureEnvironmentComboBox .setModel (envModel );
7268 azureEnvironmentComboBox .setRenderer (new SimpleListCellRenderer <>() {
7369 @ Override
74- public void customize (@ NotNull JList list , AzureEnvironment value , int index , boolean selected , boolean hasFocus ) {
70+ public void customize (@ Nonnull JList list , AzureEnvironment value , int index , boolean selected , boolean hasFocus ) {
7571 setText (azureEnvironmentDisplayString (value ));
7672 }
7773 });
74+ final ComboBoxModel <Password .SaveType > saveTypeModel = new DefaultComboBoxModel <>(Password .SaveType .values ());
75+ savePasswordComboBox .setModel (saveTypeModel );
76+ savePasswordComboBox .setRenderer (new SimpleListCellRenderer <>() {
77+ @ Override
78+ public void customize (@ Nonnull JList <? extends Password .SaveType > list , Password .SaveType value , int index , boolean selected , boolean hasFocus ) {
79+ setText (value .title ());
80+ }
81+ });
7882 azureEnvDesc .setForeground (UIUtil .getContextHelpForeground ());
7983 azureEnvDesc .setMaximumSize (new Dimension ());
8084 azureEnvironmentComboBox .addItemListener (e -> {
@@ -85,34 +89,33 @@ public void customize(@NotNull JList list, AzureEnvironment value, int index, bo
8589
8690 displayDescriptionForAzureEnv ();
8791
88- setData (Azure .az ().config ());
92+ final AzureConfiguration config = Azure .az ().config ();
93+ if (Objects .isNull (config .getDatabasePasswordSaveType ())) {
94+ config .setDatabasePasswordSaveType (Password .SaveType .UNTIL_RESTART .name ());
95+ }
96+ setData (config );
8997 }
9098
9199 public void setData (AzureConfiguration config ) {
92- if (StringUtils .isNotBlank (config .getFunctionCoreToolsPath ())) {
93- //ignore
94- funcCoreToolsPath .setValue (config .getFunctionCoreToolsPath ());
100+ this .originalConfig = config ;
101+ final AzureEnvironment oldEnv = ObjectUtils .firstNonNull (AzureEnvironmentUtils .stringToAzureEnvironment (config .getCloud ()), AzureEnvironment .AZURE );
102+ final String oldPasswordSaveType = config .getDatabasePasswordSaveType ();
103+ final Boolean oldTelemetryEnabled = config .getTelemetryEnabled ();
104+ final String oldFuncCoreToolsPath = config .getFunctionCoreToolsPath ();
105+ azureEnvironmentComboBox .setSelectedItem (oldEnv );
106+ savePasswordComboBox .setSelectedItem (Optional .ofNullable (oldPasswordSaveType ).map (Password .SaveType ::valueOf ).orElse (Password .SaveType .UNTIL_RESTART ));
107+ if (StringUtils .isNotBlank (oldFuncCoreToolsPath )) {
108+ funcCoreToolsPath .setValue (oldFuncCoreToolsPath );
95109 }
96- final String passwordSaveType = config .getDatabasePasswordSaveType ();
97- if (Objects .nonNull (passwordSaveType )) {
98- savePasswordComboBox .setValue (new AzureComboBox .ItemReference <>(passwordSaveType , e -> e .name ().toLowerCase ()));
99- } else {
100- savePasswordComboBox .setValue (Password .SaveType .UNTIL_RESTART );
101- }
102- allowTelemetryCheckBox .setSelected (config .getTelemetryEnabled ());
103-
104- azureEnvironmentComboBox .setSelectedItem (ObjectUtils .firstNonNull (AzureEnvironmentUtils .stringToAzureEnvironment (config .getCloud ()),
105- AzureEnvironment .AZURE
106- ));
107- this .originalData = getData ();
110+ allowTelemetryCheckBox .setSelected (oldTelemetryEnabled );
108111 }
109112
110113 public AzureConfiguration getData () {
111114 final AzureConfiguration data = new AzureConfiguration ();
112115 data .setCloud (AzureEnvironmentUtils .azureEnvironmentToString ((AzureEnvironment ) azureEnvironmentComboBox .getSelectedItem ()));
113- if ( savePasswordComboBox .getValue () != null ) {
114- data . setDatabasePasswordSaveType ( savePasswordComboBox . getValue () .name ());
115- }
116+ data . setDatabasePasswordSaveType ( Optional . ofNullable ( savePasswordComboBox .getSelectedItem ())
117+ . map ( i -> (( Password . SaveType ) i ) .name ())
118+ . orElse ( Password . SaveType . UNTIL_RESTART . name ()));
116119 data .setTelemetryEnabled (allowTelemetryCheckBox .isSelected ());
117120 data .setFunctionCoreToolsPath (funcCoreToolsPath .getItem ());
118121 return data ;
@@ -163,19 +166,17 @@ public String getDisplayName() {
163166
164167 @ Override
165168 public boolean doOKAction () {
166- final AzureConfiguration data = getData ();
169+ final AzureConfiguration newConfig = getData ();
167170 // set partial config to global config
168- final AzureConfiguration config = Azure .az ().config ();
169- config .setCloud (data .getCloud ());
170- config .setTelemetryEnabled (data .getTelemetryEnabled ());
171- config .setDatabasePasswordSaveType (data .getDatabasePasswordSaveType ());
172- config .setFunctionCoreToolsPath (data .getFunctionCoreToolsPath ());
171+ this .originalConfig .setCloud (newConfig .getCloud ());
172+ this .originalConfig .setTelemetryEnabled (newConfig .getTelemetryEnabled ());
173+ this .originalConfig .setDatabasePasswordSaveType (newConfig .getDatabasePasswordSaveType ());
174+ this .originalConfig .setFunctionCoreToolsPath (newConfig .getFunctionCoreToolsPath ());
173175 final String userAgent = String .format (AzurePlugin .USER_AGENT , AzurePlugin .PLUGIN_VERSION ,
174- config . getTelemetryEnabled () ? config .getMachineId () : StringUtils .EMPTY );
175- config .setUserAgent (userAgent );
176- CommonSettings .setUserAgent (config .getUserAgent ());
176+ this . originalConfig . getTelemetryEnabled () ? this . originalConfig .getMachineId () : StringUtils .EMPTY );
177+ this . originalConfig .setUserAgent (userAgent );
178+ CommonSettings .setUserAgent (newConfig .getUserAgent ());
177179 // apply changes
178- this .originalData = config ;
179180 // we need to get rid of AuthMethodManager, using az.azure_account
180181 if (AuthMethodManager .getInstance ().isSignedIn ()) {
181182 final AuthMethodManager authMethodManager = AuthMethodManager .getInstance ();
@@ -188,8 +189,8 @@ public boolean doOKAction() {
188189 }
189190 }
190191
191- if (StringUtils .isNotBlank (config .getCloud ())) {
192- Azure .az (AzureCloud .class ).setByName (config .getCloud ());
192+ if (StringUtils .isNotBlank (newConfig .getCloud ())) {
193+ Azure .az (AzureCloud .class ).setByName (newConfig .getCloud ());
193194 }
194195 AzureConfigInitializer .saveAzConfig ();
195196 return true ;
@@ -212,32 +213,21 @@ public String getHelpTopic() {
212213
213214 @ Override
214215 public boolean isModified () {
215- if (originalData == null ) {
216+ if (originalConfig == null ) {
216217 return false ;
217218 }
218-
219- final AzureConfiguration data = getData ();
220-
221- final AzureEnvironment newEnv = AzureEnvironmentUtils .stringToAzureEnvironment (data .getCloud ());
222- final AzureEnvironment oldEnv = AzureEnvironmentUtils .stringToAzureEnvironment (originalData .getCloud ());
223- if (!Objects .equals (newEnv , oldEnv )) {
224- return true ;
225- }
226-
227- if (!StringUtils .equalsIgnoreCase (data .getDatabasePasswordSaveType (), originalData .getDatabasePasswordSaveType ())) {
228- return true ;
229- }
230-
231- if (!StringUtils .equalsIgnoreCase (data .getFunctionCoreToolsPath (), originalData .getFunctionCoreToolsPath ())) {
232- return true ;
233- }
234-
235- return !Objects .equals (data .getTelemetryEnabled (), data .getTelemetryEnabled ());
219+ final AzureConfiguration newConfig = getData ();
220+ final AzureEnvironment newEnv = AzureEnvironmentUtils .stringToAzureEnvironment (newConfig .getCloud ());
221+ final AzureEnvironment oldEnv = AzureEnvironmentUtils .stringToAzureEnvironment (originalConfig .getCloud ());
222+ return !Objects .equals (newEnv , oldEnv ) ||
223+ !StringUtils .equalsIgnoreCase (newConfig .getDatabasePasswordSaveType (), originalConfig .getDatabasePasswordSaveType ()) ||
224+ !StringUtils .equalsIgnoreCase (newConfig .getFunctionCoreToolsPath (), originalConfig .getFunctionCoreToolsPath ()) ||
225+ !Objects .equals (newConfig .getTelemetryEnabled (), newConfig .getTelemetryEnabled ());
236226 }
237227
238228 @ Override
239229 public void reset () {
240- setData (originalData );
230+ setData (originalConfig );
241231 }
242232
243233 private void createUIComponents () {
0 commit comments