Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/utils/PreferencesService.java @ 0:9418ab7b7f3f
Initial import
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Fri, 16 Dec 2011 17:35:51 +0000 |
parents | |
children | d66dd5880081 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9418ab7b7f3f |
---|---|
1 /* | |
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool | |
3 | |
4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) | |
5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
6 | |
7 This program is free software: you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation, either version 3 of the License, or | |
10 (at your option) any later version. | |
11 | |
12 This program is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 package uk.ac.qmul.eecs.ccmi.utils; | |
22 | |
23 import java.util.prefs.Preferences; | |
24 | |
25 /** | |
26 * A service for storing and loading user preferences. | |
27 */ | |
28 public abstract class PreferencesService | |
29 { | |
30 /** | |
31 * Gets an instance of the service, suitable for the package of the given class. | |
32 * @return an instance of the service | |
33 */ | |
34 public static PreferencesService getInstance(){ | |
35 if (service != null) return service; | |
36 try{ | |
37 service = new DefaultPreferencesService(); | |
38 return service; | |
39 } | |
40 catch (SecurityException exception){ | |
41 // that happens when we run under Web Start | |
42 } | |
43 | |
44 return new NullPreferencesService(); | |
45 } | |
46 | |
47 /** | |
48 * Gets a previously stored string from the service. | |
49 * @param key the key of the string | |
50 * @param defval the value to return if no matching value was found | |
51 * @return the value stored with the given key, or defval if none was found | |
52 */ | |
53 public abstract String get(String key, String defval); | |
54 /** | |
55 * Saves a key/value pair for later retrieval. | |
56 * @param key the key of the string to be stored | |
57 * @param value the value to to be stored | |
58 */ | |
59 public abstract void put(String key, String value); | |
60 | |
61 private static PreferencesService service; | |
62 } | |
63 | |
64 /** | |
65 * The default preferences service that uses the java.util.prefs API. | |
66 */ | |
67 class DefaultPreferencesService extends PreferencesService{ | |
68 /** | |
69 * Gets an instance of the service, suitable for the package of the given class. | |
70 * @param appClass the main application class (only the package name is used as the path to | |
71 * app-specific preferences storage) | |
72 * @return an instance of the service | |
73 */ | |
74 public DefaultPreferencesService() | |
75 { | |
76 prefs = Preferences.userNodeForPackage(this.getClass()); | |
77 } | |
78 | |
79 public String get(String key, String defval) { return prefs.get(key, defval); } | |
80 public void put(String key, String defval) { prefs.put(key, defval); } | |
81 | |
82 private Preferences prefs; | |
83 } | |
84 | |
85 /** | |
86 * The null preferences service that is returned when we are an applet. | |
87 */ | |
88 class NullPreferencesService extends PreferencesService { | |
89 public String get(String key, String defval) { return defval; } | |
90 public void put(String key, String defval) { } | |
91 } |