1010import com .intellij .psi .PsiFile ;
1111import com .magento .idea .magento2plugin .actions .generation .NewViewModelAction ;
1212import 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 ;
1420import com .magento .idea .magento2plugin .actions .generation .generator .ModuleViewModelClassGenerator ;
1521import com .magento .idea .magento2plugin .magento .files .ViewModelPhp ;
1622import com .magento .idea .magento2plugin .magento .packages .File ;
1723import com .magento .idea .magento2plugin .magento .packages .Package ;
1824import com .magento .idea .magento2plugin .util .magento .GetModuleNameByDirectoryUtil ;
1925import java .awt .event .ActionEvent ;
20- import java .awt .event .ActionListener ;
2126import java .awt .event .KeyEvent ;
2227import java .awt .event .WindowAdapter ;
2328import java .awt .event .WindowEvent ;
2833import javax .swing .KeyStroke ;
2934
3035public 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" ;
3238 private final PsiDirectory baseDir ;
3339 private final String moduleName ;
40+
3441 private JPanel contentPanel ;
3542 private JButton buttonOK ;
3643 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 })
3753 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 })
3859 private JTextField viewModelParentDir ;
60+
3961 private final Project project ;
4062
4163 /**
@@ -50,41 +72,31 @@ public NewViewModelDialog(final Project project, final PsiDirectory directory) {
5072 this .project = project ;
5173 this .baseDir = directory ;
5274 this .moduleName = GetModuleNameByDirectoryUtil .execute (directory , project );
53- this .validator = NewViewModelValidator .getInstance (this );
5475
5576 setContentPane (contentPanel );
5677 setModal (true );
5778 setTitle ("Create a new Magento 2 View Model." );
5879 getRootPane ().setDefaultButton (buttonOK );
5980 suggestViewModelDirectory ();
6081
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 ());
7284
7385 // call onCancel() when cross is clicked
7486 setDefaultCloseOperation (DO_NOTHING_ON_CLOSE );
7587 addWindowListener (new WindowAdapter () {
88+ @ Override
7689 public void windowClosing (final WindowEvent event ) {
7790 onCancel ();
7891 }
7992 });
8093
8194 // 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+ );
88100 }
89101
90102 /**
@@ -101,7 +113,7 @@ public static void open(final Project project, final PsiDirectory directory) {
101113 }
102114
103115 protected void onOK () {
104- if (!validator . validate ()) {
116+ if (!validateFormFields ()) {
105117 return ;
106118 }
107119 generateFile ();
@@ -171,6 +183,7 @@ private String getNamespace() {
171183 return parts [0 ] + Package .fqnSeparator + parts [1 ] + Package .fqnSeparator + directoryPart ;
172184 }
173185
186+ @ Override
174187 public void onCancel () {
175188 // add your code here if necessary
176189 dispose ();
0 commit comments