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.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.DefaultListModel;
|
---|
15 | import javax.swing.ImageIcon;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JList;
|
---|
18 | import javax.swing.ListCellRenderer;
|
---|
19 | import javax.swing.UIManager;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.Bounds;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * List class that read and save its content from the bookmark file.
|
---|
27 | * @since 6340
|
---|
28 | */
|
---|
29 | public 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 | LinkedList<Bookmark> bookmarks = new LinkedList<>();
|
---|
161 | for(Collection<String> entry : args) {
|
---|
162 | try {
|
---|
163 | bookmarks.add(new Bookmark(entry));
|
---|
164 | }
|
---|
165 | catch (Exception e) {
|
---|
166 | Main.error(tr("Error reading bookmark entry: %s", e.getMessage()));
|
---|
167 | }
|
---|
168 | }
|
---|
169 | Collections.sort(bookmarks);
|
---|
170 | for (Bookmark b : bookmarks) {
|
---|
171 | model.addElement(b);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Saves all bookmarks to the preferences file
|
---|
178 | */
|
---|
179 | public final void save() {
|
---|
180 | LinkedList<Collection<String>> coll = new LinkedList<>();
|
---|
181 | for (Object o : ((DefaultListModel<Bookmark>)getModel()).toArray()) {
|
---|
182 | String[] array = new String[5];
|
---|
183 | Bookmark b = (Bookmark) o;
|
---|
184 | array[0] = b.getName();
|
---|
185 | Bounds area = b.getArea();
|
---|
186 | array[1] = String.valueOf(area.getMinLat());
|
---|
187 | array[2] = String.valueOf(area.getMinLon());
|
---|
188 | array[3] = String.valueOf(area.getMaxLat());
|
---|
189 | array[4] = String.valueOf(area.getMaxLon());
|
---|
190 | coll.add(Arrays.asList(array));
|
---|
191 | }
|
---|
192 | Main.pref.putArray("bookmarks", coll);
|
---|
193 | }
|
---|
194 |
|
---|
195 | static class BookmarkCellRenderer extends JLabel implements ListCellRenderer<BookmarkList.Bookmark> {
|
---|
196 |
|
---|
197 | private ImageIcon icon;
|
---|
198 |
|
---|
199 | public BookmarkCellRenderer() {
|
---|
200 | setOpaque(true);
|
---|
201 | icon = ImageProvider.get("dialogs", "bookmark");
|
---|
202 | setIcon(icon);
|
---|
203 | }
|
---|
204 |
|
---|
205 | protected void renderColor(boolean selected) {
|
---|
206 | if (selected) {
|
---|
207 | setBackground(UIManager.getColor("List.selectionBackground"));
|
---|
208 | setForeground(UIManager.getColor("List.selectionForeground"));
|
---|
209 | } else {
|
---|
210 | setBackground(UIManager.getColor("List.background"));
|
---|
211 | setForeground(UIManager.getColor("List.foreground"));
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | protected String buildToolTipText(Bookmark b) {
|
---|
216 | Bounds area = b.getArea();
|
---|
217 | StringBuilder sb = new StringBuilder();
|
---|
218 | sb.append("<html>min[latitude,longitude]=<strong>[")
|
---|
219 | .append(area.getMinLat()).append(",").append(area.getMinLon()).append("]</strong>")
|
---|
220 | .append("<br>")
|
---|
221 | .append("max[latitude,longitude]=<strong>[")
|
---|
222 | .append(area.getMaxLat()).append(",").append(area.getMaxLon()).append("]</strong>")
|
---|
223 | .append("</html>");
|
---|
224 | return sb.toString();
|
---|
225 |
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | public Component getListCellRendererComponent(JList<? extends Bookmark> list, Bookmark value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
230 | renderColor(isSelected);
|
---|
231 | setText(value.getName());
|
---|
232 | setToolTipText(buildToolTipText(value));
|
---|
233 | return this;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|