5
5
6
6
package com .magento .idea .magento2plugin .actions .generation .dialog ;
7
7
8
+ import com .intellij .openapi .util .Pair ;
9
+ import com .magento .idea .magento2plugin .actions .generation .dialog .reflection .ExtractComponentFromFieldUtil ;
8
10
import com .magento .idea .magento2plugin .actions .generation .dialog .util .DialogFieldErrorUtil ;
9
- import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidation ;
10
- import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation . FieldValidations ;
11
+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .TypeFieldsRulesParser ;
12
+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .data . FieldValidationData ;
11
13
import com .magento .idea .magento2plugin .actions .generation .dialog .validator .rule .ValidationRule ;
12
14
import com .magento .idea .magento2plugin .bundles .CommonBundle ;
13
15
import com .magento .idea .magento2plugin .bundles .ValidatorBundle ;
14
16
import java .awt .Container ;
15
17
import java .awt .Dimension ;
16
18
import java .awt .Toolkit ;
17
19
import java .lang .reflect .Field ;
18
- import java .lang .reflect .InvocationTargetException ;
19
- import java .util .ArrayList ;
20
- import java .util .Arrays ;
21
- import java .util .HashMap ;
22
- import java .util .LinkedHashMap ;
23
20
import java .util .LinkedList ;
24
21
import java .util .List ;
25
- import java .util .Map ;
26
22
import javax .swing .JComboBox ;
27
23
import javax .swing .JComponent ;
28
24
import javax .swing .JDialog ;
35
31
/**
36
32
* All code generate dialog should extend this class.
37
33
*/
38
- @ SuppressWarnings ({"PMD.ShortVariable" , "PMD.MissingSerialVersionUID" })
39
34
public abstract class AbstractDialog extends JDialog {
40
35
41
36
protected CommonBundle bundle ;
42
37
protected final ValidatorBundle validatorBundle = new ValidatorBundle ();
38
+ protected final List <FieldValidationData > fieldsValidationsList ;
43
39
private final String errorTitle ;
44
- private final Map <Field , List <ValidationRule >> textFieldValidationRuleMap ;
45
- private final Map <Field , Map <ValidationRule , String >> errorMessageFieldValidationRuleMap ;
46
40
private JTabbedPane tabbedPane ;
47
41
private boolean isValidationErrorShown ;
48
- private boolean dialogHasErrors ;
49
42
50
43
/**
51
44
* Abstract Dialog Constructor.
@@ -54,15 +47,14 @@ public AbstractDialog() {
54
47
super ();
55
48
bundle = new CommonBundle ();
56
49
errorTitle = bundle .message ("common.error" );
57
- textFieldValidationRuleMap = new LinkedHashMap <>();
58
- errorMessageFieldValidationRuleMap = new HashMap <>();
50
+ fieldsValidationsList = new TypeFieldsRulesParser (this ).parseValidationRules ();
59
51
}
60
52
61
53
protected void centerDialog (final AbstractDialog dialog ) {
62
54
final Dimension screenSize = Toolkit .getDefaultToolkit ().getScreenSize ();
63
- final int x = screenSize .width / 2 - dialog .getSize ().width / 2 ;
64
- final int y = screenSize .height / 2 - dialog .getSize ().height / 2 ;
65
- dialog .setLocation (x , y );
55
+ final int cordX = screenSize .width / 2 - dialog .getSize ().width / 2 ;
56
+ final int cordY = screenSize .height / 2 - dialog .getSize ().height / 2 ;
57
+ dialog .setLocation (cordX , cordY );
66
58
}
67
59
68
60
protected void onCancel () {
@@ -74,48 +66,52 @@ protected void onCancel() {
74
66
*
75
67
* @return boolean
76
68
*/
69
+ @ SuppressWarnings ({"PMD.CyclomaticComplexity" , "PMD.AvoidDeeplyNestedIfStmts" })
77
70
protected boolean validateFormFields () {
78
- addValidationRulesFromAnnotations () ;
71
+ boolean dialogHasErrors ;
79
72
isValidationErrorShown = dialogHasErrors = false ;
80
73
81
- for (final Map .Entry <Field , List <ValidationRule >> entry
82
- : textFieldValidationRuleMap .entrySet ()) {
83
- final Field field = entry .getKey ();
84
- final List <ValidationRule > rules = entry .getValue ();
74
+ for (final FieldValidationData fieldValidationData : getFieldsToValidate ()) {
75
+ final Field field = fieldValidationData .getField ();
76
+ final List <Pair <ValidationRule , String >> rules = fieldValidationData .getRules ();
85
77
86
- for (final ValidationRule rule : rules ) {
78
+ for (final Pair <ValidationRule , String > rulePair : rules ) {
79
+ final ValidationRule rule = rulePair .getFirst ();
80
+ final String message = rulePair .getSecond ();
87
81
final String value = resolveFieldValueByComponentType (field );
88
82
89
83
if (value != null && !rule .check (value )) {
90
- if (errorMessageFieldValidationRuleMap .containsKey (field )
91
- && errorMessageFieldValidationRuleMap .get (field ).containsKey (rule )) {
92
- if (!dialogHasErrors ) {
93
- final JComponent component = getComponentForField (field );
84
+ if (!dialogHasErrors ) {
85
+ final JComponent component =
86
+ ExtractComponentFromFieldUtil .extract (field , this );
94
87
95
- if (component != null && tabbedPane != null ) {
96
- navigateToTabWithComponent (component );
97
- }
88
+ if (component != null && tabbedPane != null ) {
89
+ navigateToTabWithComponent (component );
98
90
}
99
- dialogHasErrors = true ;
100
- showErrorMessage (
101
- field ,
102
- errorMessageFieldValidationRuleMap .get (field ).get (rule )
103
- );
104
91
}
92
+ dialogHasErrors = true ;
93
+ showErrorMessage (field , message );
105
94
break ;
106
95
}
107
96
}
108
97
}
109
98
110
99
if (dialogHasErrors && !isValidationErrorShown ) {
111
- showErrorMessage (
112
- validatorBundle .message ("validator.someFieldsHaveErrors" )
113
- );
100
+ showErrorMessage (validatorBundle .message ("validator.someFieldsHaveErrors" ));
114
101
}
115
102
116
103
return !dialogHasErrors ;
117
104
}
118
105
106
+ /**
107
+ * Override this method to change which fields should or shouldn't be validated.
108
+ *
109
+ * @return List[FieldValidationData]
110
+ */
111
+ protected List <FieldValidationData > getFieldsToValidate () {
112
+ return new LinkedList <>(fieldsValidationsList );
113
+ }
114
+
119
115
/**
120
116
* Tabbed pane should be registered to be possible navigate to the tab in which error occurred.
121
117
*
@@ -162,130 +158,6 @@ protected void showErrorMessage(final String errorMessage) {
162
158
isValidationErrorShown = true ;
163
159
}
164
160
165
- /**
166
- * Process validation rules from annotations.
167
- */
168
- private void addValidationRulesFromAnnotations () {
169
- final Class <?> type = this .getClass ();
170
- final List <FieldValidation > validations = new LinkedList <>();
171
-
172
- for (final Field field : type .getDeclaredFields ()) {
173
- field .setAccessible (true );
174
- validations .clear ();
175
-
176
- if (field .isAnnotationPresent (FieldValidation .class )) {
177
- validations .add (field .getAnnotation (FieldValidation .class ));
178
- }
179
- if (field .isAnnotationPresent (FieldValidations .class )) {
180
- validations .addAll (
181
- Arrays .asList (field .getAnnotation (FieldValidations .class ).value ())
182
- );
183
- }
184
-
185
- for (final FieldValidation validation : validations ) {
186
- try {
187
- addValidationRuleToField (
188
- field ,
189
- getRuleFromAnnotation (validation ),
190
- getMessageFromAnnotation (validation )
191
- );
192
- } catch (NoSuchMethodException | IllegalAccessException
193
- | InvocationTargetException | InstantiationException exception ) {
194
- return ;
195
- } finally {
196
- field .setAccessible (false );
197
- }
198
- }
199
- field .setAccessible (false );
200
- }
201
- }
202
-
203
- /**
204
- * Get error message from annotation.
205
- *
206
- * @param validation FieldValidation
207
- *
208
- * @return String
209
- */
210
- private String getMessageFromAnnotation (final FieldValidation validation ) {
211
- String [] params ;
212
- final int minMessageArrayLength = 1 ;
213
-
214
- if (validation .message ().length > minMessageArrayLength ) {
215
- params = Arrays .copyOfRange (validation .message (), 1 , validation .message ().length );
216
- } else {
217
- params = new String []{};
218
- }
219
- return validatorBundle .message (validation .message ()[0 ], (Object []) params );
220
- }
221
-
222
- /**
223
- * Get validation rule from annotation.
224
- *
225
- * @param validation FieldValidation
226
- *
227
- * @return ValidationRule
228
- */
229
- private ValidationRule getRuleFromAnnotation (final FieldValidation validation )
230
- throws NoSuchMethodException , IllegalAccessException , InvocationTargetException ,
231
- InstantiationException {
232
- final Class <?> ruleType = validation .rule ().getRule ();
233
-
234
- return (ValidationRule ) ruleType .getConstructor ().newInstance ();
235
- }
236
-
237
- /**
238
- * Add validation rule for field.
239
- *
240
- * @param field Field
241
- * @param rule ValidationRule
242
- * @param message String
243
- */
244
- protected void addValidationRuleToField (
245
- final Field field ,
246
- final ValidationRule rule ,
247
- final String message
248
- ) {
249
- if (getComponentForField (field ) == null ) {
250
- return ;
251
- }
252
- List <ValidationRule > rules ;
253
-
254
- if (textFieldValidationRuleMap .containsKey (field )) {
255
- rules = textFieldValidationRuleMap .get (field );
256
- } else {
257
- rules = new ArrayList <>();
258
- }
259
-
260
- if (!rules .contains (rule ) && rule != null ) {
261
- addFieldValidationRuleMessageAssociation (field , rule , message );
262
- rules .add (rule );
263
- textFieldValidationRuleMap .put (field , rules );
264
- }
265
- }
266
-
267
- /**
268
- * Associate validation rule with field.
269
- *
270
- * @param field Field
271
- * @param rule ValidationRule
272
- * @param message String
273
- */
274
- private void addFieldValidationRuleMessageAssociation (
275
- final Field field ,
276
- final ValidationRule rule ,
277
- final String message
278
- ) {
279
- Map <ValidationRule , String > validationRuleErrorMessageMap ;
280
- if (errorMessageFieldValidationRuleMap .containsKey (field )) {
281
- validationRuleErrorMessageMap = errorMessageFieldValidationRuleMap .get (field );
282
- } else {
283
- validationRuleErrorMessageMap = new HashMap <>();
284
- }
285
- validationRuleErrorMessageMap .put (rule , message );
286
- errorMessageFieldValidationRuleMap .put (field , validationRuleErrorMessageMap );
287
- }
288
-
289
161
/**
290
162
* Resolve value of stored component by field.
291
163
*
@@ -294,7 +166,7 @@ private void addFieldValidationRuleMessageAssociation(
294
166
* @return String
295
167
*/
296
168
private String resolveFieldValueByComponentType (final Field field ) {
297
- final JComponent component = getComponentForField (field );
169
+ final JComponent component = ExtractComponentFromFieldUtil . extract (field , this );
298
170
299
171
if (component instanceof JTextField ) {
300
172
return ((JTextField ) component ).isEditable ()
@@ -312,30 +184,6 @@ private String resolveFieldValueByComponentType(final Field field) {
312
184
return null ;
313
185
}
314
186
315
- /**
316
- * Get JComponent for field.
317
- *
318
- * @param field Field
319
- *
320
- * @return JComponent
321
- */
322
- private JComponent getComponentForField (final @ NotNull Field field ) {
323
- try {
324
- field .setAccessible (true );
325
- final Object component = field .get (this );
326
-
327
- if (component instanceof JComponent ) {
328
- return (JComponent ) component ;
329
- }
330
- } catch (IllegalAccessException exception ) {
331
- return null ;
332
- } finally {
333
- field .setAccessible (false );
334
- }
335
-
336
- return null ;
337
- }
338
-
339
187
/**
340
188
* Navigate to tab with specified component.
341
189
*
0 commit comments