1111import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ValidationRule;
1212import com.magento.idea.magento2plugin.bundles.CommonBundle;
1313import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
14+ import java.awt.Container;
1415import java.awt.Dimension;
1516import java.awt.Toolkit;
1617import java.lang.reflect.Field;
2627import javax.swing.JComponent;
2728import javax.swing.JDialog;
2829import javax.swing.JOptionPane;
30+ import javax.swing.JTabbedPane;
2931import javax.swing.JTextArea;
3032import javax.swing.JTextField;
3133import org.jetbrains.annotations.NotNull;
@@ -41,6 +43,7 @@ public abstract class AbstractDialog extends JDialog {
4143 private final String errorTitle;
4244 private final Map<Field, List<ValidationRule>> textFieldValidationRuleMap;
4345 private final Map<Field, Map<ValidationRule, String>> errorMessageFieldValidationRuleMap;
46+ private JTabbedPane tabbedPane;
4447 private boolean isValidationErrorShown;
4548 private boolean dialogHasErrors;
4649
@@ -86,6 +89,13 @@ protected boolean validateFormFields() {
8689 if (value != null && !rule.check(value)) {
8790 if (errorMessageFieldValidationRuleMap.containsKey(field)
8891 && 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+ }
8999 dialogHasErrors = true;
90100 showErrorMessage(
91101 field,
@@ -106,6 +116,15 @@ protected boolean validateFormFields() {
106116 return !dialogHasErrors;
107117 }
108118
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+
109128 /**
110129 * Show error message for field.
111130 *
@@ -316,4 +335,46 @@ private JComponent getComponentForField(final @NotNull Field field) {
316335
317336 return null;
318337 }
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+ }
319380}
0 commit comments