Skip to content

Commit 0ce58cf

Browse files
committed
Refactor add app settings action for app settings table
1 parent 3887e6e commit 0ce58cf

File tree

7 files changed

+184
-40
lines changed

7 files changed

+184
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.microsoft.azure.toolkit.intellij.legacy.appservice.table.AddAppSettingsDialog">
3+
<grid id="27dc6" binding="pnlRoot" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="0" left="0" bottom="0" right="0"/>
5+
<constraints>
6+
<xy x="20" y="20" width="451" height="89"/>
7+
</constraints>
8+
<properties/>
9+
<border type="none"/>
10+
<children>
11+
<component id="a7673" class="javax.swing.JLabel" binding="lblName">
12+
<constraints>
13+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
14+
</constraints>
15+
<properties>
16+
<text value="Name:"/>
17+
</properties>
18+
</component>
19+
<vspacer id="1ceff">
20+
<constraints>
21+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
22+
</constraints>
23+
</vspacer>
24+
<component id="d04e7" class="javax.swing.JLabel" binding="lblValue">
25+
<constraints>
26+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
27+
</constraints>
28+
<properties>
29+
<text value="Value:"/>
30+
</properties>
31+
</component>
32+
<component id="3e900" class="com.microsoft.azure.toolkit.intellij.common.AzureTextInput" binding="txtName">
33+
<constraints>
34+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
35+
</constraints>
36+
<properties/>
37+
</component>
38+
<component id="2f5bb" class="com.microsoft.azure.toolkit.intellij.common.AzureTextInput" binding="txtValue">
39+
<constraints>
40+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
41+
</constraints>
42+
<properties/>
43+
</component>
44+
</children>
45+
</grid>
46+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
package com.microsoft.azure.toolkit.intellij.legacy.appservice.table;
7+
8+
import com.microsoft.azure.toolkit.intellij.common.AzureDialog;
9+
import com.microsoft.azure.toolkit.intellij.common.AzureTextInput;
10+
import com.microsoft.azure.toolkit.lib.common.form.AzureForm;
11+
import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput;
12+
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
13+
import org.apache.commons.lang.StringUtils;
14+
import org.apache.commons.lang3.tuple.Pair;
15+
import org.jetbrains.annotations.Nullable;
16+
17+
import javax.annotation.Nonnull;
18+
import javax.swing.*;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.regex.Pattern;
22+
23+
public class AddAppSettingsDialog extends AzureDialog<Pair<String, String>>
24+
implements AzureForm<Pair<String, String>> {
25+
26+
private static final Pattern APP_SETTINGS_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_\\.]+$");
27+
28+
private JPanel pnlRoot;
29+
private AzureTextInput txtName;
30+
private AzureTextInput txtValue;
31+
private JLabel lblName;
32+
private JLabel lblValue;
33+
34+
private final AppSettingsTable table;
35+
36+
public AddAppSettingsDialog(@Nonnull final AppSettingsTable table) {
37+
super();
38+
this.table = table;
39+
$$$setupUI$$$();
40+
init();
41+
}
42+
43+
@Override
44+
protected void init() {
45+
super.init();
46+
txtName.setRequired(true);
47+
txtName.addValidator(this::validateName);
48+
txtValue.setRequired(false);
49+
50+
lblName.setLabelFor(txtName);
51+
lblValue.setLabelFor(txtValue);
52+
}
53+
54+
private AzureValidationInfo validateName() {
55+
final String value = txtName.getValue();
56+
if (StringUtils.isEmpty(value)) {
57+
return AzureValidationInfo.error("Name is a required property", txtName);
58+
} else if (!APP_SETTINGS_NAME_PATTERN.matcher(value).matches()) {
59+
return AzureValidationInfo.error("App setting names can only contain letters, numbers (0-9), periods (\".\"), and underscores (\"_\")", txtName);
60+
} else if (table.getAppSettings().containsKey(value)) {
61+
return AzureValidationInfo.error("App setting names must be unique", txtName);
62+
}
63+
return AzureValidationInfo.success(txtName);
64+
}
65+
66+
@Override
67+
public AzureForm<Pair<String, String>> getForm() {
68+
return this;
69+
}
70+
71+
@Override
72+
protected String getDialogTitle() {
73+
return "Add App Setting";
74+
}
75+
76+
@Override
77+
protected @Nullable JComponent createCenterPanel() {
78+
return pnlRoot;
79+
}
80+
81+
@Override
82+
public Pair<String, String> getValue() {
83+
return Pair.of(txtName.getValue(), txtValue.getValue());
84+
}
85+
86+
@Override
87+
public void setValue(Pair<String, String> data) {
88+
txtName.setValue(data.getKey());
89+
txtValue.setValue(data.getValue());
90+
}
91+
92+
@Override
93+
public List<AzureFormInput<?>> getInputs() {
94+
return Arrays.asList(txtName, txtValue);
95+
}
96+
97+
// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
98+
void $$$setupUI$$$() {
99+
}
100+
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appservice/src/main/java/com/microsoft/azure/toolkit/intellij/legacy/appservice/table/AppSettingModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public int getAppSettingsRow(@Nullable String key) {
127127
return ListUtils.indexOf(appSettings, pair -> StringUtils.equalsIgnoreCase(pair.getKey(), key));
128128
}
129129

130+
@Nonnull
130131
public Map<String, String> getAppSettings() {
131132
final Map<String, String> result = new HashMap<>();
132133
appSettings.forEach(pair -> result.put(pair.getKey(), pair.getValue()));

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appservice/src/main/java/com/microsoft/azure/toolkit/intellij/legacy/appservice/table/AppSettingsTable.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
package com.microsoft.azure.toolkit.intellij.legacy.appservice.table;
77

8+
import com.intellij.ui.SimpleTextAttributes;
89
import com.intellij.ui.table.JBTable;
910
import com.microsoft.azure.toolkit.ide.appservice.model.AppServiceConfig;
1011
import com.microsoft.azure.toolkit.lib.Azure;
1112
import com.microsoft.azure.toolkit.lib.appservice.AppServiceAppBase;
1213
import com.microsoft.azure.toolkit.lib.appservice.AzureAppService;
14+
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
1315
import com.microsoft.azure.toolkit.lib.common.task.AzureTask;
1416
import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager;
1517
import com.microsoft.intellij.CommonConst;
@@ -25,8 +27,11 @@
2527
import java.util.HashSet;
2628
import java.util.Map;
2729
import java.util.Objects;
30+
import java.util.Optional;
2831
import java.util.Set;
2932

33+
import static com.microsoft.azure.toolkit.intellij.common.AzureBundle.message;
34+
3035
public class AppSettingsTable extends JBTable {
3136
@Getter
3237
protected final Set<String> appSettingsKeyToRemove = new HashSet<>();
@@ -53,22 +58,32 @@ public void setAppService(@Nonnull final AppServiceConfig config) {
5358
return;
5459
}
5560
this.config = config;
56-
AzureTaskManager.getInstance().runInBackground("Loading app settings", () -> {
61+
AzureTaskManager.getInstance().runInBackground("Loading application settings", () -> {
5762
this.getEmptyText().setText(CommonConst.LOADING_TEXT);
5863
this.setEnabled(false);
5964
this.clear();
6065
final Map<String, String> remoteAppSettings = StringUtils.isNoneBlank(config.getResourceId()) ?
6166
((AppServiceAppBase<?, ?, ?>) Objects.requireNonNull(Azure.az(AzureAppService.class).getById(config.getResourceId()))).getAppSettings() : Collections.emptyMap();
6267
AzureTaskManager.getInstance().runLater(() -> {
6368
AppSettingsTable.this.setAppSettings(remoteAppSettings);
64-
config.getAppSettingsToRemove().forEach(this::removeAppSettings);
69+
Optional.ofNullable(config.getAppSettingsToRemove()).ifPresent(set -> set.forEach(this::removeAppSettings));
6570
AppSettingsTable.this.addAppSettings(config.getAppSettings());
66-
this.getEmptyText().setText("No app settings configured");
71+
this.getEmptyText().setText("No app setting configured");
72+
this.getEmptyText().appendLine("New app setting", SimpleTextAttributes.LINK_ATTRIBUTES, ignore -> addAppSettings());
6773
this.setEnabled(true);
6874
}, AzureTask.Modality.ANY);
6975
});
7076
}
7177

78+
public void addAppSettings() {
79+
final AddAppSettingsDialog dialog = new AddAppSettingsDialog(this);
80+
dialog.setOkActionListener(pair -> {
81+
this.addAppSettings(pair.getKey(), pair.getValue());
82+
dialog.close();
83+
});
84+
dialog.show();
85+
}
86+
7287
public void addAppSettings(@Nonnull String key, String value) {
7388
appSettingsKeyToRemove.remove(key);
7489
final int index = appSettingModel.addAppSettings(key, value);
@@ -91,6 +106,14 @@ public void removeAppSettings(int row) {
91106
this.refresh();
92107
}
93108

109+
public void removeAppSettings() {
110+
try {
111+
this.removeAppSettings(this.getSelectedRow());
112+
} catch (final IllegalArgumentException iae) {
113+
AzureMessager.getMessager().error(message("function.appSettings.remove.error.title"), iae.getMessage());
114+
}
115+
}
116+
94117
public void removeAppSettings(@Nonnull String key) {
95118
final int row = appSettingModel.getAppSettingsRow(key);
96119
this.removeAppSettings(row);
@@ -112,6 +135,7 @@ public String getSelectedKey() {
112135
return appSettingModel.getAppSettingsKey(getSelectedRow());
113136
}
114137

138+
@Nonnull
115139
public Map<String, String> getAppSettings() {
116140
return appSettingModel.getAppSettings();
117141
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appservice/src/main/java/com/microsoft/azure/toolkit/intellij/legacy/appservice/table/AppSettingsTableUtils.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,40 @@
88
import com.intellij.icons.AllIcons;
99
import com.intellij.openapi.actionSystem.ActionToolbarPosition;
1010
import com.intellij.openapi.actionSystem.AnActionEvent;
11-
import com.intellij.openapi.ui.Messages;
1211
import com.intellij.ui.AnActionButton;
1312
import com.intellij.ui.ToolbarDecorator;
14-
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
1513
import com.nimbusds.jose.util.ArrayUtils;
16-
import org.apache.commons.lang3.StringUtils;
1714

1815
import javax.annotation.Nonnull;
1916
import javax.swing.*;
17+
import java.awt.*;
2018
import java.awt.event.InputEvent;
2119
import java.awt.event.KeyEvent;
2220

2321
import static com.microsoft.azure.toolkit.intellij.common.AzureBundle.message;
2422

2523
public class AppSettingsTableUtils {
26-
public static JPanel createAppSettingPanel(AppSettingsTable appSettingsTable, AnActionButton... additionalActions) {
24+
public static JPanel createAppSettingPanel(@Nonnull final AppSettingsTable appSettingsTable, AnActionButton... additionalActions) {
2725
final AnActionButton btnAdd = new AnActionButton(message("common.add"), AllIcons.General.Add) {
2826
@Override
2927
public void actionPerformed(AnActionEvent anActionEvent) {
30-
addNewProperty(appSettingsTable);
28+
appSettingsTable.addAppSettings();
3129
}
3230
};
3331
btnAdd.registerCustomShortcutSet(KeyEvent.VK_ADD, InputEvent.ALT_DOWN_MASK, appSettingsTable);
3432

3533
final AnActionButton btnRemove = new AnActionButton(message("common.remove"), AllIcons.General.Remove) {
3634
@Override
3735
public void actionPerformed(AnActionEvent anActionEvent) {
38-
removeProperty(appSettingsTable);
36+
appSettingsTable.removeAppSettings();
3937
}
4038
};
4139
btnRemove.registerCustomShortcutSet(KeyEvent.VK_SUBTRACT, InputEvent.ALT_DOWN_MASK, appSettingsTable);
4240
final AnActionButton[] actionButtons = {btnAdd, btnRemove};
4341
final ToolbarDecorator tableToolbarDecorator = ToolbarDecorator.createDecorator(appSettingsTable)
44-
.addExtraActions(ArrayUtils.concat(actionButtons, additionalActions)).setToolbarPosition(ActionToolbarPosition.RIGHT);
42+
.addExtraActions(ArrayUtils.concat(actionButtons, additionalActions))
43+
.setMinimumSize(new Dimension(-1, 120))
44+
.setToolbarPosition(ActionToolbarPosition.RIGHT);
4545
return tableToolbarDecorator.createPanel();
4646
}
47-
48-
private static void removeProperty(@Nonnull final AppSettingsTable appSettingsTable) {
49-
try {
50-
appSettingsTable.removeAppSettings(appSettingsTable.getSelectedRow());
51-
appSettingsTable.repaint();
52-
} catch (final IllegalArgumentException iae) {
53-
AzureMessager.getMessager().error(message("function.appSettings.remove.error.title"), iae.getMessage());
54-
}
55-
}
56-
57-
private static void addNewProperty(@Nonnull final AppSettingsTable appSettingsTable) {
58-
final String key = Messages.showInputDialog(appSettingsTable, message("function.appSettings.add.key.message"),
59-
message("function.appSettings.add.key.title"), null);
60-
if (StringUtils.isEmpty(key)) {
61-
return;
62-
}
63-
final String value = Messages.showInputDialog(appSettingsTable, message("function.appSettings.add.value.message"),
64-
message("function.appSettings.add.value.title"), null);
65-
appSettingsTable.addAppSettings(key, value);
66-
appSettingsTable.repaint();
67-
}
6847
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appservice/src/main/java/com/microsoft/azure/toolkit/intellij/legacy/function/runner/component/table/FunctionAppSettingsTableUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public void focusLost(FocusEvent focusEvent) {
9696
final JPanel tablePanel = AppSettingsTableUtils.createAppSettingPanel(appSettingsTable, importButton, exportButton);
9797
final GridConstraints tableConstraint = new GridConstraints(0, 0, 1, 1, 0, GridConstraints.FILL_BOTH, 7, 7, null, null, null);
9898
result.add(tablePanel, tableConstraint);
99-
result.setMinimumSize(new Dimension(-1, 100));
10099
return result;
101100
}
102101

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.microsoft.azure.toolkit.intellij.legacy.webapp.runner.webappconfig.slimui.WebAppSlimSettingPanel">
3-
<grid id="27dc6" binding="pnlRoot" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="27dc6" binding="pnlRoot" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="38" y="71" width="591" height="334"/>
6+
<xy x="38" y="71" width="591" height="384"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
1010
<children>
1111
<nested-form id="dd1c2" form-file="com/microsoft/azure/toolkit/intellij/legacy/webapp/runner/webappconfig/slimui/WebAppDeployConfigurationPanel.form" binding="pnlDeployment" custom-create="true">
1212
<constraints>
13-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
13+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
1414
</constraints>
1515
</nested-form>
16-
<vspacer id="370e0">
17-
<constraints>
18-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
19-
</constraints>
20-
</vspacer>
2116
</children>
2217
</grid>
2318
</form>

0 commit comments

Comments
 (0)