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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • 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.BufferedReader;
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.IOException;
11import java.io.InputStreamReader;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.LinkedList;
17import java.util.regex.Matcher;
18import java.util.regex.Pattern;
19
20import javax.swing.DefaultListModel;
21import javax.swing.ImageIcon;
22import javax.swing.JLabel;
23import javax.swing.JList;
24import javax.swing.JOptionPane;
25import javax.swing.ListCellRenderer;
26import javax.swing.UIManager;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.data.Bounds;
30import org.openstreetmap.josm.tools.ImageProvider;
31import org.openstreetmap.josm.tools.Utils;
32
33/**
34 * List class that read and save its content from the bookmark file.
35 * @author imi
36 */
37public class BookmarkList extends JList {
38
39 /**
40 * Class holding one bookmarkentry.
41 * @author imi
42 */
43 public static class Bookmark implements Comparable<Bookmark> {
44 private String name;
45 private Bounds area;
46
47 public Bookmark(Collection<String> list) throws NumberFormatException, IllegalArgumentException {
48 ArrayList<String> array = new ArrayList<String>(list);
49 if(array.size() < 5)
50 throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark"));
51 name = array.get(0);
52 area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)),
53 Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4)));
54 }
55
56 public Bookmark() {
57 area = null;
58 name = null;
59 }
60
61 public Bookmark(Bounds area) {
62 this.area = area;
63 }
64
65 @Override public String toString() {
66 return name;
67 }
68
69 @Override
70 public int compareTo(Bookmark b) {
71 return name.toLowerCase().compareTo(b.name.toLowerCase());
72 }
73
74 public Bounds getArea() {
75 return area;
76 }
77
78 public String getName() {
79 return name;
80 }
81
82 public void setName(String name) {
83 this.name = name;
84 }
85
86 public void setArea(Bounds area) {
87 this.area = area;
88 }
89 }
90
91 /**
92 * Create a bookmark list as well as the Buttons add and remove.
93 */
94 public BookmarkList() {
95 setModel(new DefaultListModel());
96 load();
97 setVisibleRowCount(7);
98 setCellRenderer(new BookmarkCellRenderer());
99 }
100
101 /**
102 * Loads the bookmarks from file.
103 */
104 public void load() {
105 DefaultListModel model = (DefaultListModel)getModel();
106 model.removeAllElements();
107 Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
108 if(args != null) {
109 LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
110 for(Collection<String> entry : args) {
111 try {
112 bookmarks.add(new Bookmark(entry));
113 }
114 catch(Exception e) {
115 System.err.println(tr("Error reading bookmark entry: %s", e.getMessage()));
116 }
117 }
118 Collections.sort(bookmarks);
119 for (Bookmark b : bookmarks) {
120 model.addElement(b);
121 }
122 }
123 else if(!Main.applet) { /* FIXME: remove else clause after spring 2011, but fix windows installer before */
124 File bookmarkFile = new File(Main.pref.getPreferencesDir(),"bookmarks");
125 try {
126 LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
127 if (bookmarkFile.exists()) {
128 System.out.println("Try loading obsolete bookmarks file");
129 BufferedReader in = new BufferedReader(new InputStreamReader(
130 new FileInputStream(bookmarkFile), "utf-8"));
131
132 for (String line = in.readLine(); line != null; line = in.readLine()) {
133 Matcher m = Pattern.compile("^(.+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)$").matcher(line);
134 if (!m.matches() || m.groupCount() != 5) {
135 System.err.println(tr("Error: Unexpected line ''{0}'' in bookmark file ''{1}''",line, bookmarkFile.toString()));
136 continue;
137 }
138 Bookmark b = new Bookmark();
139 b.setName(m.group(1));
140 double[] values= new double[4];
141 for (int i = 0; i < 4; ++i) {
142 try {
143 values[i] = Double.parseDouble(m.group(i+2));
144 } catch(NumberFormatException e) {
145 System.err.println(tr("Error: Illegal double value ''{0}'' on line ''{1}'' in bookmark file ''{2}''",m.group(i+2),line, bookmarkFile.toString()));
146 continue;
147 }
148 }
149 b.setArea(new Bounds(values));
150 bookmarks.add(b);
151 }
152 Utils.close(in);
153 Collections.sort(bookmarks);
154 for (Bookmark b : bookmarks) {
155 model.addElement(b);
156 }
157 save();
158 System.out.println("Removing obsolete bookmarks file");
159 bookmarkFile.delete();
160 }
161 } catch (IOException e) {
162 e.printStackTrace();
163 JOptionPane.showMessageDialog(
164 Main.parent,
165 tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>",
166 bookmarkFile.toString(),
167 e.getMessage()
168 ),
169 tr("Error"),
170 JOptionPane.ERROR_MESSAGE
171 );
172 }
173 }
174 }
175
176 /**
177 * Save all bookmarks to the preferences file
178 */
179 public void save() {
180 LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
181 for (Object o : ((DefaultListModel)getModel()).toArray()) {
182 String[] array = new String[5];
183 Bookmark b = (Bookmark)o;
184 array[0] = b.getName();
185 Bounds area = b.getArea();
186 array[1] = String.valueOf(area.getMin().lat());
187 array[2] = String.valueOf(area.getMin().lon());
188 array[3] = String.valueOf(area.getMax().lat());
189 array[4] = String.valueOf(area.getMax().lon());
190 coll.add(Arrays.asList(array));
191 }
192 Main.pref.putArray("bookmarks", coll);
193 }
194
195 static class BookmarkCellRenderer extends JLabel implements ListCellRenderer {
196
197 private ImageIcon icon;
198
199 public BookmarkCellRenderer() {
200 setOpaque(true);
201 icon = ImageProvider.get("dialogs", "bookmark");
202 setIcon(icon);
203 }
204
205 protected void renderColor(boolean selected) {
206 if (selected) {
207 setBackground(UIManager.getColor("List.selectionBackground"));
208 setForeground(UIManager.getColor("List.selectionForeground"));
209 } else {
210 setBackground(UIManager.getColor("List.background"));
211 setForeground(UIManager.getColor("List.foreground"));
212 }
213 }
214
215 protected String buildToolTipText(Bookmark b) {
216 Bounds area = b.getArea();
217 StringBuffer sb = new StringBuffer();
218 sb.append("<html>min[latitude,longitude]=<strong>[")
219 .append(area.getMin().lat()).append(",").append(area.getMin().lon()).append("]</strong>")
220 .append("<br>")
221 .append("max[latitude,longitude]=<strong>[")
222 .append(area.getMax().lat()).append(",").append(area.getMax().lon()).append("]</strong>")
223 .append("</html>");
224 return sb.toString();
225
226 }
227 @Override
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.