LimeWire Consolidated API

org.limewire.setting
Class AbstractSetting

java.lang.Object
  extended by org.limewire.setting.AbstractSetting
All Implemented Interfaces:
Setting
Direct Known Subclasses:
AbstractNumberSetting, BooleanSettingImpl, CharArraySetting, ColorSetting, FileArraySetting, FileSetSetting, FileSetting, FontNameSetting, PasswordSetting, PBooleanArraySetting, PropertiesSetting, StringArraySetting, StringSetSetting, StringSetting

public abstract class AbstractSetting
extends Object
implements Setting

Provides a key-value property as an abstract class. The value is typed in subclasses to avoid casting and ensure your settings are type-safe. The value has a unique key.

When you add a new AbstractSetting subclass, add a public synchronized method to SettingsFactory to create an instance of the setting. For example, subclass IntSetting, SettingsFactory has SettingsFactory.createIntSetting(String, int) and SettingsFactory.createRemoteIntSetting(String, int, String, int, int).

AbstractSetting includes an abstract method to load a String into the key-value property. You are responsible to convert the String to the appropriate type in a subclass.

For example, if your subclass is for an integer setting you can have a integer field i.e. myIntValue, in the class. Then you would set myIntValue with the integer converted String argument, for example:

 
   protected void loadValue(String sValue) {
        try {
            value = Integer.parseInt(sValue.trim());
        } catch(NumberFormatException nfe) {
            revertToDefault();
        }
    }
 

This class, includes fields for the AbstractSetting's visibility (public vs. private) and persistence (always save vs don't save).

Visibility and persistence are just fields for a property; what the field means to your application is up to you. For example, you could give the setting a "don't save" value and when it's time to store the setting to a database, you check the setting and take appropriate actions.

See SettingsFactory for an example of creating an IntSetting object which is a subclass of AbstractSetting . Additionally the example shows how to load and save the setting to disk.


Field Summary
protected  Properties DEFAULT_PROPS
          Protected default Properties instance for subclasses.
protected  String DEFAULT_VALUE
          Constant for the default value for this Setting.
protected  String KEY
          The constant key for this property, specified upon construction.
protected  Properties PROPS
          Protected Properties instance containing properties for any subclasses.
 
Constructor Summary
protected AbstractSetting(Properties defaultProps, Properties props, String key, String defaultValue)
          Constructs a new setting with the specified key and default value.
 
Method Summary
 void addSettingListener(SettingListener l)
          Registers a SettingListener
protected  void fireSettingEvent(SettingEvent.EventType type)
          Fires a SettingEvent
protected  void fireSettingEvent(SettingEvent evt)
          Fires a SettingEvent
 String getKey()
          Get the key for this setting.
 SettingListener[] getSettingListeners()
          Returns all SettingListeners or null of there are none
 String getValueAsString()
          Returns the value as stored in the properties file.
 boolean isDefault()
          Determines whether or not the current value is the default value.
 boolean isPrivate()
          Determines whether or not a setting is private.
protected abstract  void loadValue(String sValue)
          Load value from property string value
 void reload()
          Reload value from properties object
 void removeSettingListener(SettingListener l)
          Removes a SettingListener
 boolean revertToDefault()
          Revert to the default value.
 AbstractSetting setAlwaysSave(boolean alwaysSave)
          Sets whether or not this setting should always save, even if it is default.
 Setting setPrivate(boolean isPrivate)
          Sets whether or not this setting should be reported in bug reports.
protected  void setValueInternal(String value)
          Set new property value
 boolean shouldAlwaysSave()
          Determines whether or not this value should always be saved to disk.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_PROPS

protected final Properties DEFAULT_PROPS
Protected default Properties instance for subclasses.


PROPS

protected final Properties PROPS
Protected Properties instance containing properties for any subclasses.


KEY

protected final String KEY
The constant key for this property, specified upon construction.


DEFAULT_VALUE

protected final String DEFAULT_VALUE
Constant for the default value for this Setting.

Constructor Detail

AbstractSetting

protected AbstractSetting(Properties defaultProps,
                          Properties props,
                          String key,
                          String defaultValue)
Constructs a new setting with the specified key and default value. Private access ensures that only this class can construct new Settings.

Parameters:
key - the key for the setting
defaultValue - the defaultValue for the setting
Throws:
IllegalArgumentException - if the key for this setting is already contained in the map of default settings
Method Detail

addSettingListener

public void addSettingListener(SettingListener l)
Description copied from interface: Setting
Registers a SettingListener

Specified by:
addSettingListener in interface Setting

removeSettingListener

public void removeSettingListener(SettingListener l)
Description copied from interface: Setting
Removes a SettingListener

Specified by:
removeSettingListener in interface Setting

getSettingListeners

public SettingListener[] getSettingListeners()
Description copied from interface: Setting
Returns all SettingListeners or null of there are none

Specified by:
getSettingListeners in interface Setting

reload

public void reload()
Description copied from interface: Setting
Reload value from properties object

Specified by:
reload in interface Setting

revertToDefault

public boolean revertToDefault()
Description copied from interface: Setting
Revert to the default value. It is critically important that the DEFAULT_VALUE is valid, otherwise an infinite loop will be encountered when revertToDefault is called, as invalid values call revertToDefault. Because default values are hard-coded into the program, this is okay.

Specified by:
revertToDefault in interface Setting

shouldAlwaysSave

public boolean shouldAlwaysSave()
Description copied from interface: Setting
Determines whether or not this value should always be saved to disk.

Specified by:
shouldAlwaysSave in interface Setting

setAlwaysSave

public AbstractSetting setAlwaysSave(boolean alwaysSave)
Description copied from interface: Setting
Sets whether or not this setting should always save, even if it is default. Returns this so it can be used during assignment.

Specified by:
setAlwaysSave in interface Setting

setPrivate

public Setting setPrivate(boolean isPrivate)
Description copied from interface: Setting
Sets whether or not this setting should be reported in bug reports.

Specified by:
setPrivate in interface Setting

isPrivate

public boolean isPrivate()
Description copied from interface: Setting
Determines whether or not a setting is private.

Specified by:
isPrivate in interface Setting

isDefault

public boolean isDefault()
Description copied from interface: Setting
Determines whether or not the current value is the default value.

Specified by:
isDefault in interface Setting

getKey

public String getKey()
Description copied from interface: Setting
Get the key for this setting.

Specified by:
getKey in interface Setting

getValueAsString

public String getValueAsString()
Description copied from interface: Setting
Returns the value as stored in the properties file.

Specified by:
getValueAsString in interface Setting

setValueInternal

protected void setValueInternal(String value)
Set new property value

Parameters:
value - new property value NOTE: This is protected so that only this package can update all kinds of settings using a String value. StringSetting updates the access to public.

loadValue

protected abstract void loadValue(String sValue)
Load value from property string value

Parameters:
sValue - property string value

toString

public String toString()
Overrides:
toString in class Object

fireSettingEvent

protected void fireSettingEvent(SettingEvent.EventType type)
Fires a SettingEvent


fireSettingEvent

protected void fireSettingEvent(SettingEvent evt)
Fires a SettingEvent


LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.