4646import javax .swing .table .DefaultTableModel ;
4747import java .util .Arrays ;
4848import java .util .Collections ;
49+ import java .util .HashMap ;
4950import java .util .List ;
5051import java .util .Objects ;
5152import java .util .Optional ;
5253import java .util .function .BiConsumer ;
54+ import java .util .stream .Collectors ;
5355import java .util .stream .IntStream ;
5456
5557public class SpringCloudAppPanel extends JPanel implements AzureFormPanel <SpringCloudAppConfig > {
@@ -76,15 +78,14 @@ public class SpringCloudAppPanel extends JPanel implements AzureFormPanel<Spring
7678 private JBTable tableInstances ;
7779
7880 private BiConsumer <? super Boolean , ? super SpringCloudAppConfig > listener = (aBoolean , springCloudAppConfig ) -> System .out .println (aBoolean );
79- private SpringCloudAppConfig originalData ;
81+ private SpringCloudAppConfig originalConfig ;
8082
8183 public SpringCloudAppPanel (@ Nullable SpringCloudApp app , @ Nonnull final Project project ) {
8284 super ();
83- this .app = app ;
84- this .init ();
85+ this .init (app );
8586 }
8687
87- private void init () {
88+ private void init (SpringCloudApp origin ) {
8889 this .selectorSubscription .addValueListener (s -> this .selectorCluster .setSubscription (s ));
8990 this .selectorSubscription .setRequired (true );
9091 this .selectorSubscription .setLabel ("Subscription" );
@@ -134,15 +135,15 @@ protected void textChanged(DocumentEvent documentEvent) {
134135 this .numMemory .addActionListener ((e ) -> debouncer .debounce ());
135136 this .numInstance .addChangeListener ((e ) -> debouncer .debounce ());
136137
137- final boolean appFixed = Objects .nonNull (this . app );
138+ final boolean appFixed = Objects .nonNull (origin );
138139 this .selectorSubscription .setValueFixed (appFixed );
139140 this .selectorCluster .setValueFixed (appFixed );
140141 this .selectorApp .setValueFixed (appFixed );
141142
142143 if (appFixed ) { // app is specified on construction(show properties)
143- this .selectorCluster .setItemsLoader (() -> Collections .singletonList (this . app .getCluster ()));
144- this .selectorApp .setItemsLoader (() -> Collections .singletonList (this . app ));
145- this .setApp (this . app );
144+ this .selectorCluster .setItemsLoader (() -> Collections .singletonList (origin .getCluster ()));
145+ this .selectorApp .setItemsLoader (() -> Collections .singletonList (origin ));
146+ this .setApp (origin );
146147 }
147148 }
148149
@@ -152,20 +153,23 @@ public void setValueChangedListener(BiConsumer<? super Boolean, ? super SpringCl
152153
153154 public void refresh () {
154155 Objects .requireNonNull (this .app ).refresh ();
155- this . setApp ( this . app );
156+ AzureTaskManager . getInstance (). runLater ( this :: updateForm );
156157 }
157158
158159 public void reset () {
159- AzureTaskManager .getInstance ().runLater (() -> Optional .ofNullable (this .originalData ).ifPresent (this ::setData ));
160+ AzureTaskManager .getInstance ().runLater (() -> Optional .ofNullable (this .originalConfig ).ifPresent (this ::setData ));
160161 }
161162
162163 private void setApp (@ Nonnull SpringCloudApp app ) {
163- final String title = String .format ("Loading properties of app(%s)" , Objects .requireNonNull (this .app ).name ());
164- AzureTaskManager .getInstance ().runLater (title , () -> this .setAppInner (app ));
164+ if (Objects .equals (app , this .app )) {
165+ return ;
166+ }
167+ this .app = app ;
168+ AzureTaskManager .getInstance ().runLater (this ::updateForm );
165169 }
166170
167- private void setAppInner ( @ Nonnull SpringCloudApp app ) {
168- this . app = app ;
171+ private void updateForm ( ) {
172+ assert Objects . nonNull ( app ): " app is not specified" ;
169173 final String testUrl = app .entity ().getTestUrl ();
170174 this .txtTestEndpoint .setHyperlinkText (testUrl .length () > 60 ? testUrl .substring (0 , 60 ) + "..." : testUrl );
171175 this .txtTestEndpoint .setHyperlinkTarget (testUrl );
@@ -175,19 +179,19 @@ private void setAppInner(@Nonnull SpringCloudApp app) {
175179 final DefaultComboBoxModel <Integer > numCpuModel = (DefaultComboBoxModel <Integer >) this .numCpu .getModel ();
176180 final DefaultComboBoxModel <Integer > numMemoryModel = (DefaultComboBoxModel <Integer >) this .numMemory .getModel ();
177181 numCpuModel .removeAllElements ();
182+ numCpuModel .addAll (IntStream .range (1 , 1 + (basic ? 1 : 4 )).boxed ().collect (Collectors .toList ()));
178183 numMemoryModel .removeAllElements ();
179- IntStream .range (1 , 1 + (basic ? 1 : 4 )).forEach (i -> this .numCpu .addItem (i ));
180- IntStream .range (1 , 1 + (basic ? 2 : 8 )).forEach (i -> this .numMemory .addItem (i ));
184+ numMemoryModel .addAll (IntStream .range (1 , 1 + (basic ? 2 : 8 )).boxed ().collect (Collectors .toList ()));
181185 this .numInstance .setMaximum (basic ? 25 : 500 );
182186 this .numInstance .setMajorTickSpacing (basic ? 5 : 50 );
183187 this .numInstance .setMinorTickSpacing (basic ? 1 : 10 );
184188 this .numInstance .setMinimum (0 );
185-
186189 final SpringCloudDeploymentEntity deploymentEntity = Optional .ofNullable (app .activeDeployment ()).stream ().findAny ()
187190 .or (() -> app .deployments ().stream ().findAny ())
188191 .map (SpringCloudDeployment ::entity )
189192 .orElse (new SpringCloudDeploymentEntity ("default" , app .entity ()));
190193 final List <SpringCloudDeploymentInstanceEntity > instances = deploymentEntity .getInstances ();
194+ this .numInstance .setRealMin (Math .min (instances .size (), 1 ));
191195
192196 final DefaultTableModel model = (DefaultTableModel ) this .tableInstances .getModel ();
193197 model .setRowCount (0 );
@@ -196,14 +200,14 @@ private void setAppInner(@Nonnull SpringCloudApp app) {
196200 model .setRowCount (rows );
197201 this .tableInstances .setVisibleRowCount (rows );
198202
199- this .originalData = toConfig (app );
200- this .setData (this .originalData );
203+ this .originalConfig = toConfig (app );
204+ this .setData (this .originalConfig );
201205 }
202206
203207 private void onDataChanged () {
204- if (Objects .nonNull (this .originalData ) && Objects .nonNull (this .listener ) && Objects .nonNull (this .app )) {
208+ if (Objects .nonNull (this .originalConfig ) && Objects .nonNull (this .listener ) && Objects .nonNull (this .app )) {
205209 final SpringCloudAppConfig newData = this .getData ();
206- this .listener .accept (!this .originalData .equals (newData ), newData );
210+ this .listener .accept (!this .originalConfig .equals (newData ), newData );
207211 }
208212 }
209213
@@ -227,8 +231,8 @@ private SpringCloudAppConfig toConfig(@NotNull SpringCloudApp app) { // get conf
227231 deploymentConfig .setCpu (deploymentEntity .getCpu ());
228232 deploymentConfig .setMemoryInGB (deploymentEntity .getMemoryInGB ());
229233 deploymentConfig .setInstanceCount (instances .size ());
230- deploymentConfig .setJvmOptions (deploymentEntity .getJvmOptions ());
231- deploymentConfig .setEnvironment (deploymentEntity .getEnvironmentVariables ());
234+ deploymentConfig .setJvmOptions (Optional . ofNullable ( deploymentEntity .getJvmOptions ()). map ( String :: trim ). orElse ( "" ));
235+ deploymentConfig .setEnvironment (Optional . ofNullable ( deploymentEntity .getEnvironmentVariables ()). orElse ( new HashMap <>() ));
232236 return appConfig ;
233237 }
234238
@@ -246,7 +250,7 @@ public SpringCloudAppConfig getData(@Nonnull SpringCloudAppConfig appConfig) { /
246250 deploymentConfig .setMemoryInGB (numMemory .getItem ());
247251 deploymentConfig .setInstanceCount (numInstance .getValue ());
248252 deploymentConfig .setJvmOptions (Optional .ofNullable (this .txtJvmOptions .getText ()).map (String ::trim ).orElse ("" ));
249- deploymentConfig .setEnvironment (envTable .getEnvironmentVariables ());
253+ deploymentConfig .setEnvironment (Optional . ofNullable ( envTable .getEnvironmentVariables ()). orElse ( new HashMap <>() ));
250254 return appConfig ;
251255 }
252256
0 commit comments