Ignore:
Timestamp:
2008-12-13T23:24:29+01:00 (15 years ago)
Author:
stoecker
Message:

add bookmark sorting. closes #1787

File:
1 edited

Legend:

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

    r1082 r1121  
    1212import java.util.Arrays;
    1313import java.util.Collection;
     14import java.util.Collections;
    1415import java.util.LinkedList;
    1516import java.util.Map;
    1617import java.util.Properties;
    1718import java.util.SortedMap;
    18 import java.util.StringTokenizer;
    1919import java.util.TreeMap;
    2020import java.util.Map.Entry;
     21import java.util.regex.Matcher;
     22import java.util.regex.Pattern;
    2123
    2224import org.openstreetmap.josm.Main;
     
    4951         * @author imi
    5052         */
    51         public static class Bookmark {
     53        public static class Bookmark implements Comparable<Bookmark> {
    5254                public String name;
    5355                public double[] latlon = new double[4]; // minlat, minlon, maxlat, maxlon
    5456                @Override public String toString() {
    5557                        return name;
     58                }
     59                public int compareTo(Bookmark b) {
     60                        return name.toLowerCase().compareTo(b.name.toLowerCase());
    5661                }
    5762        }
     
    290295                BufferedReader in = new BufferedReader(new FileReader(bookmarkFile));
    291296
    292                 Collection<Bookmark> bookmarks = new LinkedList<Bookmark>();
     297                LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
     298                // use pattern matches to scan text, as text may contain a "," itself
    293299                for (String line = in.readLine(); line != null; line = in.readLine()) {
    294                         StringTokenizer st = new StringTokenizer(line, ",");
    295                         if (st.countTokens() < 5)
    296                                 continue;
    297                         Bookmark b = new Bookmark();
    298                         b.name = st.nextToken();
    299                         try {
     300                        Matcher m = Pattern.compile("^(.+),(-?\\d+.\\d+),(-?\\d+.\\d+),(-?\\d+.\\d+),(-?\\d+.\\d+)$").matcher(line);
     301                        if(m.matches())
     302                        {
     303                                Bookmark b = new Bookmark();
     304                                b.name = m.group(1);
    300305                                for (int i = 0; i < b.latlon.length; ++i)
    301                                         b.latlon[i] = Double.parseDouble(st.nextToken());
     306                                        b.latlon[i] = Double.parseDouble(m.group(i+2));
    302307                                bookmarks.add(b);
    303                         } catch (NumberFormatException x) {
    304                                 // line not parsed
    305308                        }
    306309                }
    307310                in.close();
     311                Collections.sort(bookmarks);
    308312                return bookmarks;
    309313        }
     
    315319                PrintWriter out = new PrintWriter(new FileWriter(bookmarkFile));
    316320                for (Bookmark b : bookmarks) {
    317                         b.name = b.name.replace(',', '_');
    318321                        out.print(b.name+",");
    319322                        for (int i = 0; i < b.latlon.length; ++i)
Note: See TracChangeset for help on using the changeset viewer.