fiore@0: /* fiore@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@0: fiore@0: Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) fiore@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: fiore@0: package uk.ac.qmul.eecs.ccmi.utils; fiore@0: fiore@0: import java.util.prefs.Preferences; fiore@0: fiore@0: /** fiore@0: * A service for storing and loading user preferences. fiore@0: */ fiore@0: public abstract class PreferencesService fiore@0: { fiore@0: /** fiore@0: * Gets an instance of the service, suitable for the package of the given class. fiore@0: * @return an instance of the service fiore@0: */ fiore@0: public static PreferencesService getInstance(){ fiore@0: if (service != null) return service; fiore@0: try{ fiore@0: service = new DefaultPreferencesService(); fiore@0: return service; fiore@0: } fiore@0: catch (SecurityException exception){ fiore@5: throw new RuntimeException(exception); fiore@0: } fiore@0: } fiore@0: fiore@0: /** fiore@0: * Gets a previously stored string from the service. fiore@0: * @param key the key of the string fiore@0: * @param defval the value to return if no matching value was found fiore@0: * @return the value stored with the given key, or defval if none was found fiore@0: */ fiore@0: public abstract String get(String key, String defval); fiore@0: /** fiore@0: * Saves a key/value pair for later retrieval. fiore@0: * @param key the key of the string to be stored fiore@0: * @param value the value to to be stored fiore@0: */ fiore@0: public abstract void put(String key, String value); fiore@0: fiore@0: private static PreferencesService service; fiore@0: } fiore@0: fiore@0: /** fiore@0: * The default preferences service that uses the java.util.prefs API. fiore@0: */ fiore@0: class DefaultPreferencesService extends PreferencesService{ fiore@0: fiore@5: public DefaultPreferencesService(){ fiore@5: prefs = Preferences.userNodeForPackage(this.getClass()); fiore@5: } fiore@5: fiore@5: @Override fiore@5: public String get(String key, String defval) { return prefs.get(key, defval); } fiore@5: @Override fiore@5: public void put(String key, String defval) { prefs.put(key, defval); } fiore@5: fiore@5: private Preferences prefs; fiore@0: }