1414import com .intellij .ui .ListCellRendererWrapper ;
1515import com .microsoft .azure .toolkit .ide .appservice .function .FunctionAppConfig ;
1616import com .microsoft .azure .toolkit .ide .appservice .model .DeploymentSlotConfig ;
17+ import com .microsoft .azure .toolkit .intellij .common .AzureFormPanel ;
1718import com .microsoft .azure .toolkit .intellij .legacy .common .AzureSettingPanel ;
1819import com .microsoft .azure .toolkit .intellij .legacy .function .FunctionAppComboBox ;
1920import com .microsoft .azure .toolkit .intellij .legacy .function .runner .component .table .FunctionAppSettingsTable ;
2425import com .microsoft .azure .toolkit .lib .Azure ;
2526import com .microsoft .azure .toolkit .lib .appservice .AppServiceAppBase ;
2627import com .microsoft .azure .toolkit .lib .appservice .function .AzureFunctions ;
27- import com .microsoft .azure .toolkit .lib .appservice .function .FunctionApp ;
28- import com .microsoft .azure .toolkit .lib .appservice .function .FunctionAppBase ;
2928import com .microsoft .azure .toolkit .lib .common .form .AzureFormInput ;
30- import com .microsoft .azure .toolkit .lib .common .form .AzureValidationInfo ;
3129import com .microsoft .azure .toolkit .lib .common .model .AbstractAzResource ;
3230import com .microsoft .azure .toolkit .lib .common .task .AzureTask ;
3331import com .microsoft .azure .toolkit .lib .common .task .AzureTaskManager ;
3432import org .apache .commons .collections .MapUtils ;
33+ import org .apache .commons .lang3 .ObjectUtils ;
3534import org .apache .commons .lang3 .StringUtils ;
3635import org .jetbrains .annotations .NotNull ;
3736import org .jetbrains .annotations .Nullable ;
3837import org .jetbrains .idea .maven .project .MavenProject ;
3938
4039import javax .annotation .Nonnull ;
4140import javax .swing .*;
42- import java .awt .event .ItemEvent ;
4341import java .nio .file .Paths ;
4442import java .util .Arrays ;
4543import java .util .Collections ;
4644import java .util .List ;
47- import java .util .Map ;
4845import java .util .Objects ;
4946import java .util .Optional ;
5047import java .util .UUID ;
51- import java .util .stream .Collectors ;
5248
5349import static com .microsoft .azure .toolkit .intellij .common .AzureBundle .message ;
5450
5551
56- public class FunctionDeploymentPanel extends AzureSettingPanel <FunctionDeployConfiguration > {
52+ public class FunctionDeploymentPanel extends AzureSettingPanel <FunctionDeployConfiguration > implements AzureFormPanel < FunctionDeployConfiguration > {
5753
5854 private JPanel pnlRoot ;
5955 private HyperlinkLabel lblCreateFunctionApp ;
@@ -65,14 +61,15 @@ public class FunctionDeploymentPanel extends AzureSettingPanel<FunctionDeployCon
6561 private JLabel lblAppSettings ;
6662 private JCheckBox chkSlot ;
6763 private DeploymentSlotComboBox cbDeploymentSlot ;
68- private JLabel lblDeploymentSlot ;
6964 private FunctionAppSettingsTable appSettingsTable ;
7065 private String appSettingsKey ;
7166 private String appSettingsResourceId ;
7267 private Module previousModule = null ;
68+ private final FunctionDeployConfiguration configuration ;
7369
7470 public FunctionDeploymentPanel (@ NotNull Project project , @ NotNull FunctionDeployConfiguration functionDeployConfiguration ) {
7571 super (project );
72+ this .configuration = functionDeployConfiguration ;
7673 this .appSettingsKey = StringUtils .firstNonBlank (functionDeployConfiguration .getAppSettingsKey (), UUID .randomUUID ().toString ());
7774 $$$setupUI$$$ ();
7875
@@ -86,11 +83,12 @@ public void customize(JList list, Module module, int i, boolean b, boolean b1) {
8683 }
8784 });
8885 functionAppComboBox .setRequired (true );
89- chkSlot .addItemListener (e -> onSelectSlot ());
86+ chkSlot .addItemListener (e -> onSlotCheckBoxChanged ());
9087
9188 lblModule .setLabelFor (cbFunctionModule );
9289 lblFunction .setLabelFor (functionAppComboBox );
9390 lblAppSettings .setLabelFor (appSettingsTable );
91+ final JLabel lblDeploymentSlot = new JLabel ("Deployment Slot:" );
9492 lblDeploymentSlot .setLabelFor (cbDeploymentSlot );
9593 fillModules ();
9694 }
@@ -202,24 +200,23 @@ private void onSelectFunctionApp(final FunctionAppConfig value) {
202200 return ;
203201 }
204202 // disable slot for draft function
203+ this .chkSlot .setEnabled (StringUtils .isNotEmpty (value .getResourceId ()));
205204 if (StringUtils .isEmpty (value .getResourceId ())) {
206205 this .chkSlot .setSelected (false );
207206 }
208- this .chkSlot .setEnabled (StringUtils .isNotEmpty (value .getResourceId ()));
209- this .toggleDeploymentSlot (chkSlot .isSelected ());
210207 this .cbDeploymentSlot .setAppService (value .getResourceId ());
211208 if (!this .chkSlot .isSelected ()) {
212209 loadAppSettings (getResourceId (value , null ));
213210 }
214211 }
215212
216- private void loadAppSettings (@ Nonnull final String resourceId ) {
213+ private void loadAppSettings (@ Nullable final String resourceId ) {
217214 if (StringUtils .equalsIgnoreCase (resourceId , this .appSettingsResourceId ) && MapUtils .isNotEmpty (this .appSettingsTable .getAppSettings ())) {
218215 return ;
219216 }
220217 this .appSettingsResourceId = resourceId ;
221218 this .appSettingsTable .loadAppSettings (() -> {
222- final AbstractAzResource <?, ?, ?> resource = Azure .az ().getById (resourceId );
219+ final AbstractAzResource <?, ?, ?> resource = StringUtils . isBlank ( resourceId ) ? null : Azure .az ().getById (resourceId );
223220 return resource instanceof AppServiceAppBase <?, ?, ?> ? ((AppServiceAppBase <?, ?, ?>) resource ).getAppSettings () : Collections .emptyMap ();
224221 });
225222 }
@@ -247,10 +244,14 @@ private void selectModule(final Module target) {
247244 }
248245 }
249246
250- private void onSelectSlot () {
247+ private void onSlotCheckBoxChanged () {
251248 toggleDeploymentSlot (chkSlot .isSelected ());
252- if (!chkSlot .isSelected () && functionAppComboBox .getValue () != null ) {
253- // reload app settings for function app
249+ final FunctionAppConfig function = functionAppComboBox .getValue ();
250+ final DeploymentSlotConfig slot = cbDeploymentSlot .getValue ();
251+ // reload app settings when switch slot configuration
252+ if (chkSlot .isSelected () && ObjectUtils .allNotNull (function , slot )) {
253+ loadAppSettings (getResourceId (functionAppComboBox .getValue (), slot ));
254+ } else if (!chkSlot .isSelected () && Objects .nonNull (function )) {
254255 loadAppSettings (getResourceId (functionAppComboBox .getValue (), null ));
255256 }
256257 }
@@ -261,24 +262,32 @@ private void toggleDeploymentSlot(boolean isDeployToSlot) {
261262 cbDeploymentSlot .validateValueAsync ();
262263 }
263264
264- // todo: @hanli migrate to AzureFormInput
265- public List <AzureValidationInfo > getAllValidationInfos (final boolean revalidateIfNone ) {
266- final List <AzureFormInput <?>> inputs = Arrays .asList (functionAppComboBox , cbDeploymentSlot );
267- return inputs .stream ()
268- .map (input -> input .getValidationInfo (revalidateIfNone ))
269- .filter (Objects ::nonNull ).collect (Collectors .toList ());
265+ @ Override
266+ public void setValue (FunctionDeployConfiguration data ) {
267+ resetFromConfig (data );
268+ }
269+
270+ @ Override
271+ public FunctionDeployConfiguration getValue () {
272+ final FunctionDeployConfiguration result = new FunctionDeployConfiguration (configuration .getProject (), configuration .getFactory (), configuration .getName ());
273+ apply (result );
274+ return result ;
270275 }
271276
277+ @ Override
278+ public List <AzureFormInput <?>> getInputs () {
279+ return Arrays .asList (functionAppComboBox , cbDeploymentSlot );
280+ }
281+
282+ @ Nullable
272283 private String getResourceId (@ Nonnull FunctionAppConfig config , @ Nullable DeploymentSlotConfig slotConfig ) {
273284 if (Objects .isNull (slotConfig )) {
274285 return StringUtils .isNoneBlank (config .getResourceId ()) ? config .getResourceId () :
275286 Azure .az (AzureFunctions .class ).functionApps (config .getSubscriptionId ()).getOrTemp (config .getName (), config .getResourceGroupName ()).getId ();
276287 } else {
277- return Azure .az (AzureFunctions .class ).functionApp (config .getResourceId ()).slots ().getOrTemp (slotConfig .getName (), null ).getId ();
288+ return Optional .ofNullable (Azure .az (AzureFunctions .class ).functionApp (config .getResourceId ()))
289+ .map (func -> func .slots ().getOrTemp (slotConfig .getName (), null ).getId ())
290+ .orElse (null );
278291 }
279292 }
280-
281- // CHECKSTYLE IGNORE check FOR NEXT 1 LINES
282- void $$$setupUI$$$ () {
283- }
284293}
0 commit comments