Ignore:
Timestamp:
2010-09-14T11:26:51+02:00 (14 years ago)
Author:
stoecker
Message:

add Array storage to prefs, move bookmarks into preferences file

File:
1 edited

Legend:

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

    r3507 r3527  
    8484    }
    8585
    86     /**
    87      * Class holding one bookmarkentry.
    88      * @author imi
    89      */
    90     public static class Bookmark implements Comparable<Bookmark> {
    91         private String name;
    92         private Bounds area;
    93 
    94         public Bookmark() {
    95             area = null;
    96             name = null;
    97         }
    98 
    99         public Bookmark(Bounds area) {
    100             this.area = area;
    101         }
    102 
    103         @Override public String toString() {
    104             return name;
    105         }
    106 
    107         public int compareTo(Bookmark b) {
    108             return name.toLowerCase().compareTo(b.name.toLowerCase());
    109         }
    110 
    111         public Bounds getArea() {
    112             return area;
    113         }
    114 
    115         public String getName() {
    116             return name;
    117         }
    118 
    119         public void setName(String name) {
    120             this.name = name;
    121         }
    122 
    123         public void setArea(Bounds area) {
    124             this.area = area;
    125         }
    126     }
    127 
    12886    public interface ColorKey {
    12987        String getColorName();
     
    275233                    all.put(e.getKey(), e.getValue());
    276234                }
     235        return all;
     236    }
     237
     238    synchronized private Map<String, String> getAllPrefixDefault(final String prefix) {
     239        final Map<String,String> all = new TreeMap<String,String>();
     240        for (final Entry<String,String> e : defaults.entrySet())
     241            if (e.getKey().startsWith(prefix)) {
     242                all.put(e.getKey(), e.getValue());
     243            }
    277244        return all;
    278245    }
     
    531498    }
    532499
    533     /* TODO: Bookmarks should be stored in preferences */
    534     public File getBookmarksFile() {
    535         return new File(getPreferencesDir(),"bookmarks");
    536     }
    537 
    538     public Collection<Bookmark> loadBookmarks() throws IOException {
    539         File bookmarkFile = getBookmarksFile();
    540         if (!bookmarkFile.exists()) {
    541             bookmarkFile.createNewFile();
    542         }
    543         BufferedReader in = new BufferedReader(new InputStreamReader(
    544                 new FileInputStream(bookmarkFile), "utf-8"));
    545 
    546         LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
    547         for (String line = in.readLine(); line != null; line = in.readLine()) {
    548             // FIXME: legacy code using ',' sign, should be \u001e only
    549             Matcher m = Pattern.compile("^(.+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)$").matcher(line);
    550             if (!m.matches() || m.groupCount() != 5) {
    551                 System.err.println(tr("Error: Unexpected line ''{0}'' in bookmark file ''{1}''",line, bookmarkFile.toString()));
    552                 continue;
    553             }
    554             Bookmark b = new Bookmark();
    555             b.setName(m.group(1));
    556             double[] values= new double[4];
    557             for (int i = 0; i < 4; ++i) {
    558                 try {
    559                     values[i] = Double.parseDouble(m.group(i+2));
    560                 } catch(NumberFormatException e) {
    561                     System.err.println(tr("Error: Illegal double value ''{0}'' on line ''{1}'' in bookmark file ''{2}''",m.group(i+2),line, bookmarkFile.toString()));
    562                     continue;
    563                 }
    564             }
    565             b.setArea(new Bounds(values));
    566             bookmarks.add(b);
    567         }
    568         in.close();
    569         Collections.sort(bookmarks);
    570         return bookmarks;
    571     }
    572 
    573     public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {
    574         File bookmarkFile = new File(Main.pref.getPreferencesDir()+"bookmarks");
    575         if (!bookmarkFile.exists()) {
    576             bookmarkFile.createNewFile();
    577         }
    578         PrintWriter out = new PrintWriter(new OutputStreamWriter(
    579                 new FileOutputStream(bookmarkFile), "utf-8"));
    580         for (Bookmark b : bookmarks) {
    581             out.print(b.getName()+ "\u001e");
    582             Bounds area = b.getArea();
    583             out.print(area.getMin().lat() +"\u001e");
    584             out.print(area.getMin().lon() +"\u001e");
    585             out.print(area.getMax().lat() +"\u001e");
    586             out.print(area.getMax().lon());
    587             out.println();
    588         }
    589         out.close();
    590     }
    591 
    592500    /**
    593501     * Convenience method for accessing colour preferences.
     
    702610        String s = get(key);
    703611        if(def != null)
    704         {
    705             String d = null;
    706             for(String a : def)
    707             {
    708                 if(d != null) {
    709                     d += "\u001e" + a;
    710                 } else {
    711                     d = a;
    712                 }
    713             }
    714             putDefault(key, d);
    715         }
     612            putCollectionDefault(key, def);
    716613        if(s != null && s.length() != 0)
    717614            return Arrays.asList(s.split("\u001e"));
     
    738635        return put(key, s);
    739636    }
     637    synchronized private void putCollectionDefault(String key, Collection<String> val) {
     638        String s = null;
     639        if(val != null)
     640        {
     641            for(String a : val)
     642            {
     643                if(s != null) {
     644                    s += "\u001e" + a;
     645                } else {
     646                    s = a;
     647                }
     648            }
     649        }
     650        putDefault(key, s);
     651    }
     652    synchronized public Collection<Collection<String>> getArray(String key,
     653    Collection<Collection<String>> def) {
     654        if(def != null) {
     655            for(String k : getAllPrefixDefault(key + ".").keySet())
     656                put(k, null);
     657            int num = 0;
     658            for(Collection<String> c : def)
     659                putCollectionDefault(key+"."+num++, c);
     660        }
     661        String s = get(key+".0");
     662        if(s != null && s.length() != 0)
     663        {
     664            Collection<Collection<String>> col = new LinkedList<Collection<String>>();
     665            for(int num = 0; ; ++num) {
     666                Collection<String> c = getCollection(key+"."+num++, null);
     667                if(c == null)
     668                    break;
     669                col.add(c);
     670            }
     671            return col;
     672        }
     673        return def;
     674    }
     675    synchronized public boolean putArray(String key, Collection<Collection<String>> val) {
     676        boolean res = true;
     677        for(String k : getAllPrefix(key + ".").keySet())
     678            put(k, null);
     679        if(val != null) {
     680            String s = null;
     681            int num = 0;
     682            for(Collection<String> c : val) {
     683                if(!putCollection(key+"."+num++, c))
     684                    res = false;
     685            }
     686        }
     687        return res;
     688    }
    740689
    741690    /**
Note: See TracChangeset for help on using the changeset viewer.