55
66package com .magento .idea .magento2plugin .actions .generation .dialog ;
77
8+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidation ;
9+ import com .magento .idea .magento2plugin .actions .generation .dialog .validator .annotation .FieldValidations ;
810import com .magento .idea .magento2plugin .bundles .CommonBundle ;
911import java .awt .Dimension ;
1012import java .awt .Toolkit ;
11- import javax .swing .JDialog ;
13+ import java .lang .reflect .Field ;
14+ 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 ;
1220
1321/**
1422 * All code generate dialog should extend this class.
1523 */
1624@ SuppressWarnings ({"PMD.ShortVariable" , "PMD.MissingSerialVersionUID" })
1725public abstract class AbstractDialog extends JDialog {
18-
1926 protected CommonBundle bundle ;
27+ protected final ValidatorBundle validatorBundle = new ValidatorBundle ();
28+ 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 <>();
2031
2132 public AbstractDialog () {
2233 super ();
23- this .bundle = new CommonBundle ();
34+ bundle = new CommonBundle ();
35+ errorTitle = bundle .message ("common.error" );
2436 }
2537
2638 protected void centerDialog (final AbstractDialog dialog ) {
@@ -33,4 +45,124 @@ protected void centerDialog(final AbstractDialog dialog) {
3345 protected void onCancel () {
3446 this .setVisible (false );
3547 }
48+
49+ protected boolean validateFormFields () {
50+ addValidationRulesFromAnnotations ();
51+ for (Map .Entry <Object , List <ValidationRule >> entry : textFieldValidationRuleMap .entrySet ()) {
52+ Object field = entry .getKey ();
53+ List <ValidationRule > rules = entry .getValue ();
54+
55+ for (ValidationRule rule : rules ) {
56+ String value = resolveFieldValueByComponentType (field );
57+
58+ if (value != null && !rule .check (value )) {
59+ if (errorMessageFieldValidationRuleMap .containsKey (field )
60+ && errorMessageFieldValidationRuleMap .get (field ).containsKey (rule )) {
61+ showErrorMessage (errorMessageFieldValidationRuleMap .get (field ).get (rule ));
62+ }
63+ return false ;
64+ }
65+ }
66+ }
67+ return true ;
68+ }
69+
70+ protected void showErrorMessage (String errorMessage ) {
71+ JOptionPane .showMessageDialog (
72+ null ,
73+ errorMessage ,
74+ errorTitle ,
75+ JOptionPane .ERROR_MESSAGE
76+ );
77+ }
78+
79+ private void addValidationRulesFromAnnotations () {
80+ Class <?> type = this .getClass ();
81+ for (Field field : type .getDeclaredFields ()) {
82+ field .setAccessible (true );
83+ List <FieldValidation > validations = new LinkedList <>();
84+
85+ if (field .isAnnotationPresent (FieldValidation .class )) {
86+ validations .add (field .getAnnotation (FieldValidation .class ));
87+ }
88+ if (field .isAnnotationPresent (FieldValidations .class )) {
89+ validations .addAll (Arrays .asList (field .getAnnotation (FieldValidations .class ).value ()));
90+ }
91+
92+ for (FieldValidation validation : validations ) {
93+ try {
94+ addValidationRuleToField (
95+ field .get (this ),
96+ getRuleFromAnnotation (validation ),
97+ getMessageFromAnnotation (validation )
98+ );
99+ } catch (Exception exception ) {
100+ // Do nothing.
101+ }
102+ }
103+ field .setAccessible (false );
104+ }
105+ }
106+
107+ private String getMessageFromAnnotation (FieldValidation validation ) {
108+ String [] params ;
109+ if (validation .message ().length > 1 ) {
110+ params = Arrays .copyOfRange (validation .message (), 1 , validation .message ().length );
111+ } else {
112+ params = new String []{};
113+ }
114+ return validatorBundle .message (validation .message ()[0 ], params );
115+ }
116+
117+ private ValidationRule getRuleFromAnnotation (FieldValidation validation ) throws NoSuchMethodException ,
118+ IllegalAccessException , InvocationTargetException , InstantiationException {
119+ ValidationRule rule ;
120+ Class <?> ruleType = validation .rule ().getRule ();
121+ String [] ruleParams = validation .properties ();
122+
123+ if (ruleParams .length >= 1 && !ruleParams [0 ].isEmpty ()) {
124+ rule = (ValidationRule )ruleType .getConstructor (String .class ).newInstance (ruleParams );
125+ } else {
126+ rule = (ValidationRule )ruleType .getConstructor ().newInstance ();
127+ }
128+ return rule ;
129+ }
130+
131+ protected void addValidationRuleToField (Object field , ValidationRule rule , String message ) {
132+ if (!(field instanceof JComponent )) {
133+ return ;
134+ }
135+ List <ValidationRule > rules ;
136+ if (!textFieldValidationRuleMap .containsKey (field )) {
137+ rules = new ArrayList <>();
138+ } else {
139+ rules = textFieldValidationRuleMap .get (field );
140+ }
141+
142+ if (!rules .contains (rule ) && rule != null ) {
143+ addFieldValidationRuleMessageAssociation (field , rule , message );
144+ rules .add (rule );
145+ textFieldValidationRuleMap .put (field , rules );
146+ }
147+ }
148+
149+ private void addFieldValidationRuleMessageAssociation (Object field , ValidationRule rule , String message ) {
150+ Map <ValidationRule , String > validationRuleErrorMessageMap ;
151+ if (!errorMessageFieldValidationRuleMap .containsKey (field )) {
152+ validationRuleErrorMessageMap = new HashMap <>();
153+ } else {
154+ validationRuleErrorMessageMap = errorMessageFieldValidationRuleMap .get (field );
155+ }
156+ validationRuleErrorMessageMap .put (rule , message );
157+ errorMessageFieldValidationRuleMap .put (field , validationRuleErrorMessageMap );
158+ }
159+
160+ private String resolveFieldValueByComponentType (Object field ) {
161+ if (field instanceof JTextField ) {
162+ return ((JTextField ) field ).getText ();
163+ } else if (field instanceof JComboBox ) {
164+ return ((JComboBox ) field ).getSelectedItem ().toString ();
165+ }
166+ return null ;
167+ }
36168}
0 commit comments