|
From: <rga...@us...> - 2002-11-03 20:46:53
|
Update of /cvsroot/csms/csms-core/src/java/org/fanfoot/prefs In directory usw-pr-cvs1:/tmp/cvs-serv16995 Added Files: PreferenceManager.java Log Message: Improved handling of preferences. Now uses a separate Preferences manager. The user no longer needs to define parameters in the editor window, they are there by default. If the user has customised any settings they will be retained inbetween installs. --- NEW FILE: PreferenceManager.java --- /* * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Community Sports Management System. * * The Initial Developer of the Original Code is * We know What you Want.net * * Portions created by the Initial Developer are Copyright (C) 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Ross Gardler <ross [at] saafe.org> * Mike Lever <mikel [at] fanfoot.com> * Stuart Gardler <stuart [at] wkwyw.net> * * ***** END LICENSE BLOCK ***** */ /* * PreferenceManager.java */ package org.fanfoot.prefs; import java.awt.Frame; import java.util.logging.Level; import java.util.logging.Logger; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import org.saafe.ui.gui.prefs.PreferencesEditor; /** * Manages the preferences of the current user. * *@author rgardler *@created 02 November 2002 */ public class PreferenceManager { /** User preferences */ Preferences userPrefs = Preferences.userRoot(); /** System preferences */ Preferences systemPrefs = Preferences.systemRoot(); /** Where to write log info */ final Logger logger = Logger.getLogger( CSLAS_NAME ); /** The classname of this class (used by logger) */ final static String CLASS_NAME = "org.fanfoot.prefs.PreferenceManager"; /** String indicating the AutoScorer class of properties */ final static String AUTO_SCORER_CLASS = "org/fanfoot/AutoScorer"; /** Key for AutoScorer EventConfigurationFile path */ final static String AUTO_SCORER_EVENT_CONFIGURATION_FILE_KEY = "EventConfigurationFile"; /** Key for AutoScorer Scores configuration XSL path */ final static String AUTO_SCORER_SCORES_CONFIGURATION_FILE_KEY = "ScoresConfigurationFile"; /** Key for AutoScorer Scores to HTML XSL path */ final static String AUTO_SCORER_SCORES_TO_HTML_STYLESHEET_KEY = "Scores2HTMLStylesheet"; /** Control Centre class name * */ final static String CONTROL_CENTRE_CLASS = "org/fanfoot/gui/ControlCentre"; /** String indicating the GUI CONTROLCENTRE class of properties */ final static String GUI_CONTROL_CENTRE_CLASS = CONTROL_CENTRE_CLASS; /** Key for AutoScorer EventConfigurationFile path */ final static String GUI_CONTROL_CENTRE_DEBUG_KEY = "debug"; /** * Creates a new preferences manager for the version of of the * application indicated. */ public PreferenceManager() { super(); initPrefs(); } /** * Initialise the preferences to the values for this release. If a * user value is already set then this value is retained, unless * there is a conflict between versions. If no user value exists * then the default is added. This is so that the preferencesEditor * contains the necessary preference items. */ private void initPrefs() { initAutoScorerPrefs(); initGUIControlCentrePrefs(); } /** Initialise the preferecnes of class org/fanfoot/AutoScorer */ private void initAutoScorerPrefs() { Preferences prefs = userPrefs.node( AUTO_SCORER_CLASS ); if ( prefs.get( AUTO_SCORER_EVENT_CONFIGURATION_FILE_KEY, null ) == null ) { prefs.put( AUTO_SCORER_EVENT_CONFIGURATION_FILE_KEY, "http://www.fanfoot.com/test/footballEvents.xml" ); } if ( prefs.get( AUTO_SCORER_SCORES_CONFIGURATION_FILE_KEY, null ) == null ) { prefs.put( AUTO_SCORER_SCORES_CONFIGURATION_FILE_KEY, "http://www.fanfoot.com/test/ScoringConfig.xsl" ); } if ( prefs.get( AUTO_SCORER_SCORES_TO_HTML_STYLESHEET_KEY, null ) == null ) { prefs.put( AUTO_SCORER_SCORES_TO_HTML_STYLESHEET_KEY, "http://www.fanfoot.com/test/Scores2HTML.xsl" ); } } /** Initialise the preferecnes of class org/fanfoot/AutoScorer */ private void initGUIControlCentrePrefs() { Preferences prefs = userPrefs.node( GUI_CONTROL_CENTRE_CLASS ); if ( prefs.get( GUI_CONTROL_CENTRE_DEBUG_KEY, null ) == null ) { prefs.put( GUI_CONTROL_CENTRE_DEBUG_KEY, "true" ); } } /** * Get the value corresponding to the supplied node. * *@param strClass the name of the class of preference *@param strName the name of the preference requried *@return Description of the Return Value */ public String get( String strClass, String strName ) { String strResult = null; strResult = userPrefs.node( strClass ).get( strName, null ); return strResult; } /** * Get the default value for the preference indicated for this * version of the software. @ param strKey the key of the * preference requried * *@param strKey Description of the Parameter *@return The default value *@refactor The names of preferences should be in a hashtable or * something as they are duplicated here and in the * org.fanfoot.prefs.gui.preferenceEditor */ public String getDefault( String strKey ) { String strResult = null; logger.fine( "Processing request for " + strKey ); /** The path from which default fiules are retrieved */ String strConfigPath = "http://www.fanfoot.com/test/"; if ( strKey.equals( this.AUTO_SCORER_EVENT_CONFIGURATION_FILE_KEY + this.AUTO_SCORER_SCORES_CONFIGURATION_FILE_KEY ) ) { strResult = strConfigPath + "ScoringConfig.xsl"; } else if ( strKey.equals( this.AUTO_SCORER_CLASS + this.AUTO_SCORER_EVENT_CONFIGURATION_FILE_KEY ) ) { strResult = strConfigPath + "footballEvents.xml"; } else if ( strKey.equals( this.AUTO_SCORER_CLASS + this.AUTO_SCORER_SCORES_TO_HTML_STYLESHEET_KEY ) ) { strResult = strConfigPath + "Scores2HTML.xsl"; } else if ( strKey.equals( "org/fanfoot/gui/ControlCentre/debug" ) ) { strResult = "false"; } logger.fine( "Returning value: " + strResult ); return strResult; } /** * Set the user value corresponding to the supplied key to the * supplied value. * *@param strClass the class of the preferences we are setting *@param strKey Key of the preference to set *@param strValue the value of the preference */ public void set( String strClass, String strKey, String strValue ) { Preferences classPrefs = userPrefs.node( strClass ); classPrefs.put( strKey, strValue ); } /** * Return a graphical editor for the user preferences for the * indicated class. * *@param strClass the class of prefeserence we wish to edit *@param frame the parent frame for the dialog *@return The editorDialog value */ public PreferencesEditor getEditorDialog( String strClass, Frame frame ) { Preferences preferences = userPrefs.node( strClass ); PreferencesEditor pe = null; try { pe = new PreferencesEditor( frame, true, preferences ); } catch ( BackingStoreException bse ) { // FIXME: handle this error gracefully logger.log( Level.SEVERE, "Unable to create preference editor", bse ); } return pe; } } |