From 5b037e53ec5ed08392b4885af28cbfd1db348d05 Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Fri, 23 Oct 2020 00:26:49 +0530 Subject: [PATCH 1/2] Changed validator for CreateAnObserverDialog --- .../dialog/CreateAnObserverDialog.java | 42 +++- .../CreateAnObserverDialogValidator.java | 198 ------------------ .../dialog/validator/rule/PhpClassRule.java | 3 +- 3 files changed, 35 insertions(+), 208 deletions(-) delete mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAnObserverDialogValidator.java diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java index fc44c4618..aa95028cd 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java @@ -10,7 +10,12 @@ import com.magento.idea.magento2plugin.actions.generation.CreateAnObserverAction; import com.magento.idea.magento2plugin.actions.generation.data.ObserverEventsXmlData; import com.magento.idea.magento2plugin.actions.generation.data.ObserverFileData; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.CreateAnObserverDialogValidator; +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.AlphanumericWithUnderscoreRule; +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.generator.ObserverClassGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.ObserverEventsXmlGenerator; import com.magento.idea.magento2plugin.indexes.ModuleIndex; @@ -40,22 +45,42 @@ public class CreateAnObserverDialog extends AbstractDialog { @NotNull private final Project project; - @NotNull - private final CreateAnObserverDialogValidator validator; private final String targetEvent; private JPanel contentPane; private JButton buttonOK; private JButton buttonCancel; - private JTextField observerClassName; - private JTextField observerDirectory; - private FilteredComboBox observerModule; private JComboBox observerArea; - private JTextField observerName; private JLabel observerClassNameLabel; private JLabel observerDirectoryName; private JLabel selectObserverModule; private JLabel observerAreaLabel; private JLabel observerNameLabel; + private static final String OBSERVER_MODULE = "target module"; + private static final String OBSERVER_CLASS = "class name"; + private static final String OBSERVER_DIRECTORY = "directory"; + private static final String OBSERVER_NAME = "name"; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, OBSERVER_MODULE}) + private FilteredComboBox observerModule; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, OBSERVER_CLASS}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, OBSERVER_CLASS}) + private JTextField observerClassName; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, OBSERVER_DIRECTORY}) + @FieldValidation(rule = RuleRegistry.DIRECTORY, + message = {DirectoryRule.MESSAGE, OBSERVER_DIRECTORY}) + private JTextField observerDirectory; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, OBSERVER_NAME}) + @FieldValidation(rule = RuleRegistry.ALPHANUMERIC_WITH_UNDERSCORE, + message = {AlphanumericWithUnderscoreRule.MESSAGE, OBSERVER_NAME}) + private JTextField observerName; /** * Constructor. @@ -68,7 +93,6 @@ public CreateAnObserverDialog(@NotNull final Project project, final String targe this.project = project; this.targetEvent = targetEvent; - this.validator = CreateAnObserverDialogValidator.getInstance(this); setContentPane(contentPane); setModal(true); @@ -119,7 +143,7 @@ private void fillTargetAreaOptions() { * Perform code generation using input data. */ private void onOK() { - if (!validator.validate(project)) { + if (!validateFormFields()) { return; } new ObserverClassGenerator(new ObserverFileData( diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAnObserverDialogValidator.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAnObserverDialogValidator.java deleted file mode 100644 index b30661219..000000000 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/CreateAnObserverDialogValidator.java +++ /dev/null @@ -1,198 +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.CreateAnObserverDialog; -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.FieldNamingConventions", - "PMD.CyclomaticComplexity", - "PMD.NonThreadSafeSingleton", - "PMD.ExcessiveMethodLength", - "PMD.DataflowAnomalyAnalysis", - "PMD.NPathComplexity" -}) -public class CreateAnObserverDialogValidator { - private static final String OBSERVER_CLASS_NAME = "Observer Class Name"; - private static CreateAnObserverDialogValidator INSTANCE; - private final ValidatorBundle validatorBundle; - private final CommonBundle commonBundle; - private CreateAnObserverDialog dialog; - - /** - * Get instance of a class. - * - * @param dialog Create observer dialog - * - * @return CreateAnObserverDialogValidator - */ - public static CreateAnObserverDialogValidator getInstance(final CreateAnObserverDialog dialog) { - if (null == INSTANCE) { - INSTANCE = new CreateAnObserverDialogValidator(); - } - INSTANCE.dialog = dialog; - return INSTANCE; - } - - /** - * Create an observer dialog validator. - */ - public CreateAnObserverDialogValidator() { - this.validatorBundle = new ValidatorBundle(); - this.commonBundle = new CommonBundle(); - } - - /** - * Validate whenever new create observer 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 observerClassName = dialog.getObserverClassName(); - - if (!PhpNameUtil.isValidClassName(observerClassName)) { - final String errorMessage = this.validatorBundle.message( - "validator.class.isNotValid", - OBSERVER_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (observerClassName.length() == 0) { - final String errorMessage = validatorBundle.message( - "validator.notEmpty", - OBSERVER_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!observerClassName.matches(RegExUtil.ALPHANUMERIC)) { - final String errorMessage = validatorBundle.message( - "validator.alphaNumericCharacters", - OBSERVER_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!Character.isUpperCase(observerClassName.charAt(0)) - && !Character.isDigit(observerClassName.charAt(0)) - ) { - final String errorMessage = validatorBundle.message( - "validator.startWithNumberOrCapitalLetter", - OBSERVER_CLASS_NAME - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final String observerDirectory = dialog.getObserverDirectory(); - - if (observerDirectory.length() == 0) { - final String errorMessage = validatorBundle.message( - "validator.notEmpty", - "Observer Directory" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - if (!observerDirectory.matches(RegExUtil.DIRECTORY)) { - final String errorMessage = validatorBundle.message( - "validator.directory.isNotValid", - "Observer Directory" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - - final String observerModule = dialog.getObserverModule(); - if (observerModule.length() == 0) { - final String errorMessage = validatorBundle.message( - "validator.notEmpty", - "Observer Module" - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - final List allModulesList = ModuleIndex.getInstance(project) - .getEditableModuleNames(); - if (!allModulesList.contains(observerModule)) { - final String errorMessage = validatorBundle.message( - "validator.module.noSuchModule", - observerModule - ); - JOptionPane.showMessageDialog( - null, - errorMessage, - errorTitle, - JOptionPane.ERROR_MESSAGE - ); - - return false; - } - - return true; - } -} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/PhpClassRule.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/PhpClassRule.java index d128c5d95..d0e568456 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/PhpClassRule.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/PhpClassRule.java @@ -5,6 +5,7 @@ package com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule; +import com.jetbrains.php.refactoring.PhpNameUtil; import com.magento.idea.magento2plugin.util.RegExUtil; public class PhpClassRule implements ValidationRule { @@ -13,7 +14,7 @@ public class PhpClassRule implements ValidationRule { @Override public boolean check(final String value) { - return value.matches(RegExUtil.Magento.PHP_CLASS); + return value.matches(RegExUtil.Magento.PHP_CLASS) && PhpNameUtil.isValidClassName(value); } public static ValidationRule getInstance() { From a1df9505d15618a4bdcd36e469e7bc855124bc1d Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Fri, 23 Oct 2020 01:12:29 +0530 Subject: [PATCH 2/2] Fixed static test failures --- .../actions/generation/dialog/CreateAnObserverDialog.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java index aa95028cd..2cb2ef782 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java @@ -41,6 +41,7 @@ "PMD.MissingSerialVersionUID", "PMD.DataClass", "PMD.UnusedPrivateField", + "PMD.ExcessiveImports", }) public class CreateAnObserverDialog extends AbstractDialog { @NotNull