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

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

sonar - squid:S1166 - Exception handlers should preserve the original exceptions

  • Property svn:eol-style set to native
File size: 7.1 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;
13import java.util.Locale;
14import java.util.Objects;
15
16import javax.swing.DefaultListModel;
17import javax.swing.JLabel;
18import javax.swing.JList;
19import javax.swing.ListCellRenderer;
20import javax.swing.UIManager;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.Bounds;
24import org.openstreetmap.josm.tools.ImageProvider;
25
26/**
27 * List class that read and save its content from the bookmark file.
28 * @since 6340
29 */
30public class BookmarkList extends JList<BookmarkList.Bookmark> {
31
32 /**
33 * Class holding one bookmarkentry.
34 */
35 public static class Bookmark implements Comparable<Bookmark> {
36 private String name;
37 private Bounds area;
38
39 /**
40 * Constructs a new {@code Bookmark} with the given contents.
41 * @param list Bookmark contents as a list of 5 elements.
42 * First item is the name, then come bounds arguments (minlat, minlon, maxlat, maxlon)
43 * @throws NumberFormatException if the bounds arguments are not numbers
44 * @throws IllegalArgumentException if list contain less than 5 elements
45 */
46 public Bookmark(Collection<String> list) {
47 List<String> array = new ArrayList<>(list);
48 if (array.size() < 5)
49 throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark"));
50 name = array.get(0);
51 area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)),
52 Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4)));
53 }
54
55 /**
56 * Constructs a new empty {@code Bookmark}.
57 */
58 public Bookmark() {
59 area = null;
60 name = null;
61 }
62
63 /**
64 * Constructs a new unamed {@code Bookmark} for the given area.
65 * @param area The bookmark area
66 */
67 public Bookmark(Bounds area) {
68 this.area = area;
69 }
70
71 @Override public String toString() {
72 return name;
73 }
74
75 @Override
76 public int compareTo(Bookmark b) {
77 return name.toLowerCase(Locale.ENGLISH).compareTo(b.name.toLowerCase(Locale.ENGLISH));
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(name, area);
83 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) return true;
88 if (obj == null || getClass() != obj.getClass()) return false;
89 Bookmark bookmark = (Bookmark) obj;
90 return Objects.equals(name, bookmark.name) &&
91 Objects.equals(area, bookmark.area);
92 }
93
94 /**
95 * Returns the bookmark area
96 * @return The bookmark area
97 */
98 public Bounds getArea() {
99 return area;
100 }
101
102 /**
103 * Returns the bookmark name
104 * @return The bookmark name
105 */
106 public String getName() {
107 return name;
108 }
109
110 /**
111 * Sets the bookmark name
112 * @param name The bookmark name
113 */
114 public void setName(String name) {
115 this.name = name;
116 }
117
118 /**
119 * Sets the bookmark area
120 * @param area The bookmark area
121 */
122 public void setArea(Bounds area) {
123 this.area = area;
124 }
125 }
126
127 /**
128 * Creates a bookmark list as well as the Buttons add and remove.
129 */
130 public BookmarkList() {
131 setModel(new DefaultListModel<Bookmark>());
132 load();
133 setVisibleRowCount(7);
134 setCellRenderer(new BookmarkCellRenderer());
135 }
136
137 /**
138 * Loads the bookmarks from file.
139 */
140 public final void load() {
141 DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>) getModel();
142 model.removeAllElements();
143 Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
144 if (args != null) {
145 List<Bookmark> bookmarks = new LinkedList<>();
146 for (Collection<String> entry : args) {
147 try {
148 bookmarks.add(new Bookmark(entry));
149 } catch (IllegalArgumentException e) {
150 Main.error(e, tr("Error reading bookmark entry: %s", e.getMessage()));
151 }
152 }
153 Collections.sort(bookmarks);
154 for (Bookmark b : bookmarks) {
155 model.addElement(b);
156 }
157 }
158 }
159
160 /**
161 * Saves all bookmarks to the preferences file
162 */
163 public final void save() {
164 List<Collection<String>> coll = new LinkedList<>();
165 for (Object o : ((DefaultListModel<Bookmark>) getModel()).toArray()) {
166 String[] array = new String[5];
167 Bookmark b = (Bookmark) o;
168 array[0] = b.getName();
169 Bounds area = b.getArea();
170 array[1] = String.valueOf(area.getMinLat());
171 array[2] = String.valueOf(area.getMinLon());
172 array[3] = String.valueOf(area.getMaxLat());
173 array[4] = String.valueOf(area.getMaxLon());
174 coll.add(Arrays.asList(array));
175 }
176 Main.pref.putArray("bookmarks", coll);
177 }
178
179 static class BookmarkCellRenderer extends JLabel implements ListCellRenderer<BookmarkList.Bookmark> {
180
181 /**
182 * Constructs a new {@code BookmarkCellRenderer}.
183 */
184 BookmarkCellRenderer() {
185 setOpaque(true);
186 setIcon(ImageProvider.get("dialogs", "bookmark"));
187 }
188
189 protected void renderColor(boolean selected) {
190 if (selected) {
191 setBackground(UIManager.getColor("List.selectionBackground"));
192 setForeground(UIManager.getColor("List.selectionForeground"));
193 } else {
194 setBackground(UIManager.getColor("List.background"));
195 setForeground(UIManager.getColor("List.foreground"));
196 }
197 }
198
199 protected String buildToolTipText(Bookmark b) {
200 Bounds area = b.getArea();
201 StringBuilder sb = new StringBuilder(128);
202 sb.append("<html>min[latitude,longitude]=<strong>[")
203 .append(area.getMinLat()).append(',').append(area.getMinLon()).append("]</strong>"+
204 "<br>max[latitude,longitude]=<strong>[")
205 .append(area.getMaxLat()).append(',').append(area.getMaxLon()).append("]</strong>"+
206 "</html>");
207 return sb.toString();
208 }
209
210 @Override
211 public Component getListCellRendererComponent(JList<? extends Bookmark> list, Bookmark value, int index, boolean isSelected,
212 boolean cellHasFocus) {
213 renderColor(isSelected);
214 setText(value.getName());
215 setToolTipText(buildToolTipText(value));
216 return this;
217 }
218 }
219}
Note: See TracBrowser for help on using the repository browser.