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.util.Collection;
|
---|
9 | import java.util.LinkedList;
|
---|
10 |
|
---|
11 | import javax.swing.DefaultListModel;
|
---|
12 | import javax.swing.ImageIcon;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JList;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.ListCellRenderer;
|
---|
17 | import javax.swing.UIManager;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.Bounds;
|
---|
21 | import org.openstreetmap.josm.data.Preferences;
|
---|
22 | import org.openstreetmap.josm.data.Preferences.Bookmark;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * List class that read and save its content from the bookmark file.
|
---|
27 | * @author imi
|
---|
28 | */
|
---|
29 | public class BookmarkList extends JList {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Create a bookmark list as well as the Buttons add and remove.
|
---|
33 | */
|
---|
34 | public BookmarkList() {
|
---|
35 | setModel(new DefaultListModel());
|
---|
36 | load();
|
---|
37 | setVisibleRowCount(7);
|
---|
38 | setCellRenderer(new BookmarkCellRenderer());
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Loads the bookmarks from file.
|
---|
43 | */
|
---|
44 | public void load() {
|
---|
45 | DefaultListModel model = (DefaultListModel)getModel();
|
---|
46 | model.removeAllElements();
|
---|
47 | try {
|
---|
48 | for (Preferences.Bookmark b : Main.pref.loadBookmarks()) {
|
---|
49 | model.addElement(b);
|
---|
50 | }
|
---|
51 | } catch (IOException e) {
|
---|
52 | e.printStackTrace();
|
---|
53 | JOptionPane.showMessageDialog(
|
---|
54 | Main.parent,
|
---|
55 | tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>",
|
---|
56 | Main.pref.getBookmarksFile(),
|
---|
57 | e.getMessage()
|
---|
58 | ),
|
---|
59 | tr("Error"),
|
---|
60 | JOptionPane.ERROR_MESSAGE
|
---|
61 | );
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Save all bookmarks to the preferences file
|
---|
67 | */
|
---|
68 | public void save() {
|
---|
69 | try {
|
---|
70 | Collection<Preferences.Bookmark> bookmarks = new LinkedList<Preferences.Bookmark>();
|
---|
71 | for (Object o : ((DefaultListModel)getModel()).toArray()) {
|
---|
72 | bookmarks.add((Preferences.Bookmark)o);
|
---|
73 | }
|
---|
74 | Main.pref.saveBookmarks(bookmarks);
|
---|
75 | } catch (IOException e) {
|
---|
76 | JOptionPane.showMessageDialog(
|
---|
77 | Main.parent,
|
---|
78 | tr("<html>Could not write bookmark.<br>{0}</html>", e.getMessage()),
|
---|
79 | tr("Error"),
|
---|
80 | JOptionPane.ERROR_MESSAGE
|
---|
81 | );
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | static class BookmarkCellRenderer extends JLabel implements ListCellRenderer {
|
---|
86 |
|
---|
87 | private ImageIcon icon;
|
---|
88 |
|
---|
89 | public BookmarkCellRenderer() {
|
---|
90 | setOpaque(true);
|
---|
91 | icon = ImageProvider.get("dialogs", "bookmark");
|
---|
92 | setIcon(icon);
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected void renderColor(boolean selected) {
|
---|
96 | if (selected) {
|
---|
97 | setBackground(UIManager.getColor("List.selectionBackground"));
|
---|
98 | setForeground(UIManager.getColor("List.selectionForeground"));
|
---|
99 | } else {
|
---|
100 | setBackground(UIManager.getColor("List.background"));
|
---|
101 | setForeground(UIManager.getColor("List.foreground"));
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected String buildToolTipText(Bookmark b) {
|
---|
106 | Bounds area = b.getArea();
|
---|
107 | StringBuffer sb = new StringBuffer();
|
---|
108 | sb.append("<html>min[latitude,longitude]=<strong>[")
|
---|
109 | .append(area.getMin().lat()).append(",").append(area.getMin().lon()).append("]</strong>")
|
---|
110 | .append("<br>")
|
---|
111 | .append("max[latitude,longitude]=<strong>[")
|
---|
112 | .append(area.getMax().lat()).append(",").append(area.getMax().lon()).append("]</strong>")
|
---|
113 | .append("</html>");
|
---|
114 | return sb.toString();
|
---|
115 |
|
---|
116 | }
|
---|
117 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
118 | boolean cellHasFocus) {
|
---|
119 |
|
---|
120 | Bookmark b = (Bookmark) value;
|
---|
121 | renderColor(isSelected);
|
---|
122 | setText(b.getName());
|
---|
123 | setToolTipText(buildToolTipText(b));
|
---|
124 | return this;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|