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

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

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

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