net.sf.freecol.common.option
Class OptionMap

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.option.AbstractOption
          extended by net.sf.freecol.common.option.OptionGroup
              extended by net.sf.freecol.common.option.OptionMap
All Implemented Interfaces:
Option
Direct Known Subclasses:
ClientOptions, GameOptions, MapGeneratorOptions

public abstract class OptionMap
extends OptionGroup

Used for grouping objects of Option.


Field Summary
private static java.util.logging.Logger logger
           
private  java.util.HashMap<java.lang.String,Option> values
           
private  java.lang.String xmlTagName
           
 
Fields inherited from class net.sf.freecol.common.option.AbstractOption
isDefined, NO_ID, previewEnabled
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE
 
Constructor Summary
OptionMap(org.w3c.dom.Element element, java.lang.String xmlTagName)
          Creates an OptionMap from an XML representation.
OptionMap(java.lang.String xmlTagName)
          Creates a new OptionMap.
OptionMap(javax.xml.stream.XMLStreamReader in, java.lang.String xmlTagName)
          Creates an OptionMap from an XML representation.
 
Method Summary
protected abstract  void addDefaultOptions()
          Adds the default options to this OptionMap.
 void addToMap(OptionGroup og)
          Adds the Options from the given OptionGroup to the Map.
private static javax.xml.stream.XMLStreamReader createXMLStreamReader(java.io.File file)
          Creates a XMLStreamReader for reading the given file.
 boolean getBoolean(java.lang.String id)
          Gets the boolean value of an option.
 java.io.File getFile(java.lang.String id)
          Gets the File specified by an option.
 int getInteger(java.lang.String id)
          Gets the integer value of an option.
 Option getObject(java.lang.String id)
          Gets the object identified by the given id.
static java.lang.String getXMLElementTagName()
          Gets the tag name of the root element representing this object.
protected abstract  boolean isCorrectTagName(java.lang.String tagName)
           
 void load(java.io.File loadFile)
          Reads the options from the given file.
 void putOption(Option option)
           
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
 void save(java.io.File saveFile)
          Writes the options to the given file.
 void setFile(java.lang.String id, java.io.File newFileValue)
          Allow a File to be set in other ways, as from the user using the Client.
 void toXML(javax.xml.stream.XMLStreamWriter out)
          This method writes an XML-representation of this object to the given stream.
private  void updateFromXML(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
 
Methods inherited from class net.sf.freecol.common.option.OptionGroup
add, getName, getShortDescription, iterator, removeAll, toXMLImpl
 
Methods inherited from class net.sf.freecol.common.option.AbstractOption
getGroup, isPreviewEnabled, setGroup, setPreviewEnabled, toString
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readChildren, readFromArrayElement, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXML, readFromXMLElement, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, setId, toArrayElement, toArrayElement, toListElement, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute, writeAttributes, writeChildren, writeFreeColGameObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.freecol.common.option.Option
addPropertyChangeListener, getId, readFromXML, readFromXMLElement, removePropertyChangeListener, toXMLElement
 

Field Detail

logger

private static java.util.logging.Logger logger

xmlTagName

private java.lang.String xmlTagName

values

private java.util.HashMap<java.lang.String,Option> values
Constructor Detail

OptionMap

public OptionMap(java.lang.String xmlTagName)
Creates a new OptionMap.

Parameters:
xmlTagName - The tag name that should be used for the parent XML-element returned by Option.toXMLElement(org.w3c.dom.Document).

OptionMap

public OptionMap(javax.xml.stream.XMLStreamReader in,
                 java.lang.String xmlTagName)
          throws javax.xml.stream.XMLStreamException
Creates an OptionMap from an XML representation.

Parameters:
in - The XML stream to read the data from.
xmlTagName - The tag name that should be used for the parent XML-element returned by Option.toXMLElement(org.w3c.dom.Document).
Throws:
javax.xml.stream.XMLStreamException - if an error occured during parsing.

OptionMap

public OptionMap(org.w3c.dom.Element element,
                 java.lang.String xmlTagName)
Creates an OptionMap from an XML representation.

Parameters:
element - The XML Element from which this object should be constructed.
xmlTagName - The tag name that should be used for the parent XML-element returned by Option.toXMLElement(org.w3c.dom.Document).
Method Detail

addDefaultOptions

protected abstract void addDefaultOptions()
Adds the default options to this OptionMap. Needs to be implemented by subclasses.


getObject

public Option getObject(java.lang.String id)
Gets the object identified by the given id.

Parameters:
id - The ID.
Returns:
The Object with the given ID.

getInteger

public int getInteger(java.lang.String id)
Gets the integer value of an option.

Parameters:
id - The id of the option.
Returns:
The value.
Throws:
java.lang.IllegalArgumentException - If there is no integer value associated with the specified option.
java.lang.NullPointerException - if the given Option does not exist.

getBoolean

public boolean getBoolean(java.lang.String id)
Gets the boolean value of an option.

Parameters:
id - The id of the option.
Returns:
The value.
Throws:
java.lang.IllegalArgumentException - If there is no boolean value associated with the specified option.
java.lang.NullPointerException - if the given Option does not exist.

getFile

public java.io.File getFile(java.lang.String id)
Gets the File specified by an option.

Parameters:
id - The id of the option.
Returns:
The value.
Throws:
java.lang.IllegalArgumentException - If there is no File associated with the specified option.
java.lang.NullPointerException - if the given Option does not exist.

addToMap

public void addToMap(OptionGroup og)
Adds the Options from the given OptionGroup to the Map. This is done recursively if the specified group has any sub-groups.

Parameters:
og - The OptionGroup to be added.

putOption

public void putOption(Option option)

createXMLStreamReader

private static javax.xml.stream.XMLStreamReader createXMLStreamReader(java.io.File file)
                                                               throws java.io.IOException
Creates a XMLStreamReader for reading the given file. Compression is automatically detected.

Parameters:
file - The file to be read.
Returns:
The XMLStreamReader.
Throws:
java.io.IOException - if thrown while loading the game or if a XMLStreamException have been thrown by the parser.

isCorrectTagName

protected abstract boolean isCorrectTagName(java.lang.String tagName)

load

public void load(java.io.File loadFile)
Reads the options from the given file.

Parameters:
loadFile - The File to read the options from.

save

public void save(java.io.File saveFile)
Writes the options to the given file.

Parameters:
saveFile - The file where the client options should be written.
See Also:
ClientOptions

toXML

public void toXML(javax.xml.stream.XMLStreamWriter out)
           throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Specified by:
toXML in interface Option
Overrides:
toXML in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
See Also:
FreeColObject.toXML(XMLStreamWriter, Player, boolean, boolean)

readFromXMLImpl

protected void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                        throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Overrides:
readFromXMLImpl in class OptionGroup
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

updateFromXML

private void updateFromXML(javax.xml.stream.XMLStreamReader in)
                    throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Gets the tag name of the root element representing this object.

Throws:
java.lang.UnsupportedOperationException - at any time, since this class should get it's XML tag name in the constructor

setFile

public void setFile(java.lang.String id,
                    java.io.File newFileValue)
Allow a File to be set in other ways, as from the user using the Client.

Parameters:
id -
newFileValue -