Skip to content

Commit 2e9f42f

Browse files
authored
Merge pull request #338 from drpayyne/issue-296
Changed validator for NewCLICommandDialog
2 parents 47e7479 + 9a518a5 commit 2e9f42f

File tree

6 files changed

+90
-184
lines changed

6 files changed

+90
-184
lines changed

resources/magento2/validation.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ validator.class.shouldBeUnique=Duplicated class {0}
1212
validator.namespace.isNotValid=The {0} is not valid namespace name
1313
validator.directory.isNotValid={0} is not valid
1414
validator.directory.php.isNotValid=The {0} field does not contain a valid PHP directory
15+
validator.command.isNotValid=The {0} field does not contain a valid Magento 2 CLI command
1516
validator.module.noSuchModule=No such module {0}
1617
validator.file.alreadyExists={0} already exists
1718
validator.file.cantBeCreated={0} can't be created

src/com/magento/idea/magento2plugin/actions/generation/dialog/AbstractDialog.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.swing.JComponent;
2727
import javax.swing.JDialog;
2828
import javax.swing.JOptionPane;
29+
import javax.swing.JTextArea;
2930
import javax.swing.JTextField;
3031

3132
/**
@@ -185,6 +186,8 @@ private String resolveFieldValueByComponentType(final Object field) {
185186
return ((JTextField) field).getText();
186187
} else if (field instanceof JComboBox) {
187188
return ((JComboBox) field).getSelectedItem().toString();
189+
} else if (field instanceof JTextArea) {
190+
return ((JTextArea) field).getText();
188191
}
189192
return null;
190193
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCLICommandDialog.java

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77

88
import com.intellij.openapi.project.Project;
99
import com.intellij.psi.PsiDirectory;
10+
import com.jetbrains.php.lang.psi.elements.PhpClass;
1011
import com.magento.idea.magento2plugin.actions.generation.NewCLICommandAction;
1112
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandClassData;
1213
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandXmlData;
13-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewCLICommandValidator;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.CliCommandRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
19+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
1420
import com.magento.idea.magento2plugin.actions.generation.generator.CLICommandClassGenerator;
1521
import com.magento.idea.magento2plugin.actions.generation.generator.CLICommandDiXmlGenerator;
1622
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
23+
import com.magento.idea.magento2plugin.bundles.CommonBundle;
1724
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
25+
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
1826
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
1927
import java.awt.event.KeyEvent;
2028
import java.awt.event.WindowAdapter;
@@ -28,19 +36,41 @@
2836
import javax.swing.JTextField;
2937
import javax.swing.KeyStroke;
3038

31-
@SuppressWarnings({"PMD.MissingSerialVersionUID"})
39+
@SuppressWarnings({"PMD.MissingSerialVersionUID", "PMD.ExcessiveImports"})
3240
public class NewCLICommandDialog extends AbstractDialog {
3341
private JPanel contentPane;
42+
private JButton buttonCancel;
43+
private JButton buttonOK;
44+
private static final String CLASS_NAME = "class name";
45+
private static final String PARENT_DIRECTORY = "parent directory";
46+
private static final String COMMAND_NAME = "command name";
47+
private static final String COMMAND_DESCRIPTION = "description";
48+
49+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
50+
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
51+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
52+
message = {PhpClassRule.MESSAGE, CLASS_NAME})
3453
private JTextField cliCommandClassNameField;
54+
55+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
56+
message = {NotEmptyRule.MESSAGE, PARENT_DIRECTORY})
57+
@FieldValidation(rule = RuleRegistry.DIRECTORY,
58+
message = {DirectoryRule.MESSAGE, PARENT_DIRECTORY})
3559
private JTextField cliCommandParentDirectoryField;
60+
61+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
62+
message = {NotEmptyRule.MESSAGE, COMMAND_NAME})
63+
@FieldValidation(rule = RuleRegistry.CLI_COMMAND,
64+
message = {CliCommandRule.MESSAGE, COMMAND_NAME})
3665
private JTextField cliCommandNameField;
66+
67+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
68+
message = {NotEmptyRule.MESSAGE, COMMAND_DESCRIPTION})
3769
private JTextArea cliCommandDescriptionField;
38-
private JButton buttonCancel;
39-
private JButton buttonOK;
4070

71+
private final CommonBundle commonBundle;
4172
private final Project project;
4273
private final String moduleName;
43-
private final NewCLICommandValidator validator;
4474
private final CamelCaseToSnakeCase toSnakeCase;
4575

4676
/**
@@ -53,8 +83,8 @@ public NewCLICommandDialog(final Project project, final PsiDirectory directory)
5383
super();
5484
this.project = project;
5585
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
56-
this.validator = new NewCLICommandValidator();
5786
this.toSnakeCase = CamelCaseToSnakeCase.getInstance();
87+
this.commonBundle = new CommonBundle();
5888

5989
setContentPane(contentPane);
6090
setModal(true);
@@ -145,13 +175,38 @@ public String getCLICommandClassFqn() {
145175
}
146176

147177
private void onOK() {
148-
if (!validator.validate(this.project,this)) {
178+
if (!validateFormFields() || !isPHPClassValid()) {
149179
return;
150180
}
151181
this.generate();
152182
this.setVisible(false);
153183
}
154184

185+
private Boolean isPHPClassValid() {
186+
final NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
187+
moduleName,
188+
getCLICommandClassName(),
189+
getCLICommandParentDirectory()
190+
);
191+
final String namespace = namespaceBuilder.getClassFqn();
192+
final PhpClass phpClass = GetPhpClassByFQN.getInstance(project).execute(namespace);
193+
if (phpClass != null) {
194+
final String errorMessage = validatorBundle.message(
195+
"validator.file.alreadyExists",
196+
commonBundle.message("common.cli.class.title")
197+
);
198+
JOptionPane.showMessageDialog(
199+
null,
200+
errorMessage,
201+
commonBundle.message("common.validationErrorTitle"),
202+
JOptionPane.ERROR_MESSAGE
203+
);
204+
return false;
205+
}
206+
207+
return true;
208+
}
209+
155210
private void generate() {
156211
try {
157212
this.generateCLICommandClass();

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewCLICommandValidator.java

Lines changed: 0 additions & 177 deletions
This file was deleted.

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/annotation/RuleRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AclResourceIdRule;
99
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericRule;
1010
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
11+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.CliCommandRule;
1112
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
1213
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
1314
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
@@ -32,6 +33,7 @@ public enum RuleRegistry {
3233
START_WITH_NUMBER_OR_CAPITAL_LETTER(StartWithNumberOrCapitalLetterRule.class),
3334
ACL_RESOURCE_ID(AclResourceIdRule.class),
3435
LOWERCASE(Lowercase.class),
36+
CLI_COMMAND(CliCommandRule.class),
3537
NUMERIC(NumericRule.class);
3638

3739
private Class<?> rule;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule;
7+
8+
import com.magento.idea.magento2plugin.util.RegExUtil;
9+
10+
public class CliCommandRule implements ValidationRule {
11+
public static final String MESSAGE = "validator.command.isNotValid";
12+
private static final ValidationRule INSTANCE = new CliCommandRule();
13+
14+
@Override
15+
public boolean check(final String value) {
16+
return value.matches(RegExUtil.CLI_COMMAND_NAME);
17+
}
18+
19+
public static ValidationRule getInstance() {
20+
return INSTANCE;
21+
}
22+
}

0 commit comments

Comments
 (0)