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

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

SonarQube - fix more code issues

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