7
7
8
8
import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidation ;
9
9
import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidations ;
10
+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .rule .ValidationRule ;
10
11
import com .magento .idea .magento2plugin .bundles .CommonBundle ;
12
+ import com .magento .idea .magento2plugin .bundles .ValidatorBundle ;
11
13
import java .awt .Dimension ;
12
14
import java .awt .Toolkit ;
13
15
import java .lang .reflect .Field ;
14
16
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 ;
20
29
21
30
/**
22
31
* All code generate dialog should extend this class.
@@ -26,13 +35,18 @@ public abstract class AbstractDialog extends JDialog {
26
35
protected CommonBundle bundle ;
27
36
protected final ValidatorBundle validatorBundle = new ValidatorBundle ();
28
37
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 ;
31
40
41
+ /**
42
+ * Abstract Dialog Constructor.
43
+ */
32
44
public AbstractDialog () {
33
45
super ();
34
46
bundle = new CommonBundle ();
35
47
errorTitle = bundle .message ("common.error" );
48
+ textFieldValidationRuleMap = new LinkedHashMap <>();
49
+ errorMessageFieldValidationRuleMap = new HashMap <>();
36
50
}
37
51
38
52
protected void centerDialog (final AbstractDialog dialog ) {
@@ -48,12 +62,13 @@ protected void onCancel() {
48
62
49
63
protected boolean validateFormFields () {
50
64
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 ();
54
69
55
- for (ValidationRule rule : rules ) {
56
- String value = resolveFieldValueByComponentType (field );
70
+ for (final ValidationRule rule : rules ) {
71
+ final String value = resolveFieldValueByComponentType (field );
57
72
58
73
if (value != null && !rule .check (value )) {
59
74
if (errorMessageFieldValidationRuleMap .containsKey (field )
@@ -67,7 +82,7 @@ protected boolean validateFormFields() {
67
82
return true ;
68
83
}
69
84
70
- protected void showErrorMessage (String errorMessage ) {
85
+ protected void showErrorMessage (final String errorMessage ) {
71
86
JOptionPane .showMessageDialog (
72
87
null ,
73
88
errorMessage ,
@@ -77,59 +92,69 @@ protected void showErrorMessage(String errorMessage) {
77
92
}
78
93
79
94
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 ()) {
82
99
field .setAccessible (true );
83
- List < FieldValidation > validations = new LinkedList <> ();
100
+ validations . clear ();
84
101
85
102
if (field .isAnnotationPresent (FieldValidation .class )) {
86
103
validations .add (field .getAnnotation (FieldValidation .class ));
87
104
}
88
105
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
+ );
90
109
}
91
110
92
- for (FieldValidation validation : validations ) {
111
+ for (final FieldValidation validation : validations ) {
93
112
try {
94
113
addValidationRuleToField (
95
114
field .get (this ),
96
115
getRuleFromAnnotation (validation ),
97
116
getMessageFromAnnotation (validation )
98
117
);
99
- } catch (Exception exception ) {
100
- // NOPMD
118
+ } catch (Exception exception ) { // NOPMD
119
+ // We don't need to cover this case.
101
120
}
102
121
}
103
122
field .setAccessible (false );
104
123
}
105
124
}
106
125
107
- private String getMessageFromAnnotation (FieldValidation validation ) {
126
+ private String getMessageFromAnnotation (final FieldValidation validation ) {
108
127
String [] params ;
109
- if (validation .message ().length > 1 ) {
128
+ final int minMessageArrayLength = 1 ;
129
+
130
+ if (validation .message ().length > minMessageArrayLength ) {
110
131
params = Arrays .copyOfRange (validation .message (), 1 , validation .message ().length );
111
132
} else {
112
133
params = new String []{};
113
134
}
114
135
return validatorBundle .message (validation .message ()[0 ], params );
115
136
}
116
137
117
- private ValidationRule getRuleFromAnnotation (FieldValidation validation ) throws NoSuchMethodException ,
138
+ private ValidationRule getRuleFromAnnotation (final FieldValidation validation )
139
+ throws NoSuchMethodException ,
118
140
IllegalAccessException , InvocationTargetException , InstantiationException {
119
- Class <?> ruleType = validation .rule ().getRule ();
141
+ final Class <?> ruleType = validation .rule ().getRule ();
120
142
121
143
return (ValidationRule ) ruleType .getConstructor ().newInstance ();
122
144
}
123
145
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 ) {
125
150
if (!(field instanceof JComponent )) {
126
151
return ;
127
152
}
128
153
List <ValidationRule > rules ;
129
- if (!textFieldValidationRuleMap .containsKey (field )) {
130
- rules = new ArrayList <>();
131
- } else {
154
+ if (textFieldValidationRuleMap .containsKey (field )) {
132
155
rules = textFieldValidationRuleMap .get (field );
156
+ } else {
157
+ rules = new ArrayList <>();
133
158
}
134
159
135
160
if (!rules .contains (rule ) && rule != null ) {
@@ -139,18 +164,21 @@ protected void addValidationRuleToField(Object field, ValidationRule rule, Strin
139
164
}
140
165
}
141
166
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 ) {
143
171
Map <ValidationRule , String > validationRuleErrorMessageMap ;
144
- if (!errorMessageFieldValidationRuleMap .containsKey (field )) {
145
- validationRuleErrorMessageMap = new HashMap <>();
146
- } else {
172
+ if (errorMessageFieldValidationRuleMap .containsKey (field )) {
147
173
validationRuleErrorMessageMap = errorMessageFieldValidationRuleMap .get (field );
174
+ } else {
175
+ validationRuleErrorMessageMap = new HashMap <>();
148
176
}
149
177
validationRuleErrorMessageMap .put (rule , message );
150
178
errorMessageFieldValidationRuleMap .put (field , validationRuleErrorMessageMap );
151
179
}
152
180
153
- private String resolveFieldValueByComponentType (Object field ) {
181
+ private String resolveFieldValueByComponentType (final Object field ) {
154
182
if (field instanceof JTextField ) {
155
183
return ((JTextField ) field ).getText ();
156
184
} else if (field instanceof JComboBox ) {
0 commit comments