Skip to content
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
5 changes: 3 additions & 2 deletions resources/magento2/validation.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
validator.notEmpty=The {0} field must not be empty
validator.box.notEmpty=The {0} field must contain a valid selection from the dropdown
validator.package.validPath=Please specify a valid Magento 2 installation path
validator.alphaNumericCharacters=The {0} field must contain letters and numbers only
validator.alphaNumericAndUnderscoreCharacters={0} must contain letters, numbers and underscores only
Expand All @@ -21,8 +22,8 @@ validator.file.noDocumentAssociations={0} file is binary or has no document asso
validator.class.alreadyDeclared={0} already declared in the target module
validator.magentoVersionInvalid=Please specify valid Magento version or use 'any' keyword as default
validator.class.targetClassNotFound=Target class is not found. Check the di.xml file
validator.cronSchedule.invalidExpression={0} has invalid cron schedule expression (e.g. * * * * *)
validator.configPath.invalidFormat={0} has invalid config path format (e.g. section/group/field)
validator.cronSchedule.invalidExpression=The {0} field has does not contain a valid cron schedule expression (e.g. * * * * *)
validator.configPath.invalidFormat=The {0} field has does not contain a valid config path format (e.g. section/group/field)
validator.moduleNameIsTheSameAsPackage=Module name must be different from the package name
validator.mustNotBeEmptyShouldContainLettersOrNumbers=Must not be empty, should contain letters or numbers
validator.magentoRouteIdInvalid=The route id is invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@ private void addFieldValidationRuleMessageAssociation(

private String resolveFieldValueByComponentType(final Object field) {
if (field instanceof JTextField) {
return ((JTextField) field).getText();
return ((JTextField) field).isEditable() ? ((JTextField) field).getText() : null;
} else if (field instanceof JComboBox) {
return ((JComboBox) field).getSelectedItem().toString();
if (((JComboBox<?>) field).getSelectedIndex() == -1) {
return "";
} else {
return ((JComboBox) field).getSelectedItem().toString();
}
} else if (field instanceof JTextArea) {
return ((JTextArea) field).getText();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
import com.magento.idea.magento2plugin.actions.generation.NewCronjobAction;
import com.magento.idea.magento2plugin.actions.generation.data.CronjobClassData;
import com.magento.idea.magento2plugin.actions.generation.data.CrontabXmlData;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewCronjobValidator;
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.BoxNotEmptyRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ConfigPathRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.CronScheduleRule;
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.PhpClassRule;
import com.magento.idea.magento2plugin.actions.generation.generator.CronjobClassGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.CrontabXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
Expand All @@ -19,7 +27,6 @@
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
Expand All @@ -44,28 +51,64 @@
"PMD.AvoidCatchingGenericException",
"PMD.ImmutableField",
"PMD.AccessorMethodGeneration",
"PMD.ExcessiveImports",
})
public class NewCronjobDialog extends AbstractDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JTextField cronjobClassNameField;
private JTextField cronjobDirectoryField;
private JRadioButton fixedScheduleRadioButton;
private JRadioButton configurableScheduleRadioButton;
private JRadioButton everyMinuteRadioButton;
private JRadioButton customScheduleRadioButton;
private JTextField cronjobScheduleField;
private JRadioButton atMidnightRadioButton;
private JPanel fixedSchedulePanel;
private JTextField configPathField;
private JPanel configurableSchedulePanel;
private FilteredComboBox cronGroupComboBox;
private static final String CLASS_NAME = "class name";
private static final String DIRECTORY = "directory";
private static final String CRON_NAME = "name";
private static final String SCHEDULE = "schedule";
private static final String CONFIG_PATH = "config path";
private static final String CRON_GROUP = "cron group";

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
message = {PhpClassRule.MESSAGE, CLASS_NAME})
private JTextField cronjobClassNameField;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, DIRECTORY})
@FieldValidation(rule = RuleRegistry.DIRECTORY,
message = {DirectoryRule.MESSAGE, DIRECTORY})
private JTextField cronjobDirectoryField;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, CRON_NAME})
@FieldValidation(rule = RuleRegistry.IDENTIFIER,
message = {IdentifierRule.MESSAGE, CRON_NAME})
private JTextField cronjobNameField;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, SCHEDULE})
@FieldValidation(rule = RuleRegistry.CRON_SCHEDULE,
message = {CronScheduleRule.MESSAGE, SCHEDULE})
private JTextField cronjobScheduleField;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, CONFIG_PATH})
@FieldValidation(rule = RuleRegistry.CONFIG_PATH,
message = {ConfigPathRule.MESSAGE, CONFIG_PATH})
private JTextField configPathField;

@FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY,
message = {BoxNotEmptyRule.MESSAGE, CRON_GROUP})
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, CRON_GROUP})
private FilteredComboBox cronGroupComboBox;

private Project project;
private String moduleName;
private NewCronjobValidator validator;
private CamelCaseToSnakeCase camelCaseToSnakeCase;

/**
Expand All @@ -78,26 +121,29 @@ public NewCronjobDialog(final Project project, final PsiDirectory directory) {
super();
this.project = project;
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
this.validator = NewCronjobValidator.getInstance();
this.camelCaseToSnakeCase = CamelCaseToSnakeCase.getInstance();

setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
setTitle("Create a new Magento 2 cronjob..");
configPathField.setEditable(false);

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

fixedScheduleRadioButton.addActionListener(e -> {
configurableSchedulePanel.setVisible(false);
fixedSchedulePanel.setVisible(true);
configPathField.setEditable(false);
});

configurableScheduleRadioButton.addActionListener(e -> {
fixedSchedulePanel.setVisible(false);
configurableSchedulePanel.setVisible(true);
configPathField.setEditable(true);
configPathField.grabFocus();
everyMinuteRadioButton.doClick();
});

everyMinuteRadioButton.addActionListener(e -> {
Expand Down Expand Up @@ -139,16 +185,9 @@ public void windowClosing(final WindowEvent event) {
}
});

final ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
onCancel();
}
};

// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(
actionListener,
(final ActionEvent event) -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);
Expand Down Expand Up @@ -237,7 +276,7 @@ private String suggestCronjobName(final String cronjobClassname) {
* When new cronjob dialog is filled, validate the input data and generate a new cronjob.
*/
private void onOK() {
if (!validator.validate(this.project,this)) {
if (!validateFormFields()) {
return;
}

Expand Down
Loading