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