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

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

see #12012 - add log message for shortcut reassignments

  • Property svn:eol-style set to native
File size: 20.6 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 * @return the key stroke
155 */
156 public KeyStroke getKeyStroke() {
157 if (assignedModifier != -1)
158 return KeyStroke.getKeyStroke(assignedKey, assignedModifier);
159 return null;
160 }
161
162 // create a shortcut object from an string as saved in the preferences
163 private Shortcut(String prefString) {
164 List<String> s = new ArrayList<>(Main.pref.getCollection(prefString));
165 this.shortText = prefString.substring(15);
166 this.longText = s.get(0);
167 this.requestedKey = Integer.parseInt(s.get(1));
168 this.requestedGroup = Integer.parseInt(s.get(2));
169 this.assignedKey = Integer.parseInt(s.get(3));
170 this.assignedModifier = Integer.parseInt(s.get(4));
171 this.assignedDefault = Boolean.parseBoolean(s.get(5));
172 this.assignedUser = Boolean.parseBoolean(s.get(6));
173 }
174
175 private void saveDefault() {
176 Main.pref.getCollection("shortcut.entry."+shortText, Arrays.asList(new String[]{longText,
177 String.valueOf(requestedKey), String.valueOf(requestedGroup), String.valueOf(requestedKey),
178 String.valueOf(getGroupModifier(requestedGroup)), String.valueOf(true), String.valueOf(false)}));
179 }
180
181 // get a string that can be put into the preferences
182 private boolean save() {
183 if (isAutomatic() || isReset() || !isAssignedUser()) {
184 return Main.pref.putCollection("shortcut.entry."+shortText, null);
185 } else {
186 return Main.pref.putCollection("shortcut.entry."+shortText, Arrays.asList(new String[]{longText,
187 String.valueOf(requestedKey), String.valueOf(requestedGroup), String.valueOf(assignedKey),
188 String.valueOf(assignedModifier), String.valueOf(assignedDefault), String.valueOf(assignedUser)}));
189 }
190 }
191
192 private boolean isSame(int isKey, int isModifier) {
193 // an unassigned shortcut is different from any other shortcut
194 return isKey == assignedKey && isModifier == assignedModifier && assignedModifier != getGroupModifier(NONE);
195 }
196
197 public boolean isEvent(KeyEvent e) {
198 return getKeyStroke() != null && getKeyStroke().equals(
199 KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()));
200 }
201
202 /**
203 * use this to set a menu's mnemonic
204 */
205 public void setMnemonic(JMenu menu) {
206 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
207 menu.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
208 }
209 }
210 /**
211 * use this to set a buttons's mnemonic
212 */
213 public void setMnemonic(AbstractButton button) {
214 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
215 button.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
216 }
217 }
218 /**
219 * Sets the mnemonic key on a text component.
220 */
221 public void setFocusAccelerator(JTextComponent component) {
222 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {
223 component.setFocusAccelerator(KeyEvent.getKeyText(assignedKey).charAt(0));
224 }
225 }
226 /**
227 * use this to set a actions's accelerator
228 */
229 public void setAccelerator(AbstractAction action) {
230 if (getKeyStroke() != null) {
231 action.putValue(AbstractAction.ACCELERATOR_KEY, getKeyStroke());
232 }
233 }
234
235 /**
236 * Returns a human readable text for the shortcut.
237 * @return a human readable text for the shortcut
238 */
239 public String getKeyText() {
240 KeyStroke keyStroke = getKeyStroke();
241 if (keyStroke == null) return "";
242 String modifText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
243 if ("".equals(modifText)) return KeyEvent.getKeyText(keyStroke.getKeyCode());
244 return modifText + '+' + KeyEvent.getKeyText(keyStroke.getKeyCode());
245 }
246
247 @Override
248 public String toString() {
249 return getKeyText();
250 }
251
252 ///////////////////////////////
253 // everything's static below //
254 ///////////////////////////////
255
256 // here we store our shortcuts
257 private static Map<String, Shortcut> shortcuts = new LinkedHashMap<>();
258
259 // and here our modifier groups
260 private static Map<Integer, Integer> groups = new HashMap<>();
261
262 // check if something collides with an existing shortcut
263 public static Shortcut findShortcut(int requestedKey, int modifier) {
264 if (modifier == getGroupModifier(NONE))
265 return null;
266 for (Shortcut sc : shortcuts.values()) {
267 if (sc.isSame(requestedKey, modifier))
268 return sc;
269 }
270 return null;
271 }
272
273 /**
274 * Returns a list of all shortcuts.
275 * @return a list of all shortcuts
276 */
277 public static List<Shortcut> listAll() {
278 List<Shortcut> l = new ArrayList<>();
279 for (Shortcut c : shortcuts.values()) {
280 if (!"core:none".equals(c.shortText)) {
281 l.add(c);
282 }
283 }
284 return l;
285 }
286
287 /** None group: used with KeyEvent.CHAR_UNDEFINED if no shortcut is defined */
288 public static final int NONE = 5000;
289 public static final int MNEMONIC = 5001;
290 /** Reserved group: for system shortcuts only */
291 public static final int RESERVED = 5002;
292 /** Direct group: no modifier */
293 public static final int DIRECT = 5003;
294 /** Alt group */
295 public static final int ALT = 5004;
296 /** Shift group */
297 public static final int SHIFT = 5005;
298 /** Command group. Matches CTRL modifier on Windows/Linux but META modifier on OS X */
299 public static final int CTRL = 5006;
300 /** Alt-Shift group */
301 public static final int ALT_SHIFT = 5007;
302 /** Alt-Command group. Matches ALT-CTRL modifier on Windows/Linux but ALT-META modifier on OS X */
303 public static final int ALT_CTRL = 5008;
304 /** Command-Shift group. Matches CTRL-SHIFT modifier on Windows/Linux but META-SHIFT modifier on OS X */
305 public static final int CTRL_SHIFT = 5009;
306 /** Alt-Command-Shift group. Matches ALT-CTRL-SHIFT modifier on Windows/Linux but ALT-META-SHIFT modifier on OS X */
307 public static final int ALT_CTRL_SHIFT = 5010;
308
309 /* for reassignment */
310 private static int[] mods = {ALT_CTRL, ALT_SHIFT, CTRL_SHIFT, ALT_CTRL_SHIFT};
311 private static int[] keys = {KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4,
312 KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8,
313 KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12};
314
315 // bootstrap
316 private static boolean initdone;
317 private static void doInit() {
318 if (initdone) return;
319 initdone = true;
320 int commandDownMask = GuiHelper.getMenuShortcutKeyMaskEx();
321 groups.put(NONE, -1);
322 groups.put(MNEMONIC, KeyEvent.ALT_DOWN_MASK);
323 groups.put(DIRECT, 0);
324 groups.put(ALT, KeyEvent.ALT_DOWN_MASK);
325 groups.put(SHIFT, KeyEvent.SHIFT_DOWN_MASK);
326 groups.put(CTRL, commandDownMask);
327 groups.put(ALT_SHIFT, KeyEvent.ALT_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK);
328 groups.put(ALT_CTRL, KeyEvent.ALT_DOWN_MASK | commandDownMask);
329 groups.put(CTRL_SHIFT, commandDownMask | KeyEvent.SHIFT_DOWN_MASK);
330 groups.put(ALT_CTRL_SHIFT, KeyEvent.ALT_DOWN_MASK | commandDownMask | KeyEvent.SHIFT_DOWN_MASK);
331
332 // (1) System reserved shortcuts
333 Main.platform.initSystemShortcuts();
334 // (2) User defined shortcuts
335 List<Shortcut> newshortcuts = new LinkedList<>();
336 for (String s : Main.pref.getAllPrefixCollectionKeys("shortcut.entry.")) {
337 newshortcuts.add(new Shortcut(s));
338 }
339
340 for (Shortcut sc : newshortcuts) {
341 if (sc.isAssignedUser()
342 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
343 shortcuts.put(sc.getShortText(), sc);
344 }
345 }
346 // Shortcuts at their default values
347 for (Shortcut sc : newshortcuts) {
348 if (!sc.isAssignedUser() && sc.isAssignedDefault()
349 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
350 shortcuts.put(sc.getShortText(), sc);
351 }
352 }
353 // Shortcuts that were automatically moved
354 for (Shortcut sc : newshortcuts) {
355 if (!sc.isAssignedUser() && !sc.isAssignedDefault()
356 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
357 shortcuts.put(sc.getShortText(), sc);
358 }
359 }
360 }
361
362 private static int getGroupModifier(int group) {
363 Integer m = groups.get(group);
364 if (m == null)
365 m = -1;
366 return m;
367 }
368
369 private static int findModifier(int group, Integer modifier) {
370 if (modifier == null) {
371 modifier = getGroupModifier(group);
372 if (modifier == null) { // garbage in, no shortcut out
373 modifier = getGroupModifier(NONE);
374 }
375 }
376 return modifier;
377 }
378
379 // shutdown handling
380 public static boolean savePrefs() {
381 boolean changed = false;
382 for (Shortcut sc : shortcuts.values()) {
383 changed = changed | sc.save();
384 }
385 return changed;
386 }
387
388 /**
389 * FOR PLATFORMHOOK USE ONLY.
390 * <p>
391 * This registers a system shortcut. See PlatformHook for details.
392 * @param shortText an ID. re-use a {@code "system:*"} ID if possible, else use something unique.
393 * @param longText this will be displayed in the shortcut preferences dialog. Better
394 * use something the user will recognize...
395 * @param key the key. Use a {@link KeyEvent KeyEvent.VK_*} constant here.
396 * @param modifier the modifier. Use a {@link KeyEvent KeyEvent.*_MASK} constant here.
397 * @return the system shortcut
398 */
399 public static Shortcut registerSystemShortcut(String shortText, String longText, int key, int modifier) {
400 if (shortcuts.containsKey(shortText))
401 return shortcuts.get(shortText);
402 Shortcut potentialShortcut = findShortcut(key, modifier);
403 if (potentialShortcut != null) {
404 // this always is a logic error in the hook
405 Main.error("CONFLICT WITH SYSTEM KEY "+shortText);
406 return null;
407 }
408 potentialShortcut = new Shortcut(shortText, longText, key, RESERVED, key, modifier, true, false);
409 shortcuts.put(shortText, potentialShortcut);
410 return potentialShortcut;
411 }
412
413 /**
414 * Register a shortcut.
415 *
416 * Here you get your shortcuts from. The parameters are:
417 *
418 * @param shortText an ID. re-use a {@code "system:*"} ID if possible, else use something unique.
419 * {@code "menu:*"} is reserved for menu mnemonics, {@code "core:*"} is reserved for
420 * actions that are part of JOSM's core. Use something like
421 * {@code <pluginname>+":"+<actionname>}.
422 * @param longText this will be displayed in the shortcut preferences dialog. Better
423 * use something the user will recognize...
424 * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here.
425 * @param requestedGroup the group this shortcut fits best. This will determine the
426 * modifiers your shortcut will get assigned. Use the constants defined above.
427 * @return the shortcut
428 */
429 public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup) {
430 return registerShortcut(shortText, longText, requestedKey, requestedGroup, null);
431 }
432
433 // and now the workhorse. same parameters as above, just one more
434 private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier) {
435 doInit();
436 if (shortcuts.containsKey(shortText)) { // a re-register? maybe a sc already read from the preferences?
437 Shortcut sc = shortcuts.get(shortText);
438 sc.setLongText(longText); // or set by the platformHook, in this case the original longText doesn't match the real action
439 sc.saveDefault();
440 return sc;
441 }
442 Integer defaultModifier = findModifier(requestedGroup, modifier);
443 Shortcut conflict = findShortcut(requestedKey, defaultModifier);
444 if (conflict != null) {
445 if (Main.isPlatformOsx()) {
446 // Try to reassign Meta to Ctrl
447 int newmodifier = findNewOsxModifier(requestedGroup);
448 if (findShortcut(requestedKey, newmodifier) == null) {
449 Main.info("Reassigning OSX shortcut '" + shortText + "' from Meta to Ctrl because of conflict with " + conflict);
450 return reassignShortcut(shortText, longText, requestedKey, conflict, requestedGroup, requestedKey, newmodifier);
451 }
452 }
453 for (int m : mods) {
454 for (int k : keys) {
455 int newmodifier = getGroupModifier(m);
456 if (findShortcut(k, newmodifier) == null) {
457 Main.info("Reassigning shortcut '" + shortText + "' from " + modifier + " to " + newmodifier +
458 " because of conflict with " + conflict);
459 return reassignShortcut(shortText, longText, requestedKey, conflict, m, k, newmodifier);
460 }
461 }
462 }
463 } else {
464 Shortcut newsc = new Shortcut(shortText, longText, requestedKey, requestedGroup, requestedKey, defaultModifier, true, false);
465 newsc.saveDefault();
466 shortcuts.put(shortText, newsc);
467 return newsc;
468 }
469
470 return null;
471 }
472
473 private static int findNewOsxModifier(int requestedGroup) {
474 switch (requestedGroup) {
475 case CTRL: return KeyEvent.CTRL_DOWN_MASK;
476 case ALT_CTRL: return KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK;
477 case CTRL_SHIFT: return KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK;
478 case ALT_CTRL_SHIFT: return KeyEvent.ALT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK;
479 default: return 0;
480 }
481 }
482
483 private static Shortcut reassignShortcut(String shortText, String longText, int requestedKey, Shortcut conflict,
484 int m, int k, int newmodifier) {
485 Shortcut newsc = new Shortcut(shortText, longText, requestedKey, m, k, newmodifier, false, false);
486 Main.info(tr("Silent shortcut conflict: ''{0}'' moved by ''{1}'' to ''{2}''.",
487 shortText, conflict.getShortText(), newsc.getKeyText()));
488 newsc.saveDefault();
489 shortcuts.put(shortText, newsc);
490 return newsc;
491 }
492
493 /**
494 * Replies the platform specific key stroke for the 'Copy' command, i.e.
495 * 'Ctrl-C' on windows or 'Meta-C' on a Mac. null, if the platform specific
496 * copy command isn't known.
497 *
498 * @return the platform specific key stroke for the 'Copy' command
499 */
500 public static KeyStroke getCopyKeyStroke() {
501 Shortcut sc = shortcuts.get("system:copy");
502 if (sc == null) return null;
503 return sc.getKeyStroke();
504 }
505
506 /**
507 * Replies the platform specific key stroke for the 'Paste' command, i.e.
508 * 'Ctrl-V' on windows or 'Meta-V' on a Mac. null, if the platform specific
509 * paste command isn't known.
510 *
511 * @return the platform specific key stroke for the 'Paste' command
512 */
513 public static KeyStroke getPasteKeyStroke() {
514 Shortcut sc = shortcuts.get("system:paste");
515 if (sc == null) return null;
516 return sc.getKeyStroke();
517 }
518
519 /**
520 * Replies the platform specific key stroke for the 'Cut' command, i.e.
521 * 'Ctrl-X' on windows or 'Meta-X' on a Mac. null, if the platform specific
522 * 'Cut' command isn't known.
523 *
524 * @return the platform specific key stroke for the 'Cut' command
525 */
526 public static KeyStroke getCutKeyStroke() {
527 Shortcut sc = shortcuts.get("system:cut");
528 if (sc == null) return null;
529 return sc.getKeyStroke();
530 }
531}
Note: See TracBrowser for help on using the repository browser.