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