77
88import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidation ;
99import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidations ;
10+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .rule .ValidationRule ;
1011import com .magento .idea .magento2plugin .bundles .CommonBundle ;
12+ import com .magento .idea .magento2plugin .bundles .ValidatorBundle ;
1113import java .awt .Dimension ;
1214import java .awt .Toolkit ;
1315import java .lang .reflect .Field ;
1416import 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 ) {
0 commit comments