Skip to content

Commit 284f89b

Browse files
committed
Changed validator for NewCronjobDialog
1 parent f4491c5 commit 284f89b

File tree

7 files changed

+95
-306
lines changed

7 files changed

+95
-306
lines changed

resources/magento2/validation.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ validator.file.noDocumentAssociations={0} file is binary or has no document asso
2020
validator.class.alreadyDeclared={0} already declared in the target module
2121
validator.magentoVersionInvalid=Please specify valid Magento version or use 'any' keyword as default
2222
validator.class.targetClassNotFound=Target class is not found. Check the di.xml file
23-
validator.cronSchedule.invalidExpression={0} has invalid cron schedule expression (e.g. * * * * *)
24-
validator.configPath.invalidFormat={0} has invalid config path format (e.g. section/group/field)
23+
validator.cronSchedule.invalidExpression=The {0} field has does not contain a valid cron schedule expression (e.g. * * * * *)
24+
validator.configPath.invalidFormat=The {0} field has does not contain a valid config path format (e.g. section/group/field)
2525
validator.moduleNameIsTheSameAsPackage=Module name must be different from the package name
2626
validator.mustNotBeEmptyShouldContainLettersOrNumbers=Must not be empty, should contain letters or numbers
2727
validator.magentoRouteIdInvalid=The route id is invalid

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private void addFieldValidationRuleMessageAssociation(
182182

183183
private String resolveFieldValueByComponentType(final Object field) {
184184
if (field instanceof JTextField) {
185-
return ((JTextField) field).getText();
185+
return ((JTextField) field).isEditable() ? ((JTextField) field).getText() : null;
186186
} else if (field instanceof JComboBox) {
187187
return ((JComboBox) field).getSelectedItem().toString();
188188
}

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

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
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.ConfigPathRule;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.CronScheduleRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
19+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
20+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
1421
import com.magento.idea.magento2plugin.actions.generation.generator.CronjobClassGenerator;
1522
import com.magento.idea.magento2plugin.actions.generation.generator.CrontabXmlGenerator;
1623
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
@@ -19,7 +26,6 @@
1926
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
2027
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
2128
import java.awt.event.ActionEvent;
22-
import java.awt.event.ActionListener;
2329
import java.awt.event.FocusEvent;
2430
import java.awt.event.FocusListener;
2531
import java.awt.event.KeyEvent;
@@ -44,28 +50,62 @@
4450
"PMD.AvoidCatchingGenericException",
4551
"PMD.ImmutableField",
4652
"PMD.AccessorMethodGeneration",
53+
"PMD.ExcessiveImports",
4754
})
4855
public class NewCronjobDialog extends AbstractDialog {
4956
private JPanel contentPane;
5057
private JButton buttonOK;
5158
private JButton buttonCancel;
52-
private JTextField cronjobClassNameField;
53-
private JTextField cronjobDirectoryField;
5459
private JRadioButton fixedScheduleRadioButton;
5560
private JRadioButton configurableScheduleRadioButton;
5661
private JRadioButton everyMinuteRadioButton;
5762
private JRadioButton customScheduleRadioButton;
58-
private JTextField cronjobScheduleField;
5963
private JRadioButton atMidnightRadioButton;
6064
private JPanel fixedSchedulePanel;
61-
private JTextField configPathField;
6265
private JPanel configurableSchedulePanel;
63-
private FilteredComboBox cronGroupComboBox;
66+
private static final String CLASS_NAME = "class name";
67+
private static final String DIRECTORY = "directory";
68+
private static final String CRON_NAME = "name";
69+
private static final String SCHEDULE = "schedule";
70+
private static final String CONFIG_PATH = "config path";
71+
private static final String CRON_GROUP = "cron group";
72+
73+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
74+
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
75+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
76+
message = {PhpClassRule.MESSAGE, CLASS_NAME})
77+
private JTextField cronjobClassNameField;
78+
79+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
80+
message = {NotEmptyRule.MESSAGE, DIRECTORY})
81+
@FieldValidation(rule = RuleRegistry.DIRECTORY,
82+
message = {DirectoryRule.MESSAGE, DIRECTORY})
83+
private JTextField cronjobDirectoryField;
84+
85+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
86+
message = {NotEmptyRule.MESSAGE, CRON_NAME})
87+
@FieldValidation(rule = RuleRegistry.IDENTIFIER,
88+
message = {IdentifierRule.MESSAGE, CRON_NAME})
6489
private JTextField cronjobNameField;
6590

91+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
92+
message = {NotEmptyRule.MESSAGE, SCHEDULE})
93+
@FieldValidation(rule = RuleRegistry.CRON_SCHEDULE,
94+
message = {CronScheduleRule.MESSAGE, SCHEDULE})
95+
private JTextField cronjobScheduleField;
96+
97+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
98+
message = {NotEmptyRule.MESSAGE, CONFIG_PATH})
99+
@FieldValidation(rule = RuleRegistry.CONFIG_PATH,
100+
message = {ConfigPathRule.MESSAGE, CONFIG_PATH})
101+
private JTextField configPathField;
102+
103+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
104+
message = {NotEmptyRule.MESSAGE, CRON_GROUP})
105+
private FilteredComboBox cronGroupComboBox;
106+
66107
private Project project;
67108
private String moduleName;
68-
private NewCronjobValidator validator;
69109
private CamelCaseToSnakeCase camelCaseToSnakeCase;
70110

71111
/**
@@ -78,26 +118,29 @@ public NewCronjobDialog(final Project project, final PsiDirectory directory) {
78118
super();
79119
this.project = project;
80120
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
81-
this.validator = NewCronjobValidator.getInstance();
82121
this.camelCaseToSnakeCase = CamelCaseToSnakeCase.getInstance();
83122

84123
setContentPane(contentPane);
85124
setModal(true);
86125
getRootPane().setDefaultButton(buttonOK);
87126
setTitle("Create a new Magento 2 cronjob..");
127+
configPathField.setEditable(false);
88128

89129
buttonOK.addActionListener(e -> onOK());
90130
buttonCancel.addActionListener(e -> onCancel());
91131

92132
fixedScheduleRadioButton.addActionListener(e -> {
93133
configurableSchedulePanel.setVisible(false);
94134
fixedSchedulePanel.setVisible(true);
135+
configPathField.setEditable(false);
95136
});
96137

97138
configurableScheduleRadioButton.addActionListener(e -> {
98139
fixedSchedulePanel.setVisible(false);
99140
configurableSchedulePanel.setVisible(true);
141+
configPathField.setEditable(true);
100142
configPathField.grabFocus();
143+
everyMinuteRadioButton.doClick();
101144
});
102145

103146
everyMinuteRadioButton.addActionListener(e -> {
@@ -139,16 +182,9 @@ public void windowClosing(final WindowEvent event) {
139182
}
140183
});
141184

142-
final ActionListener actionListener = new ActionListener() {
143-
@Override
144-
public void actionPerformed(final ActionEvent event) {
145-
onCancel();
146-
}
147-
};
148-
149185
// call onCancel() on ESCAPE
150186
contentPane.registerKeyboardAction(
151-
actionListener,
187+
(final ActionEvent event) -> onCancel(),
152188
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
153189
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
154190
);
@@ -237,7 +273,7 @@ private String suggestCronjobName(final String cronjobClassname) {
237273
* When new cronjob dialog is filled, validate the input data and generate a new cronjob.
238274
*/
239275
private void onOK() {
240-
if (!validator.validate(this.project,this)) {
276+
if (!validateFormFields()) {
241277
return;
242278
}
243279

0 commit comments

Comments
 (0)