Package net.sf.freecol.common.option
Class OptionGroup
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColSpecObject
-
- net.sf.freecol.common.option.AbstractOption<OptionGroup>
-
- net.sf.freecol.common.option.OptionGroup
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.lang.Comparable<FreeColObject>
,ObjectWithId
,Option<OptionGroup>
,OptionContainer
- Direct Known Subclasses:
ActionManager
,ClientOptions
public class OptionGroup extends AbstractOption<OptionGroup> implements OptionContainer
Used for groupingOption
s.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
editable
Is this option group user editable?private static java.lang.String
EDITABLE_TAG
private static java.util.logging.Logger
logger
private java.util.Map<java.lang.String,Option>
optionMap
A map of all option ids to its option.private java.util.List<Option>
options
The options in this group.static java.lang.String
TAG
private boolean
visible
Should this option group be visible?private static java.lang.String
VISIBLE_TAG
-
Fields inherited from class net.sf.freecol.common.option.AbstractOption
ACTION_TAG, DEFAULT_VALUE_TAG, isDefined
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Constructor Summary
Constructors Constructor Description OptionGroup(java.lang.String id)
Creates a newOptionGroup
.OptionGroup(java.lang.String id, Specification specification)
Creates a newOptionGroup
.OptionGroup(FreeColXMLReader xr, Specification specification)
Creates a newOptionGroup
.OptionGroup(Specification specification)
Creates a newOptionGroup
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Option option)
Adds the given option to this group.private void
addOptionGroup(OptionGroup group)
Helper function to recursively add option group members to the optionMap.OptionGroup
cloneOption()
Clone this option.void
generateChoices()
Generate the choices to provide to the UI.java.lang.String
getName()
Gets the i18n-name of thisOption
.Option
getOption(java.lang.String id)
Get an option in this group (or descendents) by object identifier.<T extends Option>
TgetOption(java.lang.String id, java.lang.Class<T> returnClass)
Get an option in this group (or descendents) by object identifier.java.util.List<Option>
getOptions()
Get the options in this group.java.lang.String
getShortDescription()
Gets the i18n short description of thisOption
.OptionGroup
getValue()
Gets the value of this option.java.lang.String
getXMLTagName()
Get the serialization tag for this object.<T extends Option>
booleanhasOption(java.lang.String id, java.lang.Class<T> returnClass)
Is an option present in the container.boolean
hasOptionGroup()
Does this option group contain any subgroups?boolean
isEditable()
Is this option group editable?boolean
isNullValueOK()
Is null an acceptable value for this option? Override this in subclasses where necessary.boolean
isVisible()
Should this option group be visible?boolean
load(java.io.File file)
Loads the options from the given file.protected boolean
load(FreeColXMLReader xr)
Loads the options from the given reader.static OptionGroup
loadOptionGroup(java.io.File file, java.lang.String optionId, Specification spec)
Load a specific option group from a file by identifier.boolean
merge(Option option, LogBuilder lb)
Merges the givenOption
into this group.void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.void
readChild(FreeColXMLReader xr)
Reads a single child object.void
readChildren(FreeColXMLReader xr)
Reads the children of this object from an XML stream.Option
remove(java.lang.String id)
Remove an option.void
removeAll()
Removes all of theOption
s from thisOptionGroup
.boolean
save(java.io.File file)
Writes the object to the given file.void
setEditable(boolean editable)
Set the editable status of this group.void
setValue(java.lang.String valueString, java.lang.String defaultValueString)
Sets the value of this option from the given string representation.void
setValue(OptionGroup value)
Sets the value of this option.java.lang.String
toString()
protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.protected void
writeChildren(FreeColXMLWriter xw)
Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.common.option.AbstractOption
getEnabledBy, getGroup, readOption, setGroup, setValues
-
Methods inherited from class net.sf.freecol.common.model.FreeColSpecObject
copyIn, getSpecification, setSpecification
-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getClassIndex, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getGame, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hashCode, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, serialize, serialize, serialize, serialize, setGame, setId, toXML, toXML, toXMLPartial, toXMLPartial
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.freecol.common.ObjectWithId
getId
-
Methods inherited from interface net.sf.freecol.common.option.Option
readFromXML, toXML
-
Methods inherited from interface net.sf.freecol.common.option.OptionContainer
getBoolean, getFile, getInteger, getIntegerMinimum, getModList, getOptionGroup, getPercentage, getPercentageMultiplier, getRange, getSelection, getSelectionName, getString, getText, getUnitList, setBoolean, setFile, setInteger, setIntegerMinimum, setString, setText
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
options
private final java.util.List<Option> options
The options in this group.
-
optionMap
private final java.util.Map<java.lang.String,Option> optionMap
A map of all option ids to its option. Unlike the options array, this contains all child options of options that are themselves groups.
-
editable
private boolean editable
Is this option group user editable?
-
visible
private boolean visible
Should this option group be visible?
-
EDITABLE_TAG
private static final java.lang.String EDITABLE_TAG
- See Also:
- Constant Field Values
-
VISIBLE_TAG
private static final java.lang.String VISIBLE_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
OptionGroup
public OptionGroup(java.lang.String id)
Creates a newOptionGroup
.- Parameters:
id
- The object identifier.
-
OptionGroup
public OptionGroup(Specification specification)
Creates a newOptionGroup
.- Parameters:
specification
- TheSpecification
to refer to.
-
OptionGroup
public OptionGroup(java.lang.String id, Specification specification)
Creates a newOptionGroup
.- Parameters:
id
- The object identifier.specification
- TheSpecification
to refer to.
-
OptionGroup
public OptionGroup(FreeColXMLReader xr, Specification specification) throws javax.xml.stream.XMLStreamException
Creates a newOptionGroup
.- Parameters:
xr
- TheFreeColXMLReader
to read from.specification
- TheSpecification
to refer to.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
-
Method Detail
-
isEditable
public boolean isEditable()
Is this option group editable?- Returns:
- True if the option group is editable.
-
setEditable
public void setEditable(boolean editable)
Set the editable status of this group.- Parameters:
editable
- The new editable status.
-
isVisible
public boolean isVisible()
Should this option group be visible?- Returns:
- True if the option group should be visible.
-
getName
public java.lang.String getName()
Gets the i18n-name of thisOption
.- Returns:
- The name as provided in the constructor.
-
getShortDescription
public java.lang.String getShortDescription()
Gets the i18n short description of thisOption
. Should be suitable for use as a tooltip text.- Returns:
- A short description of this
Option
.
-
getOptions
public java.util.List<Option> getOptions()
Get the options in this group.- Returns:
- The list of
Option
s.
-
getOption
public Option getOption(java.lang.String id)
Get an option in this group (or descendents) by object identifier.- Parameters:
id
- The object identifier.- Returns:
- The option, or null if not found.
-
hasOptionGroup
public boolean hasOptionGroup()
Does this option group contain any subgroups?- Returns:
- True if there are any child
OptionGroup
s present.
-
add
public void add(Option option)
Adds the given option to this group. The option is assumed to be correct.- Parameters:
option
- TheOption
to add.
-
merge
public boolean merge(Option option, LogBuilder lb)
Merges the givenOption
into this group. - Option groups are *not* merged, but their leaf options are. - If an option is not already present it is ignored. - If an option is present, it is merged but in the option group where it is already placed. The intent is that the option group structure is never subject to merging.- Parameters:
option
- TheOption
to merge.lb
- ALogBuilder
to log to.- Returns:
- True if the merge was accepted.
-
addOptionGroup
private void addOptionGroup(OptionGroup group)
Helper function to recursively add option group members to the optionMap.- Parameters:
group
- The initialOptionGroup
to add.
-
remove
public Option remove(java.lang.String id)
Remove an option.- Parameters:
id
- The identifier of the option to remove.- Returns:
- The
Option
removed if any.
-
removeAll
public void removeAll()
Removes all of theOption
s from thisOptionGroup
.
-
load
protected boolean load(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Loads the options from the given reader.- Parameters:
xr
- TheFreeColXMLReader
to read from.- Returns:
- True if the options were loaded without error.
- Throws:
javax.xml.stream.XMLStreamException
- if there is an error reading the stream.
-
load
public boolean load(java.io.File file)
Loads the options from the given file.- Parameters:
file
- TheFile
to read the options from.- Returns:
- True if the options were loaded without error.
-
loadOptionGroup
public static OptionGroup loadOptionGroup(java.io.File file, java.lang.String optionId, Specification spec)
Load a specific option group from a file by identifier.- Parameters:
file
- TheFile
to load from.optionId
- The option identifier to look for.spec
- A baseSpecification
.- Returns:
- The
OptionGroup
found, or null on error or not found.
-
hasOption
public <T extends Option> boolean hasOption(java.lang.String id, java.lang.Class<T> returnClass)
Is an option present in the container.- Specified by:
hasOption
in interfaceOptionContainer
- Type Parameters:
T
- The actual return type.- Parameters:
id
- The object identifier.returnClass
- The expected option class.- Returns:
- True if the option is present.
-
getOption
public <T extends Option> T getOption(java.lang.String id, java.lang.Class<T> returnClass)
Get an option in this group (or descendents) by object identifier.- Specified by:
getOption
in interfaceOptionContainer
- Type Parameters:
T
- The actual return type.- Parameters:
id
- The object identifier.returnClass
- The expected option class.- Returns:
- The option, or a run time exception if the option does not exist or is of the wrong class.
-
cloneOption
public OptionGroup cloneOption()
Clone this option.- Specified by:
cloneOption
in interfaceOption<OptionGroup>
- Specified by:
cloneOption
in classAbstractOption<OptionGroup>
- Returns:
- A clone of this option.
-
getValue
public OptionGroup getValue()
Gets the value of this option.- Specified by:
getValue
in interfaceOption<OptionGroup>
- Specified by:
getValue
in classAbstractOption<OptionGroup>
- Returns:
- The value of this
Option
.
-
setValue
public void setValue(OptionGroup value)
Sets the value of this option.- Specified by:
setValue
in interfaceOption<OptionGroup>
- Specified by:
setValue
in classAbstractOption<OptionGroup>
- Parameters:
value
- The new value of thisOption
.
-
setValue
public void setValue(java.lang.String valueString, java.lang.String defaultValueString)
Sets the value of this option from the given string representation. Both parameters must not be null at the same time. This method does nothing. Override it if the option has a suitable string representation.- Overrides:
setValue
in classAbstractOption<OptionGroup>
- Parameters:
valueString
- The string representation of the value of thisOption
.defaultValueString
- The string representation of the default value of thisOption
.
-
generateChoices
public void generateChoices()
Generate the choices to provide to the UI. Override if the subclass needs to determine its choices dynamically.- Overrides:
generateChoices
in classAbstractOption<OptionGroup>
-
isNullValueOK
public boolean isNullValueOK()
Is null an acceptable value for this option? Override this in subclasses where necessary.- Overrides:
isNullValueOK
in classAbstractOption<OptionGroup>
- Returns:
- False.
-
save
public boolean save(java.io.File file)
Writes the object to the given file.- Overrides:
save
in classFreeColObject
- Parameters:
file
- TheFile
to write to.- Returns:
- True if the save proceeded without error.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeAttributes
in classAbstractOption<OptionGroup>
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
writeChildren
protected void writeChildren(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeChildren
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
readAttributes
public void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributes
in classAbstractOption<OptionGroup>
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readChildren
public void readChildren(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the children of this object from an XML stream.- Overrides:
readChildren
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readChild
public void readChild(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.- Overrides:
readChild
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagName
in classFreeColObject
- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Specified by:
toString
in interfaceOption<OptionGroup>
- Overrides:
toString
in classFreeColObject
-
-