[5927] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[5200] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import java.awt.event.InputEvent;
|
---|
| 5 | import java.awt.event.KeyEvent;
|
---|
[5927] | 6 |
|
---|
[5200] | 7 | import javax.swing.Action;
|
---|
| 8 | import javax.swing.InputMap;
|
---|
| 9 | import javax.swing.JButton;
|
---|
| 10 | import javax.swing.JComponent;
|
---|
| 11 | import javax.swing.KeyStroke;
|
---|
| 12 | import javax.swing.SwingUtilities;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
[7539] | 15 | * Tools to work with Swing InputMap.
|
---|
| 16 | * @since 5200
|
---|
[5200] | 17 | */
|
---|
[6362] | 18 | public final class InputMapUtils {
|
---|
[7509] | 19 |
|
---|
[6362] | 20 | private InputMapUtils() {
|
---|
| 21 | // Hide default constructor for utils classes
|
---|
| 22 | }
|
---|
[7509] | 23 |
|
---|
[6557] | 24 | /**
|
---|
| 25 | * Unassign Ctrl-Shift/Alt-Shift Up/Down from the given component
|
---|
| 26 | * to allow global JOSM shortcuts to work in this component.
|
---|
| 27 | * @param cmp The Swing component
|
---|
| 28 | * @param condition one of the following values:
|
---|
| 29 | * <ul>
|
---|
| 30 | * <li>JComponent.FOCUS_INPUTMAP_CREATED
|
---|
| 31 | * <li>JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
|
---|
| 32 | * <li>JComponent.WHEN_IN_FOCUSED_WINDOW
|
---|
| 33 | * </ul>
|
---|
| 34 | */
|
---|
[6362] | 35 | public static void unassignCtrlShiftUpDown(JComponent cmp, int condition) {
|
---|
[8510] | 36 | InputMap inputMap = SwingUtilities.getUIInputMap(cmp, condition);
|
---|
| 37 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK));
|
---|
| 38 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK));
|
---|
| 39 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK));
|
---|
| 40 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK));
|
---|
| 41 | SwingUtilities.replaceUIInputMap(cmp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
|
---|
[6362] | 42 | }
|
---|
[6070] | 43 |
|
---|
[6362] | 44 | /**
|
---|
[6557] | 45 | * Unassign PageUp/PageDown from the given component
|
---|
| 46 | * to allow global JOSM shortcuts to work in this component.
|
---|
| 47 | * @param cmp The Swing component
|
---|
| 48 | * @param condition one of the following values:
|
---|
| 49 | * <ul>
|
---|
| 50 | * <li>JComponent.FOCUS_INPUTMAP_CREATED
|
---|
| 51 | * <li>JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
|
---|
| 52 | * <li>JComponent.WHEN_IN_FOCUSED_WINDOW
|
---|
| 53 | * </ul>
|
---|
| 54 | * @since 6557
|
---|
| 55 | */
|
---|
| 56 | public static void unassignPageUpDown(JComponent cmp, int condition) {
|
---|
| 57 | InputMap inputMap = SwingUtilities.getUIInputMap(cmp, condition);
|
---|
| 58 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0));
|
---|
| 59 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
|
---|
| 60 | SwingUtilities.replaceUIInputMap(cmp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
[7539] | 64 | * Enable activating button on Enter (which is replaced with spacebar for certain Look-And-Feels).
|
---|
| 65 | * @param b Button
|
---|
[6362] | 66 | */
|
---|
| 67 | public static void enableEnter(JButton b) {
|
---|
[5200] | 68 | b.setFocusable(true);
|
---|
[5367] | 69 | b.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
|
---|
[7539] | 70 | b.getActionMap().put("enter", b.getAction());
|
---|
[6362] | 71 | }
|
---|
[6070] | 72 |
|
---|
[7539] | 73 | /**
|
---|
| 74 | * Add an action activated with Enter key on a component.
|
---|
| 75 | * @param c The Swing component
|
---|
| 76 | * @param a action activated with Enter key
|
---|
| 77 | */
|
---|
[6362] | 78 | public static void addEnterAction(JComponent c, Action a) {
|
---|
[5200] | 79 | c.getActionMap().put("enter", a);
|
---|
| 80 | c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
|
---|
[6362] | 81 | }
|
---|
[6070] | 82 |
|
---|
[7539] | 83 | /**
|
---|
| 84 | * Add an action activated with Spacebar key on a component.
|
---|
| 85 | * @param c The Swing component
|
---|
| 86 | * @param a action activated with Spacebar key
|
---|
| 87 | */
|
---|
[6362] | 88 | public static void addSpacebarAction(JComponent c, Action a) {
|
---|
[5200] | 89 | c.getActionMap().put("spacebar", a);
|
---|
| 90 | c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spacebar");
|
---|
[6362] | 91 | }
|
---|
[5200] | 92 | }
|
---|