1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileReader;
|
---|
7 | import java.io.FileWriter;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.PrintWriter;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.SortedMap;
|
---|
15 | import java.util.StringTokenizer;
|
---|
16 | import java.util.TreeMap;
|
---|
17 | import java.util.Map.Entry;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 |
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This class holds all preferences for JOSM.
|
---|
24 | *
|
---|
25 | * Other classes can register their beloved properties here. All properties will be
|
---|
26 | * saved upon set-access.
|
---|
27 | *
|
---|
28 | * @author imi
|
---|
29 | */
|
---|
30 | public class Preferences {
|
---|
31 |
|
---|
32 | public static interface PreferenceChangedListener {
|
---|
33 | void preferenceChanged(String key, String newValue);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Class holding one bookmarkentry.
|
---|
38 | * @author imi
|
---|
39 | */
|
---|
40 | public static class Bookmark {
|
---|
41 | public String name;
|
---|
42 | public double[] latlon = new double[4]; // minlat, minlon, maxlat, maxlon
|
---|
43 | @Override public String toString() {
|
---|
44 | return name;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public final ArrayList<PreferenceChangedListener> listener = new ArrayList<PreferenceChangedListener>();
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Map the property name to the property object.
|
---|
52 | */
|
---|
53 | protected final SortedMap<String, String> properties = new TreeMap<String, String>();
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Return the location of the user defined preferences file
|
---|
57 | */
|
---|
58 | public String getPreferencesDir() {
|
---|
59 | if (System.getenv("APPDATA") != null)
|
---|
60 | return System.getenv("APPDATA")+"/JOSM/";
|
---|
61 | return System.getProperty("user.home")+"/.josm/";
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * @return A list of all existing directories, where ressources could be stored.
|
---|
66 | */
|
---|
67 | public Collection<String> getAllPossiblePreferenceDirs() {
|
---|
68 | LinkedList<String> locations = new LinkedList<String>();
|
---|
69 | locations.add(Main.pref.getPreferencesDir());
|
---|
70 | String s;
|
---|
71 | if ((s = System.getenv("JOSM_RESOURCES")) != null) {
|
---|
72 | if (!s.endsWith("/") && !s.endsWith("\\"))
|
---|
73 | s = s + "/";
|
---|
74 | locations.add(s);
|
---|
75 | }
|
---|
76 | if ((s = System.getProperty("josm.resources")) != null) {
|
---|
77 | if (!s.endsWith("/") && !s.endsWith("\\"))
|
---|
78 | s = s + "/";
|
---|
79 | locations.add(s);
|
---|
80 | }
|
---|
81 | String appdata = System.getenv("APPDATA");
|
---|
82 | if (System.getenv("ALLUSERSPROFILE") != null && appdata != null && appdata.lastIndexOf("\\") != -1) {
|
---|
83 | appdata = appdata.substring(appdata.lastIndexOf("\\"));
|
---|
84 | locations.add(System.getenv("ALLUSERSPROFILE")+appdata+"/JOSM/");
|
---|
85 | }
|
---|
86 | locations.add("/usr/local/share/josm/");
|
---|
87 | locations.add("/usr/local/lib/josm/");
|
---|
88 | locations.add("/usr/share/josm/");
|
---|
89 | locations.add("/usr/lib/josm/");
|
---|
90 | return locations;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | synchronized public boolean hasKey(final String key) {
|
---|
95 | return properties.containsKey(key);
|
---|
96 | }
|
---|
97 | synchronized public String get(final String key) {
|
---|
98 | if (!properties.containsKey(key))
|
---|
99 | return "";
|
---|
100 | return properties.get(key);
|
---|
101 | }
|
---|
102 | synchronized public String get(final String key, final String def) {
|
---|
103 | final String prop = properties.get(key);
|
---|
104 | if (prop == null || prop.equals(""))
|
---|
105 | return def;
|
---|
106 | return prop;
|
---|
107 | }
|
---|
108 | synchronized public Map<String, String> getAllPrefix(final String prefix) {
|
---|
109 | final Map<String,String> all = new TreeMap<String,String>();
|
---|
110 | for (final Entry<String,String> e : properties.entrySet())
|
---|
111 | if (e.getKey().startsWith(prefix))
|
---|
112 | all.put(e.getKey(), e.getValue());
|
---|
113 | return all;
|
---|
114 | }
|
---|
115 | synchronized public boolean getBoolean(final String key) {
|
---|
116 | return getBoolean(key, false);
|
---|
117 | }
|
---|
118 | synchronized public boolean getBoolean(final String key, final boolean def) {
|
---|
119 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | synchronized public void put(final String key, final String value) {
|
---|
124 | if (value == null)
|
---|
125 | properties.remove(key);
|
---|
126 | else
|
---|
127 | properties.put(key, value);
|
---|
128 | save();
|
---|
129 | firePreferenceChanged(key, value);
|
---|
130 | }
|
---|
131 | synchronized public void put(final String key, final boolean value) {
|
---|
132 | properties.put(key, Boolean.toString(value));
|
---|
133 | save();
|
---|
134 | firePreferenceChanged(key, Boolean.toString(value));
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | private final void firePreferenceChanged(final String key, final String value) {
|
---|
139 | for (final PreferenceChangedListener l : listener)
|
---|
140 | l.preferenceChanged(key, value);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Called after every put. In case of a problem, do nothing but output the error
|
---|
145 | * in log.
|
---|
146 | */
|
---|
147 | protected void save() {
|
---|
148 | try {
|
---|
149 | final PrintWriter out = new PrintWriter(new FileWriter(getPreferencesDir() + "preferences"), false);
|
---|
150 | for (final Entry<String, String> e : properties.entrySet())
|
---|
151 | if (!e.getValue().equals(""))
|
---|
152 | out.println(e.getKey() + "=" + e.getValue());
|
---|
153 | out.close();
|
---|
154 | } catch (final IOException e) {
|
---|
155 | e.printStackTrace();
|
---|
156 | // do not message anything, since this can be called from strange
|
---|
157 | // places.
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void load() throws IOException {
|
---|
162 | properties.clear();
|
---|
163 | final BufferedReader in = new BufferedReader(new FileReader(getPreferencesDir()+"preferences"));
|
---|
164 | int lineNumber = 0;
|
---|
165 | for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
|
---|
166 | final int i = line.indexOf('=');
|
---|
167 | if (i == -1 || i == 0)
|
---|
168 | throw new IOException("Malformed config file at line "+lineNumber);
|
---|
169 | properties.put(line.substring(0,i), line.substring(i+1));
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | public final void resetToDefault() {
|
---|
174 | properties.clear();
|
---|
175 | properties.put("laf", "javax.swing.plaf.metal.MetalLookAndFeel");
|
---|
176 | properties.put("projection", "org.openstreetmap.josm.data.projection.Epsg4326");
|
---|
177 | properties.put("propertiesdialog.visible", "true");
|
---|
178 | properties.put("osm-server.url", "http://www.openstreetmap.org/api");
|
---|
179 | save();
|
---|
180 | }
|
---|
181 |
|
---|
182 | public Collection<Bookmark> loadBookmarks() throws IOException {
|
---|
183 | File bookmarkFile = new File(getPreferencesDir()+"bookmarks");
|
---|
184 | if (!bookmarkFile.exists())
|
---|
185 | bookmarkFile.createNewFile();
|
---|
186 | BufferedReader in = new BufferedReader(new FileReader(bookmarkFile));
|
---|
187 |
|
---|
188 | Collection<Bookmark> bookmarks = new LinkedList<Bookmark>();
|
---|
189 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
190 | StringTokenizer st = new StringTokenizer(line, ",");
|
---|
191 | if (st.countTokens() < 5)
|
---|
192 | continue;
|
---|
193 | Bookmark b = new Bookmark();
|
---|
194 | b.name = st.nextToken();
|
---|
195 | try {
|
---|
196 | for (int i = 0; i < b.latlon.length; ++i)
|
---|
197 | b.latlon[i] = Double.parseDouble(st.nextToken());
|
---|
198 | bookmarks.add(b);
|
---|
199 | } catch (NumberFormatException x) {
|
---|
200 | // line not parsed
|
---|
201 | }
|
---|
202 | }
|
---|
203 | in.close();
|
---|
204 | return bookmarks;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {
|
---|
208 | File bookmarkFile = new File(Main.pref.getPreferencesDir()+"bookmarks");
|
---|
209 | if (!bookmarkFile.exists())
|
---|
210 | bookmarkFile.createNewFile();
|
---|
211 | PrintWriter out = new PrintWriter(new FileWriter(bookmarkFile));
|
---|
212 | for (Bookmark b : bookmarks) {
|
---|
213 | b.name.replace(',', '_');
|
---|
214 | out.print(b.name+",");
|
---|
215 | for (int i = 0; i < b.latlon.length; ++i)
|
---|
216 | out.print(b.latlon[i]+(i<b.latlon.length-1?",":""));
|
---|
217 | out.println();
|
---|
218 | }
|
---|
219 | out.close();
|
---|
220 | }
|
---|
221 | }
|
---|