7
7
8
8
import com .intellij .openapi .project .Project ;
9
9
import com .intellij .psi .PsiDirectory ;
10
+ import com .jetbrains .php .lang .psi .elements .PhpClass ;
10
11
import com .magento .idea .magento2plugin .actions .generation .NewCLICommandAction ;
11
12
import com .magento .idea .magento2plugin .actions .generation .data .CLICommandClassData ;
12
13
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 ;
14
20
import com .magento .idea .magento2plugin .actions .generation .generator .CLICommandClassGenerator ;
15
21
import com .magento .idea .magento2plugin .actions .generation .generator .CLICommandDiXmlGenerator ;
16
22
import com .magento .idea .magento2plugin .actions .generation .generator .util .NamespaceBuilder ;
23
+ import com .magento .idea .magento2plugin .bundles .CommonBundle ;
17
24
import com .magento .idea .magento2plugin .util .CamelCaseToSnakeCase ;
25
+ import com .magento .idea .magento2plugin .util .GetPhpClassByFQN ;
18
26
import com .magento .idea .magento2plugin .util .magento .GetModuleNameByDirectoryUtil ;
19
27
import java .awt .event .KeyEvent ;
20
28
import java .awt .event .WindowAdapter ;
28
36
import javax .swing .JTextField ;
29
37
import javax .swing .KeyStroke ;
30
38
31
- @ SuppressWarnings ({"PMD.MissingSerialVersionUID" })
39
+ @ SuppressWarnings ({"PMD.MissingSerialVersionUID" , "PMD.ExcessiveImports" })
32
40
public class NewCLICommandDialog extends AbstractDialog {
33
41
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 })
34
53
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 })
35
59
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 })
36
65
private JTextField cliCommandNameField ;
66
+
67
+ @ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
68
+ message = {NotEmptyRule .MESSAGE , COMMAND_DESCRIPTION })
37
69
private JTextArea cliCommandDescriptionField ;
38
- private JButton buttonCancel ;
39
- private JButton buttonOK ;
40
70
71
+ private final CommonBundle commonBundle ;
41
72
private final Project project ;
42
73
private final String moduleName ;
43
- private final NewCLICommandValidator validator ;
44
74
private final CamelCaseToSnakeCase toSnakeCase ;
45
75
46
76
/**
@@ -53,8 +83,8 @@ public NewCLICommandDialog(final Project project, final PsiDirectory directory)
53
83
super ();
54
84
this .project = project ;
55
85
this .moduleName = GetModuleNameByDirectoryUtil .execute (directory , project );
56
- this .validator = new NewCLICommandValidator ();
57
86
this .toSnakeCase = CamelCaseToSnakeCase .getInstance ();
87
+ this .commonBundle = new CommonBundle ();
58
88
59
89
setContentPane (contentPane );
60
90
setModal (true );
@@ -145,13 +175,38 @@ public String getCLICommandClassFqn() {
145
175
}
146
176
147
177
private void onOK () {
148
- if (!validator . validate ( this . project , this )) {
178
+ if (!validateFormFields () || ! isPHPClassValid ( )) {
149
179
return ;
150
180
}
151
181
this .generate ();
152
182
this .setVisible (false );
153
183
}
154
184
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
+
155
210
private void generate () {
156
211
try {
157
212
this .generateCLICommandClass ();
0 commit comments