Example usage for javax.swing JComponent WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

List of usage examples for javax.swing JComponent WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

Introduction

In this page you can find the example usage for javax.swing JComponent WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.

Prototype

int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

To view the source code for javax.swing JComponent WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.

Click Source Link

Document

Constant used for registerKeyboardAction that means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component.

Usage

From source file:org.noam.intelliwtf.actions.SubmitWtfDialog.java

public SubmitWtfDialog() {
    setContentPane(contentPane);/*from www .j a  v  a  2  s.  c  om*/
    setModal(true);
    getRootPane().setDefaultButton(submitButton);
    setTitle("Submit to TDWTF");

    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            onOK();
        }
    });

    buttonCancel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            onCancel();
        }
    });

    // call onCancel() when cross is clicked
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            onCancel();
        }
    });

    // call onCancel() on ESCAPE
    contentPane.registerKeyboardAction(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            onCancel();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}

From source file:org.nuclos.client.common.NuclosCollectController.java

/**
 * @todo this method is misused - it sets shortcuts for many things other than tabs...
 * @param frame/*from www. j  a va 2 s  .  c o m*/
 */
@Override
protected void setupShortcutsForTabs(MainFrameTab frame) {
    final CollectPanel<Clct> pnlCollect = this.getCollectPanel();
    final DetailsPanel pnlDetails = this.getDetailsPanel();

    final Action actSelectSearchTab = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ev) {
            if (pnlCollect.isTabbedPaneEnabledAt(CollectPanel.TAB_SEARCH)) {
                pnlCollect.setTabbedPaneSelectedComponent(getSearchPanel());
            }
        }
    };
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.ACTIVATE_SEARCH_PANEL_1, actSelectSearchTab,
            pnlCollect);
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.ACTIVATE_SEARCH_PANEL_2, actSelectSearchTab,
            pnlCollect);

    //TODO This is a workaround. The detailpanel should keep the focus
    final Action actGrabFocus = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ev) {
            pnlDetails.grabFocus();
        }
    };

    /**
     * A <code>ChainedAction</code> is an action composed of a primary and a secondary action.
     * It behaves exactly like the primary action, except that additionally, the secondary action is performed
     * after the primary action.
     */
    class ChainedAction implements Action {
        private final Action actPrimary;
        private final Action actSecondary;

        public ChainedAction(Action actPrimary, Action actSecondary) {
            this.actPrimary = actPrimary;
            this.actSecondary = actSecondary;
        }

        @Override
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            actPrimary.addPropertyChangeListener(listener);
        }

        @Override
        public Object getValue(String sKey) {
            return actPrimary.getValue(sKey);
        }

        @Override
        public boolean isEnabled() {
            return actPrimary.isEnabled();
        }

        @Override
        public void putValue(String sKey, Object oValue) {
            actPrimary.putValue(sKey, oValue);
        }

        @Override
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            actPrimary.removePropertyChangeListener(listener);
        }

        @Override
        public void setEnabled(boolean bEnabled) {
            actPrimary.setEnabled(bEnabled);
        }

        @Override
        public void actionPerformed(ActionEvent ev) {
            actPrimary.actionPerformed(ev);
            actSecondary.actionPerformed(ev);
        }
    }

    //final Action actRefresh = new ChainedAction(this.getRefreshCurrentCollectableAction(), actGrabFocus);

    this.getCollectPanel().setTabbedPaneToolTipTextAt(CollectPanel.TAB_SEARCH,
            getSpringLocaleDelegate().getMessage("NuclosCollectController.13", "Suche (F7) (Strg+F)"));
    this.getCollectPanel().setTabbedPaneToolTipTextAt(CollectPanel.TAB_RESULT,
            getSpringLocaleDelegate().getMessage("NuclosCollectController.7", "Ergebnis (F8)"));
    this.getCollectPanel().setTabbedPaneToolTipTextAt(CollectPanel.TAB_DETAILS,
            getSpringLocaleDelegate().getMessage("NuclosCollectController.3", "Details (F2)"));

    // the search action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.START_SEARCH, this.getSearchAction(),
            pnlCollect);
    KeyBinding keybinding = KeyBindingProvider.REFRESH;

    // the refresh action
    KeyBindingProvider.removeActionFromComponent(keybinding, pnlDetails);
    pnlDetails.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keybinding.getKeystroke(),
            keybinding.getKey());
    pnlDetails.getActionMap().put(keybinding.getKey(), this.getRefreshCurrentCollectableAction());
    KeyBindingProvider.removeActionFromComponent(keybinding, getResultPanel());
    getResultPanel().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keybinding.getKeystroke(),
            keybinding.getKey());
    getResultPanel().getActionMap().put(keybinding.getKey(), getResultPanel().btnRefresh.getAction());

    // the new action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.NEW, this.getNewAction(), pnlDetails);

    // the new with search values action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.NEW_SEARCHVALUE,
            this.getNewWithSearchValuesAction(), pnlCollect);

    // the save action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.SAVE_1, this.getSaveAction(), pnlCollect);
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.SAVE_2, this.getSaveAction(), pnlCollect);

    // first the navigation actions are performed and then the focus is grabbed:
    final Action actFirst = new ChainedAction(this.getFirstAction(), actGrabFocus);
    final Action actLast = new ChainedAction(this.getLastAction(), actGrabFocus);
    final Action actPrevious = new ChainedAction(this.getPreviousAction(), actGrabFocus);
    final Action actNext = new ChainedAction(this.getNextAction(), actGrabFocus);

    // the first action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.FIRST, actFirst, pnlDetails);
    pnlDetails.btnFirst.setAction(actFirst);

    // the last action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.LAST, actLast, pnlDetails);
    pnlDetails.btnLast.setAction(actLast);

    // the previous action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.PREVIOUS_1, actPrevious, pnlDetails);
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.PREVIOUS_2, actPrevious, pnlDetails);
    pnlDetails.btnPrevious.setAction(actPrevious);

    // the next action
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.NEXT_1, actNext, pnlDetails);
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.NEXT_2, actNext, pnlDetails);
    pnlDetails.btnNext.setAction(actNext);

    Action actClose = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            getTab().dispose();
        }
    };
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.CLOSE_CHILD, actClose, pnlCollect);

    if (getResultPanel() != null && getResultTable() != null) {
        final JButton btnEdit = getResultPanel().btnEdit;
        KeyBindingProvider.bindActionToComponent(KeyBindingProvider.EDIT_1, btnEdit.getAction(),
                getResultTable());
        if (getResultTable().getActionMap().get(KeyBindingProvider.EDIT_2.getKey()) == null)
            KeyBindingProvider.bindActionToComponent(KeyBindingProvider.EDIT_2, btnEdit.getAction(),
                    getResultTable());
    }
}

From source file:org.nuclos.client.genericobject.GenericObjectCollectController.java

private void setupKeyActionsForResultPanelVerticalScrollBar() {
    // maps the default key strokes for JTable to set the vertical scrollbar, so we can intervent:
    final InputMap inputmap = getResultTable().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_END, InputEvent.CTRL_MASK), "last");
    inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "nextrow");
    inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0), "nextpage");

    final JScrollBar scrlbarVertical = getResultPanel().getResultTableScrollPane().getVerticalScrollBar();
    final DefaultBoundedRangeModel model = (DefaultBoundedRangeModel) scrlbarVertical.getModel();

    getResultTable().getActionMap().put("last", new AbstractAction() {

        @Override//  w ww .ja  v a 2 s.  com
        public void actionPerformed(ActionEvent ev) {
            final int iSupposedValue = model.getMaximum() - model.getExtent();
            model.setValue(iSupposedValue);
            // this causes the necessary rows to be loaded. Loading may be cancelled by the user.
            LOG.debug("NOW it's time to select the row...");
            if (model.getValue() == iSupposedValue)
                getCollectNavigationModel().selectLastElement();
        }
    });

    getResultTable().getActionMap().put("nextrow", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            final int iSelectedRow = getResultTable().getSelectedRow();
            final int iLastVisibleRow = TableUtils.getLastVisibleRow(getResultTable());
            if (iSelectedRow + 1 < iLastVisibleRow) {
                // next row is still visible: just select it:
                if (!getCollectNavigationModel().isLastElementSelected())
                    getCollectNavigationModel().selectNextElement();
            } else {
                // we have to move the viewport before we can select the next row:
                final int iSupposedValue = Math.min(model.getValue() + getResultTable().getRowHeight(),
                        model.getMaximum() - model.getExtent());
                model.setValue(iSupposedValue);
                // this causes the necessary rows to be loaded. Loading may be cancelled by the user.
                LOG.debug("NOW it's time to select the row...");
                if (model.getValue() == iSupposedValue)
                    if (!getCollectNavigationModel().isLastElementSelected())
                        getCollectNavigationModel().selectNextElement();
            }
        }
    });

    getResultTable().getActionMap().put("nextpage", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            final int iSupposedValue = Math.min(model.getValue() + model.getExtent(),
                    model.getMaximum() - model.getExtent());
            model.setValue(iSupposedValue);
            // this causes the necessary rows to be loaded. Loading may be cancelled by the user.
            LOG.debug("NOW it's time to select the row...");
            if (model.getValue() == iSupposedValue) {
                final int iShiftRowCount = (int) Math
                        .ceil((double) model.getExtent() / (double) getResultTable().getRowHeight());
                final int iRow = Math.min(
                        getResultTable().getSelectionModel().getAnchorSelectionIndex() + iShiftRowCount,
                        getResultTable().getRowCount() - 1);
                getResultTable().setRowSelectionInterval(iRow, iRow);
            }
        }
    });

    final Action actShowLogBook = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            cmdShowLogBook();
            getDetailsPanel().grabFocus();
        }
    };
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.SHOW_LOGBOOK, actShowLogBook,
            getDetailsPanel());

    final Action actShowStateHistory = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            cmdShowStateHistory();
            getDetailsPanel().grabFocus();
        }
    };
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.SHOW_STATE_HISTORIE, actShowStateHistory,
            getDetailsPanel());

    final Action actPrintCurrentGenericObject = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            cmdPrintCurrentGenericObject();
            getDetailsPanel().grabFocus();
        }
    };
    KeyBindingProvider.bindActionToComponent(KeyBindingProvider.PRINT_LEASED_OBJECT,
            actPrintCurrentGenericObject, getDetailsPanel());
}

From source file:org.nuclos.client.genericobject.logbook.LogbookController.java

private static void setupEscapeKey(final MainFrameTab ifrm, LogbookPanel pnlLogbook) {
    // Escape key is to close the window:
    final Action actClose = new AbstractAction() {

        @Override/*from ww  w. ja  va2s.  com*/
        public void actionPerformed(ActionEvent ev) {
            ifrm.dispose();
        }
    };

    final String KEY_CLOSE = "Close";
    ifrm.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KEY_CLOSE);
    ifrm.getRootPane().getActionMap().put(KEY_CLOSE, actClose);

    pnlLogbook.getTable().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KEY_CLOSE);
    pnlLogbook.getTable().getActionMap().put(KEY_CLOSE, actClose);
}

From source file:org.nuclos.client.genericobject.statehistory.StateHistoryController.java

private static void setupEscapeKey(final MainFrameTab ifrm, final StateHistoryPanel pnlHistory) {
    // Escape key is to close the window:
    final Action actClose = new AbstractAction() {

        @Override/* w  w w  .  ja  v a  2  s  . c  o  m*/
        public void actionPerformed(ActionEvent ev) {
            ifrm.dispose();
        }
    };

    final String KEY_CLOSE = "Close";
    ifrm.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KEY_CLOSE);
    ifrm.getRootPane().getActionMap().put(KEY_CLOSE, actClose);

    pnlHistory.getTable().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KEY_CLOSE);
    pnlHistory.getTable().getActionMap().put(KEY_CLOSE, actClose);
}

From source file:org.nuclos.client.ui.collect.CollectController.java

protected void setupShortcutsForTabs(MainFrameTab ifrm) {
    final Action actSelectSearchTab = new AbstractAction() {

        @Override//  w  ww  .  j a v  a 2 s  .co m
        public void actionPerformed(ActionEvent ev) {
            if (getCollectPanel().isTabbedPaneEnabledAt(CollectPanel.TAB_SEARCH)) {
                getCollectPanel().setTabbedPaneSelectedComponent(getSearchPanel());
            }
        }
    };

    final Action actSelectResultTab = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            if (getCollectPanel().isTabbedPaneEnabledAt(CollectPanel.TAB_RESULT)) {
                getCollectPanel().setTabbedPaneSelectedComponent(getResultPanel());
            }
        }
    };

    final Action actSelectDetailsTab = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ev) {
            if (getCollectPanel().isTabbedPaneEnabledAt(CollectPanel.TAB_DETAILS)) {
                getCollectPanel().setTabbedPaneSelectedComponent(getDetailsPanel());
            }
        }
    };

    final String sKeySelectSearchTab = "SelectSearchTab";
    ifrm.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), sKeySelectSearchTab);
    ifrm.getRootPane().getActionMap().put(sKeySelectSearchTab, actSelectSearchTab);

    final String sKeySelectResultTab = "SelectResultTab";
    ifrm.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), sKeySelectResultTab);
    ifrm.getRootPane().getActionMap().put(sKeySelectResultTab, actSelectResultTab);

    /**
     * inner class SelectTabAction
     */
    class SelectTabAction extends AbstractAction {

        private int iDirection;

        /**
         * @param iDirection -1 for previous tab, +1 for next tab
         */
        SelectTabAction(int iDirection) {
            this.iDirection = iDirection;
        }

        @Override
        public void actionPerformed(ActionEvent ev) {
            int iExternalTabIndex = getCollectPanel().getTabbedPaneSelectedIndex();

            // try to find the next enabled tab in the given direction.
            // If the selected tab is the only enabled tab, do nothing:
            for (int i = 0; i < getCollectPanel().getTabCount() - 1; ++i) {
                int iInternalTabIndex = getCollectPanel().getTabIndexOf(iExternalTabIndex);
                iInternalTabIndex = (iInternalTabIndex + iDirection + getCollectPanel().getTabCount())
                        % getCollectPanel().getTabCount();
                iExternalTabIndex = getCollectPanel().getExternalTabIndexOf(iInternalTabIndex);
                if (getCollectPanel().isTabbedPaneEnabledAt(iExternalTabIndex)) {
                    getCollectPanel().setTabbedPaneSelectedIndex(iExternalTabIndex);
                    break;
                }
            }
        }
    } // inner class SelectTabAction

    final String sKeySelectPreviousTab = "SelectPreviousTab";
    ifrm.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_DOWN_MASK), sKeySelectPreviousTab);
    ifrm.getRootPane().getActionMap().put(sKeySelectPreviousTab, new SelectTabAction(-1));

    final String sKeySelectNextTab = "SelectNextTab";
    ifrm.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_DOWN_MASK), sKeySelectNextTab);
    ifrm.getRootPane().getActionMap().put(sKeySelectNextTab, new SelectTabAction(+1));
}

From source file:org.omegat.gui.scripting.ScriptingWindow.java

private void addRunShortcutToOmegaT() {
    JRootPane appliRootPane = Core.getMainWindow().getApplicationFrame().getRootPane();
    appliRootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK, false),
            "RUN_CURRENT_SCRIPT");
    appliRootPane.getActionMap().put("RUN_CURRENT_SCRIPT", new AbstractAction() {
        private static final long serialVersionUID = 1L;

        @Override//w w w  .  j  a  va  2  s  . co  m
        public void actionPerformed(ActionEvent e) {
            runScript();
        }
    });
}

From source file:org.pentaho.reporting.engine.classic.core.modules.gui.base.AbstractExportDialog.java

protected JPanel createButtonPanel() {
    final JButton btnCancel = new JButton(getCancelAction());
    final JButton btnConfirm = new JButton(getConfirmAction());
    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1, 2, 5, 5));
    buttonPanel.add(btnConfirm);/*  ww  w  .j a  v a 2 s  .c  o  m*/
    buttonPanel.add(btnCancel);
    btnConfirm.setDefaultCapable(true);
    getRootPane().setDefaultButton(btnConfirm);
    buttonPanel.registerKeyboardAction(getConfirmAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    final JPanel buttonCarrier = new JPanel();
    buttonCarrier.setLayout(new FlowLayout(FlowLayout.RIGHT));
    buttonCarrier.add(buttonPanel);
    return buttonCarrier;
}

From source file:org.piraso.ui.base.ContextMonitorTopComponent.java

private void initKeyboardActions() {
    Action findAction = new AbstractAction() {
        @Override//w w  w .j  av a 2 s .  c om
        public void actionPerformed(ActionEvent e) {
            searcher.reset();
            btnSearch.setSelected(!btnSearch.isSelected());
            btnSearchActionPerformed(e);
        }
    };

    Action nextAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            btnNextActionPerformed(e);
        }
    };

    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.META_MASK);
    registerKeyboardAction(findAction, stroke, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    txtSearch.registerKeyboardAction(findAction, stroke, JComponent.WHEN_FOCUSED);

    stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    txtSearch.registerKeyboardAction(nextAction, stroke, JComponent.WHEN_FOCUSED);
}

From source file:org.springframework.richclient.dialog.ApplicationDialog.java

/**
 * Return the {@link InputMap} of the dialog.
 *
 * @see JLayeredPane#getInputMap(int)/*from  w w  w  .j ava  2 s  . com*/
 */
protected InputMap getInputMap() {
    return getDialog().getLayeredPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}