1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.event.InputEvent;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 |
|
---|
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 | /**
|
---|
15 | * Tools to work with Swing InputMap
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public final class InputMapUtils {
|
---|
19 |
|
---|
20 | private InputMapUtils() {
|
---|
21 | // Hide default constructor for utils classes
|
---|
22 | }
|
---|
23 |
|
---|
24 | public static void unassignCtrlShiftUpDown(JComponent cmp, int condition) {
|
---|
25 | InputMap inputMap=SwingUtilities.getUIInputMap(cmp, condition);
|
---|
26 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP,InputEvent.CTRL_MASK|InputEvent.SHIFT_MASK));
|
---|
27 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,InputEvent.CTRL_MASK|InputEvent.SHIFT_MASK));
|
---|
28 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP,InputEvent.ALT_MASK|InputEvent.SHIFT_MASK));
|
---|
29 | inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,InputEvent.ALT_MASK|InputEvent.SHIFT_MASK));
|
---|
30 | SwingUtilities.replaceUIInputMap(cmp,JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,inputMap);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Enable activating button on Enter (which is replaced with spacebar for certain Look-And-Feels)
|
---|
35 | */
|
---|
36 | public static void enableEnter(JButton b) {
|
---|
37 | b.setFocusable(true);
|
---|
38 | b.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
|
---|
39 | b.getActionMap().put("enter",b.getAction());
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static void addEnterAction(JComponent c, Action a) {
|
---|
43 | c.getActionMap().put("enter", a);
|
---|
44 | c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static void addSpacebarAction(JComponent c, Action a) {
|
---|
48 | c.getActionMap().put("spacebar", a);
|
---|
49 | c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spacebar");
|
---|
50 | }
|
---|
51 | }
|
---|