Changeset 3527 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2010-09-14T11:26:51+02:00 (14 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r3507 r3527 84 84 } 85 85 86 /**87 * Class holding one bookmarkentry.88 * @author imi89 */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 128 86 public interface ColorKey { 129 87 String getColorName(); … … 275 233 all.put(e.getKey(), e.getValue()); 276 234 } 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 } 277 244 return all; 278 245 } … … 531 498 } 532 499 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 only549 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 592 500 /** 593 501 * Convenience method for accessing colour preferences. … … 702 610 String s = get(key); 703 611 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); 716 613 if(s != null && s.length() != 0) 717 614 return Arrays.asList(s.split("\u001e")); … … 738 635 return put(key, s); 739 636 } 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 } 740 689 741 690 /** -
trunk/src/org/openstreetmap/josm/gui/BookmarkList.java
r2626 r3527 6 6 import java.awt.Component; 7 7 import java.io.IOException; 8 import java.io.BufferedReader; 9 import java.io.InputStreamReader; 10 import java.io.File; 11 import java.io.FileInputStream; 12 import java.io.FileOutputStream; 13 import java.io.OutputStreamWriter; 14 import java.io.PrintWriter; 15 import java.util.Arrays; 16 import java.util.ArrayList; 8 17 import java.util.Collection; 18 import java.util.Collections; 9 19 import java.util.LinkedList; 20 import java.util.regex.Matcher; 21 import java.util.regex.Pattern; 10 22 11 23 import javax.swing.DefaultListModel; … … 19 31 import org.openstreetmap.josm.Main; 20 32 import org.openstreetmap.josm.data.Bounds; 21 import org.openstreetmap.josm.data.Preferences;22 import org.openstreetmap.josm.data.Preferences.Bookmark;23 33 import org.openstreetmap.josm.tools.ImageProvider; 24 34 … … 30 40 31 41 /** 42 * Class holding one bookmarkentry. 43 * @author imi 44 */ 45 public static class Bookmark implements Comparable<Bookmark> { 46 private String name; 47 private Bounds area; 48 49 public Bookmark(Collection<String> list) throws NumberFormatException, IllegalArgumentException { 50 ArrayList<String> array = new ArrayList<String>(list); 51 if(array.size() < 5) 52 throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark")); 53 name = array.get(0); 54 area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)), 55 Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4))); 56 } 57 58 public Bookmark() { 59 area = null; 60 name = null; 61 } 62 63 public Bookmark(Bounds area) { 64 this.area = area; 65 } 66 67 @Override public String toString() { 68 return name; 69 } 70 71 public int compareTo(Bookmark b) { 72 return name.toLowerCase().compareTo(b.name.toLowerCase()); 73 } 74 75 public Bounds getArea() { 76 return area; 77 } 78 79 public String getName() { 80 return name; 81 } 82 83 public void setName(String name) { 84 this.name = name; 85 } 86 87 public void setArea(Bounds area) { 88 this.area = area; 89 } 90 } 91 92 /** 32 93 * Create a bookmark list as well as the Buttons add and remove. 33 94 */ … … 45 106 DefaultListModel model = (DefaultListModel)getModel(); 46 107 model.removeAllElements(); 47 try { 48 for (Preferences.Bookmark b : Main.pref.loadBookmarks()) { 108 Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null); 109 if(args != null) { 110 LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>(); 111 for(Collection<String> entry : args) { 112 try { 113 bookmarks.add(new Bookmark(entry)); 114 } 115 catch(Exception e) { 116 System.err.println(tr("Error reading bookmark entry: %s", e.getMessage())); 117 } 118 } 119 Collections.sort(bookmarks); 120 for (Bookmark b : bookmarks) { 49 121 model.addElement(b); 50 122 } 51 } catch (IOException e) { 52 e.printStackTrace(); 53 JOptionPane.showMessageDialog( 54 Main.parent, 55 tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>", 56 Main.pref.getBookmarksFile(), 57 e.getMessage() 58 ), 59 tr("Error"), 60 JOptionPane.ERROR_MESSAGE 61 ); 123 } 124 else if(!Main.applet) { /* FIXME: remove else clause after spring 2011 */ 125 File bookmarkFile = new File(Main.pref.getPreferencesDir(),"bookmarks"); 126 try { 127 LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>(); 128 if (bookmarkFile.exists()) { 129 System.out.println("Try loading obsolete bookmarks file"); 130 BufferedReader in = new BufferedReader(new InputStreamReader( 131 new FileInputStream(bookmarkFile), "utf-8")); 132 133 for (String line = in.readLine(); line != null; line = in.readLine()) { 134 Matcher m = Pattern.compile("^(.+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)$").matcher(line); 135 if (!m.matches() || m.groupCount() != 5) { 136 System.err.println(tr("Error: Unexpected line ''{0}'' in bookmark file ''{1}''",line, bookmarkFile.toString())); 137 continue; 138 } 139 Bookmark b = new Bookmark(); 140 b.setName(m.group(1)); 141 double[] values= new double[4]; 142 for (int i = 0; i < 4; ++i) { 143 try { 144 values[i] = Double.parseDouble(m.group(i+2)); 145 } catch(NumberFormatException e) { 146 System.err.println(tr("Error: Illegal double value ''{0}'' on line ''{1}'' in bookmark file ''{2}''",m.group(i+2),line, bookmarkFile.toString())); 147 continue; 148 } 149 } 150 b.setArea(new Bounds(values)); 151 bookmarks.add(b); 152 } 153 in.close(); 154 Collections.sort(bookmarks); 155 for (Bookmark b : bookmarks) { 156 model.addElement(b); 157 } 158 save(); 159 System.out.println("Removing obsolete bookmarks file"); 160 bookmarkFile.delete(); 161 } 162 } catch (IOException e) { 163 e.printStackTrace(); 164 JOptionPane.showMessageDialog( 165 Main.parent, 166 tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>", 167 bookmarkFile.toString(), 168 e.getMessage() 169 ), 170 tr("Error"), 171 JOptionPane.ERROR_MESSAGE 172 ); 173 } 62 174 } 63 175 } … … 67 179 */ 68 180 public void save() { 69 try { 70 Collection<Preferences.Bookmark> bookmarks = new LinkedList<Preferences.Bookmark>(); 71 for (Object o : ((DefaultListModel)getModel()).toArray()) { 72 bookmarks.add((Preferences.Bookmark)o); 73 } 74 Main.pref.saveBookmarks(bookmarks); 75 } catch (IOException e) { 76 JOptionPane.showMessageDialog( 77 Main.parent, 78 tr("<html>Could not write bookmark.<br>{0}</html>", e.getMessage()), 79 tr("Error"), 80 JOptionPane.ERROR_MESSAGE 81 ); 82 } 181 LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>(); 182 for (Object o : ((DefaultListModel)getModel()).toArray()) { 183 String[] array = new String[5]; 184 Bookmark b = (Bookmark)o; 185 array[0] = b.getName(); 186 Bounds area = b.getArea(); 187 array[1] = String.valueOf(area.getMin().lat()); 188 array[2] = String.valueOf(area.getMin().lon()); 189 array[3] = String.valueOf(area.getMax().lat()); 190 array[4] = String.valueOf(area.getMax().lon()); 191 coll.add(Arrays.asList(array)); 192 } 193 Main.pref.putArray("bookmarks", coll); 83 194 } 84 195 -
trunk/src/org/openstreetmap/josm/gui/download/BookmarkSelection.java
r2904 r3527 25 25 import org.openstreetmap.josm.data.Bounds; 26 26 import org.openstreetmap.josm.data.Preferences; 27 import org.openstreetmap.josm.data.Preferences.Bookmark;28 27 import org.openstreetmap.josm.data.coor.CoordinateFormat; 29 28 import org.openstreetmap.josm.gui.BookmarkList; 29 import org.openstreetmap.josm.gui.BookmarkList.Bookmark; 30 30 import org.openstreetmap.josm.gui.JMultilineLabel; 31 31 import org.openstreetmap.josm.tools.ImageProvider; … … 114 114 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 115 115 public void valueChanged(ListSelectionEvent e) { 116 Preferences.Bookmark b = (Preferences.Bookmark)bookmarks.getSelectedValue();116 Bookmark b = (Bookmark)bookmarks.getSelectedValue(); 117 117 if (b != null) { 118 118 gui.boundingBoxChanged(b.getArea(),BookmarkSelection.this);
Note:
See TracChangeset
for help on using the changeset viewer.