Skip to content

Commit 9fcd687

Browse files
committed
Merge branch '2.1.0-develop' into issue-299
2 parents 677b97c + a2257a1 commit 9fcd687

15 files changed

+199
-815
lines changed

resources/magento2/common.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ common.module.target=Target Module
2222
common.theme.target=Target Theme
2323
common.area.target=Target Area
2424
common.name=Name
25-
common.className=Class Name
25+
common.className=Class name
26+
common.argument=Argument name
2627
common.directoryPath=Directory Path
2728
common.methodType=Method Type
2829
common.sortOrder=Sort Order

resources/magento2/validation.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ validator.class.shouldBeUnique=Duplicated class {0}
1313
validator.namespace.isNotValid=The {0} is not valid namespace name
1414
validator.directory.isNotValid={0} is not valid
1515
validator.directory.php.isNotValid=The {0} field does not contain a valid PHP directory
16+
validator.command.isNotValid=The {0} field does not contain a valid Magento 2 CLI command
1617
validator.module.noSuchModule=No such module {0}
1718
validator.file.alreadyExists={0} already exists
1819
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
/**
@@ -189,6 +190,8 @@ private String resolveFieldValueByComponentType(final Object field) {
189190
} else {
190191
return ((JComboBox) field).getSelectedItem().toString();
191192
}
193+
} else if (field instanceof JTextArea) {
194+
return ((JTextArea) field).getText();
192195
}
193196
return null;
194197
}

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
import com.magento.idea.magento2plugin.actions.generation.CreateAnObserverAction;
1111
import com.magento.idea.magento2plugin.actions.generation.data.ObserverEventsXmlData;
1212
import com.magento.idea.magento2plugin.actions.generation.data.ObserverFileData;
13-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.CreateAnObserverDialogValidator;
13+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
1419
import com.magento.idea.magento2plugin.actions.generation.generator.ObserverClassGenerator;
1520
import com.magento.idea.magento2plugin.actions.generation.generator.ObserverEventsXmlGenerator;
1621
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
@@ -36,26 +41,47 @@
3641
"PMD.MissingSerialVersionUID",
3742
"PMD.DataClass",
3843
"PMD.UnusedPrivateField",
44+
"PMD.ExcessiveImports",
3945
})
4046
public class CreateAnObserverDialog extends AbstractDialog {
4147
@NotNull
4248
private final Project project;
43-
@NotNull
44-
private final CreateAnObserverDialogValidator validator;
4549
private final String targetEvent;
4650
private JPanel contentPane;
4751
private JButton buttonOK;
4852
private JButton buttonCancel;
49-
private JTextField observerClassName;
50-
private JTextField observerDirectory;
51-
private FilteredComboBox observerModule;
5253
private JComboBox observerArea;
53-
private JTextField observerName;
5454
private JLabel observerClassNameLabel;
5555
private JLabel observerDirectoryName;
5656
private JLabel selectObserverModule;
5757
private JLabel observerAreaLabel;
5858
private JLabel observerNameLabel;
59+
private static final String OBSERVER_MODULE = "target module";
60+
private static final String OBSERVER_CLASS = "class name";
61+
private static final String OBSERVER_DIRECTORY = "directory";
62+
private static final String OBSERVER_NAME = "name";
63+
64+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
65+
message = {NotEmptyRule.MESSAGE, OBSERVER_MODULE})
66+
private FilteredComboBox observerModule;
67+
68+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
69+
message = {NotEmptyRule.MESSAGE, OBSERVER_CLASS})
70+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
71+
message = {PhpClassRule.MESSAGE, OBSERVER_CLASS})
72+
private JTextField observerClassName;
73+
74+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
75+
message = {NotEmptyRule.MESSAGE, OBSERVER_DIRECTORY})
76+
@FieldValidation(rule = RuleRegistry.DIRECTORY,
77+
message = {DirectoryRule.MESSAGE, OBSERVER_DIRECTORY})
78+
private JTextField observerDirectory;
79+
80+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
81+
message = {NotEmptyRule.MESSAGE, OBSERVER_NAME})
82+
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC_WITH_UNDERSCORE,
83+
message = {AlphanumericWithUnderscoreRule.MESSAGE, OBSERVER_NAME})
84+
private JTextField observerName;
5985

6086
/**
6187
* Constructor.
@@ -68,7 +94,6 @@ public CreateAnObserverDialog(@NotNull final Project project, final String targe
6894

6995
this.project = project;
7096
this.targetEvent = targetEvent;
71-
this.validator = CreateAnObserverDialogValidator.getInstance(this);
7297

7398
setContentPane(contentPane);
7499
setModal(true);
@@ -119,7 +144,7 @@ private void fillTargetAreaOptions() {
119144
* Perform code generation using input data.
120145
*/
121146
private void onOK() {
122-
if (!validator.validate(project)) {
147+
if (!validateFormFields()) {
123148
return;
124149
}
125150
new ObserverClassGenerator(new ObserverFileData(

src/com/magento/idea/magento2plugin/actions/generation/dialog/InjectAViewModelDialog.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
108108
</constraints>
109109
<properties>
110-
<text value="Argument Name"/>
110+
<text resource-bundle="magento2/common" key="common.argument"/>
111111
</properties>
112112
</component>
113113
<component id="c3a92" class="javax.swing.JTextField" binding="viewModelArgumentName">

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
import com.intellij.ui.DocumentAdapter;
1212
import com.magento.idea.magento2plugin.actions.generation.InjectAViewModelAction;
1313
import com.magento.idea.magento2plugin.actions.generation.data.ViewModelFileData;
14-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.InjectAViewModelDialogValidator;
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.AlphanumericRule;
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;
1520
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleViewModelClassGenerator;
1621
import com.magento.idea.magento2plugin.actions.generation.generator.code.ClassArgumentInXmlConfigGenerator;
1722
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
@@ -21,7 +26,6 @@
2126
import com.magento.idea.magento2plugin.util.FirstLetterToLowercaseUtil;
2227
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
2328
import java.awt.event.ActionEvent;
24-
import java.awt.event.ActionListener;
2529
import java.awt.event.KeyEvent;
2630
import java.awt.event.WindowAdapter;
2731
import java.awt.event.WindowEvent;
@@ -35,24 +39,43 @@
3539
import javax.swing.event.DocumentEvent;
3640
import org.jetbrains.annotations.NotNull;
3741

42+
@SuppressWarnings({
43+
"PMD.ExcessiveImports"
44+
})
3845
public class InjectAViewModelDialog extends AbstractDialog {
3946
@NotNull
4047
private final Project project;
41-
@NotNull
42-
private final InjectAViewModelDialogValidator validator;
4348
private final XmlTag targetBlockTag;
4449
private JPanel contentPane;
4550
private JButton buttonOK;
4651
private JButton buttonCancel;
47-
private JTextField viewModelClassName;
48-
private JTextField viewModelDirectory;
4952
private final CommonBundle commonBundle;
5053
private final ValidatorBundle validatorBundle;
51-
private JTextField viewModelArgumentName;
5254
private JLabel inheritClassLabel;//NOPMD
5355
private JLabel viewModelDirectoryLabel;//NOPMD
5456
private JLabel viewModelClassNameLabel;//NOPMD
5557
private JLabel viewModelArgumentNameLabel;//NOPMD
58+
private static final String CLASS_NAME = "class name";
59+
private static final String DIRECTORY = "directory";
60+
private static final String ARGUMENT_NAME = "argument name";
61+
62+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
63+
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
64+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
65+
message = {PhpClassRule.MESSAGE, CLASS_NAME})
66+
private JTextField viewModelClassName;
67+
68+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
69+
message = {NotEmptyRule.MESSAGE, DIRECTORY})
70+
@FieldValidation(rule = RuleRegistry.DIRECTORY,
71+
message = {DirectoryRule.MESSAGE, DIRECTORY})
72+
private JTextField viewModelDirectory;
73+
74+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
75+
message = {NotEmptyRule.MESSAGE, ARGUMENT_NAME})
76+
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC,
77+
message = {AlphanumericRule.MESSAGE, ARGUMENT_NAME})
78+
private JTextField viewModelArgumentName;
5679

5780
/**
5881
* Constructor.
@@ -68,7 +91,6 @@ public InjectAViewModelDialog(
6891

6992
this.project = project;
7093
this.targetBlockTag = targetBlockTag;
71-
this.validator = new InjectAViewModelDialogValidator(this);
7294
this.validatorBundle = new ValidatorBundle();
7395
this.commonBundle = new CommonBundle();
7496

@@ -84,19 +106,8 @@ protected void textChanged(final @NotNull DocumentEvent event) {
84106
setModal(true);
85107
getRootPane().setDefaultButton(buttonOK);
86108

87-
buttonOK.addActionListener(new ActionListener() {
88-
@Override
89-
public void actionPerformed(final ActionEvent event) {
90-
onOK();
91-
}
92-
});
93-
94-
buttonCancel.addActionListener(new ActionListener() {
95-
@Override
96-
public void actionPerformed(final ActionEvent event) {
97-
onCancel();
98-
}
99-
});
109+
buttonOK.addActionListener((final ActionEvent event) -> onOK());
110+
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());
100111

101112
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
102113
addWindowListener(new WindowAdapter() {
@@ -106,13 +117,11 @@ public void windowClosing(final WindowEvent event) {
106117
}
107118
});
108119

109-
contentPane.registerKeyboardAction(new ActionListener() {
110-
@Override
111-
public void actionPerformed(final ActionEvent event) {
112-
onCancel();
113-
}
114-
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
115-
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
120+
contentPane.registerKeyboardAction(
121+
(final ActionEvent event) -> onCancel(),
122+
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
123+
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
124+
);
116125
}
117126

118127
protected void updateArgumentText() {
@@ -123,7 +132,7 @@ protected void updateArgumentText() {
123132
}
124133

125134
protected void onOK() {
126-
if (!validator.validate(project)) {
135+
if (!validateFormFields()) {
127136
return;
128137
}
129138
final String moduleName = GetModuleNameByDirectoryUtil.execute(

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();

0 commit comments

Comments
 (0)