|
13 | 13 | */
|
14 | 14 | public class Keys {
|
15 | 15 |
|
| 16 | + /** |
| 17 | + * Register a keybinding in the given components WHEN_IN_FOCUSED_WINDOW input |
| 18 | + * map, runing the given action when the action's accelerator key is pressed. |
| 19 | + * |
| 20 | + * Note that this is typically automatically handled when the action is |
| 21 | + * assigned to a JMenuItem, but it can still be needed for other actions, or |
| 22 | + * actions mapped to JMenuItems in a popup menu that is not added to any |
| 23 | + * window normally (and thus does not fire when the popup is closed). |
| 24 | + * |
| 25 | + * When the action is disabled, the keybinding is unregistered, and when it is |
| 26 | + * enabled, it is registered again. |
| 27 | + */ |
| 28 | + public static void bind(final JComponent component, final Action action) { |
| 29 | + bind(component, action, |
| 30 | + (KeyStroke) action.getValue(Action.ACCELERATOR_KEY)); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Register a keybinding, running the given action when the given keystroke is |
| 35 | + * pressed when the given component is in the focused window. |
| 36 | + * |
| 37 | + * This is typically used to bind an additional keystroke to a menu item, in |
| 38 | + * addition to the primary accelerator key. |
| 39 | + * |
| 40 | + * When the action is disabled, the keybinding is unregistered, and when it is |
| 41 | + * enabled, it is registered again. |
| 42 | + */ |
| 43 | + public static void bind(final JComponent component, final Action action, |
| 44 | + KeyStroke keystroke) { |
| 45 | + bind(component, action, keystroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Register a keybinding to be handled in given condition, running the given |
| 50 | + * action when the given keystroke is pressed. |
| 51 | + * |
| 52 | + * When the action is disabled, the keybinding is unregistered, and when it is |
| 53 | + * enabled, it is registered again. |
| 54 | + * |
| 55 | + * @param component |
| 56 | + * The component to register the keybinding on. |
| 57 | + * @param action |
| 58 | + * The action to run when the keystroke is pressed |
| 59 | + * @param action |
| 60 | + * The keystroke to bind |
| 61 | + * @param condition |
| 62 | + * The condition under which to run the keystroke. Should be one of |
| 63 | + * JComponent.WHEN_FOCUSED, |
| 64 | + * JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or |
| 65 | + * JComponent.WHEN_IN_FOCUSED_WINDOW. |
| 66 | + */ |
| 67 | + public static void bind(final JComponent component, final Action action, |
| 68 | + KeyStroke keystroke, int condition) { |
| 69 | + // The input map maps keystrokes to arbitrary objects (originally strings |
| 70 | + // that described the option, we just use the Action object itself). |
| 71 | + if (action.isEnabled()) |
| 72 | + enableBind(component, action, keystroke, condition); |
| 73 | + |
| 74 | + // The action map maps the arbitrary option to an Action to execute. These |
| 75 | + // be kept in the component even when the action is disabled. |
| 76 | + component.getActionMap().put(action, action); |
| 77 | + |
| 78 | + // Enable and disable the binding when the action is enabled / disabled. |
| 79 | + action.addPropertyChangeListener((PropertyChangeEvent e) -> { |
| 80 | + if (e.getPropertyName().equals("enabled")) { |
| 81 | + if (e.getNewValue().equals(Boolean.TRUE)) |
| 82 | + enableBind(component, action, keystroke, condition); |
| 83 | + else |
| 84 | + disableBind(component, action, keystroke, condition); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private static void enableBind(final JComponent component, |
| 90 | + final Action action, final KeyStroke keystroke, |
| 91 | + int condition) { |
| 92 | + component.getInputMap(condition).put(keystroke, action); |
| 93 | + } |
| 94 | + |
| 95 | + private static void disableBind(final JComponent component, |
| 96 | + final Action action, |
| 97 | + final KeyStroke keystroke, int condition) { |
| 98 | + component.getInputMap(condition).put(keystroke, action); |
| 99 | + } |
| 100 | + |
16 | 101 | private static final int CTRL = Toolkit.getDefaultToolkit()
|
17 | 102 | .getMenuShortcutKeyMask();
|
18 | 103 |
|
|
0 commit comments