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

Last change on this file since 729 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
4import java.awt.Color;
5import java.io.BufferedReader;
6import java.io.File;
7import java.io.FileReader;
8import java.io.FileWriter;
9import java.io.IOException;
10import java.io.PrintWriter;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.LinkedList;
14import java.util.Map;
15import java.util.SortedMap;
16import java.util.StringTokenizer;
17import java.util.TreeMap;
18import java.util.Map.Entry;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.tools.ColorHelper;
22
23
24/**
25 * This class holds all preferences for JOSM.
26 *
27 * Other classes can register their beloved properties here. All properties will be
28 * saved upon set-access.
29 *
30 * @author imi
31 */
32public class Preferences {
33
34 public static interface PreferenceChangedListener {
35 void preferenceChanged(String key, String newValue);
36 }
37
38 /**
39 * Class holding one bookmarkentry.
40 * @author imi
41 */
42 public static class Bookmark {
43 public String name;
44 public double[] latlon = new double[4]; // minlat, minlon, maxlat, maxlon
45 @Override public String toString() {
46 return name;
47 }
48 }
49
50 public final ArrayList<PreferenceChangedListener> listener = new ArrayList<PreferenceChangedListener>();
51
52 /**
53 * Map the property name to the property object.
54 */
55 protected final SortedMap<String, String> properties = new TreeMap<String, String>();
56
57 /**
58 * Override some values on read. This is intended to be used for technology previews
59 * where we want to temporarily modify things without changing the user's preferences
60 * file.
61 */
62 protected static final SortedMap<String, String> override = new TreeMap<String, String>();
63 static {
64 //override.put("osm-server.version", "0.5");
65 //override.put("osm-server.additional-versions", "");
66 //override.put("osm-server.url", "http://openstreetmap.gryph.de/api");
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
141 synchronized public boolean getBoolean(final String key) {
142 return getBoolean(key, false);
143 }
144 synchronized public boolean getBoolean(final String key, final boolean def) {
145 if (override.containsKey(key))
146 return override.get(key) == null ? def : Boolean.parseBoolean(override.get(key));
147 return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
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 private final void firePreferenceChanged(final String key, final String value) {
165 for (final PreferenceChangedListener l : listener)
166 l.preferenceChanged(key, value);
167 }
168
169 /**
170 * Called after every put. In case of a problem, do nothing but output the error
171 * in log.
172 */
173 public void save() {
174 try {
175 final PrintWriter out = new PrintWriter(new FileWriter(getPreferencesDir() + "preferences"), false);
176 for (final Entry<String, String> e : properties.entrySet()) {
177 if (!e.getValue().equals(""))
178 out.println(e.getKey() + "=" + e.getValue());
179 }
180 out.close();
181 } catch (final IOException e) {
182 e.printStackTrace();
183 // do not message anything, since this can be called from strange
184 // places.
185 }
186 }
187
188 public void load() throws IOException {
189 properties.clear();
190 final BufferedReader in = new BufferedReader(new FileReader(getPreferencesDir()+"preferences"));
191 int lineNumber = 0;
192 ArrayList<Integer> errLines = new ArrayList<Integer>();
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 errLines.add(lineNumber);
197 continue;
198 }
199 properties.put(line.substring(0,i), line.substring(i+1));
200 }
201 if (!errLines.isEmpty()) {
202 throw new IOException("Malformed config file at lines " + errLines);
203 }
204 }
205
206 public final void resetToDefault() {
207 properties.clear();
208 properties.put("projection", "org.openstreetmap.josm.data.projection.Epsg4326");
209 properties.put("draw.segment.direction", "true");
210 properties.put("draw.wireframe", "false");
211 properties.put("layerlist.visible", "true");
212 properties.put("propertiesdialog.visible", "true");
213 properties.put("selectionlist.visible", "true");
214 properties.put("commandstack.visible", "true");
215 properties.put("osm-server.url", "http://www.openstreetmap.org/api");
216 if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
217 properties.put("laf", "javax.swing.plaf.metal.MetalLookAndFeel");
218 } else {
219 properties.put("laf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
220 }
221 save();
222 }
223
224 public Collection<Bookmark> loadBookmarks() throws IOException {
225 File bookmarkFile = new File(getPreferencesDir()+"bookmarks");
226 if (!bookmarkFile.exists())
227 bookmarkFile.createNewFile();
228 BufferedReader in = new BufferedReader(new FileReader(bookmarkFile));
229
230 Collection<Bookmark> bookmarks = new LinkedList<Bookmark>();
231 for (String line = in.readLine(); line != null; line = in.readLine()) {
232 StringTokenizer st = new StringTokenizer(line, ",");
233 if (st.countTokens() < 5)
234 continue;
235 Bookmark b = new Bookmark();
236 b.name = st.nextToken();
237 try {
238 for (int i = 0; i < b.latlon.length; ++i)
239 b.latlon[i] = Double.parseDouble(st.nextToken());
240 bookmarks.add(b);
241 } catch (NumberFormatException x) {
242 // line not parsed
243 }
244 }
245 in.close();
246 return bookmarks;
247 }
248
249 public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {
250 File bookmarkFile = new File(Main.pref.getPreferencesDir()+"bookmarks");
251 if (!bookmarkFile.exists())
252 bookmarkFile.createNewFile();
253 PrintWriter out = new PrintWriter(new FileWriter(bookmarkFile));
254 for (Bookmark b : bookmarks) {
255 b.name.replace(',', '_');
256 out.print(b.name+",");
257 for (int i = 0; i < b.latlon.length; ++i)
258 out.print(b.latlon[i]+(i<b.latlon.length-1?",":""));
259 out.println();
260 }
261 out.close();
262 }
263
264 /**
265 * Convenience method for accessing colour preferences.
266 *
267 * @param colName name of the colour
268 * @param def default value
269 * @return a Color object for the configured colour, or the default value if none configured.
270 */
271 public static Color getPreferencesColor(String colName, Color def) {
272 String colStr = Main.pref.get("color."+colName);
273 if (colStr.equals("")) {
274 Main.pref.put("color."+colName, ColorHelper.color2html(def));
275 return def;
276 }
277 return ColorHelper.html2color(colStr);
278 }
279}
Note: See TracBrowser for help on using the repository browser.