Class OptionGroup

    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • 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?
    • Constructor Detail

      • OptionGroup

        public OptionGroup​(java.lang.String id)
        Creates a new OptionGroup.
        Parameters:
        id - The object identifier.
      • OptionGroup

        public OptionGroup​(Specification specification)
        Creates a new OptionGroup.
        Parameters:
        specification - The Specification to refer to.
      • OptionGroup

        public OptionGroup​(java.lang.String id,
                           Specification specification)
        Creates a new OptionGroup.
        Parameters:
        id - The object identifier.
        specification - The Specification to refer to.
      • OptionGroup

        public OptionGroup​(FreeColXMLReader xr,
                           Specification specification)
                    throws javax.xml.stream.XMLStreamException
        Creates a new OptionGroup.
        Parameters:
        xr - The FreeColXMLReader to read from.
        specification - The Specification 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 this Option.
        Returns:
        The name as provided in the constructor.
      • getShortDescription

        public java.lang.String getShortDescription()
        Gets the i18n short description of this Option. 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 - The Option to add.
      • merge

        public boolean merge​(Option option,
                             LogBuilder lb)
        Merges the given Option 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 - The Option to merge.
        lb - A LogBuilder 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 initial OptionGroup 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 the Options from this OptionGroup.
      • load

        protected boolean load​(FreeColXMLReader xr)
                        throws javax.xml.stream.XMLStreamException
        Loads the options from the given reader.
        Parameters:
        xr - The FreeColXMLReader 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 - The File 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 - The File to load from.
        optionId - The option identifier to look for.
        spec - A base Specification.
        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 interface OptionContainer
        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 interface OptionContainer
        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.
      • 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 class AbstractOption<OptionGroup>
        Parameters:
        valueString - The string representation of the value of this Option.
        defaultValueString - The string representation of the default value of this Option.
      • 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 class AbstractOption<OptionGroup>
      • isNullValueOK

        public boolean isNullValueOK()
        Is null an acceptable value for this option? Override this in subclasses where necessary.
        Overrides:
        isNullValueOK in class AbstractOption<OptionGroup>
        Returns:
        False.
      • save

        public boolean save​(java.io.File file)
        Writes the object to the given file.
        Overrides:
        save in class FreeColObject
        Parameters:
        file - The File 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 class AbstractOption<OptionGroup>
        Parameters:
        xw - The FreeColXMLWriter 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 class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter 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 class AbstractOption<OptionGroup>
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Returns:
        The tag.