11
11
import com .magento .idea .magento2plugin .actions .generation .dialog .validator .rule .ValidationRule ;
12
12
import com .magento .idea .magento2plugin .bundles .CommonBundle ;
13
13
import com .magento .idea .magento2plugin .bundles .ValidatorBundle ;
14
+ import java .awt .Container ;
14
15
import java .awt .Dimension ;
15
16
import java .awt .Toolkit ;
16
17
import java .lang .reflect .Field ;
26
27
import javax .swing .JComponent ;
27
28
import javax .swing .JDialog ;
28
29
import javax .swing .JOptionPane ;
30
+ import javax .swing .JTabbedPane ;
29
31
import javax .swing .JTextArea ;
30
32
import javax .swing .JTextField ;
31
33
import org .jetbrains .annotations .NotNull ;
@@ -41,6 +43,7 @@ public abstract class AbstractDialog extends JDialog {
41
43
private final String errorTitle ;
42
44
private final Map <Field , List <ValidationRule >> textFieldValidationRuleMap ;
43
45
private final Map <Field , Map <ValidationRule , String >> errorMessageFieldValidationRuleMap ;
46
+ private JTabbedPane tabbedPane ;
44
47
private boolean isValidationErrorShown ;
45
48
private boolean dialogHasErrors ;
46
49
@@ -86,6 +89,13 @@ protected boolean validateFormFields() {
86
89
if (value != null && !rule .check (value )) {
87
90
if (errorMessageFieldValidationRuleMap .containsKey (field )
88
91
&& errorMessageFieldValidationRuleMap .get (field ).containsKey (rule )) {
92
+ if (!dialogHasErrors ) {
93
+ final JComponent component = getComponentForField (field );
94
+
95
+ if (component != null && tabbedPane != null ) {
96
+ navigateToTabWithComponent (component );
97
+ }
98
+ }
89
99
dialogHasErrors = true ;
90
100
showErrorMessage (
91
101
field ,
@@ -106,6 +116,15 @@ protected boolean validateFormFields() {
106
116
return !dialogHasErrors ;
107
117
}
108
118
119
+ /**
120
+ * Tabbed pane should be registered to be possible navigate to the tab in which error occurred.
121
+ *
122
+ * @param tabbedPane JTabbedPane
123
+ */
124
+ protected void registerTabbedPane (final @ NotNull JTabbedPane tabbedPane ) {
125
+ this .tabbedPane = tabbedPane ;
126
+ }
127
+
109
128
/**
110
129
* Show error message for field.
111
130
*
@@ -316,4 +335,46 @@ private JComponent getComponentForField(final @NotNull Field field) {
316
335
317
336
return null ;
318
337
}
338
+
339
+ /**
340
+ * Navigate to tab with specified component.
341
+ *
342
+ * @param component JComponent
343
+ */
344
+ private void navigateToTabWithComponent (final @ NotNull JComponent component ) {
345
+ if (tabbedPane == null ) {
346
+ return ;
347
+ }
348
+
349
+ final int index = getParentTabPaneForComponent (component );
350
+
351
+ if (index != -1 ) {
352
+ tabbedPane .setSelectedIndex (index );
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Get parent tab index for component.
358
+ *
359
+ * @param component Container
360
+ *
361
+ * @return int
362
+ */
363
+ private int getParentTabPaneForComponent (final @ NotNull Container component ) {
364
+ if (tabbedPane == null ) {
365
+ return -1 ;
366
+ }
367
+ final int parentTabIndex = tabbedPane .indexOfComponent (component );
368
+
369
+ if (parentTabIndex != -1 ) {
370
+ return parentTabIndex ;
371
+ }
372
+ final Container parent = component .getParent ();
373
+
374
+ if (parent == null ) {
375
+ return -1 ;
376
+ }
377
+
378
+ return getParentTabPaneForComponent (parent );
379
+ }
319
380
}
0 commit comments