1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.BufferedReader;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStreamReader;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.regex.Matcher;
|
---|
19 | import java.util.regex.Pattern;
|
---|
20 |
|
---|
21 | import javax.swing.DefaultListModel;
|
---|
22 | import javax.swing.ImageIcon;
|
---|
23 | import javax.swing.JLabel;
|
---|
24 | import javax.swing.JList;
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 | import javax.swing.ListCellRenderer;
|
---|
27 | import javax.swing.UIManager;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.data.Bounds;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * List class that read and save its content from the bookmark file.
|
---|
36 | * @since 6340
|
---|
37 | */
|
---|
38 | public class BookmarkList extends JList {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Class holding one bookmarkentry.
|
---|
42 | */
|
---|
43 | public static class Bookmark implements Comparable<Bookmark> {
|
---|
44 | private String name;
|
---|
45 | private Bounds area;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructs a new {@code Bookmark} with the given contents.
|
---|
49 | * @param list Bookmark contents as a list of 5 elements. First item is the name, then come bounds arguments (minlat, minlon, maxlat, maxlon)
|
---|
50 | * @throws NumberFormatException If the bounds arguments are not numbers
|
---|
51 | * @throws IllegalArgumentException If list contain less than 5 elements
|
---|
52 | */
|
---|
53 | public Bookmark(Collection<String> list) throws NumberFormatException, IllegalArgumentException {
|
---|
54 | List<String> array = new ArrayList<String>(list);
|
---|
55 | if(array.size() < 5)
|
---|
56 | throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark"));
|
---|
57 | name = array.get(0);
|
---|
58 | area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)),
|
---|
59 | Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4)));
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructs a new empty {@code Bookmark}.
|
---|
64 | */
|
---|
65 | public Bookmark() {
|
---|
66 | area = null;
|
---|
67 | name = null;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Constructs a new unamed {@code Bookmark} for the given area.
|
---|
72 | * @param area The bookmark area
|
---|
73 | */
|
---|
74 | public Bookmark(Bounds area) {
|
---|
75 | this.area = area;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override public String toString() {
|
---|
79 | return name;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public int compareTo(Bookmark b) {
|
---|
84 | return name.toLowerCase().compareTo(b.name.toLowerCase());
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public int hashCode() {
|
---|
89 | final int prime = 31;
|
---|
90 | int result = 1;
|
---|
91 | result = prime * result + ((area == null) ? 0 : area.hashCode());
|
---|
92 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
93 | return result;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public boolean equals(Object obj) {
|
---|
98 | if (this == obj)
|
---|
99 | return true;
|
---|
100 | if (obj == null)
|
---|
101 | return false;
|
---|
102 | if (getClass() != obj.getClass())
|
---|
103 | return false;
|
---|
104 | Bookmark other = (Bookmark) obj;
|
---|
105 | if (area == null) {
|
---|
106 | if (other.area != null)
|
---|
107 | return false;
|
---|
108 | } else if (!area.equals(other.area))
|
---|
109 | return false;
|
---|
110 | if (name == null) {
|
---|
111 | if (other.name != null)
|
---|
112 | return false;
|
---|
113 | } else if (!name.equals(other.name))
|
---|
114 | return false;
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Returns the bookmark area
|
---|
120 | * @return The bookmark area
|
---|
121 | */
|
---|
122 | public Bounds getArea() {
|
---|
123 | return area;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Returns the bookmark name
|
---|
128 | * @return The bookmark name
|
---|
129 | */
|
---|
130 | public String getName() {
|
---|
131 | return name;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Sets the bookmark name
|
---|
136 | * @param name The bookmark name
|
---|
137 | */
|
---|
138 | public void setName(String name) {
|
---|
139 | this.name = name;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Sets the bookmark area
|
---|
144 | * @param area The bookmark area
|
---|
145 | */
|
---|
146 | public void setArea(Bounds area) {
|
---|
147 | this.area = area;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Creates a bookmark list as well as the Buttons add and remove.
|
---|
153 | */
|
---|
154 | public BookmarkList() {
|
---|
155 | setModel(new DefaultListModel());
|
---|
156 | load();
|
---|
157 | setVisibleRowCount(7);
|
---|
158 | setCellRenderer(new BookmarkCellRenderer());
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Loads the bookmarks from file.
|
---|
163 | */
|
---|
164 | public final void load() {
|
---|
165 | DefaultListModel model = (DefaultListModel)getModel();
|
---|
166 | model.removeAllElements();
|
---|
167 | Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
|
---|
168 | if(args != null) {
|
---|
169 | LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
|
---|
170 | for(Collection<String> entry : args) {
|
---|
171 | try {
|
---|
172 | bookmarks.add(new Bookmark(entry));
|
---|
173 | }
|
---|
174 | catch (Exception e) {
|
---|
175 | Main.error(tr("Error reading bookmark entry: %s", e.getMessage()));
|
---|
176 | }
|
---|
177 | }
|
---|
178 | Collections.sort(bookmarks);
|
---|
179 | for (Bookmark b : bookmarks) {
|
---|
180 | model.addElement(b);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | else if(!Main.applet) { /* FIXME: remove else clause after spring 2011, but fix windows installer before */
|
---|
184 | File bookmarkFile = new File(Main.pref.getPreferencesDir(),"bookmarks");
|
---|
185 | try {
|
---|
186 | LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
|
---|
187 | if (bookmarkFile.exists()) {
|
---|
188 | Main.info("Try loading obsolete bookmarks file");
|
---|
189 | BufferedReader in = new BufferedReader(new InputStreamReader(
|
---|
190 | new FileInputStream(bookmarkFile), "utf-8"));
|
---|
191 |
|
---|
192 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
193 | Matcher m = Pattern.compile("^(.+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)$").matcher(line);
|
---|
194 | if (!m.matches() || m.groupCount() != 5) {
|
---|
195 | Main.error(tr("Unexpected line ''{0}'' in bookmark file ''{1}''",line, bookmarkFile.toString()));
|
---|
196 | continue;
|
---|
197 | }
|
---|
198 | Bookmark b = new Bookmark();
|
---|
199 | b.setName(m.group(1));
|
---|
200 | double[] values= new double[4];
|
---|
201 | for (int i = 0; i < 4; ++i) {
|
---|
202 | try {
|
---|
203 | values[i] = Double.parseDouble(m.group(i+2));
|
---|
204 | } catch (NumberFormatException e) {
|
---|
205 | Main.error(tr("Illegal double value ''{0}'' on line ''{1}'' in bookmark file ''{2}''",m.group(i+2),line, bookmarkFile.toString()));
|
---|
206 | continue;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | b.setArea(new Bounds(values));
|
---|
210 | bookmarks.add(b);
|
---|
211 | }
|
---|
212 | Utils.close(in);
|
---|
213 | Collections.sort(bookmarks);
|
---|
214 | for (Bookmark b : bookmarks) {
|
---|
215 | model.addElement(b);
|
---|
216 | }
|
---|
217 | save();
|
---|
218 | Main.info("Removing obsolete bookmarks file");
|
---|
219 | if (!bookmarkFile.delete()) {
|
---|
220 | bookmarkFile.deleteOnExit();
|
---|
221 | }
|
---|
222 | }
|
---|
223 | } catch (IOException e) {
|
---|
224 | Main.error(e);
|
---|
225 | JOptionPane.showMessageDialog(
|
---|
226 | Main.parent,
|
---|
227 | tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>",
|
---|
228 | bookmarkFile.toString(),
|
---|
229 | e.getMessage()
|
---|
230 | ),
|
---|
231 | tr("Error"),
|
---|
232 | JOptionPane.ERROR_MESSAGE
|
---|
233 | );
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Saves all bookmarks to the preferences file
|
---|
240 | */
|
---|
241 | public final void save() {
|
---|
242 | LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
|
---|
243 | for (Object o : ((DefaultListModel)getModel()).toArray()) {
|
---|
244 | String[] array = new String[5];
|
---|
245 | Bookmark b = (Bookmark)o;
|
---|
246 | array[0] = b.getName();
|
---|
247 | Bounds area = b.getArea();
|
---|
248 | array[1] = String.valueOf(area.getMinLat());
|
---|
249 | array[2] = String.valueOf(area.getMinLon());
|
---|
250 | array[3] = String.valueOf(area.getMaxLat());
|
---|
251 | array[4] = String.valueOf(area.getMaxLon());
|
---|
252 | coll.add(Arrays.asList(array));
|
---|
253 | }
|
---|
254 | Main.pref.putArray("bookmarks", coll);
|
---|
255 | }
|
---|
256 |
|
---|
257 | static class BookmarkCellRenderer extends JLabel implements ListCellRenderer {
|
---|
258 |
|
---|
259 | private ImageIcon icon;
|
---|
260 |
|
---|
261 | public BookmarkCellRenderer() {
|
---|
262 | setOpaque(true);
|
---|
263 | icon = ImageProvider.get("dialogs", "bookmark");
|
---|
264 | setIcon(icon);
|
---|
265 | }
|
---|
266 |
|
---|
267 | protected void renderColor(boolean selected) {
|
---|
268 | if (selected) {
|
---|
269 | setBackground(UIManager.getColor("List.selectionBackground"));
|
---|
270 | setForeground(UIManager.getColor("List.selectionForeground"));
|
---|
271 | } else {
|
---|
272 | setBackground(UIManager.getColor("List.background"));
|
---|
273 | setForeground(UIManager.getColor("List.foreground"));
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | protected String buildToolTipText(Bookmark b) {
|
---|
278 | Bounds area = b.getArea();
|
---|
279 | StringBuffer sb = new StringBuffer();
|
---|
280 | sb.append("<html>min[latitude,longitude]=<strong>[")
|
---|
281 | .append(area.getMinLat()).append(",").append(area.getMinLon()).append("]</strong>")
|
---|
282 | .append("<br>")
|
---|
283 | .append("max[latitude,longitude]=<strong>[")
|
---|
284 | .append(area.getMaxLat()).append(",").append(area.getMaxLon()).append("]</strong>")
|
---|
285 | .append("</html>");
|
---|
286 | return sb.toString();
|
---|
287 |
|
---|
288 | }
|
---|
289 | @Override
|
---|
290 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
291 | boolean cellHasFocus) {
|
---|
292 |
|
---|
293 | Bookmark b = (Bookmark) value;
|
---|
294 | renderColor(isSelected);
|
---|
295 | setText(b.getName());
|
---|
296 | setToolTipText(buildToolTipText(b));
|
---|
297 | return this;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|