source: josm/trunk/src/org/openstreetmap/josm/data/Preferences.java@ 343

Last change on this file since 343 was 343, checked in by gebner, 17 years ago

Merge 0.5.

File size: 7.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
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.ArrayList;
11import java.util.Collection;
12import java.util.LinkedList;
13import java.util.Map;
14import java.util.SortedMap;
15import java.util.StringTokenizer;
16import java.util.TreeMap;
17import java.util.Map.Entry;
18
19import 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 */
30public 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 * Override some values on read. This is intended to be used for technology previews
57 * where we want to temporarily modify things without changing the user's preferences
58 * file.
59 */
60 protected static final SortedMap<String, String> override = new TreeMap<String, String>();
61 static {
62 override.put("osm-server.version", "0.5");
63 override.put("osm-server.additional-versions", "");
64 override.put("osm-server.url", "http://openstreetmap.gryph.de/api");
65 override.put("osm-server.username", "fred@remote.org");
66 override.put("osm-server.password", "fredfred");
67 override.put("plugins", null);
68 }
69
70 /**
71 * Return the location of the user defined preferences file
72 */
73 public String getPreferencesDir() {
74 if (System.getenv("APPDATA") != null)
75 return System.getenv("APPDATA")+"/JOSM/";
76 return System.getProperty("user.home")+"/.josm/";
77 }
78
79 /**
80 * @return A list of all existing directories, where resources could be stored.
81 */
82 public Collection<String> getAllPossiblePreferenceDirs() {
83 LinkedList<String> locations = new LinkedList<String>();
84 locations.add(Main.pref.getPreferencesDir());
85 String s;
86 if ((s = System.getenv("JOSM_RESOURCES")) != null) {
87 if (!s.endsWith("/") && !s.endsWith("\\"))
88 s = s + "/";
89 locations.add(s);
90 }
91 if ((s = System.getProperty("josm.resources")) != null) {
92 if (!s.endsWith("/") && !s.endsWith("\\"))
93 s = s + "/";
94 locations.add(s);
95 }
96 String appdata = System.getenv("APPDATA");
97 if (System.getenv("ALLUSERSPROFILE") != null && appdata != null && appdata.lastIndexOf("\\") != -1) {
98 appdata = appdata.substring(appdata.lastIndexOf("\\"));
99 locations.add(System.getenv("ALLUSERSPROFILE")+appdata+"/JOSM/");
100 }
101 locations.add("/usr/local/share/josm/");
102 locations.add("/usr/local/lib/josm/");
103 locations.add("/usr/share/josm/");
104 locations.add("/usr/lib/josm/");
105 return locations;
106 }
107
108
109 synchronized public boolean hasKey(final String key) {
110 return override.containsKey(key) ? override.get(key) != null : properties.containsKey(key);
111 }
112 synchronized public String get(final String key) {
113 if (override.containsKey(key))
114 return override.get(key);
115 if (!properties.containsKey(key))
116 return "";
117 return properties.get(key);
118 }
119 synchronized public String get(final String key, final String def) {
120 if (override.containsKey(key))
121 return override.get(key);
122 final String prop = properties.get(key);
123 if (prop == null || prop.equals(""))
124 return def;
125 return prop;
126 }
127 synchronized public Map<String, String> getAllPrefix(final String prefix) {
128 final Map<String,String> all = new TreeMap<String,String>();
129 for (final Entry<String,String> e : properties.entrySet())
130 if (e.getKey().startsWith(prefix))
131 all.put(e.getKey(), e.getValue());
132 for (final Entry<String,String> e : override.entrySet())
133 if (e.getKey().startsWith(prefix))
134 if (e.getValue() == null)
135 all.remove(e.getKey());
136 else
137 all.put(e.getKey(), e.getValue());
138 return all;
139 }
140 synchronized public boolean getBoolean(final String key) {
141 return getBoolean(key, false);
142 }
143 synchronized public boolean getBoolean(final String key, final boolean def) {
144 if (override.containsKey(key))
145 return override.get(key) == null ? def : Boolean.parseBoolean(override.get(key));
146 return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
147 }
148
149
150 synchronized public void put(final String key, final String value) {
151 if (value == null)
152 properties.remove(key);
153 else
154 properties.put(key, value);
155 save();
156 firePreferenceChanged(key, value);
157 }
158 synchronized public void put(final String key, final boolean value) {
159 properties.put(key, Boolean.toString(value));
160 save();
161 firePreferenceChanged(key, Boolean.toString(value));
162 }
163
164
165 private final void firePreferenceChanged(final String key, final String value) {
166 for (final PreferenceChangedListener l : listener)
167 l.preferenceChanged(key, value);
168 }
169
170 /**
171 * Called after every put. In case of a problem, do nothing but output the error
172 * in log.
173 */
174 protected void save() {
175 try {
176 final PrintWriter out = new PrintWriter(new FileWriter(getPreferencesDir() + "preferences"), false);
177 for (final Entry<String, String> e : properties.entrySet()) {
178 if (!e.getValue().equals(""))
179 out.println(e.getKey() + "=" + e.getValue());
180 }
181 out.close();
182 } catch (final IOException e) {
183 e.printStackTrace();
184 // do not message anything, since this can be called from strange
185 // places.
186 }
187 }
188
189 public void load() throws IOException {
190 properties.clear();
191 final BufferedReader in = new BufferedReader(new FileReader(getPreferencesDir()+"preferences"));
192 int lineNumber = 0;
193 for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
194 final int i = line.indexOf('=');
195 if (i == -1 || i == 0)
196 throw new IOException("Malformed config file at line "+lineNumber);
197 properties.put(line.substring(0,i), line.substring(i+1));
198 }
199 }
200
201 public final void resetToDefault() {
202 properties.clear();
203 properties.put("laf", "javax.swing.plaf.metal.MetalLookAndFeel");
204 properties.put("projection", "org.openstreetmap.josm.data.projection.Epsg4326");
205 properties.put("propertiesdialog.visible", "true");
206 properties.put("osm-server.url", "http://www.openstreetmap.org/api");
207 save();
208 }
209
210 public Collection<Bookmark> loadBookmarks() throws IOException {
211 File bookmarkFile = new File(getPreferencesDir()+"bookmarks");
212 if (!bookmarkFile.exists())
213 bookmarkFile.createNewFile();
214 BufferedReader in = new BufferedReader(new FileReader(bookmarkFile));
215
216 Collection<Bookmark> bookmarks = new LinkedList<Bookmark>();
217 for (String line = in.readLine(); line != null; line = in.readLine()) {
218 StringTokenizer st = new StringTokenizer(line, ",");
219 if (st.countTokens() < 5)
220 continue;
221 Bookmark b = new Bookmark();
222 b.name = st.nextToken();
223 try {
224 for (int i = 0; i < b.latlon.length; ++i)
225 b.latlon[i] = Double.parseDouble(st.nextToken());
226 bookmarks.add(b);
227 } catch (NumberFormatException x) {
228 // line not parsed
229 }
230 }
231 in.close();
232 return bookmarks;
233 }
234
235 public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {
236 File bookmarkFile = new File(Main.pref.getPreferencesDir()+"bookmarks");
237 if (!bookmarkFile.exists())
238 bookmarkFile.createNewFile();
239 PrintWriter out = new PrintWriter(new FileWriter(bookmarkFile));
240 for (Bookmark b : bookmarks) {
241 b.name.replace(',', '_');
242 out.print(b.name+",");
243 for (int i = 0; i < b.latlon.length; ++i)
244 out.print(b.latlon[i]+(i<b.latlon.length-1?",":""));
245 out.println();
246 }
247 out.close();
248 }
249}
Note: See TracBrowser for help on using the repository browser.