|
11 | 11 | import com.microsoft.azure.toolkit.intellij.common.AzureTextInput; |
12 | 12 | import com.microsoft.azure.toolkit.lib.common.form.AzureForm; |
13 | 13 | import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput; |
| 14 | +import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo; |
14 | 15 | import org.apache.commons.lang3.StringUtils; |
15 | 16 | import org.jetbrains.annotations.Nullable; |
16 | 17 |
|
|
20 | 21 | import java.util.List; |
21 | 22 | import java.util.stream.Collectors; |
22 | 23 |
|
| 24 | +import static com.microsoft.azure.toolkit.intellij.common.AzureBundle.message; |
| 25 | +import static com.microsoft.intellij.util.ValidationUtils.isValidAppServiceName; |
| 26 | + |
23 | 27 | public class DeploymentSlotCreationDialog extends AzureDialog<DeploymentSlotConfig> implements AzureForm<DeploymentSlotConfig> { |
24 | 28 | private static final String DO_NOT_CLONE_SETTINGS = "Do not clone settings"; |
25 | 29 | private static final String PARENT = "parent"; |
26 | 30 |
|
27 | 31 | private AzureTextInput txtName; |
28 | 32 | private AzureComboBox<String> cbConfigurationSource; |
29 | 33 | private JPanel pnlRoot; |
| 34 | + private final List<DeploymentSlotConfig> existingSlots; |
30 | 35 |
|
31 | 36 | public DeploymentSlotCreationDialog(@Nullable Project project, @Nonnull List<DeploymentSlotConfig> existingSlots) { |
32 | 37 | super(project); |
| 38 | + this.existingSlots = existingSlots; |
33 | 39 | $$$setupUI$$$(); |
34 | 40 | this.cbConfigurationSource.setItemsLoader(() -> { |
35 | 41 | final List<String> collect = existingSlots.stream().map(DeploymentSlotConfig::getName).collect(Collectors.toList()); |
36 | 42 | collect.add(0, PARENT); |
37 | 43 | collect.add(DO_NOT_CLONE_SETTINGS); |
38 | 44 | return collect; |
39 | 45 | }); |
| 46 | + this.txtName.addValidator(this::validateDeploymentSlotName); |
40 | 47 | super.init(); |
41 | 48 | } |
42 | 49 |
|
@@ -76,6 +83,24 @@ public List<AzureFormInput<?>> getInputs() { |
76 | 83 | return Arrays.asList(txtName, cbConfigurationSource); |
77 | 84 | } |
78 | 85 |
|
| 86 | + private AzureValidationInfo validateDeploymentSlotName() { |
| 87 | + final String value = txtName.getValue(); |
| 88 | + if (StringUtils.isEmpty(value)) { |
| 89 | + return AzureValidationInfo.error(message("appService.name.validate.empty"), txtName); |
| 90 | + } |
| 91 | + if (value.length() < 2 || value.length() > 60) { |
| 92 | + return AzureValidationInfo.error(message("appService.name.validate.length"), txtName); |
| 93 | + } |
| 94 | + if (!isValidAppServiceName(value)) { |
| 95 | + return AzureValidationInfo.error(message("appService.name.validate.invalidName"), txtName); |
| 96 | + } |
| 97 | + final DeploymentSlotConfig existingSlot = existingSlots.stream() |
| 98 | + .filter(slot -> StringUtils.equalsIgnoreCase(slot.getName(), value)) |
| 99 | + .findFirst().orElse(null); |
| 100 | + return existingSlot == null ? AzureValidationInfo.success(txtName) : |
| 101 | + AzureValidationInfo.error(String.format("Slot with name (%s) already exists", value), txtName); |
| 102 | + } |
| 103 | + |
79 | 104 | void $$$setupUI$$$() { |
80 | 105 | } |
81 | 106 | } |
0 commit comments