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

Last change on this file since 8929 was 8929, checked in by Don-vip, 8 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 6.9 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>, and the number of columns is set to 0.
33 */
34 public DisableShortcutsOnFocusGainedTextField() {
35 // Contents can be set with parent methods
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 columns is 0.
41 *
42 * @param text the text to be displayed, or <code>null</code>
43 */
44 public DisableShortcutsOnFocusGainedTextField(String text) {
45 super(text);
46 }
47
48 /**
49 * Constructs a new empty <code>TextField</code> with the specified number of columns.
50 * A default model is created and the initial string is set to <code>null</code>.
51 *
52 * @param columns the number of columns to use to calculate
53 * the preferred width; if columns is set to zero, the
54 * preferred width will be whatever naturally results from the component implementation
55 */
56 public DisableShortcutsOnFocusGainedTextField(int columns) {
57 super(columns);
58 }
59
60 /**
61 * Constructs a new <code>TextField</code> initialized with the
62 * specified text and columns. A default model is created.
63 *
64 * @param text the text to be displayed, or <code>null</code>
65 * @param columns the number of columns to use to calculate
66 * the preferred width; if columns is set to zero, the
67 * preferred width will be whatever naturally results from the component implementation
68 */
69 public DisableShortcutsOnFocusGainedTextField(String text, int columns) {
70 super(text, columns);
71 }
72
73 /**
74 * Constructs a new <code>JTextField</code> that uses the given text
75 * storage model and the given number of columns.
76 * This is the constructor through which the other constructors feed.
77 * If the document is <code>null</code>, a default model is created.
78 *
79 * @param doc the text storage to use; if this is <code>null</code>,
80 * a default will be provided by calling the
81 * <code>createDefaultModel</code> method
82 * @param text the initial string to display, or <code>null</code>
83 * @param columns the number of columns to use to calculate
84 * the preferred width &gt;= 0; if <code>columns</code>
85 * is set to zero, the preferred width will be whatever
86 * naturally results from the component implementation
87 * @throws IllegalArgumentException if <code>columns</code> &lt; 0
88 */
89 public DisableShortcutsOnFocusGainedTextField(Document doc, String text, int columns) {
90 super(doc, text, columns);
91 }
92
93 private final transient List<Pair<Action, Shortcut>> unregisteredActionShortcuts = new ArrayList<>();
94 private final Set<JosmAction> disabledMenuActions = new HashSet<>();
95
96 @Override
97 public void focusGained(FocusEvent e) {
98 super.focusGained(e);
99 disableMenuActions();
100 unregisterActionShortcuts();
101 }
102
103 @Override
104 public void focusLost(FocusEvent e) {
105 super.focusLost(e);
106 restoreActionShortcuts();
107 restoreMenuActions();
108 }
109
110 /**
111 * Disables all relevant menu actions.
112 * @see #hasToBeDisabled
113 */
114 protected void disableMenuActions() {
115 disabledMenuActions.clear();
116 for (int i = 0; i < Main.main.menu.getMenuCount(); i++) {
117 JMenu menu = Main.main.menu.getMenu(i);
118 if (menu != null) {
119 for (int j = 0; j < menu.getItemCount(); j++) {
120 JMenuItem item = menu.getItem(j);
121 if (item != null) {
122 Action action = item.getAction();
123 if (action instanceof JosmAction && action.isEnabled()) {
124 Shortcut shortcut = ((JosmAction) action).getShortcut();
125 if (shortcut != null) {
126 KeyStroke ks = shortcut.getKeyStroke();
127 if (hasToBeDisabled(ks)) {
128 action.setEnabled(false);
129 disabledMenuActions.add((JosmAction) action);
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138
139 /**
140 * Unregisters all relevant action shortcuts.
141 * @see #hasToBeDisabled
142 */
143 protected void unregisterActionShortcuts() {
144 unregisteredActionShortcuts.clear();
145 // Unregister all actions without modifiers to avoid them to be triggered by typing in this text field
146 for (Shortcut shortcut : Shortcut.listAll()) {
147 KeyStroke ks = shortcut.getKeyStroke();
148 if (hasToBeDisabled(ks)) {
149 Action action = Main.getRegisteredActionShortcut(shortcut);
150 if (action != null) {
151 Main.unregisterActionShortcut(action, shortcut);
152 unregisteredActionShortcuts.add(new Pair<>(action, shortcut));
153 }
154 }
155 }
156 }
157
158 /**
159 * Returns true if the given shortcut has no modifier and is not an actions key.
160 * @param ks key stroke
161 * @return {@code true} if the given shortcut has to be disabled
162 * @see KeyEvent#isActionKey()
163 */
164 protected boolean hasToBeDisabled(KeyStroke ks) {
165 return ks != null && ks.getModifiers() == 0 && !new KeyEvent(
166 this, KeyEvent.KEY_PRESSED, 0, ks.getModifiers(), ks.getKeyCode(), ks.getKeyChar()).isActionKey();
167 }
168
169 /**
170 * Restore all actions previously disabled
171 */
172 protected void restoreMenuActions() {
173 for (JosmAction a : disabledMenuActions) {
174 a.setEnabled(true);
175 }
176 disabledMenuActions.clear();
177 }
178
179 /**
180 * Restore all action shortcuts previously unregistered
181 */
182 protected void restoreActionShortcuts() {
183 for (Pair<Action, Shortcut> p : unregisteredActionShortcuts) {
184 Main.registerActionShortcut(p.a, p.b);
185 }
186 unregisteredActionShortcuts.clear();
187 }
188}
Note: See TracBrowser for help on using the repository browser.