source: josm/src/org/openstreetmap/josm/gui/BookmarkList.java@ 73

Last change on this file since 73 was 73, checked in by imi, 18 years ago

fixed bug where lat/lon was exchanged in download dialog and quick fix for Java6 - gui problem

File size: 3.3 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import java.awt.Component;
4import java.io.BufferedReader;
5import java.io.File;
6import java.io.FileReader;
7import java.io.FileWriter;
8import java.io.IOException;
9import java.io.PrintWriter;
10import java.util.StringTokenizer;
11
12import javax.swing.DefaultListCellRenderer;
13import javax.swing.DefaultListModel;
14import javax.swing.JLabel;
15import javax.swing.JList;
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.Preferences;
20import org.openstreetmap.josm.tools.ImageProvider;
21
22/**
23 * List class that read and save its content from the bookmark file.
24 * @author imi
25 */
26public class BookmarkList extends JList {
27
28 /**
29 * Class holding one bookmarkentry.
30 * @author imi
31 */
32 public static class Bookmark {
33 public String name;
34 public double[] latlon = new double[4]; // minlat, minlon, maxlat, maxlon
35 public boolean rawgps;
36 @Override public String toString() {
37 return name;
38 }
39 }
40
41 /**
42 * Create a bookmark list as well as the Buttons add and remove.
43 */
44 public BookmarkList() {
45 setModel(new DefaultListModel());
46 load();
47 setVisibleRowCount(7);
48 setCellRenderer(new DefaultListCellRenderer(){
49 @Override
50 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
51 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
52 if (c instanceof JLabel) {
53 Bookmark b = (Bookmark)value;
54 ((JLabel)c).setIcon(ImageProvider.get("layer/"+(b.rawgps?"rawgps_small":"osmdata_small")));
55 }
56 return c;
57 }
58 });
59 }
60
61 /**
62 * Loads the bookmarks from file.
63 */
64 public void load() {
65 DefaultListModel model = (DefaultListModel)getModel();
66 model.removeAllElements();
67 File bookmarkFile = new File(Preferences.getPreferencesDir()+"bookmarks");
68 try {
69 if (!bookmarkFile.exists())
70 bookmarkFile.createNewFile();
71 BufferedReader in = new BufferedReader(new FileReader(bookmarkFile));
72
73 for (String line = in.readLine(); line != null; line = in.readLine()) {
74 StringTokenizer st = new StringTokenizer(line, ",");
75 if (st.countTokens() != 6)
76 continue;
77 Bookmark b = new Bookmark();
78 b.name = st.nextToken();
79 try {
80 for (int i = 0; i < b.latlon.length; ++i)
81 b.latlon[i] = Double.parseDouble(st.nextToken());
82 b.rawgps = Boolean.parseBoolean(st.nextToken());
83 model.addElement(b);
84 } catch (NumberFormatException x) {
85 // line not parsed
86 }
87 }
88 in.close();
89 } catch (IOException e) {
90 JOptionPane.showMessageDialog(Main.main, "Could not read bookmarks.\n"+e.getMessage());
91 }
92 }
93
94 /**
95 * Save all bookmarks to the preferences file
96 */
97 public void save() {
98 File bookmarkFile = new File(Preferences.getPreferencesDir()+"bookmarks");
99 try {
100 if (!bookmarkFile.exists())
101 bookmarkFile.createNewFile();
102 PrintWriter out = new PrintWriter(new FileWriter(bookmarkFile));
103 DefaultListModel m = (DefaultListModel)getModel();
104 for (Object o : m.toArray()) {
105 Bookmark b = (Bookmark)o;
106 b.name.replace(',', '_');
107 out.print(b.name+",");
108 for (int i = 0; i < b.latlon.length; ++i)
109 out.print(b.latlon[i]+",");
110 out.println(b.rawgps);
111 }
112 out.close();
113 } catch (IOException e) {
114 JOptionPane.showMessageDialog(Main.main, "Could not write bookmark.\n"+e.getMessage());
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.