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