Skip to content

Commit 1f7ee8f

Browse files
committed
Changed validator for NewGraphQlResolverDialog
1 parent a9c7c86 commit 1f7ee8f

File tree

7 files changed

+58
-182
lines changed

7 files changed

+58
-182
lines changed

resources/magento2/validation.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ validator.startWithNumberOrCapitalLetter={0} must start from a number or a capit
77
validator.onlyNumbers={0} must contain numbers only
88
validator.mustNotBeNegative={0} must not be negative
99
validator.identifier={0} must contain letters, numbers, dashes, and underscores only
10-
validator.class.isNotValid={0} is not valid class name
10+
validator.class.isNotValid=The {0} field does not contain a valid class name
1111
validator.class.shouldBeUnique=Duplicated class {0}
1212
validator.namespace.isNotValid={0} is not valid namespace name
1313
validator.directory.isNotValid={0} is not valid
14+
validator.directory.php.isNotValid=The {0} field does not contain a valid PHP directory
1415
validator.module.noSuchModule=No such module {0}
1516
validator.file.alreadyExists={0} already exists
1617
validator.file.cantBeCreated={0} can't be created

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
import com.intellij.psi.PsiFile;
1111
import com.magento.idea.magento2plugin.actions.generation.NewGraphQlResolverAction;
1212
import com.magento.idea.magento2plugin.actions.generation.data.GraphQlResolverFileData;
13-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewGraphQlResolverValidator;
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.NotEmptyRule;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpDirectoryRule;
1418
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleGraphQlResolverClassGenerator;
1519
import com.magento.idea.magento2plugin.magento.files.GraphQlResolverPhp;
1620
import com.magento.idea.magento2plugin.magento.packages.File;
1721
import com.magento.idea.magento2plugin.magento.packages.Package;
1822
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
1923
import java.awt.event.ActionEvent;
20-
import java.awt.event.ActionListener;
2124
import java.awt.event.KeyEvent;
2225
import java.awt.event.WindowAdapter;
2326
import java.awt.event.WindowEvent;
@@ -28,15 +31,26 @@
2831
import javax.swing.KeyStroke;
2932

3033
public class NewGraphQlResolverDialog extends AbstractDialog {
31-
private final NewGraphQlResolverValidator validator;
3234
private final PsiDirectory baseDir;
3335
private final String moduleName;
3436
private JPanel contentPanel;
3537
private JButton buttonOK;
3638
private JButton buttonCancel;
39+
private final Project project;
40+
private static final String CLASS_NAME = "class name";
41+
private static final String PARENT_DIRECTORY = "directory";
42+
43+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
44+
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
45+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
46+
message = {PhpClassRule.MESSAGE, CLASS_NAME})
3747
private JTextField graphQlResolverClassName;
48+
49+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
50+
message = {NotEmptyRule.MESSAGE, PARENT_DIRECTORY})
51+
@FieldValidation(rule = RuleRegistry.PHP_DIRECTORY,
52+
message = {PhpDirectoryRule.MESSAGE, PARENT_DIRECTORY})
3853
private JTextField graphQlResolverParentDir;
39-
private final Project project;
4054

4155
/**
4256
* Constructor.
@@ -50,25 +64,15 @@ public NewGraphQlResolverDialog(final Project project, final PsiDirectory direct
5064
this.project = project;
5165
this.baseDir = directory;
5266
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
53-
this.validator = NewGraphQlResolverValidator.getInstance(this);
5467

5568
setContentPane(contentPanel);
5669
setModal(true);
5770
setTitle("Create a new Magento 2 GraphQL Resolver.");
5871
getRootPane().setDefaultButton(buttonOK);
5972
suggestGraphQlResolverDirectory();
6073

61-
buttonOK.addActionListener(new ActionListener() {
62-
public void actionPerformed(final ActionEvent event) {
63-
onOK();
64-
}
65-
});
66-
67-
buttonCancel.addActionListener(new ActionListener() {
68-
public void actionPerformed(final ActionEvent event) {
69-
onCancel();
70-
}
71-
});
74+
buttonOK.addActionListener((final ActionEvent event) -> onOK());
75+
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());
7276

7377
// call onCancel() when cross is clicked
7478
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
@@ -79,12 +83,11 @@ public void windowClosing(final WindowEvent event) {
7983
});
8084

8185
// call onCancel() on ESCAPE
82-
contentPanel.registerKeyboardAction(new ActionListener() {
83-
public void actionPerformed(final ActionEvent event) {
84-
onCancel();
85-
}
86-
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
87-
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
86+
contentPanel.registerKeyboardAction(
87+
(final ActionEvent event) -> onCancel(),
88+
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
89+
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
90+
);
8891
}
8992

9093
/**
@@ -101,7 +104,7 @@ public static void open(final Project project, final PsiDirectory directory) {
101104
}
102105

103106
protected void onOK() {
104-
if (!validator.validate()) {
107+
if (!validateFormFields()) {
105108
return;
106109
}
107110
generateFile();
@@ -176,6 +179,7 @@ private String getGraphQlResolverClassFqn() {
176179
return getNamespace().concat(Package.fqnSeparator).concat(getGraphQlResolverClassName());
177180
}
178181

182+
@Override
179183
public void onCancel() {
180184
// add your code here if necessary
181185
dispose();

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewGraphQlResolverValidator.java

Lines changed: 0 additions & 156 deletions
This file was deleted.

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/annotation/RuleRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
1515
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NumericRule;
1616
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpDirectoryRule;
1718
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpNamespaceNameRule;
1819
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.RouteIdRule;
1920
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule;
@@ -25,6 +26,7 @@ public enum RuleRegistry {
2526
ALPHANUMERIC(AlphanumericRule.class),
2627
ALPHANUMERIC_WITH_UNDERSCORE(AlphanumericWithUnderscoreRule.class),
2728
DIRECTORY(DirectoryRule.class),
29+
PHP_DIRECTORY(PhpDirectoryRule.class),
2830
IDENTIFIER(IdentifierRule.class),
2931
PHP_NAMESPACE_NAME(PhpNamespaceNameRule.class),
3032
START_WITH_NUMBER_OR_CAPITAL_LETTER(StartWithNumberOrCapitalLetterRule.class),

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/PhpClassRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
package com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule;
77

8-
import com.jetbrains.php.refactoring.PhpNameUtil;
8+
import com.magento.idea.magento2plugin.util.RegExUtil;
99

1010
public class PhpClassRule implements ValidationRule {
1111
public static final String MESSAGE = "validator.class.isNotValid";
1212
private static final ValidationRule INSTANCE = new PhpClassRule();
1313

1414
@Override
1515
public boolean check(final String value) {
16-
return PhpNameUtil.isValidClassName(value);
16+
return value.matches(RegExUtil.Magento.PHP_CLASS);
1717
}
1818

1919
public static ValidationRule getInstance() {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule;
7+
8+
import com.magento.idea.magento2plugin.util.RegExUtil;
9+
10+
public class PhpDirectoryRule implements ValidationRule {
11+
public static final String MESSAGE = "validator.directory.php.isNotValid";
12+
private static final ValidationRule INSTANCE = new PhpDirectoryRule();
13+
14+
@Override
15+
public boolean check(final String value) {
16+
return value.matches(RegExUtil.Magento.PHP_CLASS);
17+
}
18+
19+
public static ValidationRule getInstance() {
20+
return INSTANCE;
21+
}
22+
}

src/com/magento/idea/magento2plugin/util/RegExUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class RegExUtil {
3232
= "(\\d+)\\.(\\d+)\\.(\\d+)[a-zA-Z0-9_\\-]*";
3333

3434
public static class Magento {
35+
public static final String PHP_CLASS
36+
= "[A-Z][a-zA-Z0-9]+";
37+
3538
public static final String MODULE_NAME
3639
= "[A-Z][a-zA-Z0-9]+_[A-Z][a-zA-Z0-9]+";
3740

0 commit comments

Comments
 (0)