Changeset 4975 in josm for trunk


Ignore:
Timestamp:
2012-02-18T00:56:36+01:00 (8 years ago)
Author:
stoecker
Message:

some more cleanup in shortcut handling

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r4908 r4975  
    357357    public JMenu addMenu(JMenu menu, String name, int mnemonicKey, int position, String relativeHelpTopic) {
    358358        Shortcut.registerShortcut("menu:" + name, tr("Menu: {0}", tr(name)), mnemonicKey,
    359                 Shortcut.GROUP_MNEMONIC).setMnemonic(menu);
     359                Shortcut.MNEMONIC).setMnemonic(menu);
    360360        add(menu, position);
    361361        menu.putClientProperty("help", relativeHelpTopic);
  • trunk/src/org/openstreetmap/josm/tools/Shortcut.java

    r4971 r4975  
    156156    }
    157157
    158     private boolean isSame(int isKey, int isModifier) {
    159         // -1 --- an unassigned shortcut is different from any other shortcut
    160         return( isKey == assignedKey && isModifier == assignedModifier && assignedModifier != getGroupModifier(NONE));
    161     }
    162 
    163158    // create a shortcut object from an string as saved in the preferences
    164159    private Shortcut(String prefString) {
     
    191186    }
    192187
    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()));
    195196    }
    196197
     
    199200     */
    200201    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) {
    202203            menu.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
    203204        }
     
    207208     */
    208209    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) {
    210211            button.setMnemonic(KeyEvent.getKeyText(assignedKey).charAt(0)); //getKeyStroke().getKeyChar() seems not to work here
    211212        }
     
    248249    // check if something collides with an existing shortcut
    249250    private static Shortcut findShortcut(int requestedKey, int modifier) {
    250         if (modifier == getGroupModifier(GROUP_NONE))
     251        if (modifier == getGroupModifier(NONE))
    251252            return null;
    252253        for (Shortcut sc : shortcuts.values()) {
     
    282283    public static final int CTRL_SHIFT = 5009;
    283284    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};
    284291
    285292    /* old */
     
    297304    @Deprecated public static final int GROUPS_ALT2 = 200;
    298305    @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    }
    299309
    300310    // bootstrap
     
    348358        Main.platform.initSystemShortcuts();
    349359        // (2) User defined shortcuts
    350         LinkedList<Shortcut> shortcuts = new LinkedList<Shortcut>();
     360        LinkedList<Shortcut> newshortcuts = new LinkedList<Shortcut>();
    351361        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);
    357369            }
    358370        }
    359371        // 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);
    363376            }
    364377        }
    365378        // 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);
    369383            }
    370384        }
     
    378392    }
    379393
     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
    380409    // shutdown handling
    381410    public static boolean savePrefs() {
     
    385414        }
    386415        return changed;
    387     }
    388 
    389     // this is used to register a shortcut that was read from the preferences
    390     private static void registerShortcut(Shortcut sc) {
    391         // put a user configured shortcut in as-is -- unless there's a conflict
    392         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         }
    399416    }
    400417
     
    431448     * @param requestedKey the key you'd prefer. Use a {@link KeyEvent KeyEvent.VK_*} constant here.
    432449     * @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.
    457451     */
    458452    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) {
    488458        doInit();
    489459        Integer defaultModifier = findModifier(requestedGroup, modifier);
     
    557527        return sc.getKeyStroke();
    558528    }
    559 
    560     public boolean isEvent(KeyEvent e) {
    561         return getKeyStroke() != null && getKeyStroke().equals(
    562         KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()));
    563     }
    564529}
Note: See TracChangeset for help on using the changeset viewer.