public static void addKeyboardShortcut(final JComponent target, final AbstractButton button, final KeyStroke keyStroke) { target.registerKeyboardAction( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (button.isEnabled()) { button.doClick(); } } }, keyStroke, JComponent.WHEN_FOCUSED ); }
javax.swing.JComponent.registerKeyboardAction(ActionListener, String, KeyStroke, int)
public static void setupCancelWhenEscIsPressed(JDialog d, Runnable cancelAction) { JComponent contentPane = (JComponent) d.getContentPane(); contentPane.registerKeyboardAction(e -> cancelAction.run(), ESC, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); }
/** * Configures a {@code JDialog} to be closed when {@code ESC} key is pressed. * * @param dialog {@code JDialog} * @since 0.2.3 */ public static void configureCloseDialogOnEscape(JDialog dialog) { Container contentPane = dialog.getContentPane(); if (contentPane instanceof JComponent) ((JComponent) contentPane).registerKeyboardAction(new CloseDialogOnEscape(dialog), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); }
/** * Registers all actions registered on the source component and registered them on the target component at the * specified condition. * * @param sourceComponent the source component. * @param targetComponent the target component. * @param keyStrokes the keystrokes * @param condition the condition which will be used in {@link javax.swing.JComponent#registerKeyboardAction(java.awt.event.ActionListener, * javax.swing.KeyStroke, int)} as the last parameter. */ public static void synchronizeKeyboardActions(JComponent sourceComponent, JComponent targetComponent, KeyStroke[] keyStrokes, int condition) { for (KeyStroke keyStroke : keyStrokes) { ActionListener actionListener = sourceComponent.getActionForKeyStroke(keyStroke); if (actionListener != null) { targetComponent.registerKeyboardAction(actionListener, keyStroke, condition); } } }
/** * Constructor * @param owner owner frame * @param title title text - will be diaplayed on the title bar * @param modal true if modal, false otherwise */ public CenteredDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); Action escape = new AbstractAction() { public void actionPerformed(java.awt.event.ActionEvent arg1) { dispose(); } }; ((JComponent)getContentPane()).registerKeyboardAction(escape, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); ((JComponent)getContentPane()).registerKeyboardAction(escape, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED); }
/** * Constructor * @param owner owner dialog * @param title title text - will be diaplayed on the title bar * @param modal true if modal, false otherwise */ public CenteredDialog(Dialog owner, String title, boolean modal) { super(owner, title, modal); Action escape = new AbstractAction() { public void actionPerformed(java.awt.event.ActionEvent arg1) { dispose(); } }; ((JComponent)getContentPane()).registerKeyboardAction(escape, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); ((JComponent)getContentPane()).registerKeyboardAction(escape, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED); }
public void registerKeyboardAction(Action action, KeyStroke keyStroke) { JComponent comp = getEntryPanel().getTextComponent(); comp.registerKeyboardAction(action, keyStroke, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } }
javax.swing.JComponent.registerKeyboardAction(ActionListener, KeyStroke, int) javax.swing.JComponent.getInputMap(int) javax.swing.JComponent.getActionMap()
/** * Installs the actions for arrow keys to allow user to navigate components using arrow keys. * * @param container the container such as ButtonPanel, JPanel etc. */ public void install(JComponent container) { for (int keyCode : _keyCode) { InputMap inputMap = container.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0); Object actionName = inputMap.get(keyStroke); if (actionName != null) { container.putClientProperty(CLIENT_PROPERTY_ARROWKEY_NAVIGATION_SUPPORT, actionName); } container.registerKeyboardAction(new NavigationAction(container, keyCode), "ArrowKeyNavigation " + keyCode, keyStroke, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } }
public static void restoreAction(JComponent component, int condition, KeyStroke keyStroke) { if (component == null) { return; } ActionListener action = component.getActionForKeyStroke(keyStroke); if (action instanceof DelegateAction) { Action actualAction = ((DelegateAction) action).getAction(); if (actualAction != null) { component.registerKeyboardAction(actualAction, keyStroke, condition); } else { component.unregisterKeyboardAction(keyStroke); } } }
comp.registerKeyboardAction(findAction, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);
public static void restoreAction(JComponent component, int condition, KeyStroke keyStroke, Class<?> actionClass) { ActionListener action = component.getActionForKeyStroke(keyStroke); ActionListener parent = action; ActionListener top = action; while (action instanceof DelegateAction) { if (actionClass.isAssignableFrom(action.getClass())) { if (top == action) { Action a = ((DelegateAction) action).getAction(); if (a == null) { component.unregisterKeyboardAction(keyStroke); } else { component.registerKeyboardAction(a, keyStroke, condition); } } else { ((DelegateAction) parent).setAction(((DelegateAction) action).getAction()); } break; } parent = action; action = ((DelegateAction) action).getAction(); } }
public static void restoreAction(JComponent component, int condition, KeyStroke keyStroke, Action actionToBeRemoved) { ActionListener action = component.getActionForKeyStroke(keyStroke); ActionListener parent = action; ActionListener top = action; while (action instanceof DelegateAction) { if (actionToBeRemoved == action) { if (top == action) { component.registerKeyboardAction(((DelegateAction) action).getAction(), keyStroke, condition); } else { ((DelegateAction) parent).setAction(((DelegateAction) action).getAction()); } break; } parent = action; action = ((DelegateAction) action).getAction(); } } }
public void run() { ISQLEntryPanel findEntryPanel = objectTreePanel.getFindController().getFindEntryPanel(); CodeCompletionInfoCollection c = new CodeCompletionInfoCollection(session, CodeCompletionPlugin.this, false); CompleteCodeAction cca = new CompleteCodeAction(session.getApplication(), CodeCompletionPlugin.this, findEntryPanel, session, c, objectTreePanel); JComponent comp = findEntryPanel.getTextComponent(); comp.registerKeyboardAction(cca, _resources.getKeyStroke(cca), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); }
public UndoHandlerImpl(IApplication application, ISQLEntryPanel entry) { if (!entry.hasOwnUndoableManager()) { SquirrelDefaultUndoManager undoManager = new SquirrelDefaultUndoManager(); Resources res = application.getResources(); _undoAction = new UndoAction(application, undoManager); _redoAction = new RedoAction(application, undoManager); JComponent comp = entry.getTextComponent(); comp.registerKeyboardAction(_undoAction, res.getKeyStroke(_undoAction), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); comp.registerKeyboardAction(_redoAction, res.getKeyStroke(_redoAction), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); entry.setUndoManager(undoManager); } else { IUndoHandler undoHandler = entry.createUndoHandler(); _undoAction = new UndoAction(application, undoHandler.getUndoAction()); _redoAction = new RedoAction(application, undoHandler.getRedoAction()); } entry.addRedoUndoActionsToSQLEntryAreaMenu(_undoAction, _redoAction); }
public UndoHandlerImpl(IApplication application, ISQLEntryPanel entry) { if (!entry.hasOwnUndoableManager()) { SquirrelDefaultUndoManager undoManager = new SquirrelDefaultUndoManager(); Resources res = application.getResources(); _undoAction = new UndoAction(application, undoManager); _redoAction = new RedoAction(application, undoManager); JComponent comp = entry.getTextComponent(); comp.registerKeyboardAction(_undoAction, res.getKeyStroke(_undoAction), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); comp.registerKeyboardAction(_redoAction, res.getKeyStroke(_redoAction), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); entry.setUndoManager(undoManager); } else { IUndoHandler undoHandler = entry.createUndoHandler(); _undoAction = new UndoAction(application, undoHandler.getUndoAction()); _redoAction = new RedoAction(application, undoHandler.getRedoAction()); } entry.addRedoUndoActionsToSQLEntryAreaMenu(_undoAction, _redoAction); }
public void run() { CodeCompletionInfoCollection c = new CodeCompletionInfoCollection(session, CodeCompletionPlugin.this, true); CompleteCodeAction cca = new CompleteCodeAction(session.getApplication(), CodeCompletionPlugin.this, sqlPaneAPI.getSQLEntryPanel(), session, c, null); JMenuItem item = sqlPaneAPI.addToSQLEntryAreaMenu(cca); _resources.configureMenuItem(cca, item); JComponent comp = sqlPaneAPI.getSQLEntryPanel().getTextComponent(); comp.registerKeyboardAction(cca, _resources.getKeyStroke(cca), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); sqlPaneAPI.addToToolsPopUp("completecode", cca); }
public void sqlInternalFrameOpened(SQLInternalFrame sqlInternalFrame, ISession sess) { ActionCollection coll = getApplication().getActionCollection(); sqlInternalFrame.addSeparatorToToolbar(); sqlInternalFrame.addToToolbar(coll.get(AddBookmarkAction.class)); sqlInternalFrame.addToToolbar(coll.get(EditBookmarksAction.class)); sqlInternalFrame.addToToolsPopUp("bookmarkadd", coll.get(AddBookmarkAction.class)); sqlInternalFrame.addToToolsPopUp("bookmarkedit", coll.get(EditBookmarksAction.class)); ISQLPanelAPI sqlPaneAPI = sqlInternalFrame.getSQLPanelAPI(); CompleteBookmarkAction cba = new CompleteBookmarkAction(sess.getApplication(), _resources, sqlPaneAPI.getSQLEntryPanel(), SQLBookmarkPlugin.this); JMenuItem item = sqlPaneAPI.addToSQLEntryAreaMenu(cba); _resources.configureMenuItem(cba, item); JComponent comp = sqlPaneAPI.getSQLEntryPanel().getTextComponent(); comp.registerKeyboardAction(cba, _resources.getKeyStroke(cba), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); sqlInternalFrame.addToToolsPopUp("bookmarkselect", cba); }
private void addBookmarkAction(ISession session) { ActionCollection coll = getApplication().getActionCollection(); session.addSeparatorToToolbar(); session.addToToolbar(coll.get(AddBookmarkAction.class)); session.addToToolbar(coll.get(EditBookmarksAction.class)); session.getSessionInternalFrame().addToToolsPopUp("bookmarkadd", coll.get(AddBookmarkAction.class)); session.getSessionInternalFrame().addToToolsPopUp("bookmarkedit", coll.get(EditBookmarksAction.class)); ISQLPanelAPI sqlPaneAPI = session.getSessionInternalFrame().getSQLPanelAPI(); CompleteBookmarkAction cba = new CompleteBookmarkAction(session.getApplication(), _resources, sqlPaneAPI.getSQLEntryPanel(), SQLBookmarkPlugin.this); JMenuItem item = sqlPaneAPI.addToSQLEntryAreaMenu(cba); _resources.configureMenuItem(cba, item); JComponent comp = sqlPaneAPI.getSQLEntryPanel().getTextComponent(); comp.registerKeyboardAction(cba, _resources.getKeyStroke(cba), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); session.getSessionInternalFrame().addToToolsPopUp("bookmarkselect", cba); }