10
10
import com .intellij .psi .PsiFile ;
11
11
import com .magento .idea .magento2plugin .actions .generation .NewViewModelAction ;
12
12
import com .magento .idea .magento2plugin .actions .generation .data .ViewModelFileData ;
13
- import com .magento .idea .magento2plugin .actions .generation .dialog .validator .NewViewModelValidator ;
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 .AlphanumericRule ;
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 ;
19
+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .rule .StartWithNumberOrCapitalLetterRule ;
14
20
import com .magento .idea .magento2plugin .actions .generation .generator .ModuleViewModelClassGenerator ;
15
21
import com .magento .idea .magento2plugin .magento .files .ViewModelPhp ;
16
22
import com .magento .idea .magento2plugin .magento .packages .File ;
17
23
import com .magento .idea .magento2plugin .magento .packages .Package ;
18
24
import com .magento .idea .magento2plugin .util .magento .GetModuleNameByDirectoryUtil ;
19
25
import java .awt .event .ActionEvent ;
20
- import java .awt .event .ActionListener ;
21
26
import java .awt .event .KeyEvent ;
22
27
import java .awt .event .WindowAdapter ;
23
28
import java .awt .event .WindowEvent ;
28
33
import javax .swing .KeyStroke ;
29
34
30
35
public class NewViewModelDialog extends AbstractDialog {
31
- private final NewViewModelValidator validator ;
36
+ private static final String VIEW_MODEL_NAME = "View Model Name" ;
37
+ private static final String VIEW_MODEL_DIR = "View Model Directory" ;
32
38
private final PsiDirectory baseDir ;
33
39
private final String moduleName ;
40
+
34
41
private JPanel contentPanel ;
35
42
private JButton buttonOK ;
36
43
private JButton buttonCancel ;
44
+
45
+ @ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
46
+ message = {NotEmptyRule .MESSAGE , VIEW_MODEL_NAME })
47
+ @ FieldValidation (rule = RuleRegistry .PHP_CLASS ,
48
+ message = {PhpClassRule .MESSAGE , VIEW_MODEL_NAME })
49
+ @ FieldValidation (rule = RuleRegistry .ALPHANUMERIC ,
50
+ message = {AlphanumericRule .MESSAGE , VIEW_MODEL_NAME })
51
+ @ FieldValidation (rule = RuleRegistry .START_WITH_NUMBER_OR_CAPITAL_LETTER ,
52
+ message = {StartWithNumberOrCapitalLetterRule .MESSAGE , VIEW_MODEL_NAME })
37
53
private JTextField viewModelName ;
54
+
55
+ @ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
56
+ message = {NotEmptyRule .MESSAGE , VIEW_MODEL_DIR })
57
+ @ FieldValidation (rule = RuleRegistry .DIRECTORY ,
58
+ message = {DirectoryRule .MESSAGE , VIEW_MODEL_DIR })
38
59
private JTextField viewModelParentDir ;
60
+
39
61
private final Project project ;
40
62
41
63
/**
@@ -50,41 +72,31 @@ public NewViewModelDialog(final Project project, final PsiDirectory directory) {
50
72
this .project = project ;
51
73
this .baseDir = directory ;
52
74
this .moduleName = GetModuleNameByDirectoryUtil .execute (directory , project );
53
- this .validator = NewViewModelValidator .getInstance (this );
54
75
55
76
setContentPane (contentPanel );
56
77
setModal (true );
57
78
setTitle ("Create a new Magento 2 View Model." );
58
79
getRootPane ().setDefaultButton (buttonOK );
59
80
suggestViewModelDirectory ();
60
81
61
- buttonOK .addActionListener (new ActionListener () {
62
- public void actionPerformed (final ActionEvent event ) {
63
- onOK ();
64
- }
65
- });
66
-
67
- buttonCancel .addActionListener (new ActionListener () {
68
- public void actionPerformed (final ActionEvent event ) {
69
- onCancel ();
70
- }
71
- });
82
+ buttonOK .addActionListener ((final ActionEvent event ) -> onOK ());
83
+ buttonCancel .addActionListener ((final ActionEvent event ) -> onCancel ());
72
84
73
85
// call onCancel() when cross is clicked
74
86
setDefaultCloseOperation (DO_NOTHING_ON_CLOSE );
75
87
addWindowListener (new WindowAdapter () {
88
+ @ Override
76
89
public void windowClosing (final WindowEvent event ) {
77
90
onCancel ();
78
91
}
79
92
});
80
93
81
94
// call onCancel() on ESCAPE
82
- contentPanel .registerKeyboardAction (new ActionListener () {
83
- public void actionPerformed (final ActionEvent event ) {
84
- onCancel ();
85
- }
86
- }, KeyStroke .getKeyStroke (KeyEvent .VK_ESCAPE , 0 ),
87
- JComponent .WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
95
+ contentPanel .registerKeyboardAction (
96
+ (final ActionEvent event ) -> onCancel (),
97
+ KeyStroke .getKeyStroke (KeyEvent .VK_ESCAPE , 0 ),
98
+ JComponent .WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
99
+ );
88
100
}
89
101
90
102
/**
@@ -101,7 +113,7 @@ public static void open(final Project project, final PsiDirectory directory) {
101
113
}
102
114
103
115
protected void onOK () {
104
- if (!validator . validate ()) {
116
+ if (!validateFormFields ()) {
105
117
return ;
106
118
}
107
119
generateFile ();
@@ -171,6 +183,7 @@ private String getNamespace() {
171
183
return parts [0 ] + Package .fqnSeparator + parts [1 ] + Package .fqnSeparator + directoryPart ;
172
184
}
173
185
186
+ @ Override
174
187
public void onCancel () {
175
188
// add your code here if necessary
176
189
dispose ();
0 commit comments