Changeset 11170 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-10-26T18:49:40+02:00 (8 years ago)
Author:
simon04
Message:

fix #13808 see #12030 - Custom shortcuts are broken

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Shortcut.java

    r11122 r11170  
    77import java.util.ArrayList;
    88import java.util.Arrays;
     9import java.util.Collection;
    910import java.util.HashMap;
    1011import java.util.LinkedList;
     
    1314import java.util.Optional;
    1415import java.util.concurrent.CopyOnWriteArrayList;
     16import java.util.function.Predicate;
    1517import java.util.stream.Collectors;
    1618
     
    270272
    271273    // here we store our shortcuts
    272     private static List<Shortcut> shortcuts = new CopyOnWriteArrayList<>();
     274    private static Collection<Shortcut> shortcuts = new CopyOnWriteArrayList<Shortcut>() {
     275        @Override
     276        public boolean add(Shortcut shortcut) {
     277            // expensive consistency check only in debug mode
     278            if (Main.isDebugEnabled()
     279                    && stream().map(Shortcut::getShortText).anyMatch(shortcut.getShortText()::equals)) {
     280                Main.warn(new AssertionError(shortcut.getShortText() + " already added"));
     281            }
     282            return super.add(shortcut);
     283        }
     284    };
    273285
    274286    // and here our modifier groups
     
    289301
    290302    private static Optional<Shortcut> findShortcutByKeyOrShortText(int requestedKey, int modifier, String shortText) {
    291         if (modifier == getGroupModifier(NONE))
    292             return Optional.empty();
     303        final Predicate<Shortcut> sameKey = sc -> modifier != getGroupModifier(NONE) && sc.isSame(requestedKey, modifier);
     304        final Predicate<Shortcut> sameShortText = sc -> sc.getShortText().equals(shortText);
    293305        return shortcuts.stream()
    294                 .filter(sc -> sc.isSame(requestedKey, modifier) || (shortText != null && shortText.equals(sc.getShortText())))
     306                .filter(sameKey.or(sameShortText))
    295307                .findAny();
    296 
    297308    }
    298309
     
    401412    // shutdown handling
    402413    public static boolean savePrefs() {
    403         boolean changed = false;
    404         for (Shortcut sc : shortcuts) {
    405             changed = changed | sc.save();
    406         }
    407         return changed;
     414        return shortcuts.stream()
     415                .map(Shortcut::save)
     416                .reduce(false, Boolean::logicalOr); // has changed
    408417    }
    409418
     
    510519            shortText, conflict.getShortText(), newsc.getKeyText()));
    511520        newsc.saveDefault();
    512         shortcuts.replaceAll(sc -> shortText.equals(sc.getShortText()) ? newsc : sc);
     521        shortcuts.add(newsc);
    513522        return newsc;
    514523    }
Note: See TracChangeset for help on using the changeset viewer.