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

Last change on this file since 14689 was 14689, checked in by simon04, 5 years ago

fix #17202 - InputMapUtils: display Ctrl+Enter in action tooltip

  • Property svn:eol-style set to native
File size: 5.2 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;
6import java.util.Optional;
7
8import javax.swing.Action;
9import javax.swing.InputMap;
10import javax.swing.JButton;
11import javax.swing.JComponent;
12import javax.swing.KeyStroke;
13import javax.swing.SwingUtilities;
14
15/**
16 * Tools to work with Swing InputMap.
17 * @since 5200
18 */
19public final class InputMapUtils {
20
21 private InputMapUtils() {
22 // Hide default constructor for utils classes
23 }
24
25 /**
26 * Unassign Ctrl-Shift/Alt-Shift Up/Down from the given component
27 * to allow global JOSM shortcuts to work in this component.
28 * @param cmp The Swing component
29 * @param condition one of the following values:
30 * <ul>
31 * <li>JComponent.FOCUS_INPUTMAP_CREATED
32 * <li>JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
33 * <li>JComponent.WHEN_IN_FOCUSED_WINDOW
34 * </ul>
35 */
36 public static void unassignCtrlShiftUpDown(JComponent cmp, int condition) {
37 InputMap inputMap = SwingUtilities.getUIInputMap(cmp, condition);
38 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
39 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
40 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
41 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
42 SwingUtilities.replaceUIInputMap(cmp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
43 }
44
45 /**
46 * Unassign PageUp/PageDown from the given component
47 * to allow global JOSM shortcuts to work in this component.
48 * @param cmp The Swing component
49 * @param condition one of the following values:
50 * <ul>
51 * <li>JComponent.FOCUS_INPUTMAP_CREATED
52 * <li>JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
53 * <li>JComponent.WHEN_IN_FOCUSED_WINDOW
54 * </ul>
55 * @since 6557
56 */
57 public static void unassignPageUpDown(JComponent cmp, int condition) {
58 InputMap inputMap = SwingUtilities.getUIInputMap(cmp, condition);
59 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0));
60 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
61 SwingUtilities.replaceUIInputMap(cmp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
62 }
63
64 /**
65 * Enable activating button on Enter (which is replaced with spacebar for certain Look-And-Feels).
66 * @param b Button
67 */
68 public static void enableEnter(JButton b) {
69 b.setFocusable(true);
70 addEnterAction(b, b.getAction());
71 }
72
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 * @see JComponent#WHEN_FOCUSED
78 */
79 public static void addEnterAction(JComponent c, Action a) {
80 addEnterAction(c, a, JComponent.WHEN_FOCUSED);
81 }
82
83 /**
84 * Add an action activated with Enter key on a component or its children.
85 * @param c The Swing component
86 * @param a action activated with Enter key
87 * @see JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
88 * @since 10790
89 */
90 public static void addEnterActionWhenAncestor(JComponent c, Action a) {
91 addEnterAction(c, a, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
92 }
93
94 private static void addEnterAction(JComponent c, Action a, int condition) {
95 c.getActionMap().put("enter", a);
96 c.getInputMap(condition).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
97 }
98
99 /**
100 * Add an action activated with Spacebar key on a component.
101 * @param c The Swing component
102 * @param a action activated with Spacebar key
103 */
104 public static void addSpacebarAction(JComponent c, Action a) {
105 c.getActionMap().put("spacebar", a);
106 c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spacebar");
107 }
108
109 /**
110 * Add an action activated with ESCAPE key on a component or its children.
111 * @param c The Swing component
112 * @param a action activated with ESCAPE key
113 * @see JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
114 * @since 10791
115 */
116 public static void addEscapeAction(JComponent c, Action a) {
117 c.getActionMap().put("escape", a);
118 c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
119 }
120
121 /**
122 * Add an action activated with Ctrl+Enter key on a component.
123 * @param c The Swing component
124 * @param a action activated with Ctrl+Enter key
125 * @see JComponent#WHEN_IN_FOCUSED_WINDOW
126 */
127 public static void addCtrlEnterAction(JComponent c, Action a) {
128 final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK);
129 c.getActionMap().put("ctrl_enter", a);
130 c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ctrl_enter");
131 Optional.ofNullable(a.getValue(Action.SHORT_DESCRIPTION))
132 .map(String::valueOf)
133 .ifPresent(text -> Shortcut.setTooltip(a, text, stroke));
134 }
135}
Note: See TracBrowser for help on using the repository browser.