source: josm/trunk/src/org/openstreetmap/josm/tools/InputMapUtils.java@ 14397

Last change on this file since 14397 was 14106, checked in by simon04, 6 years ago

DownloadDialog, PreferenceDialog: bind Ctrl+Enter to main action

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.event.InputEvent;
5import java.awt.event.KeyEvent;
6
7import javax.swing.Action;
8import javax.swing.InputMap;
9import javax.swing.JButton;
10import javax.swing.JComponent;
11import javax.swing.KeyStroke;
12import javax.swing.SwingUtilities;
13
14/**
15 * Tools to work with Swing InputMap.
16 * @since 5200
17 */
18public final class InputMapUtils {
19
20 private InputMapUtils() {
21 // Hide default constructor for utils classes
22 }
23
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 */
35 public static void unassignCtrlShiftUpDown(JComponent cmp, int condition) {
36 InputMap inputMap = SwingUtilities.getUIInputMap(cmp, condition);
37 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
38 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
39 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
40 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
41 SwingUtilities.replaceUIInputMap(cmp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
42 }
43
44 /**
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 /**
64 * Enable activating button on Enter (which is replaced with spacebar for certain Look-And-Feels).
65 * @param b Button
66 */
67 public static void enableEnter(JButton b) {
68 b.setFocusable(true);
69 addEnterAction(b, b.getAction());
70 }
71
72 /**
73 * Add an action activated with Enter key on a component.
74 * @param c The Swing component
75 * @param a action activated with Enter key
76 * @see JComponent#WHEN_FOCUSED
77 */
78 public static void addEnterAction(JComponent c, Action a) {
79 addEnterAction(c, a, JComponent.WHEN_FOCUSED);
80 }
81
82 /**
83 * Add an action activated with Enter key on a component or its children.
84 * @param c The Swing component
85 * @param a action activated with Enter key
86 * @see JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
87 * @since 10790
88 */
89 public static void addEnterActionWhenAncestor(JComponent c, Action a) {
90 addEnterAction(c, a, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
91 }
92
93 private static void addEnterAction(JComponent c, Action a, int condition) {
94 c.getActionMap().put("enter", a);
95 c.getInputMap(condition).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
96 }
97
98 /**
99 * Add an action activated with Spacebar key on a component.
100 * @param c The Swing component
101 * @param a action activated with Spacebar key
102 */
103 public static void addSpacebarAction(JComponent c, Action a) {
104 c.getActionMap().put("spacebar", a);
105 c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spacebar");
106 }
107
108 /**
109 * Add an action activated with ESCAPE key on a component or its children.
110 * @param c The Swing component
111 * @param a action activated with ESCAPE key
112 * @see JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
113 * @since 10791
114 */
115 public static void addEscapeAction(JComponent c, Action a) {
116 c.getActionMap().put("escape", a);
117 c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
118 }
119
120 /**
121 * Add an action activated with Ctrl+Enter key on a component.
122 * @param c The Swing component
123 * @param a action activated with Ctrl+Enter key
124 * @see JComponent#WHEN_IN_FOCUSED_WINDOW
125 */
126 public static void addCtrlEnterAction(JComponent c, Action a) {
127 final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK);
128 c.getActionMap().put("ctrl_enter", a);
129 c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ctrl_enter");
130 }
131}
Note: See TracBrowser for help on using the repository browser.