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