Ticket #12030: 12030-cowal.patch

File 12030-cowal.patch, 9.2 KB (added by simon04, 8 years ago)
  • src/org/openstreetmap/josm/tools/Shortcut.java

    diff --git a/src/org/openstreetmap/josm/tools/Shortcut.java b/src/org/openstreetmap/josm/tools/Shortcut.java
    index 2448aec..8c212bc 100644
    a b  
    66import java.awt.event.KeyEvent;
    77import java.util.ArrayList;
    88import java.util.Arrays;
     9import java.util.Collections;
    910import java.util.HashMap;
    10 import java.util.LinkedHashMap;
    1111import java.util.LinkedList;
    1212import java.util.List;
    1313import java.util.Map;
     14import java.util.Optional;
     15import java.util.concurrent.CopyOnWriteArrayList;
    1416
    1517import javax.swing.AbstractAction;
    1618import javax.swing.AbstractButton;
    public String toString() {  
    267269    ///////////////////////////////
    268270
    269271    // here we store our shortcuts
    270     private static Map<String, Shortcut> shortcuts = new LinkedHashMap<>();
     272    private static List<Shortcut> shortcuts = new CopyOnWriteArrayList<>();
    271273
    272274    // and here our modifier groups
    273275    private static Map<Integer, Integer> groups = new HashMap<>();
    274276
    275277    // check if something collides with an existing shortcut
     278
     279    /**
     280     * Returns the registered shortcut fot the key and modifier
     281     * @param requestedKey the requested key
     282     * @param modifier the modifier
     283     * @return the registered shortcut or {@code null}
     284     */
    276285    public static Shortcut findShortcut(int requestedKey, int modifier) {
     286        return findShortcutByKeyOrShortText(requestedKey, modifier, null)
     287                .orElse(null);
     288    }
     289
     290    private static Optional<Shortcut> findShortcutByKeyOrShortText(int requestedKey, int modifier, String shortText) {
    277291        if (modifier == getGroupModifier(NONE))
    278             return null;
    279         for (Shortcut sc : shortcuts.values()) {
    280             if (sc.isSame(requestedKey, modifier))
    281                 return sc;
    282         }
    283         return null;
     292            return Optional.empty();
     293        return shortcuts.stream()
     294                .filter(sc -> sc.isSame(requestedKey, modifier) || (shortText != null && shortText.equals(sc.getShortText())))
     295                .findAny();
     296
    284297    }
    285298
    286299    /**
    public static Shortcut findShortcut(int requestedKey, int modifier) {  
    288301     * @return a list of all shortcuts
    289302     */
    290303    public static List<Shortcut> listAll() {
    291         List<Shortcut> l = new ArrayList<>();
    292         for (Shortcut c : shortcuts.values()) {
    293             if (!"core:none".equals(c.shortText)) {
    294                 l.add(c);
    295             }
    296         }
    297         return l;
     304        return Collections.unmodifiableList(shortcuts);
    298305    }
    299306
    300307    /** None group: used with KeyEvent.CHAR_UNDEFINED if no shortcut is defined */
    private static void doInit() {  
    353360        for (Shortcut sc : newshortcuts) {
    354361            if (sc.isAssignedUser()
    355362            && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
    356                 shortcuts.put(sc.getShortText(), sc);
     363                shortcuts.add(sc);
    357364            }
    358365        }
    359366        // Shortcuts at their default values
    360367        for (Shortcut sc : newshortcuts) {
    361368            if (!sc.isAssignedUser() && sc.isAssignedDefault()
    362369            && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
    363                 shortcuts.put(sc.getShortText(), sc);
     370                shortcuts.add(sc);
    364371            }
    365372        }
    366373        // Shortcuts that were automatically moved
    367374        for (Shortcut sc : newshortcuts) {
    368375            if (!sc.isAssignedUser() && !sc.isAssignedDefault()
    369376            && findShortcut(sc.getAssignedKey(), sc.getAssignedModifier()) == null) {
    370                 shortcuts.put(sc.getShortText(), sc);
     377                shortcuts.add(sc);
    371378            }
    372379        }
    373380    }
    private static int findModifier(int group, Integer modifier) {  
    392399    // shutdown handling
    393400    public static boolean savePrefs() {
    394401        boolean changed = false;
    395         for (Shortcut sc : shortcuts.values()) {
     402        for (Shortcut sc : shortcuts) {
    396403            changed = changed | sc.save();
    397404        }
    398405        return changed;
    public static boolean savePrefs() {  
    410417     * @return the system shortcut
    411418     */
    412419    public static Shortcut registerSystemShortcut(String shortText, String longText, int key, int modifier) {
    413         if (shortcuts.containsKey(shortText))
    414             return shortcuts.get(shortText);
    415         Shortcut potentialShortcut = findShortcut(key, modifier);
    416         if (potentialShortcut != null) {
     420        final Optional<Shortcut> existing = findShortcutByKeyOrShortText(key, modifier, shortText);
     421        if (existing.isPresent() && shortText.equals(existing.get().getShortText())) {
     422            return existing.get();
     423        } else if (existing.isPresent()) {
    417424            // this always is a logic error in the hook
    418             Main.error("CONFLICT WITH SYSTEM KEY "+shortText+": "+potentialShortcut);
     425            Main.error("CONFLICT WITH SYSTEM KEY " + shortText + ": " + existing.get());
    419426            return null;
    420427        }
    421         potentialShortcut = new Shortcut(shortText, longText, key, RESERVED, key, modifier, true, false);
    422         shortcuts.put(shortText, potentialShortcut);
    423         return potentialShortcut;
     428        final Shortcut shortcut = new Shortcut(shortText, longText, key, RESERVED, key, modifier, true, false);
     429        shortcuts.add(shortcut);
     430        return shortcut;
    424431    }
    425432
    426433    /**
    public static Shortcut registerShortcut(String shortText, String longText, int r  
    446453    // and now the workhorse. same parameters as above, just one more
    447454    private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier) {
    448455        doInit();
    449         if (shortcuts.containsKey(shortText)) { // a re-register? maybe a sc already read from the preferences?
    450             Shortcut sc = shortcuts.get(shortText);
     456        Integer defaultModifier = findModifier(requestedGroup, modifier);
     457        final Optional<Shortcut> existing = findShortcutByKeyOrShortText(requestedKey, defaultModifier, shortText);
     458        if (existing.isPresent() && shortText.equals(existing.get().getShortText())) {
     459            // a re-register? maybe a sc already read from the preferences?
     460            final Shortcut sc = existing.get();
    451461            sc.setLongText(longText); // or set by the platformHook, in this case the original longText doesn't match the real action
    452462            sc.saveDefault();
    453463            return sc;
    454         }
    455         Integer defaultModifier = findModifier(requestedGroup, modifier);
    456         Shortcut conflict = findShortcut(requestedKey, defaultModifier);
    457         if (conflict != null) {
     464        } else if (existing.isPresent()) {
     465            final Shortcut conflict = existing.get();
    458466            if (Main.isPlatformOsx()) {
    459467                // Try to reassign Meta to Ctrl
    460468                int newmodifier = findNewOsxModifier(requestedGroup);
    private static Shortcut registerShortcut(String shortText, String longText, int  
    476484        } else {
    477485            Shortcut newsc = new Shortcut(shortText, longText, requestedKey, requestedGroup, requestedKey, defaultModifier, true, false);
    478486            newsc.saveDefault();
    479             shortcuts.put(shortText, newsc);
     487            shortcuts.add(newsc);
    480488            return newsc;
    481489        }
    482490
    private static Shortcut reassignShortcut(String shortText, String longText, int  
    499507        Main.info(tr("Silent shortcut conflict: ''{0}'' moved by ''{1}'' to ''{2}''.",
    500508            shortText, conflict.getShortText(), newsc.getKeyText()));
    501509        newsc.saveDefault();
    502         shortcuts.put(shortText, newsc);
     510        shortcuts.replaceAll(sc -> shortText.equals(sc.getShortText()) ? newsc : sc);
    503511        return newsc;
    504512    }
    505513
    private static Shortcut reassignShortcut(String shortText, String longText, int  
    511519     * @return the platform specific key stroke for the  'Copy' command
    512520     */
    513521    public static KeyStroke getCopyKeyStroke() {
    514         Shortcut sc = shortcuts.get("system:copy");
    515         if (sc == null) return null;
    516         return sc.getKeyStroke();
     522        return getKeyStrokeForShortKey("system:copy");
    517523    }
    518524
    519525    /**
    public static KeyStroke getCopyKeyStroke() {  
    524530     * @return the platform specific key stroke for the 'Paste' command
    525531     */
    526532    public static KeyStroke getPasteKeyStroke() {
    527         Shortcut sc = shortcuts.get("system:paste");
    528         if (sc == null) return null;
    529         return sc.getKeyStroke();
     533        return getKeyStrokeForShortKey("system:paste");
    530534    }
    531535
    532536    /**
    public static KeyStroke getPasteKeyStroke() {  
    537541     * @return the platform specific key stroke for the 'Cut' command
    538542     */
    539543    public static KeyStroke getCutKeyStroke() {
    540         Shortcut sc = shortcuts.get("system:cut");
    541         if (sc == null) return null;
    542         return sc.getKeyStroke();
     544        return getKeyStrokeForShortKey("system:cut");
     545    }
     546
     547    private static KeyStroke getKeyStrokeForShortKey(String shortKey) {
     548        return shortcuts.stream()
     549                .filter(sc -> shortKey.equals(sc.getShortText()))
     550                .findAny()
     551                .map(Shortcut::getKeyStroke)
     552                .orElse(null);
    543553    }
    544554}