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