Changeset 4113 in josm


Ignore:
Timestamp:
Jun 1, 2011 9:50:26 PM (2 years ago)
Author:
stoecker
Message:

fix #6233 - better sorting for upload dialog

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/APIDataSet.java

    r4104 r4113  
    2020import org.openstreetmap.josm.data.osm.Node; 
    2121import org.openstreetmap.josm.data.osm.OsmPrimitive; 
     22import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator; 
    2223import org.openstreetmap.josm.data.osm.PrimitiveId; 
    2324import org.openstreetmap.josm.data.osm.Relation; 
     
    7071            } 
    7172        } 
    72         sortDeleted(); 
    73         sortNew(); 
    74         sortUpdated(); 
    75     } 
    76  
    77     /** 
    78      * Ensures that primitives are deleted in the following order: Relations, then Ways, 
    79      * then Nodes. 
    80      * 
    81      */ 
    82     protected void sortDeleted() { 
    83         Collections.sort( 
    84                 toDelete, 
    85                 new Comparator<OsmPrimitive>() { 
    86                     public int compare(OsmPrimitive o1, OsmPrimitive o2) { 
    87                         if (o1 instanceof Node && o2 instanceof Node) 
    88                             return 0; 
    89                         else if (o1 instanceof Node) 
    90                             return 1; 
    91                         else if (o2 instanceof Node) 
    92                             return -1; 
    93  
    94                         if (o1 instanceof Way && o2 instanceof Way) 
    95                             return 0; 
    96                         else if (o1 instanceof Way && o2 instanceof Relation) 
    97                             return 1; 
    98                         else if (o2 instanceof Way && o1 instanceof Relation) 
    99                             return -1; 
    100  
    101                         return 0; 
    102                     } 
    103                 } 
    104         ); 
    105     } 
    106  
    107     /** 
    108      * Ensures that primitives are added in the following order: Nodes, then Ways, 
    109      * then Relations. 
    110      * 
    111      */ 
    112     protected void sortNew() { 
    113         Collections.sort( 
    114                 toAdd, 
    115                 new Comparator<OsmPrimitive>() { 
    116                     public int compare(OsmPrimitive o1, OsmPrimitive o2) { 
    117                         if (o1 instanceof Node && o2 instanceof Node) 
    118                             return 0; 
    119                         else if (o1 instanceof Node) 
    120                             return -1; 
    121                         else if (o2 instanceof Node) 
    122                             return 1; 
    123  
    124                         if (o1 instanceof Way && o2 instanceof Way) 
    125                             return 0; 
    126                         else if (o1 instanceof Way && o2 instanceof Relation) 
    127                             return -1; 
    128                         else if (o2 instanceof Way && o1 instanceof Relation) 
    129                             return 1; 
    130  
    131                         return 0; 
    132                     } 
    133                 } 
    134         ); 
    135     } 
    136     /*  
    137      * Sort list of updated elements, so it looks neat in the confirmation dialog. 
    138      */ 
    139     protected void sortUpdated() { 
    140         Collections.sort( 
    141                 toUpdate, 
    142                 new Comparator<OsmPrimitive>() { 
    143                     public int compare(OsmPrimitive o1, OsmPrimitive o2) { 
    144                         if (o1 instanceof Node && o2 instanceof Node) 
    145                             return 0; 
    146                         else if (o1 instanceof Node) 
    147                             return -1; 
    148                         else if (o2 instanceof Node) 
    149                             return 1; 
    150  
    151                         if (o1 instanceof Way && o2 instanceof Way) 
    152                             return 0; 
    153                         else if (o1 instanceof Way && o2 instanceof Relation) 
    154                             return -1; 
    155                         else if (o2 instanceof Way && o1 instanceof Relation) 
    156                             return 1; 
    157  
    158                         return 0; 
    159                     } 
    160                 } 
    161         ); 
    162     } 
    163      
     73        OsmPrimitiveComparator c = new OsmPrimitiveComparator(); 
     74        c.relationsFirst = true; 
     75        Collections.sort(toDelete, c);  
     76        Collections.sort(toAdd, c);  
     77        Collections.sort(toUpdate, c);  
     78    } 
     79 
    16480    /** 
    16581     * initializes the API data set with the modified primitives in <code>ds</code> 
     
    236152            } 
    237153        } 
    238         sortNew(); 
    239         sortDeleted(); 
     154        OsmPrimitiveComparator c = new OsmPrimitiveComparator(); 
     155        c.relationsFirst = true; 
     156        Collections.sort(toDelete, c);  
     157        Collections.sort(toAdd, c);  
     158        Collections.sort(toUpdate, c);  
    240159    } 
    241160 
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r4068 r4113  
    5151import org.openstreetmap.josm.data.osm.Node; 
    5252import org.openstreetmap.josm.data.osm.OsmPrimitive; 
     53import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator; 
    5354import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 
    5455import org.openstreetmap.josm.data.osm.Relation; 
     
    916917    } 
    917918 
    918     /** Comparator, comparing by type and objects display names */ 
    919     static private class OsmPrimitiveComparator implements Comparator<OsmPrimitive> { 
    920         final private HashMap<OsmPrimitive, String> cache= new HashMap<OsmPrimitive, String>(); 
    921         final private DefaultNameFormatter df  = DefaultNameFormatter.getInstance(); 
    922  
    923         private String cachedName(OsmPrimitive p) { 
    924             String name = cache.get(p); 
    925             if (name == null) { 
    926                 name = p.getDisplayName(df); 
    927                 cache.put(p, name); 
    928             } 
    929             return name; 
    930         } 
    931  
    932         private int compareName(OsmPrimitive a, OsmPrimitive b) { 
    933             String an = cachedName(a); 
    934             String bn = cachedName(b); 
    935             // make sure display names starting with digits are the end of the 
    936             // list 
    937             if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0))) 
    938                 return an.compareTo(bn); 
    939             else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0))) 
    940                 return 1; 
    941             else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0))) 
    942                 return -1; 
    943             return an.compareTo(bn); 
     919    /** Quicker comparator, comparing just by type and ID's */ 
     920    static private class OsmPrimitiveQuickComparator implements Comparator<OsmPrimitive> { 
     921 
     922        private int compareId(OsmPrimitive a, OsmPrimitive b) { 
     923            long id_a=a.getUniqueId(); 
     924            long id_b=b.getUniqueId(); 
     925            if (id_a<id_b) return -1; 
     926            if (id_a>id_b) return 1; 
     927            return 0; 
    944928        } 
    945929 
     
    956940        public int compare(OsmPrimitive a, OsmPrimitive b) { 
    957941            if (a.getType().equals(b.getType())) 
    958                 return compareName(a, b); 
    959             return compareType(a, b); 
    960         } 
    961     } 
    962  
    963     /** Quicker comparator, comparing just by type and ID's */ 
    964     static private class OsmPrimitiveQuickComparator implements Comparator<OsmPrimitive> { 
    965  
    966         private int compareId(OsmPrimitive a, OsmPrimitive b) { 
    967             long id_a=a.getUniqueId(); 
    968             long id_b=b.getUniqueId(); 
    969             if (id_a<id_b) return -1; 
    970             if (id_a>id_b) return 1; 
    971             return 0; 
    972         } 
    973  
    974         private int compareType(OsmPrimitive a, OsmPrimitive b) { 
    975             // show ways before relations, then nodes 
    976             if (a.getType().equals(OsmPrimitiveType.WAY)) return -1; 
    977             if (a.getType().equals(OsmPrimitiveType.NODE)) return 1; 
    978             // a is a relation 
    979             if (b.getType().equals(OsmPrimitiveType.WAY)) return 1; 
    980             // b is a node 
    981             return -1; 
    982         } 
    983  
    984         public int compare(OsmPrimitive a, OsmPrimitive b) { 
    985             if (a.getType().equals(b.getType())) 
    986942                return compareId(a, b); 
    987943            return compareType(a, b); 
Note: See TracChangeset for help on using the changeset viewer.