Skip to content

Commit edc0e82

Browse files
author
Vitaliy
authored
Merge pull request #339 from drpayyne/issue-299
Changed validator for NewCronjobDialog
2 parents 7889b1f + 0670240 commit edc0e82

File tree

9 files changed

+137
-308
lines changed

9 files changed

+137
-308
lines changed

resources/magento2/validation.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
validator.notEmpty=The {0} field must not be empty
2+
validator.box.notEmpty=The {0} field must contain a valid selection from the dropdown
23
validator.package.validPath=Please specify a valid Magento 2 installation path
34
validator.alphaNumericCharacters=The {0} field must contain letters and numbers only
45
validator.alphaNumericAndUnderscoreCharacters={0} must contain letters, numbers and underscores only
@@ -21,8 +22,8 @@ validator.file.noDocumentAssociations={0} file is binary or has no document asso
2122
validator.class.alreadyDeclared={0} already declared in the target module
2223
validator.magentoVersionInvalid=Please specify valid Magento version or use 'any' keyword as default
2324
validator.class.targetClassNotFound=Target class is not found. Check the di.xml file
24-
validator.cronSchedule.invalidExpression={0} has invalid cron schedule expression (e.g. * * * * *)
25-
validator.configPath.invalidFormat={0} has invalid config path format (e.g. section/group/field)
25+
validator.cronSchedule.invalidExpression=The {0} field has does not contain a valid cron schedule expression (e.g. * * * * *)
26+
validator.configPath.invalidFormat=The {0} field has does not contain a valid config path format (e.g. section/group/field)
2627
validator.moduleNameIsTheSameAsPackage=Module name must be different from the package name
2728
validator.mustNotBeEmptyShouldContainLettersOrNumbers=Must not be empty, should contain letters or numbers
2829
validator.magentoRouteIdInvalid=The route id is invalid

src/com/magento/idea/magento2plugin/actions/generation/dialog/AbstractDialog.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ private void addFieldValidationRuleMessageAssociation(
183183

184184
private String resolveFieldValueByComponentType(final Object field) {
185185
if (field instanceof JTextField) {
186-
return ((JTextField) field).getText();
186+
return ((JTextField) field).isEditable() ? ((JTextField) field).getText() : null;
187187
} else if (field instanceof JComboBox) {
188-
return ((JComboBox) field).getSelectedItem().toString();
188+
if (((JComboBox<?>) field).getSelectedIndex() == -1) {
189+
return "";
190+
} else {
191+
return ((JComboBox) field).getSelectedItem().toString();
192+
}
189193
} else if (field instanceof JTextArea) {
190194
return ((JTextArea) field).getText();
191195
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCronjobDialog.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
import com.magento.idea.magento2plugin.actions.generation.NewCronjobAction;
1111
import com.magento.idea.magento2plugin.actions.generation.data.CronjobClassData;
1212
import com.magento.idea.magento2plugin.actions.generation.data.CrontabXmlData;
13-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewCronjobValidator;
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.BoxNotEmptyRule;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ConfigPathRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.CronScheduleRule;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
19+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
20+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
21+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
1422
import com.magento.idea.magento2plugin.actions.generation.generator.CronjobClassGenerator;
1523
import com.magento.idea.magento2plugin.actions.generation.generator.CrontabXmlGenerator;
1624
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
@@ -19,7 +27,6 @@
1927
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
2028
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
2129
import java.awt.event.ActionEvent;
22-
import java.awt.event.ActionListener;
2330
import java.awt.event.FocusEvent;
2431
import java.awt.event.FocusListener;
2532
import java.awt.event.KeyEvent;
@@ -44,28 +51,64 @@
4451
"PMD.AvoidCatchingGenericException",
4552
"PMD.ImmutableField",
4653
"PMD.AccessorMethodGeneration",
54+
"PMD.ExcessiveImports",
4755
})
4856
public class NewCronjobDialog extends AbstractDialog {
4957
private JPanel contentPane;
5058
private JButton buttonOK;
5159
private JButton buttonCancel;
52-
private JTextField cronjobClassNameField;
53-
private JTextField cronjobDirectoryField;
5460
private JRadioButton fixedScheduleRadioButton;
5561
private JRadioButton configurableScheduleRadioButton;
5662
private JRadioButton everyMinuteRadioButton;
5763
private JRadioButton customScheduleRadioButton;
58-
private JTextField cronjobScheduleField;
5964
private JRadioButton atMidnightRadioButton;
6065
private JPanel fixedSchedulePanel;
61-
private JTextField configPathField;
6266
private JPanel configurableSchedulePanel;
63-
private FilteredComboBox cronGroupComboBox;
67+
private static final String CLASS_NAME = "class name";
68+
private static final String DIRECTORY = "directory";
69+
private static final String CRON_NAME = "name";
70+
private static final String SCHEDULE = "schedule";
71+
private static final String CONFIG_PATH = "config path";
72+
private static final String CRON_GROUP = "cron group";
73+
74+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
75+
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
76+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
77+
message = {PhpClassRule.MESSAGE, CLASS_NAME})
78+
private JTextField cronjobClassNameField;
79+
80+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
81+
message = {NotEmptyRule.MESSAGE, DIRECTORY})
82+
@FieldValidation(rule = RuleRegistry.DIRECTORY,
83+
message = {DirectoryRule.MESSAGE, DIRECTORY})
84+
private JTextField cronjobDirectoryField;
85+
86+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
87+
message = {NotEmptyRule.MESSAGE, CRON_NAME})
88+
@FieldValidation(rule = RuleRegistry.IDENTIFIER,
89+
message = {IdentifierRule.MESSAGE, CRON_NAME})
6490
private JTextField cronjobNameField;
6591

92+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
93+
message = {NotEmptyRule.MESSAGE, SCHEDULE})
94+
@FieldValidation(rule = RuleRegistry.CRON_SCHEDULE,
95+
message = {CronScheduleRule.MESSAGE, SCHEDULE})
96+
private JTextField cronjobScheduleField;
97+
98+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
99+
message = {NotEmptyRule.MESSAGE, CONFIG_PATH})
100+
@FieldValidation(rule = RuleRegistry.CONFIG_PATH,
101+
message = {ConfigPathRule.MESSAGE, CONFIG_PATH})
102+
private JTextField configPathField;
103+
104+
@FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY,
105+
message = {BoxNotEmptyRule.MESSAGE, CRON_GROUP})
106+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
107+
message = {NotEmptyRule.MESSAGE, CRON_GROUP})
108+
private FilteredComboBox cronGroupComboBox;
109+
66110
private Project project;
67111
private String moduleName;
68-
private NewCronjobValidator validator;
69112
private CamelCaseToSnakeCase camelCaseToSnakeCase;
70113

71114
/**
@@ -78,26 +121,29 @@ public NewCronjobDialog(final Project project, final PsiDirectory directory) {
78121
super();
79122
this.project = project;
80123
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
81-
this.validator = NewCronjobValidator.getInstance();
82124
this.camelCaseToSnakeCase = CamelCaseToSnakeCase.getInstance();
83125

84126
setContentPane(contentPane);
85127
setModal(true);
86128
getRootPane().setDefaultButton(buttonOK);
87129
setTitle("Create a new Magento 2 cronjob..");
130+
configPathField.setEditable(false);
88131

89132
buttonOK.addActionListener(e -> onOK());
90133
buttonCancel.addActionListener(e -> onCancel());
91134

92135
fixedScheduleRadioButton.addActionListener(e -> {
93136
configurableSchedulePanel.setVisible(false);
94137
fixedSchedulePanel.setVisible(true);
138+
configPathField.setEditable(false);
95139
});
96140

97141
configurableScheduleRadioButton.addActionListener(e -> {
98142
fixedSchedulePanel.setVisible(false);
99143
configurableSchedulePanel.setVisible(true);
144+
configPathField.setEditable(true);
100145
configPathField.grabFocus();
146+
everyMinuteRadioButton.doClick();
101147
});
102148

103149
everyMinuteRadioButton.addActionListener(e -> {
@@ -139,16 +185,9 @@ public void windowClosing(final WindowEvent event) {
139185
}
140186
});
141187

142-
final ActionListener actionListener = new ActionListener() {
143-
@Override
144-
public void actionPerformed(final ActionEvent event) {
145-
onCancel();
146-
}
147-
};
148-
149188
// call onCancel() on ESCAPE
150189
contentPane.registerKeyboardAction(
151-
actionListener,
190+
(final ActionEvent event) -> onCancel(),
152191
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
153192
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
154193
);
@@ -237,7 +276,7 @@ private String suggestCronjobName(final String cronjobClassname) {
237276
* When new cronjob dialog is filled, validate the input data and generate a new cronjob.
238277
*/
239278
private void onOK() {
240-
if (!validator.validate(this.project,this)) {
279+
if (!validateFormFields()) {
241280
return;
242281
}
243282

0 commit comments

Comments
 (0)