source: josm/trunk/src/org/openstreetmap/josm/gui/BookmarkList.java@ 4067

Last change on this file since 4067 was 3697, checked in by bastiK, 13 years ago

fixme comment

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8import java.io.BufferedReader;
9import java.io.InputStreamReader;
10import java.io.File;
11import java.io.FileInputStream;
12import java.io.FileOutputStream;
13import java.io.OutputStreamWriter;
14import java.io.PrintWriter;
15import java.util.Arrays;
16import java.util.ArrayList;
17import java.util.Collection;
18import java.util.Collections;
19import java.util.LinkedList;
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
22
23import javax.swing.DefaultListModel;
24import javax.swing.ImageIcon;
25import javax.swing.JLabel;
26import javax.swing.JList;
27import javax.swing.JOptionPane;
28import javax.swing.ListCellRenderer;
29import javax.swing.UIManager;
30
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.data.Bounds;
33import org.openstreetmap.josm.tools.ImageProvider;
34
35/**
36 * List class that read and save its content from the bookmark file.
37 * @author imi
38 */
39public class BookmarkList extends JList {
40
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 /**
93 * Create a bookmark list as well as the Buttons add and remove.
94 */
95 public BookmarkList() {
96 setModel(new DefaultListModel());
97 load();
98 setVisibleRowCount(7);
99 setCellRenderer(new BookmarkCellRenderer());
100 }
101
102 /**
103 * Loads the bookmarks from file.
104 */
105 public void load() {
106 DefaultListModel model = (DefaultListModel)getModel();
107 model.removeAllElements();
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) {
121 model.addElement(b);
122 }
123 }
124 else if(!Main.applet) { /* FIXME: remove else clause after spring 2011, but fix windows installer before */
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 }
174 }
175 }
176
177 /**
178 * Save all bookmarks to the preferences file
179 */
180 public void save() {
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);
194 }
195
196 static class BookmarkCellRenderer extends JLabel implements ListCellRenderer {
197
198 private ImageIcon icon;
199
200 public BookmarkCellRenderer() {
201 setOpaque(true);
202 icon = ImageProvider.get("dialogs", "bookmark");
203 setIcon(icon);
204 }
205
206 protected void renderColor(boolean selected) {
207 if (selected) {
208 setBackground(UIManager.getColor("List.selectionBackground"));
209 setForeground(UIManager.getColor("List.selectionForeground"));
210 } else {
211 setBackground(UIManager.getColor("List.background"));
212 setForeground(UIManager.getColor("List.foreground"));
213 }
214 }
215
216 protected String buildToolTipText(Bookmark b) {
217 Bounds area = b.getArea();
218 StringBuffer sb = new StringBuffer();
219 sb.append("<html>min[latitude,longitude]=<strong>[")
220 .append(area.getMin().lat()).append(",").append(area.getMin().lon()).append("]</strong>")
221 .append("<br>")
222 .append("max[latitude,longitude]=<strong>[")
223 .append(area.getMax().lat()).append(",").append(area.getMax().lon()).append("]</strong>")
224 .append("</html>");
225 return sb.toString();
226
227 }
228 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
229 boolean cellHasFocus) {
230
231 Bookmark b = (Bookmark) value;
232 renderColor(isSelected);
233 setText(b.getName());
234 setToolTipText(buildToolTipText(b));
235 return this;
236 }
237 }
238}
Note: See TracBrowser for help on using the repository browser.