Search in sources :

Example 1 with FocusableComponent

use of com.haulmont.cuba.desktop.sys.vcl.FocusableComponent in project cuba by cuba-platform.

the class DesktopComponentsHelper method focusProblemComponent.

public static void focusProblemComponent(ValidationErrors errors) {
    Component component = null;
    if (!errors.getAll().isEmpty()) {
        component = errors.getAll().iterator().next().component;
    }
    if (component != null) {
        try {
            final JComponent jComponent = DesktopComponentsHelper.unwrap(component);
            java.awt.Component c = jComponent;
            java.awt.Component prevC = null;
            while (c != null) {
                if (c instanceof JTabbedPane && !((JTabbedPane) c).getSelectedComponent().equals(prevC)) {
                    final JTabbedPane tabbedPane = (JTabbedPane) c;
                    // do not focus tabbed pane on programmaticaly selection change
                    JTabbedPaneExt.setFocusOnSelectionChange(false);
                    tabbedPane.setSelectedComponent(prevC);
                    break;
                }
                if (c instanceof CollapsiblePanel && !((CollapsiblePanel) c).isExpanded()) {
                    ((CollapsiblePanel) c).setExpanded(true);
                    break;
                }
                prevC = c;
                c = c.getParent();
            }
            if (!JTabbedPaneExt.isFocusOnSelectionChange()) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        JTabbedPaneExt.setFocusOnSelectionChange(true);
                    }
                });
            }
            if (jComponent instanceof FocusableComponent) {
                ((FocusableComponent) jComponent).focus();
            } else {
                // focus first up component
                c = jComponent;
                while (c != null) {
                    if (c.isFocusable()) {
                        c.requestFocus();
                        break;
                    }
                    c = c.getParent();
                }
            }
        } catch (Exception e) {
            LoggerFactory.getLogger(DesktopComponentsHelper.class).warn("Error while problem component focusing", e);
        }
    }
}
Also used : FocusableComponent(com.haulmont.cuba.desktop.sys.vcl.FocusableComponent) FocusableComponent(com.haulmont.cuba.desktop.sys.vcl.FocusableComponent) Component(com.haulmont.cuba.gui.components.Component) CollapsiblePanel(com.haulmont.cuba.desktop.sys.vcl.CollapsiblePanel) java.awt(java.awt)

Aggregations

CollapsiblePanel (com.haulmont.cuba.desktop.sys.vcl.CollapsiblePanel)1 FocusableComponent (com.haulmont.cuba.desktop.sys.vcl.FocusableComponent)1 Component (com.haulmont.cuba.gui.components.Component)1 java.awt (java.awt)1