source: josm/trunk/src/org/openstreetmap/josm/tools/Shortcut.java@ 8923

Last change on this file since 8923 was 8846, checked in by Don-vip, 9 years ago

sonar - fb-contrib - minor performance improvements:

  • Method passes constant String of length 1 to character overridden method
  • Method needlessly boxes a boolean constant
  • Method uses iterator().next() on a List to get the first item
  • Method converts String to boxed primitive using excessive boxing
  • Method converts String to primitive using excessive boxing
  • Method creates array using constants
  • Class defines List based fields but uses them like Sets
  • Property svn:eol-style set to native
File size: 19.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.KeyEvent;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.HashMap;
10import java.util.LinkedHashMap;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Map;
14
15import javax.swing.AbstractAction;
16import javax.swing.AbstractButton;
17import javax.swing.JMenu;
18import javax.swing.KeyStroke;
19import javax.swing.text.JTextComponent;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.gui.util.GuiHelper;
23
24/**
25 * Global shortcut class.
26 *
27 * Note: This class represents a single shortcut, contains the factory to obtain
28 * shortcut objects from, manages shortcuts and shortcut collisions, and
29 * finally manages loading and saving shortcuts to/from the preferences.
30 *
31 * Action authors: You only need the {@link #registerShortcut} factory. Ignore everything else.
32 *
33 * All: Use only public methods that are also marked to be used. The others are
34 * public so the shortcut preferences can use them.
35 * @since 1084
36 */
37public final class Shortcut {
38 /** the unique ID of the shortcut */
39 private final String shortText;
40 /** a human readable description that will be shown in the preferences */
41 private String longText;
42 /** the key, the caller requested */
43 private final int requestedKey;
44 /** the group, the caller requested */
45 private final int requestedGroup;
46 /** the key that actually is used */
47 private int assignedKey;
48 /** the modifiers that are used */
49 private int assignedModifier;
50 /** true if it got assigned what was requested.
51 * (Note: modifiers will be ignored in favour of group when loading it from the preferences then.) */
52 private boolean assignedDefault;
53 /** true if the user changed this shortcut */
54 private boolean assignedUser;
55 /** true if the user cannot change this shortcut (Note: it also will not be saved into the preferences) */
56 private boolean automatic;
57 /** true if the user requested this shortcut to be set to its default value
58 * (will happen on next restart, as this shortcut will not be saved to the preferences) */
59 private boolean reset;
60
61 // simple constructor
62 private Shortcut(String shortText, String longText, int requestedKey, int requestedGroup, int assignedKey, int assignedModifier,
63 boolean assignedDefault, boolean assignedUser) {
64 this.shortText = shortText;
65 this.longText = longText;
66 this.requestedKey = requestedKey;
67 this.requestedGroup = requestedGroup;
68 this.assignedKey = assignedKey;
69 this.assignedModifier = assignedModifier;
70 this.assignedDefault = assignedDefault;
71 this.assignedUser = assignedUser;
72 this.automatic = false;
73 this.reset = false;
74 }
75
76 public String getShortText() {
77 return shortText;
78 }
79
80 public String getLongText() {
81 return longText;
82 }
83
84 // a shortcut will be renamed when it is handed out again, because the original name may be a dummy
85 private void setLongText(String longText) {
86 this.longText = longText;
87 }
88
89 public int getAssignedKey() {
90 return assignedKey;
91 }
92
93 public int getAssignedModifier() {
94 return assignedModifier;
95 }
96
97 public boolean isAssignedDefault() {
98 return assignedDefault;
99 }
100
101 public boolean isAssignedUser() {
102 return assignedUser;
103 }
104
105 public boolean isAutomatic() {
106 return automatic;
107 }
108
109 public boolean isChangeable() {
110 return !automatic && !"core:none".equals(shortText);
111 }
112
113 private boolean isReset() {
114 return reset;
115 }
116
117 /**
118 * FOR PREF PANE ONLY
119 */
120 public void setAutomatic() {
121 automatic = true;
122 }
123
124 /**
125 * FOR PREF PANE ONLY
126 */
127 public void setAssignedModifier(int assignedModifier) {
128 this.assignedModifier = assignedModifier;
129 }
130
131 /**
132 * FOR PREF PANE ONLY
133 */
134 public void setAssignedKey(int assignedKey) {
135 this.assignedKey = assignedKey;
136 }
137
138 /**
139 * FOR PREF PANE ONLY
140 */
141 public void setAssignedUser(boolean assignedUser) {
142 this.reset = (this.assignedUser || reset) && !assignedUser;
143 if (assignedUser) {
144 assignedDefault = false;
145 } else if (reset) {
146 assignedKey = requestedKey;
147 assignedModifier = findModifier(requestedGroup, null);
148 }
149 this.assignedUser = assignedUser;
150 }
151
152 /**
153 * Use this to register the shortcut with Swing
154 */
155 public KeyStroke getKeyStroke() {
156 if (assignedModifier != -1)
157 return KeyStroke.getKeyStroke(assignedKey, assignedModifier);
158 return null;
159 }
160
161 // create a shortcut object from an string as saved in the preferences
162 private Shortcut(String prefString) {
163 List<String> s = new ArrayList<>(Main.pref.getCollection(prefString));
164 this.shortText = prefString.substring(15);
165 this.longText = s.get(0);
166 this.requestedKey = Integer.parseInt(s.get(1));
167 this.requestedGroup = Integer.parseInt(s.get(2));
168 this.assignedKey = Integer.parseInt(s.get(3));
169 this.assignedModifier = Integer.parseInt(s.get(4));
170 this.assignedDefault = Boolean.parseBoolean(s.get(5));
171 this.assignedUser = Boolean.parseBoolean(s.get(6));
172 }
173
174 private void saveDefault() {
175 Main.pref.getCollection("shortcut.entry."+shortText, Arrays.asList(new String[]{longText,
176 String.valueOf(requestedKey), String.valueOf(requestedGroup), String.valueOf(requestedKey),
177 String.valueOf(getGroupModifier(requestedGroup)), String.valueOf(true), String.valueOf(false)}));
178 }
179
180 // get a string that can be put into the preferences
181 private boolean save() {
182 if (isAutomatic() || isReset() || !isAssignedUser()) {
183 return Main.pref.putCollection("shortcut.entry."+shortText, null);
184 } else {
185 return Main.pref.putCollection("shortcut.entry."+shortText, Arrays.asList(new String[]{longText,
186 String.valueOf(requestedKey), String.valueOf(requestedGroup), String.valueOf(assignedKey),
187 String.valueOf(assignedModifier), String.valueOf(assignedDefault), String.valueOf(assignedUser)}));
188 }
189 }
190
191 private boolean isSame(int isKey, int isModifier) {
192 // an unassigned shortcut is different from any other shortcut
193 return isKey == assignedKey && isModifier == assignedModifier && assignedModifier != getGroupModifier(NONE);
194 }
195
196 public boolean isEvent(KeyEvent e) {
197 return getKeyStroke() != null && getKeyStroke().equals(
198 KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()));
199 }
200
201 /**
202 * use this to set a menu's mnemonic
203 */
204 public void setMnemonic(JMenu menu) {
205 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
206 menu.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
207 }
208 }
209 /**
210 * use this to set a buttons's mnemonic
211 */
212 public void setMnemonic(AbstractButton button) {
213 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
214 button.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
215 }
216 }
217 /**
218 * Sets the mnemonic key on a text component.
219 */
220 public void setFocusAccelerator(JTextComponent component) {
221 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
222 component.setFocusAccelerator(KeyEvent.getKeyText(assignedKey).charAt(0));
223 }
224 }
225 /**
226 * use this to set a actions's accelerator
227 */
228 public void setAccelerator(AbstractAction action) {
229 if (getKeyStroke() != null) {
230 action.putValue(AbstractAction.ACCELERATOR_KEY, getKeyStroke());
231 }
232 }
233
234 /**
235 * use this to get a human readable text for your shortcut
236 */
237 public String getKeyText() {
238 KeyStroke keyStroke = getKeyStroke();
239 if (keyStroke == null) return "";
240 String modifText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
241 if ("".equals(modifText)) return KeyEvent.getKeyText(keyStroke.getKeyCode());
242 return modifText + '+' + KeyEvent.getKeyText(keyStroke.getKeyCode());
243 }
244
245 @Override
246 public String toString() {
247 return getKeyText();
248 }
249
250 ///////////////////////////////
251 // everything's static below //
252 ///////////////////////////////
253
254 // here we store our shortcuts
255 private static Map<String, Shortcut> shortcuts = new LinkedHashMap<>();
256
257 // and here our modifier groups
258 private static Map<Integer, Integer> groups = new HashMap<>();
259
260 // check if something collides with an existing shortcut
261 public static Shortcut findShortcut(int requestedKey, int modifier) {
262 if (modifier == getGroupModifier(NONE))
263 return null;
264 for (Shortcut sc : shortcuts.values()) {
265 if (sc.isSame(requestedKey, modifier))
266 return sc;
267 }
268 return null;
269 }
270
271 /**
272 * FOR PREF PANE ONLY
273 */
274 public static List<Shortcut> listAll() {
275 List<Shortcut> l = new ArrayList<>();
276 for (Shortcut c : shortcuts.values()) {
277 if (!"core:none".equals(c.shortText)) {
278 l.add(c);
279 }
280 }
281 return l;
282 }
283
284 /** None group: used with KeyEvent.CHAR_UNDEFINED if no shortcut is defined */
285 public static final int NONE = 5000;
286 public static final int MNEMONIC = 5001;
287 /** Reserved group: for system shortcuts only */
288 public static final int RESERVED = 5002;
289 /** Direct group: no modifier */
290 public static final int DIRECT = 5003;
291 /** Alt group */
292 public static final int ALT = 5004;
293 /** Shift group */
294 public static final int SHIFT = 5005;
295 /** Command group. Matches CTRL modifier on Windows/Linux but META modifier on OS X */
296 public static final int CTRL = 5006;
297 /** Alt-Shift group */
298 public static final int ALT_SHIFT = 5007;
299 /** Alt-Command group. Matches ALT-CTRL modifier on Windows/Linux but ALT-META modifier on OS X */
300 public static final int ALT_CTRL = 5008;
301 /** Command-Shift group. Matches CTRL-SHIFT modifier on Windows/Linux but META-SHIFT modifier on OS X */
302 public static final int CTRL_SHIFT = 5009;
303 /** Alt-Command-Shift group. Matches ALT-CTRL-SHIFT modifier on Windows/Linux but ALT-META-SHIFT modifier on OS X */
304 public static final int ALT_CTRL_SHIFT = 5010;
305
306 /* for reassignment */
307 private static int[] mods = {ALT_CTRL, ALT_SHIFT, CTRL_SHIFT, ALT_CTRL_SHIFT};
308 private static int[] keys = {KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4,
309 KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8,
310 KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12};
311
312 // bootstrap
313 private static boolean initdone;
314 private static void doInit() {
315 if (initdone) return;
316 initdone = true;
317 int commandDownMask = GuiHelper.getMenuShortcutKeyMaskEx();
318 groups.put(NONE, -1);
319 groups.put(MNEMONIC, KeyEvent.ALT_DOWN_MASK);
320 groups.put(DIRECT, 0);
321 groups.put(ALT, KeyEvent.ALT_DOWN_MASK);
322 groups.put(SHIFT, KeyEvent.SHIFT_DOWN_MASK);
323 groups.put(CTRL, commandDownMask);
324 groups.put(ALT_SHIFT, KeyEvent.ALT_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
325 groups.put(ALT_CTRL, KeyEvent.ALT_DOWN_MASK | commandDownMask);
326 groups.put(CTRL_SHIFT, commandDownMask | KeyEvent.SHIFT_DOWN_MASK);
327 groups.put(ALT_CTRL_SHIFT, KeyEvent.ALT_DOWN_MASK | commandDownMask | KeyEvent.SHIFT_DOWN_MASK);
328
329 // (1) System reserved shortcuts
330 Main.platform.initSystemShortcuts();
331 // (2) User defined shortcuts
332 List<Shortcut> newshortcuts = new LinkedList<>();
333 for (String s : Main.pref.getAllPrefixCollectionKeys("shortcut.entry.")) {
334 newshortcuts.add(new Shortcut(s));
335 }
336
337 for (Shortcut sc : newshortcuts) {
338 if (sc.isAssignedUser()
339 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
340 shortcuts.put(sc.getShortText(), sc);
341 }
342 }
343 // Shortcuts at their default values
344 for (Shortcut sc : newshortcuts) {
345 if (!sc.isAssignedUser() && sc.isAssignedDefault()
346 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
347 shortcuts.put(sc.getShortText(), sc);
348 }
349 }
350 // Shortcuts that were automatically moved
351 for (Shortcut sc : newshortcuts) {
352 if (!sc.isAssignedUser() && !sc.isAssignedDefault()
353 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
354 shortcuts.put(sc.getShortText(), sc);
355 }
356 }
357 }
358
359 private static int getGroupModifier(int group) {
360 Integer m = groups.get(group);
361 if (m == null)
362 m = -1;
363 return m;
364 }
365
366 private static int findModifier(int group, Integer modifier) {
367 if (modifier == null) {
368 modifier = getGroupModifier(group);
369 if (modifier == null) { // garbage in, no shortcut out
370 modifier = getGroupModifier(NONE);
371 }
372 }
373 return modifier;
374 }
375
376 // shutdown handling
377 public static boolean savePrefs() {
378 boolean changed = false;
379 for (Shortcut sc : shortcuts.values()) {
380 changed = changed | sc.save();
381 }
382 return changed;
383 }
384
385 /**
386 * FOR PLATFORMHOOK USE ONLY
387 *
388 * This registers a system shortcut. See PlatformHook for details.
389 */
390 public static Shortcut registerSystemShortcut(String shortText, String longText, int key, int modifier) {
391 if (shortcuts.containsKey(shortText))
392 return shortcuts.get(shortText);
393 Shortcut potentialShortcut = findShortcut(key, modifier);
394 if (potentialShortcut != null) {
395 // this always is a logic error in the hook
396 Main.error("CONFLICT WITH SYSTEM KEY "+shortText);
397 return null;
398 }
399 potentialShortcut = new Shortcut(shortText, longText, key, RESERVED, key, modifier, true, false);
400 shortcuts.put(shortText, potentialShortcut);
401 return potentialShortcut;
402 }
403
404 /**
405 * Register a shortcut.
406 *
407 * Here you get your shortcuts from. The parameters are:
408 *
409 * @param shortText an ID. re-use a {@code "system:*"} ID if possible, else use something unique.
410 * {@code "menu:*"} is reserved for menu mnemonics, {@code "core:*"} is reserved for
411 * actions that are part of JOSM's core. Use something like
412 * {@code <pluginname>+":"+<actionname>}.
413 * @param longText this will be displayed in the shortcut preferences dialog. Better
414 * use something the user will recognize...
415 * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here.
416 * @param requestedGroup the group this shortcut fits best. This will determine the
417 * modifiers your shortcut will get assigned. Use the constants defined above.
418 */
419 public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup) {
420 return registerShortcut(shortText, longText, requestedKey, requestedGroup, null);
421 }
422
423 // and now the workhorse. same parameters as above, just one more
424 private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier) {
425 doInit();
426 Integer defaultModifier = findModifier(requestedGroup, modifier);
427 if (shortcuts.containsKey(shortText)) { // a re-register? maybe a sc already read from the preferences?
428 Shortcut sc = shortcuts.get(shortText);
429 sc.setLongText(longText); // or set by the platformHook, in this case the original longText doesn't match the real action
430 sc.saveDefault();
431 return sc;
432 }
433 Shortcut conflict = findShortcut(requestedKey, defaultModifier);
434 if (conflict != null) {
435 if (Main.isPlatformOsx()) {
436 // Try to reassign Meta to Ctrl
437 int newmodifier = findNewOsxModifier(requestedGroup);
438 if (findShortcut(requestedKey, newmodifier) == null) {
439 return reassignShortcut(shortText, longText, requestedKey, conflict, requestedGroup, requestedKey, newmodifier);
440 }
441 }
442 for (int m : mods) {
443 for (int k : keys) {
444 int newmodifier = getGroupModifier(m);
445 if (findShortcut(k, newmodifier) == null) {
446 return reassignShortcut(shortText, longText, requestedKey, conflict, m, k, newmodifier);
447 }
448 }
449 }
450 } else {
451 Shortcut newsc = new Shortcut(shortText, longText, requestedKey, requestedGroup, requestedKey, defaultModifier, true, false);
452 newsc.saveDefault();
453 shortcuts.put(shortText, newsc);
454 return newsc;
455 }
456
457 return null;
458 }
459
460 private static int findNewOsxModifier(int requestedGroup) {
461 switch (requestedGroup) {
462 case CTRL: return KeyEvent.CTRL_DOWN_MASK;
463 case ALT_CTRL: return KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK;
464 case CTRL_SHIFT: return KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK;
465 case ALT_CTRL_SHIFT: return KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK;
466 default: return 0;
467 }
468 }
469
470 private static Shortcut reassignShortcut(String shortText, String longText, int requestedKey, Shortcut conflict,
471 int m, int k, int newmodifier) {
472 Shortcut newsc = new Shortcut(shortText, longText, requestedKey, m, k, newmodifier, false, false);
473 Main.info(tr("Silent shortcut conflict: ''{0}'' moved by ''{1}'' to ''{2}''.",
474 shortText, conflict.getShortText(), newsc.getKeyText()));
475 newsc.saveDefault();
476 shortcuts.put(shortText, newsc);
477 return newsc;
478 }
479
480 /**
481 * Replies the platform specific key stroke for the 'Copy' command, i.e.
482 * 'Ctrl-C' on windows or 'Meta-C' on a Mac. null, if the platform specific
483 * copy command isn't known.
484 *
485 * @return the platform specific key stroke for the 'Copy' command
486 */
487 public static KeyStroke getCopyKeyStroke() {
488 Shortcut sc = shortcuts.get("system:copy");
489 if (sc == null) return null;
490 return sc.getKeyStroke();
491 }
492
493 /**
494 * Replies the platform specific key stroke for the 'Paste' command, i.e.
495 * 'Ctrl-V' on windows or 'Meta-V' on a Mac. null, if the platform specific
496 * paste command isn't known.
497 *
498 * @return the platform specific key stroke for the 'Paste' command
499 */
500 public static KeyStroke getPasteKeyStroke() {
501 Shortcut sc = shortcuts.get("system:paste");
502 if (sc == null) return null;
503 return sc.getKeyStroke();
504 }
505
506 /**
507 * Replies the platform specific key stroke for the 'Cut' command, i.e.
508 * 'Ctrl-X' on windows or 'Meta-X' on a Mac. null, if the platform specific
509 * 'Cut' command isn't known.
510 *
511 * @return the platform specific key stroke for the 'Cut' command
512 */
513 public static KeyStroke getCutKeyStroke() {
514 Shortcut sc = shortcuts.get("system:cut");
515 if (sc == null) return null;
516 return sc.getKeyStroke();
517 }
518}
Note: See TracBrowser for help on using the repository browser.