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

Last change on this file since 8342 was 8342, checked in by Don-vip, 9 years ago

code style - Close curly brace and the next "else", "catch" and "finally" keywords should be located on the same line

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.download;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.LinkedList;
12import java.util.List;
13
14import javax.swing.DefaultListModel;
15import javax.swing.ImageIcon;
16import javax.swing.JLabel;
17import javax.swing.JList;
18import javax.swing.ListCellRenderer;
19import javax.swing.UIManager;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.tools.ImageProvider;
24
25/**
26 * List class that read and save its content from the bookmark file.
27 * @since 6340
28 */
29public class BookmarkList extends JList<BookmarkList.Bookmark> {
30
31 /**
32 * Class holding one bookmarkentry.
33 */
34 public static class Bookmark implements Comparable<Bookmark> {
35 private String name;
36 private Bounds area;
37
38 /**
39 * Constructs a new {@code Bookmark} with the given contents.
40 * @param list Bookmark contents as a list of 5 elements. First item is the name, then come bounds arguments (minlat, minlon, maxlat, maxlon)
41 * @throws NumberFormatException if the bounds arguments are not numbers
42 * @throws IllegalArgumentException if list contain less than 5 elements
43 */
44 public Bookmark(Collection<String> list) throws NumberFormatException, IllegalArgumentException {
45 List<String> array = new ArrayList<>(list);
46 if(array.size() < 5)
47 throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark"));
48 name = array.get(0);
49 area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)),
50 Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4)));
51 }
52
53 /**
54 * Constructs a new empty {@code Bookmark}.
55 */
56 public Bookmark() {
57 area = null;
58 name = null;
59 }
60
61 /**
62 * Constructs a new unamed {@code Bookmark} for the given area.
63 * @param area The bookmark area
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 @Override
79 public int hashCode() {
80 final int prime = 31;
81 int result = 1;
82 result = prime * result + ((area == null) ? 0 : area.hashCode());
83 result = prime * result + ((name == null) ? 0 : name.hashCode());
84 return result;
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj)
90 return true;
91 if (obj == null)
92 return false;
93 if (getClass() != obj.getClass())
94 return false;
95 Bookmark other = (Bookmark) obj;
96 if (area == null) {
97 if (other.area != null)
98 return false;
99 } else if (!area.equals(other.area))
100 return false;
101 if (name == null) {
102 if (other.name != null)
103 return false;
104 } else if (!name.equals(other.name))
105 return false;
106 return true;
107 }
108
109 /**
110 * Returns the bookmark area
111 * @return The bookmark area
112 */
113 public Bounds getArea() {
114 return area;
115 }
116
117 /**
118 * Returns the bookmark name
119 * @return The bookmark name
120 */
121 public String getName() {
122 return name;
123 }
124
125 /**
126 * Sets the bookmark name
127 * @param name The bookmark name
128 */
129 public void setName(String name) {
130 this.name = name;
131 }
132
133 /**
134 * Sets the bookmark area
135 * @param area The bookmark area
136 */
137 public void setArea(Bounds area) {
138 this.area = area;
139 }
140 }
141
142 /**
143 * Creates a bookmark list as well as the Buttons add and remove.
144 */
145 public BookmarkList() {
146 setModel(new DefaultListModel<Bookmark>());
147 load();
148 setVisibleRowCount(7);
149 setCellRenderer(new BookmarkCellRenderer());
150 }
151
152 /**
153 * Loads the bookmarks from file.
154 */
155 public final void load() {
156 DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>)getModel();
157 model.removeAllElements();
158 Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
159 if(args != null) {
160 List<Bookmark> bookmarks = new LinkedList<>();
161 for(Collection<String> entry : args) {
162 try {
163 bookmarks.add(new Bookmark(entry));
164 } catch (Exception e) {
165 Main.error(tr("Error reading bookmark entry: %s", e.getMessage()));
166 }
167 }
168 Collections.sort(bookmarks);
169 for (Bookmark b : bookmarks) {
170 model.addElement(b);
171 }
172 }
173 }
174
175 /**
176 * Saves all bookmarks to the preferences file
177 */
178 public final void save() {
179 List<Collection<String>> coll = new LinkedList<>();
180 for (Object o : ((DefaultListModel<Bookmark>)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.getMinLat());
186 array[2] = String.valueOf(area.getMinLon());
187 array[3] = String.valueOf(area.getMaxLat());
188 array[4] = String.valueOf(area.getMaxLon());
189 coll.add(Arrays.asList(array));
190 }
191 Main.pref.putArray("bookmarks", coll);
192 }
193
194 static class BookmarkCellRenderer extends JLabel implements ListCellRenderer<BookmarkList.Bookmark> {
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 StringBuilder sb = new StringBuilder();
217 sb.append("<html>min[latitude,longitude]=<strong>[")
218 .append(area.getMinLat()).append(",").append(area.getMinLon()).append("]</strong>")
219 .append("<br>")
220 .append("max[latitude,longitude]=<strong>[")
221 .append(area.getMaxLat()).append(",").append(area.getMaxLon()).append("]</strong>")
222 .append("</html>");
223 return sb.toString();
224
225 }
226
227 @Override
228 public Component getListCellRendererComponent(JList<? extends Bookmark> list, Bookmark value, int index, boolean isSelected, boolean cellHasFocus) {
229 renderColor(isSelected);
230 setText(value.getName());
231 setToolTipText(buildToolTipText(value));
232 return this;
233 }
234 }
235}
Note: See TracBrowser for help on using the repository browser.