diff --git a/resources/magento2/common.properties b/resources/magento2/common.properties index 1fa9b8e10..889008f79 100644 --- a/resources/magento2/common.properties +++ b/resources/magento2/common.properties @@ -22,8 +22,8 @@ common.module.target=Target Module common.theme.target=Target Theme common.area.target=Target Area common.name=Name -common.className=Class name -common.argument=Argument name +common.className=Class Name +common.pluginName=Plugin Name common.directoryPath=Directory Path common.methodType=Method Type common.sortOrder=Sort Order diff --git a/resources/magento2/validation.properties b/resources/magento2/validation.properties index d3648e78e..df750cfae 100644 --- a/resources/magento2/validation.properties +++ b/resources/magento2/validation.properties @@ -1,16 +1,16 @@ validator.notEmpty=The {0} field must not be empty validator.package.validPath=Please specify a valid Magento 2 installation path -validator.alphaNumericCharacters=The {0} must contain letters and numbers only +validator.alphaNumericCharacters=The {0} field must contain letters and numbers only validator.alphaNumericAndUnderscoreCharacters={0} must contain letters, numbers and underscores only -validator.alreadyDeclared={0} is already declared in the {1} module. -validator.startWithNumberOrCapitalLetter=The {0} must start from a number or a capital letter -validator.onlyNumbers={0} must contain numbers only +validator.alreadyDeclared={0} is already declared in the {1} module +validator.startWithNumberOrCapitalLetter=The {0} field must start with a number or a capital letter +validator.onlyNumbers=The {0} field must contain numbers only validator.mustNotBeNegative={0} must not be negative -validator.identifier={0} must contain letters, numbers, dashes, and underscores only +validator.identifier=The {0} field must contain letters, numbers, dashes, and underscores only validator.class.isNotValid=The {0} field does not contain a valid class name validator.class.shouldBeUnique=Duplicated class {0} -validator.namespace.isNotValid=The {0} is not valid namespace name -validator.directory.isNotValid={0} is not valid +validator.namespace.isNotValid=The {0} field does not contain a valid namespace +validator.directory.isNotValid=The {0} field does not contain a valid directory validator.directory.php.isNotValid=The {0} field does not contain a valid PHP directory validator.command.isNotValid=The {0} field does not contain a valid Magento 2 CLI command validator.module.noSuchModule=No such module {0} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form index a0094bfb7..4434c7a9b 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form @@ -108,7 +108,7 @@ - + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java index edfea1381..2232581e4 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java @@ -11,7 +11,13 @@ import com.magento.idea.magento2plugin.actions.generation.CreateAPluginAction; import com.magento.idea.magento2plugin.actions.generation.data.PluginDiXmlData; import com.magento.idea.magento2plugin.actions.generation.data.PluginFileData; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.CreateAPluginDialogValidator; +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.DirectoryRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NumericRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule; import com.magento.idea.magento2plugin.actions.generation.generator.PluginClassGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.PluginDiXmlGenerator; import com.magento.idea.magento2plugin.indexes.ModuleIndex; @@ -21,7 +27,6 @@ import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.ui.FilteredComboBox; 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; @@ -35,24 +40,57 @@ import javax.swing.KeyStroke; import org.jetbrains.annotations.NotNull; -@SuppressWarnings({"PMD.TooManyFields", "PMD.DataClass", "PMD.UnusedPrivateMethod"}) +@SuppressWarnings({ + "PMD.TooManyFields", + "PMD.DataClass", + "PMD.UnusedPrivateMethod", + "PMD.ExcessiveImports" +}) public class CreateAPluginDialog extends AbstractDialog { @NotNull private final Project project; private final Method targetMethod; private final PhpClass targetClass; - @NotNull - private final CreateAPluginDialogValidator validator; private JPanel contentPane; private JButton buttonOK; private JButton buttonCancel; - private JTextField pluginClassName; - private JTextField pluginDirectory; private JComboBox pluginType; - private FilteredComboBox pluginModule; private JComboBox pluginArea; + + private static final String CLASS_NAME = "class name"; + private static final String DIRECTORY = "directory path"; + private static final String SORT_ORDER = "sort order"; + private static final String PLUGIN_NAME = "plugin name"; + private static final String TARGET_MODULE = "target module"; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, TARGET_MODULE}) + private FilteredComboBox pluginModule; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, CLASS_NAME}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, CLASS_NAME}) + private JTextField pluginClassName; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, DIRECTORY}) + @FieldValidation(rule = RuleRegistry.DIRECTORY, + message = {DirectoryRule.MESSAGE, DIRECTORY}) + private JTextField pluginDirectory; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, SORT_ORDER}) + @FieldValidation(rule = RuleRegistry.NUMERIC, + message = {NumericRule.MESSAGE, SORT_ORDER}) private JTextField pluginSortOrder; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, PLUGIN_NAME}) + @FieldValidation(rule = RuleRegistry.IDENTIFIER, + message = {IdentifierRule.MESSAGE, PLUGIN_NAME}) private JTextField pluginName; + private JLabel pluginDirectoryName;//NOPMD private JLabel selectPluginModule;//NOPMD private JLabel pluginTypeLabel;//NOPMD @@ -77,7 +115,6 @@ public CreateAPluginDialog( this.project = project; this.targetMethod = targetMethod; this.targetClass = targetClass; - this.validator = CreateAPluginDialogValidator.getInstance(this); setContentPane(contentPane); setModal(true); @@ -85,17 +122,8 @@ public CreateAPluginDialog( fillPluginTypeOptions(); fillTargetAreaOptions(); - 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()); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { @@ -104,11 +132,9 @@ public void windowClosing(final WindowEvent event) { } }); - contentPane.registerKeyboardAction(new ActionListener() { - public void actionPerformed(final ActionEvent event) { - onCancel(); - } - }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + contentPane.registerKeyboardAction( + (final ActionEvent event) -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); } @@ -126,7 +152,7 @@ private void fillTargetAreaOptions() { } protected void onOK() { - if (!validator.validate(project)) { + if (!validateFormFields()) { return; } new PluginClassGenerator(new PluginFileData( diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAPluginDialogValidator.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAPluginDialogValidator.java deleted file mode 100644 index b22f9da9f..000000000 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAPluginDialogValidator.java +++ /dev/null @@ -1,259 +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.intellij.openapi.project.Project; -import com.jetbrains.php.refactoring.PhpNameUtil; -import com.magento.idea.magento2plugin.actions.generation.dialog.CreateAPluginDialog; -import com.magento.idea.magento2plugin.bundles.CommonBundle; -import com.magento.idea.magento2plugin.bundles.ValidatorBundle; -import com.magento.idea.magento2plugin.indexes.ModuleIndex; -import com.magento.idea.magento2plugin.util.RegExUtil; -import java.util.List; -import javax.swing.JOptionPane; - -@SuppressWarnings({ - "PMD.OnlyOneReturn", - "PMD.CyclomaticComplexity", - "PMD.NonThreadSafeSingleton", - "PMD.ExcessiveMethodLength", - "PMD.NPathComplexity", - "PMD.FieldNamingConventions" -}) -public class CreateAPluginDialogValidator { - private static final String NOT_EMPTY = "validator.notEmpty"; - private static final String PLUGIN_CLASS_NAME = "Plugin Class Name"; - private static CreateAPluginDialogValidator INSTANCE; - private final ValidatorBundle validatorBundle; - private final CommonBundle commonBundle; - private CreateAPluginDialog dialog; - - /** - * Get instance of a class. - * - * @param dialog Create plugin dialog - * - * @return CreateAPluginDialogValidator - */ - public static CreateAPluginDialogValidator getInstance(final CreateAPluginDialog dialog) { - if (null == INSTANCE) { - INSTANCE = new CreateAPluginDialogValidator(); - } - - INSTANCE.dialog = dialog; - return INSTANCE; - } - - /** - * Create a plugin dialog validator. - */ - public CreateAPluginDialogValidator() { - this.validatorBundle = new ValidatorBundle(); - this.commonBundle = new CommonBundle(); - } - - /** - * Validate whenever new create plugin dialog data is ready for generation. - * - * @param project Project - * - * @return Boolean - */ - public boolean validate(final Project project) { - final String errorTitle = commonBundle.message("common.error"); - final String pluginClassName = dialog.getPluginClassName(); - - if (!PhpNameUtil.isValidClassName(pluginClassName)) { - final String errorMessage = this.validatorBundle.message( - "validator.class.isNotValid", - PLUGIN_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (pluginClassName.length() == 0) { - final String errorMessage = validatorBundle.message( - NOT_EMPTY, - PLUGIN_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!pluginClassName.matches(RegExUtil.ALPHANUMERIC)) { - final String errorMessage = validatorBundle.message( - "validator.alphaNumericCharacters", - PLUGIN_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!Character.isUpperCase(pluginClassName.charAt(0)) - && !Character.isDigit(pluginClassName.charAt(0)) - ) { - final String errorMessage = validatorBundle.message( - "validator.startWithNumberOrCapitalLetter", - PLUGIN_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final String pluginDirectory = dialog.getPluginDirectory(); - if (pluginDirectory.length() == 0) { - final String errorMessage = validatorBundle.message( - NOT_EMPTY, - "Plugin Directory" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!pluginDirectory.matches(RegExUtil.DIRECTORY)) { - final String errorMessage = validatorBundle.message( - "validator.directory.isNotValid", - "Plugin Directory" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final String pluginName = dialog.getPluginName(); - if (pluginName.length() == 0) { - final String errorMessage = validatorBundle.message( - NOT_EMPTY, - "Plugin Name" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!pluginName.matches(RegExUtil.IDENTIFIER)) { - final String errorMessage = validatorBundle.message( - "validator.identifier", - "Plugin Name" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final String sortOrder = dialog.getPluginSortOrder(); - if (sortOrder.length() == 0) { - final String errorMessage = validatorBundle.message( - NOT_EMPTY, - "Sort Order" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!sortOrder.matches(RegExUtil.NUMERIC)) { - final String errorMessage = validatorBundle.message( - "validator.onlyNumbers", - "Sort Order" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final String pluginModule = dialog.getPluginModule(); - if (pluginModule.length() == 0) { - final String errorMessage = validatorBundle.message( - NOT_EMPTY, - "Plugin Module" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final List allModulesList = ModuleIndex - .getInstance(project).getEditableModuleNames(); - if (!allModulesList.contains(pluginModule)) { - final String errorMessage = validatorBundle.message( - "validator.module.noSuchModule", - pluginModule - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - return true; - } -}