Changeset 4975 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2012-02-18T00:56:36+01:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MainMenu.java
r4908 r4975 357 357 public JMenu addMenu(JMenu menu, String name, int mnemonicKey, int position, String relativeHelpTopic) { 358 358 Shortcut.registerShortcut("menu:" + name, tr("Menu: {0}", tr(name)), mnemonicKey, 359 Shortcut. GROUP_MNEMONIC).setMnemonic(menu);359 Shortcut.MNEMONIC).setMnemonic(menu); 360 360 add(menu, position); 361 361 menu.putClientProperty("help", relativeHelpTopic); -
trunk/src/org/openstreetmap/josm/tools/Shortcut.java
r4971 r4975 156 156 } 157 157 158 private boolean isSame(int isKey, int isModifier) {159 // -1 --- an unassigned shortcut is different from any other shortcut160 return( isKey == assignedKey && isModifier == assignedModifier && assignedModifier != getGroupModifier(NONE));161 }162 163 158 // create a shortcut object from an string as saved in the preferences 164 159 private Shortcut(String prefString) { … … 191 186 } 192 187 193 private boolean isSame(Shortcut other) { 194 return assignedKey == other.assignedKey && assignedModifier == other.assignedModifier; 188 private boolean isSame(int isKey, int isModifier) { 189 // an unassigned shortcut is different from any other shortcut 190 return isKey == assignedKey && isModifier == assignedModifier && assignedModifier != getGroupModifier(NONE); 191 } 192 193 public boolean isEvent(KeyEvent e) { 194 return getKeyStroke() != null && getKeyStroke().equals( 195 KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers())); 195 196 } 196 197 … … 199 200 */ 200 201 public void setMnemonic(JMenu menu) { 201 if ( requestedGroup == GROUP_MNEMONIC && assignedModifier == getGroupModifier(requestedGroup + GROUPS_DEFAULT) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {202 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) { 202 203 menu.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here 203 204 } … … 207 208 */ 208 209 public void setMnemonic(AbstractButton button) { 209 if ( requestedGroup == GROUP_MNEMONIC && assignedModifier == getGroupModifier(requestedGroup + GROUPS_DEFAULT)&& getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) {210 if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) { 210 211 button.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here 211 212 } … … 248 249 // check if something collides with an existing shortcut 249 250 private static Shortcut findShortcut(int requestedKey, int modifier) { 250 if (modifier == getGroupModifier( GROUP_NONE))251 if (modifier == getGroupModifier(NONE)) 251 252 return null; 252 253 for (Shortcut sc : shortcuts.values()) { … … 282 283 public static final int CTRL_SHIFT = 5009; 283 284 public static final int ALT_CTRL_SHIFT = 5010; 285 286 /* for reassignment */ 287 private static int[] mods = {ALT_CTRL, ALT_SHIFT, CTRL_SHIFT, ALT_CTRL_SHIFT}; 288 private static int[] keys = {KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4, 289 KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8, 290 KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12}; 284 291 285 292 /* old */ … … 297 304 @Deprecated public static final int GROUPS_ALT2 = 200; 298 305 @Deprecated public static final int SHIFT_DEFAULT = 1; 306 @Deprecated public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, int modifier) { 307 return registerShortcut(shortText, longText, requestedKey, requestedGroup, modifier); 308 } 299 309 300 310 // bootstrap … … 348 358 Main.platform.initSystemShortcuts(); 349 359 // (2) User defined shortcuts 350 LinkedList<Shortcut> shortcuts = new LinkedList<Shortcut>();360 LinkedList<Shortcut> newshortcuts = new LinkedList<Shortcut>(); 351 361 for(String s : Main.pref.getAllPrefixCollectionKeys("shortcut.entry.")) { 352 shortcuts.add(new Shortcut(s)); 353 } 354 for(Shortcut sc : shortcuts) { 355 if (sc.getAssignedUser()) { 356 registerShortcut(sc); 362 newshortcuts.add(new Shortcut(s)); 363 } 364 365 for(Shortcut sc : newshortcuts) { 366 if (sc.getAssignedUser() 367 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) { 368 shortcuts.put(sc.getShortText(), sc); 357 369 } 358 370 } 359 371 // Shortcuts at their default values 360 for(Shortcut sc : shortcuts) { 361 if (!sc.getAssignedUser() && sc.getAssignedDefault()) { 362 registerShortcut(sc); 372 for(Shortcut sc : newshortcuts) { 373 if (!sc.getAssignedUser() && sc.getAssignedDefault() 374 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) { 375 shortcuts.put(sc.getShortText(), sc); 363 376 } 364 377 } 365 378 // Shortcuts that were automatically moved 366 for(Shortcut sc : shortcuts) { 367 if (!sc.getAssignedUser() && !sc.getAssignedDefault()) { 368 registerShortcut(sc); 379 for(Shortcut sc : newshortcuts) { 380 if (!sc.getAssignedUser() && !sc.getAssignedDefault() 381 && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) { 382 shortcuts.put(sc.getShortText(), sc); 369 383 } 370 384 } … … 378 392 } 379 393 394 private static int findModifier(int group, Integer modifier) { 395 Integer defaultModifier = getGroupModifier(group); 396 if(modifier != null) { 397 if(modifier == SHIFT_DEFAULT) { 398 defaultModifier |= KeyEvent.SHIFT_DOWN_MASK; 399 } else { 400 defaultModifier = modifier; 401 } 402 } 403 else if (defaultModifier == null) { // garbage in, no shortcut out 404 defaultModifier = getGroupModifier(NONE); 405 } 406 return defaultModifier; 407 } 408 380 409 // shutdown handling 381 410 public static boolean savePrefs() { … … 385 414 } 386 415 return changed; 387 }388 389 // this is used to register a shortcut that was read from the preferences390 private static void registerShortcut(Shortcut sc) {391 // put a user configured shortcut in as-is -- unless there's a conflict392 if(sc.getAssignedUser() && findShortcut(sc.getAssignedKey(),393 sc.getAssignedModifier()) == null) {394 shortcuts.put(sc.getShortText(), sc);395 } else {396 registerShortcut(sc.getShortText(), sc.getLongText(), sc.getRequestedKey(),397 sc.getRequestedGroup(), sc.getAssignedModifier(), sc);398 }399 416 } 400 417 … … 431 448 * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here. 432 449 * @param requestedGroup the group this shortcut fits best. This will determine the 433 * modifiers your shortcut will get assigned. Use the {@code GROUP_*} 434 * constants defined above. 435 * @param modifier to register a {@code ctrl+shift} command, use {@see #SHIFT_DEFAULT}. 436 */ 437 @Deprecated 438 public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, int modifier) { 439 return registerShortcut(shortText, longText, requestedKey, requestedGroup, modifier, null); 440 } 441 442 /** 443 * Register a shortcut. 444 * 445 * Here you get your shortcuts from. The parameters are: 446 * 447 * @param shortText an ID. re-use a {@code "system:*"} ID if possible, else use something unique. 448 * {@code "menu:*"} is reserved for menu mnemonics, {@code "core:*"} is reserved for 449 * actions that are part of JOSM's core. Use something like 450 * {@code <pluginname>+":"+<actionname>}. 451 * @param longText this will be displayed in the shortcut preferences dialog. Better 452 * use something the user will recognize... 453 * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here. 454 * @param requestedGroup the group this shortcut fits best. This will determine the 455 * modifiers your shortcut will get assigned. Use the {@code GROUP_*} 456 * constants defined above. 450 * modifiers your shortcut will get assigned. Use the constants defined above. 457 451 */ 458 452 public static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup) { 459 return registerShortcut(shortText, longText, requestedKey, requestedGroup, null, null); 460 } 461 462 private static int findModifier(int group, Integer modifier) { 463 Integer defaultModifier = getGroupModifier(group); 464 if(modifier != null) { 465 if(modifier == SHIFT_DEFAULT) { 466 defaultModifier |= KeyEvent.SHIFT_DOWN_MASK; 467 } else { 468 defaultModifier = modifier; 469 } 470 } 471 else if (defaultModifier == null) { // garbage in, no shortcut out 472 defaultModifier = getGroupModifier(NONE); 473 } 474 return defaultModifier; 475 } 476 477 private static int[] mods = {ALT_CTRL, ALT_SHIFT, CTRL_SHIFT, ALT_CTRL_SHIFT}; 478 private static int[] keys = {KeyEvent.VK_F1, KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4, 479 KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7, KeyEvent.VK_F8, 480 KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12}; 481 // and now the workhorse. same parameters as above, just one more: if originalShortcut is not null and 482 // is different from the shortcut that will be assigned, a popup warning will be displayed to the user. 483 // This is used when registering shortcuts that have been visible to the user before (read: have been 484 // read from the preferences file). New shortcuts will never warn, even when they land on some funny 485 // random fallback key like Ctrl+Alt+Shift+Z for "File Open..." <g> 486 private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier, 487 Shortcut originalShortcut) { 453 return registerShortcut(shortText, longText, requestedKey, requestedGroup); 454 } 455 456 // and now the workhorse. same parameters as above, just one more 457 private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier) { 488 458 doInit(); 489 459 Integer defaultModifier = findModifier(requestedGroup, modifier); … … 557 527 return sc.getKeyStroke(); 558 528 } 559 560 public boolean isEvent(KeyEvent e) {561 return getKeyStroke() != null && getKeyStroke().equals(562 KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()));563 }564 529 }
Note:
See TracChangeset
for help on using the changeset viewer.