1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.io.BufferedReader;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStreamReader;
|
---|
13 | import java.io.OutputStreamWriter;
|
---|
14 | import java.io.PrintWriter;
|
---|
15 | import java.nio.channels.FileChannel;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Arrays;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Collections;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.Properties;
|
---|
23 | import java.util.SortedMap;
|
---|
24 | import java.util.TreeMap;
|
---|
25 | import java.util.Map.Entry;
|
---|
26 | import java.util.regex.Matcher;
|
---|
27 | import java.util.regex.Pattern;
|
---|
28 |
|
---|
29 | import javax.swing.JOptionPane;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.ProxyPreferences;
|
---|
33 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * This class holds all preferences for JOSM.
|
---|
37 | *
|
---|
38 | * Other classes can register their beloved properties here. All properties will be
|
---|
39 | * saved upon set-access.
|
---|
40 | *
|
---|
41 | * @author imi
|
---|
42 | */
|
---|
43 | public class Preferences {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Internal storage for the preference directory.
|
---|
47 | * Do not access this variable directly!
|
---|
48 | * @see #getPreferencesDirFile()
|
---|
49 | */
|
---|
50 | private File preferencesDirFile = null;
|
---|
51 |
|
---|
52 | public static interface PreferenceChangedListener {
|
---|
53 | void preferenceChanged(String key, String newValue);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Class holding one bookmarkentry.
|
---|
58 | * @author imi
|
---|
59 | */
|
---|
60 | public static class Bookmark implements Comparable<Bookmark> {
|
---|
61 | private String name;
|
---|
62 | private Bounds area;
|
---|
63 |
|
---|
64 | public Bookmark() {
|
---|
65 | area = null;
|
---|
66 | name = null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public Bookmark(Bounds area) {
|
---|
70 | this.area = area;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override public String toString() {
|
---|
74 | return name;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public int compareTo(Bookmark b) {
|
---|
78 | return name.toLowerCase().compareTo(b.name.toLowerCase());
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Bounds getArea() {
|
---|
82 | return area;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public String getName() {
|
---|
86 | return name;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void setName(String name) {
|
---|
90 | this.name = name;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void setArea(Bounds area) {
|
---|
94 | this.area = area;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public final ArrayList<PreferenceChangedListener> listener = new ArrayList<PreferenceChangedListener>();
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Map the property name to the property object.
|
---|
102 | */
|
---|
103 | protected final SortedMap<String, String> properties = new TreeMap<String, String>();
|
---|
104 | protected final SortedMap<String, String> defaults = new TreeMap<String, String>();
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Override some values on read. This is intended to be used for technology previews
|
---|
108 | * where we want to temporarily modify things without changing the user's preferences
|
---|
109 | * file.
|
---|
110 | */
|
---|
111 | protected static final SortedMap<String, String> override = new TreeMap<String, String>();
|
---|
112 | static {
|
---|
113 | //override.put("osm-server.version", "0.5");
|
---|
114 | //override.put("osm-server.additional-versions", "");
|
---|
115 | //override.put("osm-server.url", "http://openstreetmap.gryph.de/api");
|
---|
116 | //override.put("plugins", null);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Return the location of the user defined preferences file
|
---|
121 | */
|
---|
122 | public String getPreferencesDir() {
|
---|
123 | final String path = getPreferencesDirFile().getPath();
|
---|
124 | if (path.endsWith(File.separator))
|
---|
125 | return path;
|
---|
126 | return path + File.separator;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public File getPreferencesDirFile() {
|
---|
130 | if (preferencesDirFile != null)
|
---|
131 | return preferencesDirFile;
|
---|
132 | String path;
|
---|
133 | path = System.getProperty("josm.home");
|
---|
134 | if (path != null) {
|
---|
135 | preferencesDirFile = new File(path);
|
---|
136 | } else {
|
---|
137 | path = System.getenv("APPDATA");
|
---|
138 | if (path != null) {
|
---|
139 | preferencesDirFile = new File(path, "JOSM");
|
---|
140 | } else {
|
---|
141 | preferencesDirFile = new File(System.getProperty("user.home"), ".josm");
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return preferencesDirFile;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public File getPreferenceFile() {
|
---|
148 | return new File(getPreferencesDirFile(), "preferences");
|
---|
149 | }
|
---|
150 |
|
---|
151 | public File getPluginsDirFile() {
|
---|
152 | return new File(getPreferencesDirFile(), "plugins");
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @return A list of all existing directories where resources could be stored.
|
---|
157 | */
|
---|
158 | public Collection<String> getAllPossiblePreferenceDirs() {
|
---|
159 | LinkedList<String> locations = new LinkedList<String>();
|
---|
160 | locations.add(Main.pref.getPreferencesDir());
|
---|
161 | String s;
|
---|
162 | if ((s = System.getenv("JOSM_RESOURCES")) != null) {
|
---|
163 | if (!s.endsWith(File.separator)) {
|
---|
164 | s = s + File.separator;
|
---|
165 | }
|
---|
166 | locations.add(s);
|
---|
167 | }
|
---|
168 | if ((s = System.getProperty("josm.resources")) != null) {
|
---|
169 | if (!s.endsWith(File.separator)) {
|
---|
170 | s = s + File.separator;
|
---|
171 | }
|
---|
172 | locations.add(s);
|
---|
173 | }
|
---|
174 | String appdata = System.getenv("APPDATA");
|
---|
175 | if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
|
---|
176 | && appdata.lastIndexOf(File.separator) != -1) {
|
---|
177 | appdata = appdata.substring(appdata.lastIndexOf(File.separator));
|
---|
178 | locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
|
---|
179 | appdata), "JOSM").getPath());
|
---|
180 | }
|
---|
181 | locations.add("/usr/local/share/josm/");
|
---|
182 | locations.add("/usr/local/lib/josm/");
|
---|
183 | locations.add("/usr/share/josm/");
|
---|
184 | locations.add("/usr/lib/josm/");
|
---|
185 | return locations;
|
---|
186 | }
|
---|
187 |
|
---|
188 | synchronized public boolean hasKey(final String key) {
|
---|
189 | return override.containsKey(key) ? override.get(key) != null : properties.containsKey(key);
|
---|
190 | }
|
---|
191 |
|
---|
192 | synchronized public String get(final String key) {
|
---|
193 | putDefault(key, null);
|
---|
194 | if (override.containsKey(key))
|
---|
195 | return override.get(key);
|
---|
196 | if (!properties.containsKey(key))
|
---|
197 | return "";
|
---|
198 | return properties.get(key);
|
---|
199 | }
|
---|
200 |
|
---|
201 | synchronized public String get(final String key, final String def) {
|
---|
202 | putDefault(key, def);
|
---|
203 | if (override.containsKey(key))
|
---|
204 | return override.get(key);
|
---|
205 | final String prop = properties.get(key);
|
---|
206 | if (prop == null || prop.equals(""))
|
---|
207 | return def;
|
---|
208 | return prop;
|
---|
209 | }
|
---|
210 |
|
---|
211 | synchronized public Map<String, String> getAllPrefix(final String prefix) {
|
---|
212 | final Map<String,String> all = new TreeMap<String,String>();
|
---|
213 | for (final Entry<String,String> e : properties.entrySet())
|
---|
214 | if (e.getKey().startsWith(prefix)) {
|
---|
215 | all.put(e.getKey(), e.getValue());
|
---|
216 | }
|
---|
217 | for (final Entry<String,String> e : override.entrySet())
|
---|
218 | if (e.getKey().startsWith(prefix))
|
---|
219 | if (e.getValue() == null) {
|
---|
220 | all.remove(e.getKey());
|
---|
221 | } else {
|
---|
222 | all.put(e.getKey(), e.getValue());
|
---|
223 | }
|
---|
224 | return all;
|
---|
225 | }
|
---|
226 |
|
---|
227 | synchronized public TreeMap<String, String> getAllColors() {
|
---|
228 | final TreeMap<String,String> all = new TreeMap<String,String>();
|
---|
229 | for (final Entry<String,String> e : defaults.entrySet())
|
---|
230 | if (e.getKey().startsWith("color.") && e.getValue() != null) {
|
---|
231 | all.put(e.getKey().substring(6), e.getValue());
|
---|
232 | }
|
---|
233 | for (final Entry<String,String> e : properties.entrySet())
|
---|
234 | if (e.getKey().startsWith("color.")) {
|
---|
235 | all.put(e.getKey().substring(6), e.getValue());
|
---|
236 | }
|
---|
237 | for (final Entry<String,String> e : override.entrySet())
|
---|
238 | if (e.getKey().startsWith("color."))
|
---|
239 | if (e.getValue() == null) {
|
---|
240 | all.remove(e.getKey().substring(6));
|
---|
241 | } else {
|
---|
242 | all.put(e.getKey().substring(6), e.getValue());
|
---|
243 | }
|
---|
244 | return all;
|
---|
245 | }
|
---|
246 |
|
---|
247 | synchronized public Map<String, String> getDefaults() {
|
---|
248 | return defaults;
|
---|
249 | }
|
---|
250 |
|
---|
251 | synchronized public void putDefault(final String key, final String def) {
|
---|
252 | if(!defaults.containsKey(key) || defaults.get(key) == null) {
|
---|
253 | defaults.put(key, def);
|
---|
254 | } else if(def != null && !defaults.get(key).equals(def)) {
|
---|
255 | System.out.println("Defaults for " + key + " differ: " + def + " != " + defaults.get(key));
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | synchronized public boolean getBoolean(final String key) {
|
---|
260 | putDefault(key, null);
|
---|
261 | if (override.containsKey(key))
|
---|
262 | return override.get(key) == null ? false : Boolean.parseBoolean(override.get(key));
|
---|
263 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : false;
|
---|
264 | }
|
---|
265 |
|
---|
266 | synchronized public boolean getBoolean(final String key, final boolean def) {
|
---|
267 | putDefault(key, Boolean.toString(def));
|
---|
268 | if (override.containsKey(key))
|
---|
269 | return override.get(key) == null ? def : Boolean.parseBoolean(override.get(key));
|
---|
270 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
|
---|
271 | }
|
---|
272 |
|
---|
273 | synchronized public boolean put(final String key, String value) {
|
---|
274 | String oldvalue = properties.get(key);
|
---|
275 | if(value != null && value.length() == 0) {
|
---|
276 | value = null;
|
---|
277 | }
|
---|
278 | if(!((oldvalue == null && (value == null || value.equals(defaults.get(key))))
|
---|
279 | || (value != null && oldvalue != null && oldvalue.equals(value))))
|
---|
280 | {
|
---|
281 | if (value == null) {
|
---|
282 | properties.remove(key);
|
---|
283 | } else {
|
---|
284 | properties.put(key, value);
|
---|
285 | }
|
---|
286 | try {
|
---|
287 | save();
|
---|
288 | } catch(IOException e){
|
---|
289 | System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
290 | }
|
---|
291 | firePreferenceChanged(key, value);
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 | return false;
|
---|
295 | }
|
---|
296 |
|
---|
297 | synchronized public boolean put(final String key, final boolean value) {
|
---|
298 | return put(key, Boolean.toString(value));
|
---|
299 | }
|
---|
300 |
|
---|
301 | synchronized public boolean putInteger(final String key, final Integer value) {
|
---|
302 | return put(key, Integer.toString(value));
|
---|
303 | }
|
---|
304 |
|
---|
305 | synchronized public boolean putDouble(final String key, final Double value) {
|
---|
306 | return put(key, Double.toString(value));
|
---|
307 | }
|
---|
308 |
|
---|
309 | synchronized public boolean putLong(final String key, final Long value) {
|
---|
310 | return put(key, Long.toString(value));
|
---|
311 | }
|
---|
312 |
|
---|
313 | private final void firePreferenceChanged(final String key, final String value) {
|
---|
314 | for (final PreferenceChangedListener l : listener) {
|
---|
315 | l.preferenceChanged(key, value);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Called after every put. In case of a problem, do nothing but output the error
|
---|
321 | * in log.
|
---|
322 | */
|
---|
323 | public void save() throws IOException {
|
---|
324 | /* currently unused, but may help to fix configuration issues in future */
|
---|
325 | putInteger("josm.version", Version.getInstance().getVersion());
|
---|
326 |
|
---|
327 | updateSystemProperties();
|
---|
328 | File prefFile = new File(getPreferencesDirFile(), "preferences");
|
---|
329 |
|
---|
330 | // Backup old preferences if there are old preferences
|
---|
331 | if(prefFile.exists()) {
|
---|
332 | copyFile(prefFile, new File(prefFile + "_backup"));
|
---|
333 | }
|
---|
334 |
|
---|
335 | final PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
---|
336 | new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
|
---|
337 | for (final Entry<String, String> e : properties.entrySet()) {
|
---|
338 | String s = defaults.get(e.getKey());
|
---|
339 | /* don't save default values */
|
---|
340 | if(s == null || !s.equals(e.getValue())) {
|
---|
341 | out.println(e.getKey() + "=" + e.getValue());
|
---|
342 | }
|
---|
343 | }
|
---|
344 | out.close();
|
---|
345 |
|
---|
346 | File tmpFile = new File(prefFile + "_tmp");
|
---|
347 | copyFile(tmpFile, prefFile);
|
---|
348 | tmpFile.delete();
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * Simple file copy function that will overwrite the target file
|
---|
353 | * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)
|
---|
354 | * @param in
|
---|
355 | * @param out
|
---|
356 | * @throws IOException
|
---|
357 | */
|
---|
358 | public static void copyFile(File in, File out) throws IOException {
|
---|
359 | FileChannel inChannel = new FileInputStream(in).getChannel();
|
---|
360 | FileChannel outChannel = new FileOutputStream(out).getChannel();
|
---|
361 | try {
|
---|
362 | inChannel.transferTo(0, inChannel.size(),
|
---|
363 | outChannel);
|
---|
364 | }
|
---|
365 | catch (IOException e) {
|
---|
366 | throw e;
|
---|
367 | }
|
---|
368 | finally {
|
---|
369 | if (inChannel != null) {
|
---|
370 | inChannel.close();
|
---|
371 | }
|
---|
372 | if (outChannel != null) {
|
---|
373 | outChannel.close();
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | public void load() throws IOException {
|
---|
380 | properties.clear();
|
---|
381 | final BufferedReader in = new BufferedReader(new InputStreamReader(
|
---|
382 | new FileInputStream(getPreferencesDir()+"preferences"), "utf-8"));
|
---|
383 | int lineNumber = 0;
|
---|
384 | ArrayList<Integer> errLines = new ArrayList<Integer>();
|
---|
385 | for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
|
---|
386 | final int i = line.indexOf('=');
|
---|
387 | if (i == -1 || i == 0) {
|
---|
388 | errLines.add(lineNumber);
|
---|
389 | continue;
|
---|
390 | }
|
---|
391 | properties.put(line.substring(0,i), line.substring(i+1));
|
---|
392 | }
|
---|
393 | if (!errLines.isEmpty())
|
---|
394 | throw new IOException(tr("Malformed config file at lines {0}", errLines));
|
---|
395 | updateSystemProperties();
|
---|
396 | }
|
---|
397 |
|
---|
398 | public void init(boolean reset){
|
---|
399 | // get the preferences.
|
---|
400 | File prefDir = getPreferencesDirFile();
|
---|
401 | if (prefDir.exists()) {
|
---|
402 | if(!prefDir.isDirectory()) {
|
---|
403 | System.err.println(tr("Warning: Failed to initialize preferences. Preference directory ''{0}'' isn't a directory.", prefDir.getAbsoluteFile()));
|
---|
404 | JOptionPane.showMessageDialog(
|
---|
405 | Main.parent,
|
---|
406 | tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' isn't a directory.</html>", prefDir.getAbsoluteFile()),
|
---|
407 | tr("Error"),
|
---|
408 | JOptionPane.ERROR_MESSAGE
|
---|
409 | );
|
---|
410 | return;
|
---|
411 | }
|
---|
412 | } else {
|
---|
413 | if (! prefDir.mkdirs()) {
|
---|
414 | System.err.println(tr("Warning: Failed to initialize preferences. Failed to create missing preference directory: {0}", prefDir.getAbsoluteFile()));
|
---|
415 | JOptionPane.showMessageDialog(
|
---|
416 | Main.parent,
|
---|
417 | tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",prefDir.getAbsoluteFile()),
|
---|
418 | tr("Error"),
|
---|
419 | JOptionPane.ERROR_MESSAGE
|
---|
420 | );
|
---|
421 | return;
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | File preferenceFile = getPreferenceFile();
|
---|
426 | try {
|
---|
427 | if (!preferenceFile.exists()) {
|
---|
428 | System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
429 | resetToDefault();
|
---|
430 | save();
|
---|
431 | } else if (reset) {
|
---|
432 | System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
433 | resetToDefault();
|
---|
434 | save();
|
---|
435 | }
|
---|
436 | } catch(IOException e) {
|
---|
437 | e.printStackTrace();
|
---|
438 | JOptionPane.showMessageDialog(
|
---|
439 | Main.parent,
|
---|
440 | tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",getPreferenceFile().getAbsoluteFile()),
|
---|
441 | tr("Error"),
|
---|
442 | JOptionPane.ERROR_MESSAGE
|
---|
443 | );
|
---|
444 | return;
|
---|
445 | }
|
---|
446 | try {
|
---|
447 | load();
|
---|
448 | } catch (IOException e) {
|
---|
449 | e.printStackTrace();
|
---|
450 | File backupFile = new File(prefDir,"preferences.bak");
|
---|
451 | JOptionPane.showMessageDialog(
|
---|
452 | Main.parent,
|
---|
453 | tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> and creating a new default preference file.</html>", backupFile.getAbsoluteFile()),
|
---|
454 | tr("Error"),
|
---|
455 | JOptionPane.ERROR_MESSAGE
|
---|
456 | );
|
---|
457 | preferenceFile.renameTo(backupFile);
|
---|
458 | try {
|
---|
459 | resetToDefault();
|
---|
460 | save();
|
---|
461 | } catch(IOException e1) {
|
---|
462 | e1.printStackTrace();
|
---|
463 | System.err.println(tr("Warning: Failed to initialize preferences.Failed to reset preference file to default: {0}", getPreferenceFile()));
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | public final void resetToDefault(){
|
---|
469 | properties.clear();
|
---|
470 | }
|
---|
471 |
|
---|
472 | public File getBookmarksFile() {
|
---|
473 | return new File(getPreferencesDir(),"bookmarks");
|
---|
474 | }
|
---|
475 |
|
---|
476 | public Collection<Bookmark> loadBookmarks() throws IOException {
|
---|
477 | File bookmarkFile = getBookmarksFile();
|
---|
478 | if (!bookmarkFile.exists()) {
|
---|
479 | bookmarkFile.createNewFile();
|
---|
480 | }
|
---|
481 | BufferedReader in = new BufferedReader(new InputStreamReader(
|
---|
482 | new FileInputStream(bookmarkFile), "utf-8"));
|
---|
483 |
|
---|
484 | LinkedList<Bookmark> bookmarks = new LinkedList<Bookmark>();
|
---|
485 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
486 | // FIXME: legacy code using ',' sign, should be \u001e only
|
---|
487 | Matcher m = Pattern.compile("^(.+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)[,\u001e](-?\\d+.\\d+)$").matcher(line);
|
---|
488 | if (!m.matches() || m.groupCount() != 5) {
|
---|
489 | System.err.println(tr("Error: Unexpected line ''{0}'' in bookmark file ''{1}''",line, bookmarkFile.toString()));
|
---|
490 | continue;
|
---|
491 | }
|
---|
492 | Bookmark b = new Bookmark();
|
---|
493 | b.setName(m.group(1));
|
---|
494 | double[] values= new double[4];
|
---|
495 | for (int i = 0; i < 4; ++i) {
|
---|
496 | try {
|
---|
497 | values[i] = Double.parseDouble(m.group(i+2));
|
---|
498 | } catch(NumberFormatException e) {
|
---|
499 | System.err.println(tr("Error: Illegal double value ''{0}'' on line ''{1}'' in bookmark file ''{2}''",m.group(i+2),line, bookmarkFile.toString()));
|
---|
500 | continue;
|
---|
501 | }
|
---|
502 | }
|
---|
503 | b.setArea(new Bounds(values));
|
---|
504 | bookmarks.add(b);
|
---|
505 | }
|
---|
506 | in.close();
|
---|
507 | Collections.sort(bookmarks);
|
---|
508 | return bookmarks;
|
---|
509 | }
|
---|
510 |
|
---|
511 | public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {
|
---|
512 | File bookmarkFile = new File(Main.pref.getPreferencesDir()+"bookmarks");
|
---|
513 | if (!bookmarkFile.exists()) {
|
---|
514 | bookmarkFile.createNewFile();
|
---|
515 | }
|
---|
516 | PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
---|
517 | new FileOutputStream(bookmarkFile), "utf-8"));
|
---|
518 | for (Bookmark b : bookmarks) {
|
---|
519 | out.print(b.getName()+ "\u001e");
|
---|
520 | Bounds area = b.getArea();
|
---|
521 | out.print(area.getMin().lat() +"\u001e");
|
---|
522 | out.print(area.getMin().lon() +"\u001e");
|
---|
523 | out.print(area.getMax().lat() +"\u001e");
|
---|
524 | out.print(area.getMax().lon());
|
---|
525 | out.println();
|
---|
526 | }
|
---|
527 | out.close();
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Convenience method for accessing colour preferences.
|
---|
532 | *
|
---|
533 | * @param colName name of the colour
|
---|
534 | * @param def default value
|
---|
535 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
536 | */
|
---|
537 | synchronized public Color getColor(String colName, Color def) {
|
---|
538 | return getColor(colName, null, def);
|
---|
539 | }
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Convenience method for accessing colour preferences.
|
---|
543 | *
|
---|
544 | * @param colName name of the colour
|
---|
545 | * @param specName name of the special colour settings
|
---|
546 | * @param def default value
|
---|
547 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
548 | */
|
---|
549 | synchronized public Color getColor(String colName, String specName, Color def) {
|
---|
550 | putDefault("color."+colName, ColorHelper.color2html(def));
|
---|
551 | String colStr = specName != null ? get("color."+specName) : "";
|
---|
552 | if(colStr.equals("")) {
|
---|
553 | colStr = get("color."+colName);
|
---|
554 | }
|
---|
555 | return colStr.equals("") ? def : ColorHelper.html2color(colStr);
|
---|
556 | }
|
---|
557 |
|
---|
558 | synchronized public Color getDefaultColor(String colName) {
|
---|
559 | String colStr = defaults.get("color."+colName);
|
---|
560 | return colStr.equals("") ? null : ColorHelper.html2color(colStr);
|
---|
561 | }
|
---|
562 |
|
---|
563 | synchronized public boolean putColor(String colName, Color val) {
|
---|
564 | return put("color."+colName, val != null ? ColorHelper.color2html(val) : null);
|
---|
565 | }
|
---|
566 |
|
---|
567 | synchronized public int getInteger(String key, int def) {
|
---|
568 | putDefault(key, Integer.toString(def));
|
---|
569 | String v = get(key);
|
---|
570 | if(null == v)
|
---|
571 | return def;
|
---|
572 |
|
---|
573 | try {
|
---|
574 | return Integer.parseInt(v);
|
---|
575 | } catch(NumberFormatException e) {
|
---|
576 | // fall out
|
---|
577 | }
|
---|
578 | return def;
|
---|
579 | }
|
---|
580 |
|
---|
581 | synchronized public long getLong(String key, long def) {
|
---|
582 | putDefault(key, Long.toString(def));
|
---|
583 | String v = get(key);
|
---|
584 | if(null == v)
|
---|
585 | return def;
|
---|
586 |
|
---|
587 | try {
|
---|
588 | return Long.parseLong(v);
|
---|
589 | } catch(NumberFormatException e) {
|
---|
590 | // fall out
|
---|
591 | }
|
---|
592 | return def;
|
---|
593 | }
|
---|
594 |
|
---|
595 | synchronized public double getDouble(String key, double def) {
|
---|
596 | putDefault(key, Double.toString(def));
|
---|
597 | String v = get(key);
|
---|
598 | if(null == v)
|
---|
599 | return def;
|
---|
600 |
|
---|
601 | try {
|
---|
602 | return Double.parseDouble(v);
|
---|
603 | } catch(NumberFormatException e) {
|
---|
604 | // fall out
|
---|
605 | }
|
---|
606 | return def;
|
---|
607 | }
|
---|
608 |
|
---|
609 | synchronized public double getDouble(String key, String def) {
|
---|
610 | putDefault(key, def);
|
---|
611 | String v = get(key);
|
---|
612 | if(v != null && v.length() != 0) {
|
---|
613 | try { return Double.parseDouble(v); } catch(NumberFormatException e) {}
|
---|
614 | }
|
---|
615 | try { return Double.parseDouble(def); } catch(NumberFormatException e) {}
|
---|
616 | return 0.0;
|
---|
617 | }
|
---|
618 |
|
---|
619 | synchronized public Collection<String> getCollection(String key, Collection<String> def) {
|
---|
620 | String s = get(key);
|
---|
621 | if(def != null)
|
---|
622 | {
|
---|
623 | String d = null;
|
---|
624 | for(String a : def)
|
---|
625 | {
|
---|
626 | if(d != null) {
|
---|
627 | d += "\u001e" + a;
|
---|
628 | } else {
|
---|
629 | d = a;
|
---|
630 | }
|
---|
631 | }
|
---|
632 | putDefault(key, d);
|
---|
633 | }
|
---|
634 | if(s != null && s.length() != 0)
|
---|
635 | {
|
---|
636 | if(s.indexOf("\u001e") < 0) /* FIXME: legacy code, remove later */
|
---|
637 | {
|
---|
638 | String r =s;
|
---|
639 | if(r.indexOf("§§§") > 0) {
|
---|
640 | r = r.replaceAll("§§§","\u001e");
|
---|
641 | } else {
|
---|
642 | r = r.replace(';','\u001e');
|
---|
643 | }
|
---|
644 | if(!r.equals(s)) /* save the converted string */
|
---|
645 | {
|
---|
646 | put(key,r);
|
---|
647 | s = r;
|
---|
648 | }
|
---|
649 | }
|
---|
650 | return Arrays.asList(s.split("\u001e"));
|
---|
651 | }
|
---|
652 | return def;
|
---|
653 | }
|
---|
654 | synchronized public void removeFromCollection(String key, String value) {
|
---|
655 | ArrayList<String> a = new ArrayList<String>(getCollection(key, null));
|
---|
656 | if(a != null)
|
---|
657 | {
|
---|
658 | a.remove(value);
|
---|
659 | putCollection(key, a);
|
---|
660 | }
|
---|
661 | }
|
---|
662 | synchronized public boolean putCollection(String key, Collection<String> val) {
|
---|
663 | String s = null;
|
---|
664 | if(val != null)
|
---|
665 | {
|
---|
666 | for(String a : val)
|
---|
667 | {
|
---|
668 | if(s != null) {
|
---|
669 | s += "\u001e" + a;
|
---|
670 | } else {
|
---|
671 | s = a;
|
---|
672 | }
|
---|
673 | }
|
---|
674 | }
|
---|
675 | return put(key, s);
|
---|
676 | }
|
---|
677 |
|
---|
678 | /**
|
---|
679 | * Updates system properties with the current values in the preferences.
|
---|
680 | *
|
---|
681 | */
|
---|
682 | public void updateSystemProperties() {
|
---|
683 | Properties sysProp = System.getProperties();
|
---|
684 | if (getBoolean(ProxyPreferences.PROXY_ENABLE)) {
|
---|
685 | sysProp.put("proxySet", "true");
|
---|
686 | sysProp.put("http.proxyHost", get(ProxyPreferences.PROXY_HOST));
|
---|
687 | sysProp.put("proxyPort", get(ProxyPreferences.PROXY_PORT));
|
---|
688 | if (!getBoolean(ProxyPreferences.PROXY_ANONYMOUS)) {
|
---|
689 | sysProp.put("proxyUser", get(ProxyPreferences.PROXY_USER));
|
---|
690 | sysProp.put("proxyPassword", get(ProxyPreferences.PROXY_PASS));
|
---|
691 | }
|
---|
692 |
|
---|
693 | }
|
---|
694 | sysProp.put("http.agent", Version.getInstance().getAgentString());
|
---|
695 | System.setProperties(sysProp);
|
---|
696 | }
|
---|
697 | }
|
---|