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