Skip to content

Added navigation to tab with error on field #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ValidationRule;
import com.magento.idea.magento2plugin.bundles.CommonBundle;
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.lang.reflect.Field;
Expand All @@ -26,6 +27,7 @@
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.jetbrains.annotations.NotNull;
Expand All @@ -41,6 +43,7 @@ public abstract class AbstractDialog extends JDialog {
private final String errorTitle;
private final Map<Field, List<ValidationRule>> textFieldValidationRuleMap;
private final Map<Field, Map<ValidationRule, String>> errorMessageFieldValidationRuleMap;
private JTabbedPane tabbedPane;
private boolean isValidationErrorShown;
private boolean dialogHasErrors;

Expand Down Expand Up @@ -86,6 +89,13 @@ protected boolean validateFormFields() {
if (value != null && !rule.check(value)) {
if (errorMessageFieldValidationRuleMap.containsKey(field)
&& errorMessageFieldValidationRuleMap.get(field).containsKey(rule)) {
if (!dialogHasErrors) {
final JComponent component = getComponentForField(field);

if (component != null && tabbedPane != null) {
navigateToTabWithComponent(component);
}
}
dialogHasErrors = true;
showErrorMessage(
field,
Expand All @@ -106,6 +116,15 @@ protected boolean validateFormFields() {
return !dialogHasErrors;
}

/**
* Tabbed pane should be registered to be possible navigate to the tab in which error occurred.
*
* @param tabbedPane JTabbedPane
*/
protected void registerTabbedPane(final @NotNull JTabbedPane tabbedPane) {
this.tabbedPane = tabbedPane;
}

/**
* Show error message for field.
*
Expand Down Expand Up @@ -316,4 +335,46 @@ private JComponent getComponentForField(final @NotNull Field field) {

return null;
}

/**
* Navigate to tab with specified component.
*
* @param component JComponent
*/
private void navigateToTabWithComponent(final @NotNull JComponent component) {
if (tabbedPane == null) {
return;
}

final int index = getParentTabPaneForComponent(component);

if (index != -1) {
tabbedPane.setSelectedIndex(index);
}
}

/**
* Get parent tab index for component.
*
* @param component Container
*
* @return int
*/
private int getParentTabPaneForComponent(final @NotNull Container component) {
if (tabbedPane == null) {
return -1;
}
final int parentTabIndex = tabbedPane.indexOfComponent(component);

if (parentTabIndex != -1) {
return parentTabIndex;
}
final Container parent = component.getParent();

if (parent == null) {
return -1;
}

return getParentTabPaneForComponent(parent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
toggleUiComponentsPanel();

createUiComponent.addItemListener(event -> toggleUiComponentsPanel());
registerTabbedPane(tabbedPane1);
}

/**
Expand Down