wyvern.common.util
Class UserProps

java.lang.Object
  extended bywyvern.common.util.UserProps

public class UserProps
extends java.lang.Object

This class can read a properties file, but also looks for a ".local" version of the file. Maintains the distinction between user settings and default settings. Writes out user settings to a ".local" version of the file, which can be re-read later.

Version:
1.0, Apr 05, 2003
Author:
Steve Yegge

Field Summary
protected  java.util.Properties defaultProps_
           
protected  java.lang.String LOCAL_SETTINGS_DIR
           
protected  java.lang.String LOCAL_SETTINGS_PATH
           
protected  java.util.Properties mergedProps_
           
protected  java.util.Properties userProps_
           
 
Constructor Summary
UserProps(java.lang.String filename)
          Constructs a new UserProps.
 
Method Summary
 void debug(java.lang.String msg)
          Prints debugging message if debugging is on.
 int getIntProperty(java.lang.String name)
          Returns a property with an integer value.
protected  java.lang.String getLocalSettingsPath(java.lang.String filename)
          Gets the absolute path to the .local version of the file.
 java.lang.String getProperty(java.lang.String name)
          Returns true if a game engine property is set.
 java.util.Properties getSettings()
          Returns a Properties containing all the application settings.
 java.util.Properties getUserSettings()
          Returns the set of user-specified overrides.
 boolean isPropertySet(java.lang.String name)
          Returns true if the property is "true", else false.
protected  void loadLocalSettings(java.lang.String localpath)
          Tries to load the ".local" version of the file, containing any user overrides for the default settings.
 void mergeUserSettings()
          Notifies us that someone modified the Properties object (probably by removing one or more properties) returned from getUserSettings().
 void printDefaultProperties()
          Prints default settings, for debugging.
 void printUserProperties()
          Prints current user settings, for debugging.
 void saveUserSettings()
          Saves the current user settings we've stored.
 void saveUserSettings(java.util.Properties props)
          Saves user settings to mapedit.ini.local
 void setIntUserProperty(java.lang.String name, int value)
          Saves an int setting.
 void setProperty(java.lang.String name, java.lang.String value)
          Sets an arbitrary property by name.
 void setUserProperty(java.lang.String name, java.lang.String value)
          Sets a user-override for a property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultProps_

protected java.util.Properties defaultProps_

userProps_

protected java.util.Properties userProps_

mergedProps_

protected java.util.Properties mergedProps_

LOCAL_SETTINGS_PATH

protected final java.lang.String LOCAL_SETTINGS_PATH

LOCAL_SETTINGS_DIR

protected java.lang.String LOCAL_SETTINGS_DIR
Constructor Detail

UserProps

public UserProps(java.lang.String filename)
Constructs a new UserProps. Will try to open the file from disk, and failing that, it'll try to load it from the jar file.

Parameters:
filename - the relative path to the initfile, in the form "wyvern/wherever/myfile.ini".
Method Detail

loadLocalSettings

protected void loadLocalSettings(java.lang.String localpath)
Tries to load the ".local" version of the file, containing any user overrides for the default settings.

Parameters:
localpath - the absolute path to the file

getLocalSettingsPath

protected java.lang.String getLocalSettingsPath(java.lang.String filename)
Gets the absolute path to the .local version of the file.

Parameters:
filename - the relative ("wyvern/whatever/foo.ini") initfile path

getSettings

public java.util.Properties getSettings()
Returns a Properties containing all the application settings. Contains values for any settings that have been overridden by the user.

Returns:
the user-specified configuration settings, plus default values for any the user hasn't overridden.

getUserSettings

public java.util.Properties getUserSettings()
Returns the set of user-specified overrides. Doesn't include settings with default values that the user hasn't overridden.

Returns:
a Properties object containing user settings

saveUserSettings

public void saveUserSettings(java.util.Properties props)
                      throws java.lang.Exception
Saves user settings to mapedit.ini.local

Parameters:
props - the set of user settings to save. ALL user-overridden settings must be in this list. It's OK to have default settings in there as well.
Throws:
an - exception if the file couldn't be written
java.lang.Exception

saveUserSettings

public void saveUserSettings()
                      throws java.lang.Exception
Saves the current user settings we've stored.

Throws:
an - exception if the file couldn't be written
java.lang.Exception

mergeUserSettings

public void mergeUserSettings()
Notifies us that someone modified the Properties object (probably by removing one or more properties) returned from getUserSettings(). We need to reconstruct the consolidated list from the default settings and the new user settings. Doesn't actually save anything to disk. Call saveUserSettings() to write out the modified user values.


getProperty

public java.lang.String getProperty(java.lang.String name)
Returns true if a game engine property is set.

Parameters:
name - the property name
Returns:
the property value, null if it's not set

isPropertySet

public boolean isPropertySet(java.lang.String name)
Returns true if the property is "true", else false.

Parameters:
name - the property name
Returns:
true if the property exists and is set to true, else false.

setProperty

public void setProperty(java.lang.String name,
                        java.lang.String value)
Sets an arbitrary property by name.

Parameters:
name - the property to set
value - the value to set it to

setUserProperty

public void setUserProperty(java.lang.String name,
                            java.lang.String value)
Sets a user-override for a property.


setIntUserProperty

public void setIntUserProperty(java.lang.String name,
                               int value)
Saves an int setting.


getIntProperty

public int getIntProperty(java.lang.String name)
Returns a property with an integer value. Returns user value, if present, or default value if not.

Parameters:
name - the property name
Returns:
the property value, as an int. If the property isn't set, or has a non-integer value, returns -1.

printUserProperties

public void printUserProperties()
Prints current user settings, for debugging.


printDefaultProperties

public void printDefaultProperties()
Prints default settings, for debugging.


debug

public void debug(java.lang.String msg)
Prints debugging message if debugging is on.