diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewViewModelDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewViewModelDialog.java index e65d7b7c1..3a0cfd8bb 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewViewModelDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewViewModelDialog.java @@ -10,14 +10,19 @@ import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.actions.generation.NewViewModelAction; import com.magento.idea.magento2plugin.actions.generation.data.ViewModelFileData; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewViewModelValidator; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleViewModelClassGenerator; import com.magento.idea.magento2plugin.magento.files.ViewModelPhp; import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -28,14 +33,31 @@ import javax.swing.KeyStroke; public class NewViewModelDialog extends AbstractDialog { - private final NewViewModelValidator validator; + private static final String VIEW_MODEL_NAME = "View Model Name"; + private static final String VIEW_MODEL_DIR = "View Model Directory"; private final PsiDirectory baseDir; private final String moduleName; + private JPanel contentPanel; private JButton buttonOK; private JButton buttonCancel; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, VIEW_MODEL_NAME}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, VIEW_MODEL_NAME}) + @FieldValidation(rule = RuleRegistry.ALPHANUMERIC, + message = {AlphanumericRule.MESSAGE, VIEW_MODEL_NAME}) + @FieldValidation(rule = RuleRegistry.START_WITH_NUMBER_OR_CAPITAL_LETTER, + message = {StartWithNumberOrCapitalLetterRule.MESSAGE, VIEW_MODEL_NAME}) private JTextField viewModelName; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, VIEW_MODEL_DIR}) + @FieldValidation(rule = RuleRegistry.DIRECTORY, + message = {DirectoryRule.MESSAGE, VIEW_MODEL_DIR}) private JTextField viewModelParentDir; + private final Project project; /** @@ -50,7 +72,6 @@ public NewViewModelDialog(final Project project, final PsiDirectory directory) { this.project = project; this.baseDir = directory; this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project); - this.validator = NewViewModelValidator.getInstance(this); setContentPane(contentPanel); setModal(true); @@ -58,33 +79,24 @@ public NewViewModelDialog(final Project project, final PsiDirectory directory) { getRootPane().setDefaultButton(buttonOK); suggestViewModelDirectory(); - buttonOK.addActionListener(new ActionListener() { - public void actionPerformed(final ActionEvent event) { - onOK(); - } - }); - - buttonCancel.addActionListener(new ActionListener() { - public void actionPerformed(final ActionEvent event) { - onCancel(); - } - }); + buttonOK.addActionListener((final ActionEvent event) -> onOK()); + buttonCancel.addActionListener((final ActionEvent event) -> onCancel()); // call onCancel() when cross is clicked setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { + @Override public void windowClosing(final WindowEvent event) { onCancel(); } }); // call onCancel() on ESCAPE - contentPanel.registerKeyboardAction(new ActionListener() { - public void actionPerformed(final ActionEvent event) { - onCancel(); - } - }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), - JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + contentPanel.registerKeyboardAction( + (final ActionEvent event) -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); } /** @@ -101,7 +113,7 @@ public static void open(final Project project, final PsiDirectory directory) { } protected void onOK() { - if (!validator.validate()) { + if (!validateFormFields()) { return; } generateFile(); @@ -171,6 +183,7 @@ private String getNamespace() { return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart; } + @Override public void onCancel() { // add your code here if necessary dispose(); diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewViewModelValidator.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewViewModelValidator.java deleted file mode 100644 index de2efbd58..000000000 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewViewModelValidator.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -package com.magento.idea.magento2plugin.actions.generation.dialog.validator; - -import com.jetbrains.php.refactoring.PhpNameUtil; -import com.magento.idea.magento2plugin.actions.generation.dialog.NewViewModelDialog; -import com.magento.idea.magento2plugin.bundles.CommonBundle; -import com.magento.idea.magento2plugin.bundles.ValidatorBundle; -import com.magento.idea.magento2plugin.util.RegExUtil; -import javax.swing.JOptionPane; - -@SuppressWarnings({ - "PMD.OnlyOneReturn", - "PMD.FieldNamingConventions", - "PMD.DataflowAnomalyAnalysis", - "PMD.NonThreadSafeSingleton", - "PMD.NPathComplexity" -}) -public class NewViewModelValidator { - private static NewViewModelValidator INSTANCE; - private static final String VIEW_MODEL_NAME = "View Model Name"; - private static final String VIEW_MODEL_DIR = "View Model Directory"; - private final ValidatorBundle validatorBundle; - private final CommonBundle commonBundle; - private NewViewModelDialog dialog; - - /** - * Get instance of a class. - * - * @param dialog New view model dialog - * - * @return NewViewModelValidator - */ - public static NewViewModelValidator getInstance(final NewViewModelDialog dialog) { - if (null == INSTANCE) { - INSTANCE = new NewViewModelValidator(); - } - INSTANCE.dialog = dialog; - return INSTANCE; - } - - /** - * New view model validator constructor. - */ - public NewViewModelValidator() { - this.validatorBundle = new ValidatorBundle(); - this.commonBundle = new CommonBundle(); - } - - /** - * Validate whenever new view model dialog data is ready for generation. - * - * @return Boolean - */ - public boolean validate() { - final String errorTitle = commonBundle.message("common.error"); - final String moduleName = dialog.getViewModelName(); - - if (!PhpNameUtil.isValidClassName(moduleName)) { - final String errorMessage = this.validatorBundle.message( - "validator.class.isNotValid", - VIEW_MODEL_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (moduleName.length() == 0) { - final String errorMessage = validatorBundle.message( - "validator.notEmpty", - VIEW_MODEL_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!moduleName.matches(RegExUtil.ALPHANUMERIC)) { - final String errorMessage = validatorBundle.message( - "validator.alphaNumericCharacters", - VIEW_MODEL_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!Character.isUpperCase(moduleName.charAt(0)) - && !Character.isDigit(moduleName.charAt(0)) - ) { - final String errorMessage = validatorBundle.message( - "validator.startWithNumberOrCapitalLetter", - VIEW_MODEL_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final String pluginDirectory = dialog.getViewModelDirectory(); - if (pluginDirectory.length() == 0) { - final String errorMessage = validatorBundle.message( - "validator.notEmpty", - VIEW_MODEL_DIR - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!pluginDirectory.matches(RegExUtil.DIRECTORY)) { - final String errorMessage = validatorBundle.message( - "validator.directory.isNotValid", - VIEW_MODEL_DIR - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - return true; - } -}