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 groupingOptions.
-
-
Field Summary
Fields Modifier and Type Field Description private booleaneditableIs this option group user editable?private static java.lang.StringEDITABLE_TAGprivate static java.util.logging.Loggerloggerprivate java.util.Map<java.lang.String,Option>optionMapA map of all option ids to its option.private java.util.List<Option>optionsThe options in this group.static java.lang.StringTAGprivate booleanvisibleShould this option group be visible?private static java.lang.StringVISIBLE_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 voidadd(Option option)Adds the given option to this group.private voidaddOptionGroup(OptionGroup group)Helper function to recursively add option group members to the optionMap.OptionGroupcloneOption()Clone this option.voidgenerateChoices()Generate the choices to provide to the UI.java.lang.StringgetName()Gets the i18n-name of thisOption.OptiongetOption(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.StringgetShortDescription()Gets the i18n short description of thisOption.OptionGroupgetValue()Gets the value of this option.java.lang.StringgetXMLTagName()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.booleanhasOptionGroup()Does this option group contain any subgroups?booleanisEditable()Is this option group editable?booleanisNullValueOK()Is null an acceptable value for this option? Override this in subclasses where necessary.booleanisVisible()Should this option group be visible?booleanload(java.io.File file)Loads the options from the given file.protected booleanload(FreeColXMLReader xr)Loads the options from the given reader.static OptionGrouploadOptionGroup(java.io.File file, java.lang.String optionId, Specification spec)Load a specific option group from a file by identifier.booleanmerge(Option option, LogBuilder lb)Merges the givenOptioninto this group.voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.voidreadChild(FreeColXMLReader xr)Reads a single child object.voidreadChildren(FreeColXMLReader xr)Reads the children of this object from an XML stream.Optionremove(java.lang.String id)Remove an option.voidremoveAll()Removes all of theOptions from thisOptionGroup.booleansave(java.io.File file)Writes the object to the given file.voidsetEditable(boolean editable)Set the editable status of this group.voidsetValue(java.lang.String valueString, java.lang.String defaultValueString)Sets the value of this option from the given string representation.voidsetValue(OptionGroup value)Sets the value of this option.java.lang.StringtoString()protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.protected voidwriteChildren(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- TheSpecificationto refer to.
-
OptionGroup
public OptionGroup(java.lang.String id, Specification specification)Creates a newOptionGroup.- Parameters:
id- The object identifier.specification- TheSpecificationto refer to.
-
OptionGroup
public OptionGroup(FreeColXMLReader xr, Specification specification) throws javax.xml.stream.XMLStreamException
Creates a newOptionGroup.- Parameters:
xr- TheFreeColXMLReaderto read from.specification- TheSpecificationto 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
Options.
-
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
OptionGroups present.
-
add
public void add(Option option)
Adds the given option to this group. The option is assumed to be correct.- Parameters:
option- TheOptionto add.
-
merge
public boolean merge(Option option, LogBuilder lb)
Merges the givenOptioninto 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- TheOptionto merge.lb- ALogBuilderto 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 initialOptionGroupto add.
-
remove
public Option remove(java.lang.String id)
Remove an option.- Parameters:
id- The identifier of the option to remove.- Returns:
- The
Optionremoved if any.
-
removeAll
public void removeAll()
Removes all of theOptions from thisOptionGroup.
-
load
protected boolean load(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Loads the options from the given reader.- Parameters:
xr- TheFreeColXMLReaderto 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- TheFileto 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- TheFileto load from.optionId- The option identifier to look for.spec- A baseSpecification.- Returns:
- The
OptionGroupfound, 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:
hasOptionin 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:
getOptionin 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:
cloneOptionin interfaceOption<OptionGroup>- Specified by:
cloneOptionin classAbstractOption<OptionGroup>- Returns:
- A clone of this option.
-
getValue
public OptionGroup getValue()
Gets the value of this option.- Specified by:
getValuein interfaceOption<OptionGroup>- Specified by:
getValuein classAbstractOption<OptionGroup>- Returns:
- The value of this
Option.
-
setValue
public void setValue(OptionGroup value)
Sets the value of this option.- Specified by:
setValuein interfaceOption<OptionGroup>- Specified by:
setValuein 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:
setValuein 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:
generateChoicesin classAbstractOption<OptionGroup>
-
isNullValueOK
public boolean isNullValueOK()
Is null an acceptable value for this option? Override this in subclasses where necessary.- Overrides:
isNullValueOKin classAbstractOption<OptionGroup>- Returns:
- False.
-
save
public boolean save(java.io.File file)
Writes the object to the given file.- Overrides:
savein classFreeColObject- Parameters:
file- TheFileto 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:
writeAttributesin classAbstractOption<OptionGroup>- Parameters:
xw- TheFreeColXMLWriterto 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:
writeChildrenin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readAttributesin classAbstractOption<OptionGroup>- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildrenin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Specified by:
toStringin interfaceOption<OptionGroup>- Overrides:
toStringin classFreeColObject
-
-