net.sf.sfac.setting
Class SubSettingsProxy

java.lang.Object
  extended by net.sf.sfac.setting.SubSettingsProxy
All Implemented Interfaces:
Comparable<SubSettingsProxy>, Settings

public class SubSettingsProxy
extends Object
implements Settings, Comparable<SubSettingsProxy>

Implementation of the Settings interface, allowing to present a subset of the settings as another settings object.
A prefix is added by this proxy to all the keys before to put them in the parent settings. It allows to easily make profiles (= different set of identical settings saved in the same global settings file).

Author:
Olivier Berlanger

Field Summary
static String SUB_SETTING_NAME
           
 
Constructor Summary
SubSettingsProxy(Settings parentSettings, String prefix)
          Construct a new settings implementation bound to a given property file.
SubSettingsProxy(Settings parentSettings, String prefix, int i)
          Construct a new indexed settings implementation bound to a given property file.
 
Method Summary
 void addPropertyChangeListener(String settingKey, PropertyChangeListener listener)
          Add a PropertyChangeListener for a specific setting.
 void clear()
          Remove all the settings corresponding to this proxy from the parent settings.
 int compareTo(SubSettingsProxy other)
           
 boolean containsProperty(String key)
          Check if there is a property value defined for the given key.
 void copyValues(Settings other)
          Copy all the values of the other settings into this one.
 void dump()
           
 boolean equals(Object other)
           
 boolean getBooleanProperty(String key, boolean defaultValue)
          Get a boolean property value.
 Boolean getBooleanProperty(String key, Boolean defaultValue)
          Get a Boolean property value.
 Color getColorProperty(String key, Color defaultValue)
          Get a Color property value.
 Dimension getDimensionProperty(String key, Dimension defaultValue)
          Get a Dimension property value.
 double getDoubleProperty(String key, double defaultValue)
          Get a double property value.
<T extends Enum<T>>
T
getEnumProperty(String key, T defaultValue)
          Get a enumerated property value.
 FilePathUtils getFilePathUtils()
          Get the FilePathUtils used by this settings object.
 String getFileProperty(String key, String defaultValue)
          Get a absolute File or Directory property value.
 String getFileProperty(String key, String defaultValue, boolean absolute)
          Get a File or Directory property value.
 int getIndex()
           
 int[] getIntArrayProperty(String key, int[] defaultValue)
          Get a int array property value.
 Integer getIntegerProperty(String key, Integer defaultValue)
          Get a Integer property value.
 int getIntProperty(String key, int defaultValue)
          Get a int property value.
 Iterator<String> getKeys()
          Return an iterator for all the defined keys.
 String getName()
           
 String getPasswordProperty(String key, String defaultValue)
          Get a String property value to be used as a password.
<T> T
getProperty(String key, TypeHelper<T> helper, T defaultValue)
          Get a property of type defined by the helper.
 String getPropertyAsInternalString(String key)
          Get a property as stored internally by the map (as string).
 String getPropertyFilePath()
          Get the path to the property file used to store the settings.
 Rectangle getRectangleProperty(String key, Rectangle defaultValue)
          Get a Rectangle property value.
 String[] getStringArrayProperty(String key, String[] defaultValue)
          Get a String array property value.
 String getStringProperty(String key, String defaultValue)
          Get a String property value.
 int hashCode()
           
 void load()
          Ask the settings implementation to load the settings values.
 void removeProperty(String key)
          Remove the property.
 void removePropertyChangeListener(String settingKey, PropertyChangeListener listener)
          Remove a PropertyChangeListener for a specific setting.
 void save()
          Ask the settings implementation to save the settings values.
 void saveAs(File fil)
          Save all the settings to a new property file.
 void setBooleanProperty(String key, boolean value)
          Set a new value for a boolean property.
 void setBooleanProperty(String key, Boolean value)
          Set a new value for a Boolean property.
 void setColorProperty(String key, Color value)
          Set a new value for a Color property.
 void setDimensionProperty(String key, Dimension value)
          Set a new value for a Dimension property.
 void setDoubleProperty(String key, double value)
          Set a new value for a double property.
<T extends Enum<T>>
void
setEnumProperty(String key, T value)
          Set a new value for a enumerated property.
 void setFileProperty(String key, String value)
          Set a new value for a File or Directory property.
 void setIntArrayProperty(String key, int[] value)
          Set a new value for a int array property.
 void setIntegerProperty(String key, Integer value)
          Set a new value for a Integer property.
 void setIntProperty(String key, int value)
          Set a new value for a int property.
 void setName(String newName)
           
 void setPasswordProperty(String key, String value)
          Set a new value for a String property.
<T> void
setProperty(String key, TypeHelper<T> helper, T value)
          Set a property of type defined by the helper.
 void setPropertyAsInternalString(String key, String value)
          Set a property value using its internal string representation.
 void setPropertyFile(File fil)
          Set a new property file to store the settings.
 void setRectangleProperty(String key, Rectangle value)
          Set a new value for a Rectangle property.
 void setStringArrayProperty(String key, String[] value)
          Set a new value for a String array property.
 void setStringProperty(String key, String value)
          Set a new value for a String property.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SUB_SETTING_NAME

public static final String SUB_SETTING_NAME
See Also:
Constant Field Values
Constructor Detail

SubSettingsProxy

public SubSettingsProxy(Settings parentSettings,
                        String prefix)
Construct a new settings implementation bound to a given property file.

Parameters:
propertyFile - File where the properties are stored.
descr - Description of properties written as header in properties file.

SubSettingsProxy

public SubSettingsProxy(Settings parentSettings,
                        String prefix,
                        int i)
Construct a new indexed settings implementation bound to a given property file.
Note the index is just added in the key.

Parameters:
propertyFile - File where the properties are stored.
descr - Description of properties written as header in properties file.
i - the index of this SubSettingsProxy (if they are indexed). -1 for non-indexed.
Method Detail

getIndex

public int getIndex()

clear

public void clear()
Remove all the settings corresponding to this proxy from the parent settings.


copyValues

public void copyValues(Settings other)
Copy all the values of the other settings into this one.
This method doesn't care about the current content of this settings, existing values are simply overwritten. If you don't want to keep the current content, don't forget to do a clear() before copying.

Parameters:
other - the other settings containing the values to copy.

dump

public void dump()

toString

public String toString()
Overrides:
toString in class Object

getName

public String getName()

setName

public void setName(String newName)

compareTo

public int compareTo(SubSettingsProxy other)
Specified by:
compareTo in interface Comparable<SubSettingsProxy>

equals

public boolean equals(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

getPropertyAsInternalString

public String getPropertyAsInternalString(String key)
Description copied from interface: Settings
Get a property as stored internally by the map (as string).
This is used to act on the key/values independently actual type of the value (that can be not known yet). The actual format of the string object depends on the implementation.

Specified by:
getPropertyAsInternalString in interface Settings
Parameters:
key - Key for the property value.
Returns:
The property value associated to the given key using the internal string representation. returns null if the property is not defined.

setPropertyAsInternalString

public void setPropertyAsInternalString(String key,
                                        String value)
Description copied from interface: Settings
Set a property value using its internal string representation. Setting a null value will remove the property.

Specified by:
setPropertyAsInternalString in interface Settings
Parameters:
key - Key for the property value.
value - The new value of the property to associate to the given key.

getProperty

public <T> T getProperty(String key,
                         TypeHelper<T> helper,
                         T defaultValue)
Description copied from interface: Settings
Get a property of type defined by the helper. This method is useful when you want to extend this class to new types (by creating new TypeHelper classes). For properties using a pre-defined type you should use the type-specific method.

Note that if you create a new data type the same type helper INSTANCE should be used in all set and get for this type.

Specified by:
getProperty in interface Settings
Parameters:
key - Key for the property value.
helper - Helper identifying the value type and knowing how to convert between string and the requested type. If the helper is null, a string value is returned.
Returns:
The property value associated to the given key and of given type.

setProperty

public <T> void setProperty(String key,
                            TypeHelper<T> helper,
                            T value)
Description copied from interface: Settings
Set a property of type defined by the helper. This method is usefull when you want to extend this class to new types (by creating new TypeHelper classes). For properties using a pre-defined type you should use the type-specific method.
Giving a null value removes the property (values and keys are removed).
If the new value is different than the previous one, a property change event is fired.

Note that if you create a new data type the same type helper INSTANCE should be used in all set and get for this type.

Specified by:
setProperty in interface Settings
Parameters:
key - Key for the property value.
helper - Helper identifying the value type and knowing how to convert between string and the requested type. If the given helper is null, the property will be stored as string (with basic toString conversion in the backing properties map). When the given helper is null, no propertyChange event is generated.
value - The new value of the property to associate to the given key.

getPropertyFilePath

public String getPropertyFilePath()
Description copied from interface: Settings
Get the path to the property file used to store the settings.
The returned path is absolute.

Specified by:
getPropertyFilePath in interface Settings
Returns:
The path to the property file used to store the settings.

getFilePathUtils

public FilePathUtils getFilePathUtils()
Description copied from interface: Settings
Get the FilePathUtils used by this settings object.
This FilePathUtils is used to convert between absolute and relative file path settings. (So it holds the default base path).

Specified by:
getFilePathUtils in interface Settings
Returns:
The FilePathUtils used by this settings object.

load

public void load()
          throws IOException
Description copied from interface: Settings
Ask the settings implementation to load the settings values. (the real effect of it will depend of the concrete Settings implementation.)

Specified by:
load in interface Settings
Throws:
IOException - If something prevents the settings to be loaded.

save

public void save()
          throws IOException
Description copied from interface: Settings
Ask the settings implementation to save the settings values. (the real effect of it will depend of the concrete Settings implementation.)

Specified by:
save in interface Settings
Throws:
IOException - If something prevents the settings to be saved.

setPropertyFile

public void setPropertyFile(File fil)
Description copied from interface: Settings
Set a new property file to store the settings. The given property file will be set as new reference for paths.

Specified by:
setPropertyFile in interface Settings
Parameters:
fil - The new properties file.

saveAs

public void saveAs(File fil)
            throws IOException
Description copied from interface: Settings
Save all the settings to a new property file. If the file don't exists, it is created, if the file exists it is overriden without warning. The given property file will be set as new reference for paths.

Specified by:
saveAs in interface Settings
Parameters:
fil - The new properties file.
Throws:
IOException - If something prevents the settings to be saved.

containsProperty

public boolean containsProperty(String key)
Description copied from interface: Settings
Check if there is a property value defined for the given key.

Specified by:
containsProperty in interface Settings
Parameters:
key - Key for the value.
Returns:
True iff there is a property value defined for the given key.

getStringProperty

public String getStringProperty(String key,
                                String defaultValue)
Description copied from interface: Settings
Get a String property value.

Specified by:
getStringProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setStringProperty

public void setStringProperty(String key,
                              String value)
Description copied from interface: Settings
Set a new value for a String property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setStringProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getPasswordProperty

public String getPasswordProperty(String key,
                                  String defaultValue)
Description copied from interface: Settings
Get a String property value to be used as a password.
The password will be obfuscated (obfuscation is much lighter that encryption, so don't store very sensitive password using this mechanism).

Specified by:
getPasswordProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setPasswordProperty

public void setPasswordProperty(String key,
                                String value)
Description copied from interface: Settings
Set a new value for a String property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value. The password will be obfuscated (obfuscation is much lighter that encryption, so don't store very sensitive password using this mechanism).

Specified by:
setPasswordProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getStringArrayProperty

public String[] getStringArrayProperty(String key,
                                       String[] defaultValue)
Description copied from interface: Settings
Get a String array property value.

Specified by:
getStringArrayProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setStringArrayProperty

public void setStringArrayProperty(String key,
                                   String[] value)
Description copied from interface: Settings
Set a new value for a String array property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setStringArrayProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getIntProperty

public int getIntProperty(String key,
                          int defaultValue)
Description copied from interface: Settings
Get a int property value.

Specified by:
getIntProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setIntProperty

public void setIntProperty(String key,
                           int value)
Description copied from interface: Settings
Set a new value for a int property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setIntProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getIntegerProperty

public Integer getIntegerProperty(String key,
                                  Integer defaultValue)
Description copied from interface: Settings
Get a Integer property value.

Specified by:
getIntegerProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setIntegerProperty

public void setIntegerProperty(String key,
                               Integer value)
Description copied from interface: Settings
Set a new value for a Integer property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setIntegerProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getDoubleProperty

public double getDoubleProperty(String key,
                                double defaultValue)
Description copied from interface: Settings
Get a double property value.

Specified by:
getDoubleProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setDoubleProperty

public void setDoubleProperty(String key,
                              double value)
Description copied from interface: Settings
Set a new value for a double property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setDoubleProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getBooleanProperty

public boolean getBooleanProperty(String key,
                                  boolean defaultValue)
Description copied from interface: Settings
Get a boolean property value.

Specified by:
getBooleanProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setBooleanProperty

public void setBooleanProperty(String key,
                               boolean value)
Description copied from interface: Settings
Set a new value for a boolean property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setBooleanProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getBooleanProperty

public Boolean getBooleanProperty(String key,
                                  Boolean defaultValue)
Description copied from interface: Settings
Get a Boolean property value.

Specified by:
getBooleanProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setBooleanProperty

public void setBooleanProperty(String key,
                               Boolean value)
Description copied from interface: Settings
Set a new value for a Boolean property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setBooleanProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getIntArrayProperty

public int[] getIntArrayProperty(String key,
                                 int[] defaultValue)
Description copied from interface: Settings
Get a int array property value.

Specified by:
getIntArrayProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setIntArrayProperty

public void setIntArrayProperty(String key,
                                int[] value)
Description copied from interface: Settings
Set a new value for a int array property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setIntArrayProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getRectangleProperty

public Rectangle getRectangleProperty(String key,
                                      Rectangle defaultValue)
Description copied from interface: Settings
Get a Rectangle property value.

Specified by:
getRectangleProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setRectangleProperty

public void setRectangleProperty(String key,
                                 Rectangle value)
Description copied from interface: Settings
Set a new value for a Rectangle property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setRectangleProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getDimensionProperty

public Dimension getDimensionProperty(String key,
                                      Dimension defaultValue)
Description copied from interface: Settings
Get a Dimension property value.

Specified by:
getDimensionProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setDimensionProperty

public void setDimensionProperty(String key,
                                 Dimension value)
Description copied from interface: Settings
Set a new value for a Dimension property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setDimensionProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getColorProperty

public Color getColorProperty(String key,
                              Color defaultValue)
Description copied from interface: Settings
Get a Color property value.

Specified by:
getColorProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Returns:
The value associated to the given key (or default value if no value was found).

setColorProperty

public void setColorProperty(String key,
                             Color value)
Description copied from interface: Settings
Set a new value for a Color property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setColorProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getEnumProperty

public <T extends Enum<T>> T getEnumProperty(String key,
                                             T defaultValue)
Description copied from interface: Settings
Get a enumerated property value.

Specified by:
getEnumProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key.
Note: this default value cannot be null, it must be a value of the enumeration.
Returns:
The value associated to the given key (or default value if no value was found).

setEnumProperty

public <T extends Enum<T>> void setEnumProperty(String key,
                                                T value)
Description copied from interface: Settings
Set a new value for a enumerated property.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setEnumProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.

getFileProperty

public String getFileProperty(String key,
                              String defaultValue)
Description copied from interface: Settings
Get a absolute File or Directory property value.

Specified by:
getFileProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key. (if used, the default value will be processed to ensure it's absolute)
Returns:
The value associated to the given key (or default value if no value was found).

getFileProperty

public String getFileProperty(String key,
                              String defaultValue,
                              boolean absolute)
Description copied from interface: Settings
Get a File or Directory property value.

Specified by:
getFileProperty in interface Settings
Parameters:
key - Key for the value.
defaultValue - Default value to be returned if no value is found for the key. (if used, the default value will be processed to ensure it's relative or absolute as requested)
absolute - tells if the returned file should be absolute or relative to the settings base directory.
Returns:
The absolute or relative file path associated to the given key (or default value if no value was found).

setFileProperty

public void setFileProperty(String key,
                            String value)
                     throws InvalidPathException
Description copied from interface: Settings
Set a new value for a File or Directory property.
The specified file can be absolute or relative. If a relative file is specifed, it's taken as relative to the base path of this settings.
This method will fire an propertyChangeEvent to all registered listeners if the value set is different than the current value.

Specified by:
setFileProperty in interface Settings
Parameters:
key - Key for the value.
value - New value to be associated with the key.
Throws:
InvalidPathException - if the file set is invalid.

addPropertyChangeListener

public void addPropertyChangeListener(String settingKey,
                                      PropertyChangeListener listener)
Description copied from interface: Settings
Add a PropertyChangeListener for a specific setting.

Specified by:
addPropertyChangeListener in interface Settings
Parameters:
settingKey - The key of the setting to listen on.
listener - The PropertyChangeListener to be added

removePropertyChangeListener

public void removePropertyChangeListener(String settingKey,
                                         PropertyChangeListener listener)
Description copied from interface: Settings
Remove a PropertyChangeListener for a specific setting.

Specified by:
removePropertyChangeListener in interface Settings
Parameters:
settingKey - The key of the setting that was listened on.
listener - The PropertyChangeListener to be removed

getKeys

public Iterator<String> getKeys()
Description copied from interface: Settings
Return an iterator for all the defined keys.

Specified by:
getKeys in interface Settings
Returns:
an iterator for all the defined keys.

removeProperty

public void removeProperty(String key)
Description copied from interface: Settings
Remove the property.
If the property was not defined, it does nothing.

Specified by:
removeProperty in interface Settings
Parameters:
key - Key of the property to remove.


Copyright © 2012. All Rights Reserved.