Skip to content

Changed validator for CreateAPluginDialog #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/magento2/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions resources/magento2/validation.properties
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="magento2/common" key="common.name"/>
<text resource-bundle="magento2/common" key="common.pluginName"/>
</properties>
</component>
<component id="9431c" class="javax.swing.JTextField" binding="pluginName">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -77,25 +115,15 @@ public CreateAPluginDialog(
this.project = project;
this.targetMethod = targetMethod;
this.targetClass = targetClass;
this.validator = CreateAPluginDialogValidator.getInstance(this);

setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
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() {
Expand All @@ -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
);
}
Expand All @@ -126,7 +152,7 @@ private void fillTargetAreaOptions() {
}

protected void onOK() {
if (!validator.validate(project)) {
if (!validateFormFields()) {
return;
}
new PluginClassGenerator(new PluginFileData(
Expand Down
Loading