Skip to content

Commit 2592a1a

Browse files
89: Fixing code style issues
1 parent 8f4e226 commit 2592a1a

22 files changed

+523
-244
lines changed

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

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77

88
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
99
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidations;
10+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ValidationRule;
1011
import com.magento.idea.magento2plugin.bundles.CommonBundle;
12+
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
1113
import java.awt.Dimension;
1214
import java.awt.Toolkit;
1315
import java.lang.reflect.Field;
1416
import java.lang.reflect.InvocationTargetException;
15-
import java.util.*;
16-
import javax.swing.*;
17-
18-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ValidationRule;
19-
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
17+
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.LinkedHashMap;
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
import java.util.Map;
24+
import javax.swing.JComboBox;
25+
import javax.swing.JComponent;
26+
import javax.swing.JDialog;
27+
import javax.swing.JOptionPane;
28+
import javax.swing.JTextField;
2029

2130
/**
2231
* All code generate dialog should extend this class.
@@ -26,13 +35,18 @@ public abstract class AbstractDialog extends JDialog {
2635
protected CommonBundle bundle;
2736
protected final ValidatorBundle validatorBundle = new ValidatorBundle();
2837
private final String errorTitle;
29-
private final Map<Object, List<ValidationRule>> textFieldValidationRuleMap = new LinkedHashMap<>();
30-
private final Map<Object, Map<ValidationRule, String>> errorMessageFieldValidationRuleMap = new HashMap<>();
38+
private final Map<Object, List<ValidationRule>> textFieldValidationRuleMap;
39+
private final Map<Object, Map<ValidationRule, String>> errorMessageFieldValidationRuleMap;
3140

41+
/**
42+
* Abstract Dialog Constructor.
43+
*/
3244
public AbstractDialog() {
3345
super();
3446
bundle = new CommonBundle();
3547
errorTitle = bundle.message("common.error");
48+
textFieldValidationRuleMap = new LinkedHashMap<>();
49+
errorMessageFieldValidationRuleMap = new HashMap<>();
3650
}
3751

3852
protected void centerDialog(final AbstractDialog dialog) {
@@ -48,12 +62,13 @@ protected void onCancel() {
4862

4963
protected boolean validateFormFields() {
5064
addValidationRulesFromAnnotations();
51-
for (Map.Entry<Object, List<ValidationRule>> entry : textFieldValidationRuleMap.entrySet()) {
52-
Object field = entry.getKey();
53-
List<ValidationRule> rules = entry.getValue();
65+
for (final Map.Entry<Object, List<ValidationRule>> entry
66+
: textFieldValidationRuleMap.entrySet()) {
67+
final Object field = entry.getKey();
68+
final List<ValidationRule> rules = entry.getValue();
5469

55-
for (ValidationRule rule : rules) {
56-
String value = resolveFieldValueByComponentType(field);
70+
for (final ValidationRule rule : rules) {
71+
final String value = resolveFieldValueByComponentType(field);
5772

5873
if (value != null && !rule.check(value)) {
5974
if (errorMessageFieldValidationRuleMap.containsKey(field)
@@ -67,7 +82,7 @@ protected boolean validateFormFields() {
6782
return true;
6883
}
6984

70-
protected void showErrorMessage(String errorMessage) {
85+
protected void showErrorMessage(final String errorMessage) {
7186
JOptionPane.showMessageDialog(
7287
null,
7388
errorMessage,
@@ -77,59 +92,69 @@ protected void showErrorMessage(String errorMessage) {
7792
}
7893

7994
private void addValidationRulesFromAnnotations() {
80-
Class<?> type = this.getClass();
81-
for (Field field : type.getDeclaredFields()) {
95+
final Class<?> type = this.getClass();
96+
final List<FieldValidation> validations = new LinkedList<>();
97+
98+
for (final Field field : type.getDeclaredFields()) {
8299
field.setAccessible(true);
83-
List<FieldValidation> validations = new LinkedList<>();
100+
validations.clear();
84101

85102
if (field.isAnnotationPresent(FieldValidation.class)) {
86103
validations.add(field.getAnnotation(FieldValidation.class));
87104
}
88105
if (field.isAnnotationPresent(FieldValidations.class)) {
89-
validations.addAll(Arrays.asList(field.getAnnotation(FieldValidations.class).value()));
106+
validations.addAll(
107+
Arrays.asList(field.getAnnotation(FieldValidations.class).value())
108+
);
90109
}
91110

92-
for (FieldValidation validation : validations) {
111+
for (final FieldValidation validation : validations) {
93112
try {
94113
addValidationRuleToField(
95114
field.get(this),
96115
getRuleFromAnnotation(validation),
97116
getMessageFromAnnotation(validation)
98117
);
99-
} catch (Exception exception) {
100-
// NOPMD
118+
} catch (Exception exception) { // NOPMD
119+
// We don't need to cover this case.
101120
}
102121
}
103122
field.setAccessible(false);
104123
}
105124
}
106125

107-
private String getMessageFromAnnotation(FieldValidation validation) {
126+
private String getMessageFromAnnotation(final FieldValidation validation) {
108127
String[] params;
109-
if (validation.message().length > 1) {
128+
final int minMessageArrayLength = 1;
129+
130+
if (validation.message().length > minMessageArrayLength) {
110131
params = Arrays.copyOfRange(validation.message(), 1, validation.message().length);
111132
} else {
112133
params = new String[]{};
113134
}
114135
return validatorBundle.message(validation.message()[0], params);
115136
}
116137

117-
private ValidationRule getRuleFromAnnotation(FieldValidation validation) throws NoSuchMethodException,
138+
private ValidationRule getRuleFromAnnotation(final FieldValidation validation)
139+
throws NoSuchMethodException,
118140
IllegalAccessException, InvocationTargetException, InstantiationException {
119-
Class<?> ruleType = validation.rule().getRule();
141+
final Class<?> ruleType = validation.rule().getRule();
120142

121143
return (ValidationRule) ruleType.getConstructor().newInstance();
122144
}
123145

124-
protected void addValidationRuleToField(Object field, ValidationRule rule, String message) {
146+
protected void addValidationRuleToField(
147+
final Object field,
148+
final ValidationRule rule,
149+
final String message) {
125150
if (!(field instanceof JComponent)) {
126151
return;
127152
}
128153
List<ValidationRule> rules;
129-
if (!textFieldValidationRuleMap.containsKey(field)) {
130-
rules = new ArrayList<>();
131-
} else {
154+
if (textFieldValidationRuleMap.containsKey(field)) {
132155
rules = textFieldValidationRuleMap.get(field);
156+
} else {
157+
rules = new ArrayList<>();
133158
}
134159

135160
if (!rules.contains(rule) && rule != null) {
@@ -139,18 +164,21 @@ protected void addValidationRuleToField(Object field, ValidationRule rule, Strin
139164
}
140165
}
141166

142-
private void addFieldValidationRuleMessageAssociation(Object field, ValidationRule rule, String message) {
167+
private void addFieldValidationRuleMessageAssociation(
168+
final Object field,
169+
final ValidationRule rule,
170+
final String message) {
143171
Map<ValidationRule, String> validationRuleErrorMessageMap;
144-
if (!errorMessageFieldValidationRuleMap.containsKey(field)) {
145-
validationRuleErrorMessageMap = new HashMap<>();
146-
} else {
172+
if (errorMessageFieldValidationRuleMap.containsKey(field)) {
147173
validationRuleErrorMessageMap = errorMessageFieldValidationRuleMap.get(field);
174+
} else {
175+
validationRuleErrorMessageMap = new HashMap<>();
148176
}
149177
validationRuleErrorMessageMap.put(rule, message);
150178
errorMessageFieldValidationRuleMap.put(field, validationRuleErrorMessageMap);
151179
}
152180

153-
private String resolveFieldValueByComponentType(Object field) {
181+
private String resolveFieldValueByComponentType(final Object field) {
154182
if (field instanceof JTextField) {
155183
return ((JTextField) field).getText();
156184
} else if (field instanceof JComboBox) {

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

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@
2020
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFileData;
2121
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
2222
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
23-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.*;
24-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.uiComponent.FormButtonsValidator;
25-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.uiComponent.FormFieldsValidator;
26-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.uiComponent.FormFieldsetsValidator;
23+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AclResourceIdRule;
24+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericRule;
25+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
26+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
27+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
28+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
29+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpNamespaceNameRule;
30+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.RouteIdRule;
31+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule;
32+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.ui.component.FormButtonsValidator;
33+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.ui.component.FormFieldsValidator;
34+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.ui.component.FormFieldsetsValidator;
2735
import com.magento.idea.magento2plugin.actions.generation.generator.LayoutXmlGenerator;
2836
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator;
2937
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
@@ -42,8 +50,7 @@
4250
import com.magento.idea.magento2plugin.ui.table.DeleteRowButton;
4351
import com.magento.idea.magento2plugin.ui.table.TableButton;
4452
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
45-
46-
import java.awt.*;
53+
import java.awt.Dimension;
4754
import java.awt.event.ActionEvent;
4855
import java.awt.event.ActionListener;
4956
import java.awt.event.KeyEvent;
@@ -82,6 +89,11 @@ public class NewUiComponentFormDialog extends AbstractDialog {
8289
private JButton buttonCancel;
8390
private FilteredComboBox formAreaSelect;
8491

92+
private static final String VIEW_ACTION_NAME = "View Action Name";
93+
private static final String SUBMIT_ACTION_NAME = "Submit Action Name";
94+
private static final String DATA_PROVIDER_CLASS_NAME = "Data Provider class name";
95+
private static final String DATA_PROVIDER_DIRECTORY = "Data Provider directory";
96+
8597
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, "Name"})
8698
@FieldValidation(rule = RuleRegistry.IDENTIFIER, message = {IdentifierRule.MESSAGE, "Name"})
8799
private JTextField formName;
@@ -103,34 +115,44 @@ public class NewUiComponentFormDialog extends AbstractDialog {
103115
message = {PhpNamespaceNameRule.MESSAGE, "View Controller Name"})
104116
private JTextField viewControllerName;
105117

106-
@FieldValidation(rule = RuleRegistry.PHP_CLASS, message = {PhpClassRule.MESSAGE, "View Action Name"})
107-
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, "View Action Name"})
108-
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC, message = {AlphanumericRule.MESSAGE, "View Action Name"})
118+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
119+
message = {PhpClassRule.MESSAGE, VIEW_ACTION_NAME})
120+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
121+
message = {NotEmptyRule.MESSAGE, VIEW_ACTION_NAME})
122+
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC,
123+
message = {AlphanumericRule.MESSAGE, VIEW_ACTION_NAME})
109124
@FieldValidation(rule = RuleRegistry.START_WITH_NUMBER_OR_CAPITAL_LETTER,
110-
message = {StartWithNumberOrCapitalLetterRule.MESSAGE, "View Action Name"})
125+
message = {StartWithNumberOrCapitalLetterRule.MESSAGE, VIEW_ACTION_NAME})
111126
private JTextField viewActionName;
112127

113128
@FieldValidation(rule = RuleRegistry.PHP_NAMESPACE_NAME,
114129
message = {PhpNamespaceNameRule.MESSAGE, "Submit Controller Name"})
115130
private JTextField submitControllerName;
116131

117-
@FieldValidation(rule = RuleRegistry.PHP_CLASS, message = {PhpClassRule.MESSAGE, "Submit Action Name"})
118-
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, "Submit Action Name"})
119-
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC, message = {AlphanumericRule.MESSAGE, "Submit Action Name"})
132+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
133+
message = {PhpClassRule.MESSAGE, SUBMIT_ACTION_NAME})
134+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
135+
message = {NotEmptyRule.MESSAGE, SUBMIT_ACTION_NAME})
136+
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC,
137+
message = {AlphanumericRule.MESSAGE, SUBMIT_ACTION_NAME})
120138
@FieldValidation(rule = RuleRegistry.START_WITH_NUMBER_OR_CAPITAL_LETTER,
121-
message = {StartWithNumberOrCapitalLetterRule.MESSAGE, "Submit Action Name"})
139+
message = {StartWithNumberOrCapitalLetterRule.MESSAGE, SUBMIT_ACTION_NAME})
122140
private JTextField submitActionName;
123141

124-
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, "Data Provider class name"})
125-
@FieldValidation(rule = RuleRegistry.PHP_CLASS, message = {PhpClassRule.MESSAGE, "Data Provider class name"})
142+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
143+
message = {NotEmptyRule.MESSAGE, DATA_PROVIDER_CLASS_NAME})
144+
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
145+
message = {PhpClassRule.MESSAGE, DATA_PROVIDER_CLASS_NAME})
126146
@FieldValidation(rule = RuleRegistry.ALPHANUMERIC,
127-
message = {AlphanumericRule.MESSAGE, "Data Provider class name"})
147+
message = {AlphanumericRule.MESSAGE, DATA_PROVIDER_CLASS_NAME})
128148
private JTextField dataProviderClassName;
129149

130-
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, "Data Provider directory"})
131-
@FieldValidation(rule = RuleRegistry.DIRECTORY, message = {DirectoryRule.MESSAGE, "Data Provider directory"})
150+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
151+
message = {NotEmptyRule.MESSAGE, DATA_PROVIDER_DIRECTORY})
152+
@FieldValidation(rule = RuleRegistry.DIRECTORY,
153+
message = {DirectoryRule.MESSAGE, DATA_PROVIDER_DIRECTORY})
132154
@FieldValidation(rule = RuleRegistry.START_WITH_NUMBER_OR_CAPITAL_LETTER,
133-
message = {AlphanumericRule.MESSAGE, "Data Provider directory"})
155+
message = {AlphanumericRule.MESSAGE, DATA_PROVIDER_DIRECTORY})
134156
private JTextField dataProviderDirectory;
135157

136158
private JLabel aclLabel;
@@ -494,6 +516,7 @@ private PsiFile generateLayoutFile() {
494516
), project).generate(NewUiComponentFormAction.ACTION_NAME, false);
495517
}
496518

519+
@Override
497520
protected void onCancel() {
498521
dispose();
499522
}
@@ -701,7 +724,7 @@ protected boolean validateFormFields() {
701724
}
702725

703726
private void updateDialogSizeToDefaults() {
704-
Dimension screenSize = getToolkit().getScreenSize();
705-
setPreferredSize(new Dimension(screenSize.width/2, screenSize.height/2));
727+
final Dimension screenSize = getToolkit().getScreenSize();
728+
setPreferredSize(new Dimension(screenSize.width / 2, screenSize.height / 2));
706729
}
707730
}
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
16
package com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation;
27

3-
import java.lang.annotation.*;
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Repeatable;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
413

514
@Repeatable(FieldValidations.class)
615
@Retention(RetentionPolicy.RUNTIME)
716
@Target(ElementType.FIELD)
817
public @interface FieldValidation {
9-
public RuleRegistry rule();
10-
public String[] message();
18+
19+
/**
20+
* Get specified in the annotation RuleRegistry enum value to access ValidationRule class.
21+
*
22+
* @return RuleRegistry
23+
*/
24+
RuleRegistry rule();
25+
26+
/**
27+
* Get specified in the annotation message array.
28+
* The first element of array is a bundle message identifier,
29+
* туче elements are the arguments for the bundle message.
30+
*
31+
* @return String[]
32+
*/
33+
String[] message();
1134
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
16
package com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation;
27

38
import java.lang.annotation.ElementType;
@@ -8,5 +13,12 @@
813
@Retention(RetentionPolicy.RUNTIME)
914
@Target(ElementType.FIELD)
1015
public @interface FieldValidations {
11-
public FieldValidation[] value();
16+
17+
/**
18+
* Get the array of FieldValidation annotations.
19+
* Used to add possibility to read multiple FieldValidation annotations from the class fields.
20+
*
21+
* @return FieldValidation[]
22+
*/
23+
FieldValidation[] value();
1224
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
16
package com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation;
27

3-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.*;
8+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AclResourceIdRule;
9+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericRule;
10+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
11+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
13+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NumericRule;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpNamespaceNameRule;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.RouteIdRule;
19+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule;
420

521
public enum RuleRegistry {
622
NOT_EMPTY(NotEmptyRule.class),

0 commit comments

Comments
 (0)