Skip to content

Commit ac85ebc

Browse files
Added navigation to tab with error on field
1 parent ebeab55 commit ac85ebc

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/com/magento/idea/magento2plugin/actions/generation/dialog/AbstractDialog.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ValidationRule;
1212
import com.magento.idea.magento2plugin.bundles.CommonBundle;
1313
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
14+
import java.awt.Container;
1415
import java.awt.Dimension;
1516
import java.awt.Toolkit;
1617
import java.lang.reflect.Field;
@@ -26,6 +27,7 @@
2627
import javax.swing.JComponent;
2728
import javax.swing.JDialog;
2829
import javax.swing.JOptionPane;
30+
import javax.swing.JTabbedPane;
2931
import javax.swing.JTextArea;
3032
import javax.swing.JTextField;
3133
import 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
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
251251
toggleUiComponentsPanel();
252252

253253
createUiComponent.addItemListener(event -> toggleUiComponentsPanel());
254+
registerTabbedPane(tabbedPane1);
254255
}
255256

256257
/**

0 commit comments

Comments
 (0)