source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/DisableShortcutsOnFocusGainedTextField.java@ 7937

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.event.FocusEvent;
5import java.awt.event.KeyEvent;
6import java.util.ArrayList;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Set;
10
11import javax.swing.Action;
12import javax.swing.JMenu;
13import javax.swing.JMenuItem;
14import javax.swing.KeyStroke;
15import javax.swing.text.Document;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.tools.Pair;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * A JTextField that disabled all JOSM shortcuts composed of a single key without modifier (except F1 to F12),
24 * in order to avoid them to be triggered while typing.
25 * This allows to include text fields in toggle dialogs (needed for relation filter).
26 * @since 5696
27 */
28public class DisableShortcutsOnFocusGainedTextField extends JosmTextField {
29
30 /**
31 * Constructs a new <code>TextField</code>. A default model is created,
32 * the initial string is <code>null</code>,
33 * and the number of columns is set to 0.
34 */
35 public DisableShortcutsOnFocusGainedTextField() {
36 }
37
38 /**
39 * Constructs a new <code>TextField</code> initialized with the
40 * specified text. A default model is created and the number of
41 * columns is 0.
42 *
43 * @param text the text to be displayed, or <code>null</code>
44 */
45 public DisableShortcutsOnFocusGainedTextField(String text) {
46 super(text);
47 }
48
49 /**
50 * Constructs a new empty <code>TextField</code> with the specified
51 * number of columns.
52 * A default model is created and the initial string is set to
53 * <code>null</code>.
54 *
55 * @param columns the number of columns to use to calculate
56 * the preferred width; if columns is set to zero, the
57 * preferred width will be whatever naturally results from
58 * the component implementation
59 */
60 public DisableShortcutsOnFocusGainedTextField(int columns) {
61 super(columns);
62 }
63
64 /**
65 * Constructs a new <code>TextField</code> initialized with the
66 * specified text and columns. A default model is created.
67 *
68 * @param text the text to be displayed, or <code>null</code>
69 * @param columns the number of columns to use to calculate
70 * the preferred width; if columns is set to zero, the
71 * preferred width will be whatever naturally results from
72 * the component implementation
73 */
74 public DisableShortcutsOnFocusGainedTextField(String text, int columns) {
75 super(text, columns);
76 }
77
78 /**
79 * Constructs a new <code>JTextField</code> that uses the given text
80 * storage model and the given number of columns.
81 * This is the constructor through which the other constructors feed.
82 * If the document is <code>null</code>, a default model is created.
83 *
84 * @param doc the text storage to use; if this is <code>null</code>,
85 * a default will be provided by calling the
86 * <code>createDefaultModel</code> method
87 * @param text the initial string to display, or <code>null</code>
88 * @param columns the number of columns to use to calculate
89 * the preferred width &gt;= 0; if <code>columns</code>
90 * is set to zero, the preferred width will be whatever
91 * naturally results from the component implementation
92 * @exception IllegalArgumentException if <code>columns</code> &lt; 0
93 */
94 public DisableShortcutsOnFocusGainedTextField(Document doc, String text, int columns) {
95 super(doc, text, columns);
96 }
97
98 private final List<Pair<Action,Shortcut>> unregisteredActionShortcuts = new ArrayList<>();
99 private final Set<JosmAction> disabledMenuActions = new HashSet<>();
100
101 @Override
102 public void focusGained(FocusEvent e) {
103 super.focusGained(e);
104 disableMenuActions();
105 unregisterActionShortcuts();
106 }
107
108 @Override
109 public void focusLost(FocusEvent e) {
110 super.focusLost(e);
111 restoreActionShortcuts();
112 restoreMenuActions();
113 }
114
115 /**
116 * Disables all relevant menu actions.
117 * @see #hasToBeDisabled
118 */
119 protected void disableMenuActions() {
120 disabledMenuActions.clear();
121 for (int i = 0; i < Main.main.menu.getMenuCount(); i++) {
122 JMenu menu = Main.main.menu.getMenu(i);
123 if (menu != null) {
124 for (int j = 0; j < menu.getItemCount(); j++) {
125 JMenuItem item = menu.getItem(j);
126 if (item != null) {
127 Action action = item.getAction();
128 if (action instanceof JosmAction && action.isEnabled()) {
129 Shortcut shortcut = ((JosmAction) action).getShortcut();
130 if (shortcut != null) {
131 KeyStroke ks = shortcut.getKeyStroke();
132 if (hasToBeDisabled(ks)) {
133 action.setEnabled(false);
134 disabledMenuActions.add((JosmAction) action);
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143
144 /**
145 * Unregisters all relevant action shortcuts.
146 * @see #hasToBeDisabled
147 */
148 protected void unregisterActionShortcuts() {
149 unregisteredActionShortcuts.clear();
150 // Unregister all actions without modifiers to avoid them to be triggered by typing in this text field
151 for (Shortcut shortcut : Shortcut.listAll()) {
152 KeyStroke ks = shortcut.getKeyStroke();
153 if (hasToBeDisabled(ks)) {
154 Action action = Main.getRegisteredActionShortcut(shortcut);
155 if (action != null) {
156 Main.unregisterActionShortcut(action, shortcut);
157 unregisteredActionShortcuts.add(new Pair<>(action,shortcut));
158 }
159 }
160 }
161 }
162
163 /**
164 * Returns true if the given shortcut has no modifier and is not an actions key.
165 * @see KeyEvent#isActionKey()
166 */
167 protected boolean hasToBeDisabled(KeyStroke ks) {
168 return ks != null && ks.getModifiers() == 0 && !new KeyEvent(
169 this, KeyEvent.KEY_PRESSED, 0, ks.getModifiers(), ks.getKeyCode(), ks.getKeyChar()).isActionKey();
170 }
171
172 /**
173 * Restore all actions previously disabled
174 */
175 protected void restoreMenuActions() {
176 for (JosmAction a : disabledMenuActions) {
177 a.setEnabled(true);
178 }
179 disabledMenuActions.clear();
180 }
181
182 /**
183 * Restore all action shortcuts previously unregistered
184 */
185 protected void restoreActionShortcuts() {
186 for (Pair<Action,Shortcut> p : unregisteredActionShortcuts) {
187 Main.registerActionShortcut(p.a, p.b);
188 }
189 unregisteredActionShortcuts.clear();
190 }
191}
Note: See TracBrowser for help on using the repository browser.