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

Last change on this file since 192 was 192, checked in by imi, 17 years ago
  • added command line parameter --default-classloader (for debug and packaging purpose)
File size: 6.5 KB
Line 
1package org.openstreetmap.josm.data;
2
3import static org.xnap.commons.i18n.I18n.marktr;
4
5import java.awt.Color;
6import java.io.BufferedReader;
7import java.io.File;
8import java.io.FileReader;
9import java.io.FileWriter;
10import java.io.IOException;
11import java.io.PrintWriter;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.LinkedList;
15import java.util.Map;
16import java.util.SortedMap;
17import java.util.StringTokenizer;
18import java.util.TreeMap;
19import java.util.Map.Entry;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
23import org.openstreetmap.josm.tools.ColorHelper;
24
25
26/**
27 * This class holds all preferences for JOSM.
28 *
29 * Other classes can register their beloved properties here. All properties will be
30 * saved upon set-access.
31 *
32 * @author imi
33 */
34public class Preferences {
35
36 public static interface PreferenceChangedListener {
37 void preferenceChanged(String key, String newValue);
38 }
39
40 /**
41 * Class holding one bookmarkentry.
42 * @author imi
43 */
44 public static class Bookmark {
45 public String name;
46 public double[] latlon = new double[4]; // minlat, minlon, maxlat, maxlon
47 @Override public String toString() {
48 return name;
49 }
50 }
51
52 public final ArrayList<PreferenceChangedListener> listener = new ArrayList<PreferenceChangedListener>();
53
54 /**
55 * Map the property name to the property object.
56 */
57 private final SortedMap<String, String> properties = new TreeMap<String, String>();
58
59 /**
60 * Return the location of the preferences file
61 */
62 public String getPreferencesDir() {
63 return System.getProperty("user.home")+"/.josm/";
64 }
65
66 synchronized public boolean hasKey(final String key) {
67 return properties.containsKey(key);
68 }
69 synchronized public String get(final String key) {
70 if (!properties.containsKey(key))
71 return "";
72 return properties.get(key);
73 }
74 synchronized public String get(final String key, final String def) {
75 final String prop = properties.get(key);
76 if (prop == null || prop.equals(""))
77 return def;
78 return prop;
79 }
80 synchronized public Map<String, String> getAllPrefix(final String prefix) {
81 final Map<String,String> all = new TreeMap<String,String>();
82 for (final Entry<String,String> e : properties.entrySet())
83 if (e.getKey().startsWith(prefix))
84 all.put(e.getKey(), e.getValue());
85 return all;
86 }
87 synchronized public boolean getBoolean(final String key) {
88 return getBoolean(key, false);
89 }
90 synchronized public boolean getBoolean(final String key, final boolean def) {
91 return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
92 }
93
94
95 synchronized public void put(final String key, final String value) {
96 if (value == null)
97 properties.remove(key);
98 else
99 properties.put(key, value);
100 save();
101 firePreferenceChanged(key, value);
102 }
103 synchronized public void put(final String key, final boolean value) {
104 properties.put(key, Boolean.toString(value));
105 save();
106 firePreferenceChanged(key, Boolean.toString(value));
107 }
108
109
110 private final void firePreferenceChanged(final String key, final String value) {
111 for (final PreferenceChangedListener l : listener)
112 l.preferenceChanged(key, value);
113 }
114
115 /**
116 * Called after every put. In case of a problem, do nothing but output the error
117 * in log.
118 */
119 protected void save() {
120 try {
121 final PrintWriter out = new PrintWriter(new FileWriter(getPreferencesDir() + "preferences"), false);
122 for (final Entry<String, String> e : properties.entrySet())
123 if (!e.getValue().equals(""))
124 out.println(e.getKey() + "=" + e.getValue());
125 out.close();
126 } catch (final IOException e) {
127 e.printStackTrace();
128 // do not message anything, since this can be called from strange
129 // places.
130 }
131 }
132
133 public void load() throws IOException {
134 properties.clear();
135 final BufferedReader in = new BufferedReader(new FileReader(getPreferencesDir()+"preferences"));
136 int lineNumber = 0;
137 for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
138 final int i = line.indexOf('=');
139 if (i == -1 || i == 0)
140 throw new IOException("Malformed config file at line "+lineNumber);
141 properties.put(line.substring(0,i), line.substring(i+1));
142 }
143 }
144
145 public final void resetToDefault() {
146 properties.clear();
147 properties.put("laf", "javax.swing.plaf.metal.MetalLookAndFeel");
148 properties.put("projection", "org.openstreetmap.josm.data.projection.Epsg4326");
149 properties.put("propertiesdialog.visible", "true");
150 properties.put("osm-server.url", "http://www.openstreetmap.org/api");
151 properties.put("color."+marktr("background"), ColorHelper.color2html(Color.black));
152 properties.put("color."+marktr("node"), ColorHelper.color2html(Color.red));
153 properties.put("color."+marktr("segment"), ColorHelper.color2html(SimplePaintVisitor.darkgreen));
154 properties.put("color."+marktr("way"), ColorHelper.color2html(SimplePaintVisitor.darkblue));
155 properties.put("color."+marktr("incomplete way"), ColorHelper.color2html(SimplePaintVisitor.darkerblue));
156 properties.put("color."+marktr("selected"), ColorHelper.color2html(Color.white));
157 properties.put("color."+marktr("gps point"), ColorHelper.color2html(Color.gray));
158 properties.put("color."+marktr("conflict"), ColorHelper.color2html(Color.gray));
159 properties.put("color."+marktr("scale"), ColorHelper.color2html(Color.white));
160 save();
161 }
162
163 public Collection<Bookmark> loadBookmarks() throws IOException {
164 File bookmarkFile = new File(getPreferencesDir()+"bookmarks");
165 if (!bookmarkFile.exists())
166 bookmarkFile.createNewFile();
167 BufferedReader in = new BufferedReader(new FileReader(bookmarkFile));
168
169 Collection<Bookmark> bookmarks = new LinkedList<Bookmark>();
170 for (String line = in.readLine(); line != null; line = in.readLine()) {
171 StringTokenizer st = new StringTokenizer(line, ",");
172 if (st.countTokens() < 5)
173 continue;
174 Bookmark b = new Bookmark();
175 b.name = st.nextToken();
176 try {
177 for (int i = 0; i < b.latlon.length; ++i)
178 b.latlon[i] = Double.parseDouble(st.nextToken());
179 bookmarks.add(b);
180 } catch (NumberFormatException x) {
181 // line not parsed
182 }
183 }
184 in.close();
185 return bookmarks;
186 }
187
188 public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {
189 File bookmarkFile = new File(Main.pref.getPreferencesDir()+"bookmarks");
190 if (!bookmarkFile.exists())
191 bookmarkFile.createNewFile();
192 PrintWriter out = new PrintWriter(new FileWriter(bookmarkFile));
193 for (Bookmark b : bookmarks) {
194 b.name.replace(',', '_');
195 out.print(b.name+",");
196 for (int i = 0; i < b.latlon.length; ++i)
197 out.print(b.latlon[i]+(i<b.latlon.length-1?",":""));
198 out.println();
199 }
200 out.close();
201 }
202}
Note: See TracBrowser for help on using the repository browser.