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