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

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

clean up imports

  • Property svn:eol-style set to native
File size: 8.5 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 public int compareTo(Bookmark b) {
70 return name.toLowerCase().compareTo(b.name.toLowerCase());
71 }
72
73 public Bounds getArea() {
74 return area;
75 }
76
77 public String getName() {
78 return name;
79 }
80
81 public void setName(String name) {
82 this.name = name;
83 }
84
85 public void setArea(Bounds area) {
86 this.area = area;
87 }
88 }
89
90 /**
91 * Create a bookmark list as well as the Buttons add and remove.
92 */
93 public BookmarkList() {
94 setModel(new DefaultListModel());
95 load();
96 setVisibleRowCount(7);
97 setCellRenderer(new BookmarkCellRenderer());
98 }
99
100 /**
101 * Loads the bookmarks from file.
102 */
103 public void load() {
104 DefaultListModel model = (DefaultListModel)getModel();
105 model.removeAllElements();
106 Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
107 if(args != null) {
108 LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
109 for(Collection<String> entry : args) {
110 try {
111 bookmarks.add(new Bookmark(entry));
112 }
113 catch(Exception e) {
114 System.err.println(tr("Error reading bookmark entry: %s", e.getMessage()));
115 }
116 }
117 Collections.sort(bookmarks);
118 for (Bookmark b : bookmarks) {
119 model.addElement(b);
120 }
121 }
122 else if(!Main.applet) { /* FIXME: remove else clause after spring 2011, but fix windows installer before */
123 File bookmarkFile = new File(Main.pref.getPreferencesDir(),"bookmarks");
124 try {
125 LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
126 if (bookmarkFile.exists()) {
127 System.out.println("Try loading obsolete bookmarks file");
128 BufferedReader in = new BufferedReader(new InputStreamReader(
129 new FileInputStream(bookmarkFile), "utf-8"));
130
131 for (String line = in.readLine(); line != null; line = in.readLine()) {
132 Matcher m = Pattern.compile("^(.+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)$").matcher(line);
133 if (!m.matches() || m.groupCount() != 5) {
134 System.err.println(tr("Error: Unexpected line ''{0}'' in bookmark file ''{1}''",line, bookmarkFile.toString()));
135 continue;
136 }
137 Bookmark b = new Bookmark();
138 b.setName(m.group(1));
139 double[] values= new double[4];
140 for (int i = 0; i < 4; ++i) {
141 try {
142 values[i] = Double.parseDouble(m.group(i+2));
143 } catch(NumberFormatException e) {
144 System.err.println(tr("Error: Illegal double value ''{0}'' on line ''{1}'' in bookmark file ''{2}''",m.group(i+2),line, bookmarkFile.toString()));
145 continue;
146 }
147 }
148 b.setArea(new Bounds(values));
149 bookmarks.add(b);
150 }
151 Utils.close(in);
152 Collections.sort(bookmarks);
153 for (Bookmark b : bookmarks) {
154 model.addElement(b);
155 }
156 save();
157 System.out.println("Removing obsolete bookmarks file");
158 bookmarkFile.delete();
159 }
160 } catch (IOException e) {
161 e.printStackTrace();
162 JOptionPane.showMessageDialog(
163 Main.parent,
164 tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>",
165 bookmarkFile.toString(),
166 e.getMessage()
167 ),
168 tr("Error"),
169 JOptionPane.ERROR_MESSAGE
170 );
171 }
172 }
173 }
174
175 /**
176 * Save all bookmarks to the preferences file
177 */
178 public void save() {
179 LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
180 for (Object o : ((DefaultListModel)getModel()).toArray()) {
181 String[] array = new String[5];
182 Bookmark b = (Bookmark)o;
183 array[0] = b.getName();
184 Bounds area = b.getArea();
185 array[1] = String.valueOf(area.getMin().lat());
186 array[2] = String.valueOf(area.getMin().lon());
187 array[3] = String.valueOf(area.getMax().lat());
188 array[4] = String.valueOf(area.getMax().lon());
189 coll.add(Arrays.asList(array));
190 }
191 Main.pref.putArray("bookmarks", coll);
192 }
193
194 static class BookmarkCellRenderer extends JLabel implements ListCellRenderer {
195
196 private ImageIcon icon;
197
198 public BookmarkCellRenderer() {
199 setOpaque(true);
200 icon = ImageProvider.get("dialogs", "bookmark");
201 setIcon(icon);
202 }
203
204 protected void renderColor(boolean selected) {
205 if (selected) {
206 setBackground(UIManager.getColor("List.selectionBackground"));
207 setForeground(UIManager.getColor("List.selectionForeground"));
208 } else {
209 setBackground(UIManager.getColor("List.background"));
210 setForeground(UIManager.getColor("List.foreground"));
211 }
212 }
213
214 protected String buildToolTipText(Bookmark b) {
215 Bounds area = b.getArea();
216 StringBuffer sb = new StringBuffer();
217 sb.append("<html>min[latitude,longitude]=<strong>[")
218 .append(area.getMin().lat()).append(",").append(area.getMin().lon()).append("]</strong>")
219 .append("<br>")
220 .append("max[latitude,longitude]=<strong>[")
221 .append(area.getMax().lat()).append(",").append(area.getMax().lon()).append("]</strong>")
222 .append("</html>");
223 return sb.toString();
224
225 }
226 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
227 boolean cellHasFocus) {
228
229 Bookmark b = (Bookmark) value;
230 renderColor(isSelected);
231 setText(b.getName());
232 setToolTipText(buildToolTipText(b));
233 return this;
234 }
235 }
236}
Note: See TracBrowser for help on using the repository browser.