Changeset 8492 in josm


Ignore:
Timestamp:
2015-06-19T16:57:12+02:00 (9 years ago)
Author:
simon04
Message:

fix #3145 - Allow listing custom presets within default groups

The non-translated group names are taken as criterion.

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
4 edited

Legend:

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

    r8444 r8492  
    3333import java.util.EventObject;
    3434import java.util.HashMap;
    35 import java.util.HashSet;
    3635import java.util.Iterator;
     36import java.util.LinkedHashSet;
    3737import java.util.List;
    3838import java.util.Map;
     
    15791579         */
    15801580        public final Set<String> getActiveUrls() {
    1581             Set<String> urls = new HashSet<>();
     1581            Set<String> urls = new LinkedHashSet<>(); // retain order
    15821582            for (SourceEntry e : get()) {
    15831583                if (e.active) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r8444 r8492  
    124124    }
    125125
     126    /**
     127     * Returns the translated name of this preset, prefixed with the group names it belongs to.
     128     */
    126129    public String getName() {
    127130        return group != null ? group.getName() + "/" + getLocaleName() : getLocaleName();
    128131    }
     132
     133    /**
     134     * Returns the non translated name of this preset, prefixed with the (non translated) group names it belongs to.
     135     */
    129136    public String getRawName() {
    130137        return group != null ? group.getRawName() + "/" + name : name;
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetMenu.java

    r8345 r8492  
    1313import java.util.Comparator;
    1414import java.util.List;
     15import java.util.Objects;
    1516
    1617import javax.swing.Action;
     
    3637                return AlphanumComparator.getInstance().compare(o1.getText(), o2.getText());
    3738        }
     39    }
     40
     41    /**
     42     * {@code TaggingPresetMenu} are considered equivalent if (and only if) their {@link #getRawName()} match.
     43     */
     44    @Override
     45    public boolean equals(Object o) {
     46        if (this == o) return true;
     47        if (o == null || getClass() != o.getClass()) return false;
     48        TaggingPresetMenu that = (TaggingPresetMenu) o;
     49        return Objects.equals(getRawName(), that.getRawName());
     50    }
     51
     52    @Override
     53    public int hashCode() {
     54        return Objects.hash(getRawName());
    3855    }
    3956
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java

    r8338 r8492  
    1616import java.util.HashMap;
    1717import java.util.Iterator;
     18import java.util.LinkedHashSet;
    1819import java.util.LinkedList;
    1920import java.util.List;
     
    2728import org.openstreetmap.josm.io.CachedFile;
    2829import org.openstreetmap.josm.io.UTFInputStreamReader;
     30import org.openstreetmap.josm.tools.Predicates;
     31import org.openstreetmap.josm.tools.Utils;
    2932import org.openstreetmap.josm.tools.XmlObjectParser;
    3033import org.xml.sax.SAXException;
     
    97100    }
    98101
     102    static class HashSetWithLast<E> extends LinkedHashSet<E> {
     103        protected E last = null;
     104
     105        @Override
     106        public boolean add(E e) {
     107            last = e;
     108            return super.add(e);
     109        }
     110
     111        /**
     112         * Returns the last inserted element.
     113         */
     114        public E getLast() {
     115            return last;
     116        }
     117    }
     118
    99119    /**
    100120     * Reads all tagging presets from the input reader.
     
    105125     */
    106126    public static Collection<TaggingPreset> readAll(Reader in, boolean validate) throws SAXException {
     127        return readAll(in, validate, new HashSetWithLast<TaggingPreset>());
     128    }
     129
     130    /**
     131     * Reads all tagging presets from the input reader.
     132     * @param in The input reader
     133     * @param validate if {@code true}, XML validation will be performed
     134     * @param all the accumulator for parsed tagging presets
     135     * @return the accumulator
     136     * @throws SAXException if any XML error occurs
     137     */
     138    static Collection<TaggingPreset> readAll(Reader in, boolean validate, HashSetWithLast<TaggingPreset> all) throws SAXException {
    107139        XmlObjectParser parser = buildParser();
    108140
    109         Deque<TaggingPreset> all = new LinkedList<>();
     141        /** to detect end of {@code <group>} */
    110142        TaggingPresetMenu lastmenu = null;
     143        /** to detect end of reused {@code <group>} */
     144        TaggingPresetMenu lastmenuOriginal = null;
    111145        TaggingPresetItems.Roles lastrole = null;
    112146        final List<TaggingPresetItems.Check> checks = new LinkedList<>();
     
    173207            if (o instanceof TaggingPresetMenu) {
    174208                TaggingPresetMenu tp = (TaggingPresetMenu) o;
    175                 if (tp == lastmenu) {
     209                if (tp == lastmenu || tp == lastmenuOriginal) {
    176210                    lastmenu = tp.group;
    177211                } else {
    178212                    tp.group = lastmenu;
    179                     tp.setDisplayName();
     213                    if (all.contains(tp)) {
     214                        lastmenuOriginal = tp;
     215                        tp = (TaggingPresetMenu) Utils.filter(all, Predicates.<TaggingPreset>equalTo(tp)).iterator().next();
     216                        lastmenuOriginal.group = null;
     217                    } else {
     218                        tp.setDisplayName();
     219                        all.add(tp);
     220                        lastmenuOriginal = null;
     221                    }
    180222                    lastmenu = tp;
    181                     all.add(tp);
    182223                }
    183224                lastrole = null;
     
    253294     */
    254295    public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
     296        return readAll(source, validate, new HashSetWithLast<TaggingPreset>());
     297    }
     298
     299    /**
     300     * Reads all tagging presets from the given source.
     301     * @param source a given filename, URL or internal resource
     302     * @param validate if {@code true}, XML validation will be performed
     303     * @param all the accumulator for parsed tagging presets
     304     * @return the accumulator
     305     * @throws SAXException if any XML error occurs
     306     * @throws IOException if any I/O error occurs
     307     */
     308    static Collection<TaggingPreset> readAll(String source, boolean validate, HashSetWithLast<TaggingPreset> all) throws SAXException, IOException {
    255309        Collection<TaggingPreset> tp;
    256310        CachedFile cf = new CachedFile(source).setHttpAccept(PRESET_MIME_TYPES);
     
    263317            }
    264318            try (InputStreamReader r = UTFInputStreamReader.create(zip == null ? cf.getInputStream() : zip)) {
    265                 tp = readAll(new BufferedReader(r), validate);
     319                tp = readAll(new BufferedReader(r), validate, all);
    266320            }
    267321        }
     
    287341     */
    288342    public static Collection<TaggingPreset> readAll(Collection<String> sources, boolean validate, boolean displayErrMsg) {
    289         List<TaggingPreset> allPresets = new LinkedList<>();
     343        HashSetWithLast<TaggingPreset> allPresets = new HashSetWithLast<>();
    290344        for(String source : sources)  {
    291345            try {
    292                 allPresets.addAll(readAll(source, validate));
     346                readAll(source, validate, allPresets);
    293347            } catch (IOException e) {
    294348                Main.error(e, false);
Note: See TracChangeset for help on using the changeset viewer.