Changeset 3124 in josm


Ignore:
Timestamp:
2010-03-12T19:36:17+01:00 (14 years ago)
Author:
Gubaer
Message:

fixed #4725: Much slower selection list handling since 3102

File:
1 edited

Legend:

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

    r3123 r3124  
    1818import java.util.Collection;
    1919import java.util.Collections;
     20import java.util.Comparator;
     21import java.util.HashMap;
    2022import java.util.HashSet;
    2123import java.util.LinkedList;
     
    4951import org.openstreetmap.josm.data.osm.Node;
    5052import org.openstreetmap.josm.data.osm.OsmPrimitive;
     53import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    5154import org.openstreetmap.josm.data.osm.Relation;
    5255import org.openstreetmap.josm.data.osm.Way;
     
    6467import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    6568import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     69import org.openstreetmap.josm.gui.DefaultNameFormatter;
    6670import org.openstreetmap.josm.gui.MapView;
    6771import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
     
    517521            }
    518522            this.selection.addAll(selection);
     523            sort();
    519524            fireContentsChanged(this, 0, getSize());
    520525            remember(selection);
     
    566571        }
    567572
     573        /**
     574         * Sorts the current elements in the selection
     575         */
     576        public void sort() {
     577            Collections.sort(this.selection, new OsmPrimitiveComparator());
     578        }
     579
    568580        /* ------------------------------------------------------------------------ */
    569581        /* interface EditLayerChangeListener                                        */
     
    764776        }
    765777    }
     778
     779    static private class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
     780        final private HashMap<Object, String> cache= new HashMap<Object, String>();
     781        final private DefaultNameFormatter df  = DefaultNameFormatter.getInstance();
     782
     783        private String cachedName(OsmPrimitive p) {
     784            String name = cache.get(p);
     785            if (name == null) {
     786                name = p.getDisplayName(df);
     787                cache.put(p, name);
     788            }
     789            return name;
     790        }
     791
     792        private int compareName(OsmPrimitive a, OsmPrimitive b) {
     793            String an = cachedName(a);
     794            String bn = cachedName(b);
     795            // make sure display names starting with digits are the end of the
     796            // list
     797            if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
     798                return an.compareTo(bn);
     799            else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
     800                return 1;
     801            else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
     802                return -1;
     803            return an.compareTo(bn);
     804        }
     805
     806        private int compareType(OsmPrimitive a, OsmPrimitive b) {
     807            // show ways before relations, then nodes
     808            //
     809            if (a.getType().equals(b.getType())) return 0;
     810            if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
     811            if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
     812            // a is a relation
     813            if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
     814            // b is a node
     815            return -1;
     816        }
     817        public int compare(OsmPrimitive a, OsmPrimitive b) {
     818            if (a.getType().equals(b.getType()))
     819                return compareName(a, b);
     820            return compareType(a, b);
     821        }
     822    }
    766823}
Note: See TracChangeset for help on using the changeset viewer.