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. |
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.
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 settingdefaultValue - the defaultValue for the setting
- Throws:
IllegalArgumentException - if the key for this
setting is already contained in the map of default settings
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
Copyright © 2009. All Rights Reserved.