Mercurial > hg > ccmieditor
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/src/uk/ac/qmul/eecs/ccmi/utils/PreferencesService.java Fri Dec 16 17:35:51 2011 +0000 @@ -0,0 +1,91 @@ +/* + CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool + + Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) + Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package uk.ac.qmul.eecs.ccmi.utils; + +import java.util.prefs.Preferences; + +/** + * A service for storing and loading user preferences. + */ +public abstract class PreferencesService +{ + /** + * Gets an instance of the service, suitable for the package of the given class. + * @return an instance of the service + */ + public static PreferencesService getInstance(){ + if (service != null) return service; + try{ + service = new DefaultPreferencesService(); + return service; + } + catch (SecurityException exception){ + // that happens when we run under Web Start + } + + return new NullPreferencesService(); + } + + /** + * Gets a previously stored string from the service. + * @param key the key of the string + * @param defval the value to return if no matching value was found + * @return the value stored with the given key, or defval if none was found + */ + public abstract String get(String key, String defval); + /** + * Saves a key/value pair for later retrieval. + * @param key the key of the string to be stored + * @param value the value to to be stored + */ + public abstract void put(String key, String value); + + private static PreferencesService service; +} + +/** + * The default preferences service that uses the java.util.prefs API. + */ +class DefaultPreferencesService extends PreferencesService{ + /** + * Gets an instance of the service, suitable for the package of the given class. + * @param appClass the main application class (only the package name is used as the path to + * app-specific preferences storage) + * @return an instance of the service + */ + public DefaultPreferencesService() + { + prefs = Preferences.userNodeForPackage(this.getClass()); + } + + public String get(String key, String defval) { return prefs.get(key, defval); } + public void put(String key, String defval) { prefs.put(key, defval); } + + private Preferences prefs; +} + +/** + * The null preferences service that is returned when we are an applet. + */ +class NullPreferencesService extends PreferencesService { + public String get(String key, String defval) { return defval; } + public void put(String key, String defval) { } +} \ No newline at end of file