Class AbstractRadioButtonHandler<T>

  • Type Parameters:
    T - the type of the data supported by this ComponentHandler
    All Implemented Interfaces:
    CompositeComponentHandler<T,​Boolean>, ComponentHandler<T>
    Direct Known Subclasses:
    DefaultRadioButtonHandler

    public abstract class AbstractRadioButtonHandler<T>
    extends AbstractCompositeComponentHandler<T,​Boolean>

    A base class for ComponentHandler implementations for radio buttons.

    Radio buttons typically are not used in isolation, but work together in a group. This base class provides functionality to manage a number of child ComponentHandler objects representing the radio buttons in the associated group. It supports the conversion of the data of the child handlers to a combined data object.

    Because a number of radio buttons form a group an application is typically not interested in the data of the single radio buttons (i.e. the selected state). The relevant information is which radio button in the group is selected. This information has to be encoded somehow and is stored in the data of the hosting form. This base class provides two abstract methods for dealing with this encoding:

    • getDataForButton(int) is called by getData(). getData() determines the index of the selected button in the group. Then getDataForButton() has to return a corresponding data object.
    • getButtonIndex() is called by setData(Object). setData() passes the value object to be set to getButtonIndex() method in order to find out which radio button in the group must be selected.
    So these two methods basically implement a mapping between the index of the selected radio button and the data of this composite component handler.

    Implementation note: This class is not thread-safe.

    Version:
    $Id: AbstractRadioButtonHandler.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • AbstractRadioButtonHandler

        protected AbstractRadioButtonHandler​(Class<T> dataType)
        Creates a new instance of AbstractRadioButtonHandler.
        Parameters:
        dataType - the data type of this handler
    • Method Detail

      • getData

        public T getData()
        Returns the data of this ComponentHandler. This implementation determines which radio button in the associated group is selected. If none is selected, the result of getUnselectedData() is returned. Otherwise, the object returned by getDataForButton(int) is returned.
        Returns:
        the data of this handler
      • setData

        public void setData​(T data)
        Sets the data of this handler. If the data passed in is null, the index of the child handler to be selected is determined by calling getUnselectedIndex(). (This method may return an invalid index causing all radio buttons to be unselected.) Otherwise, getButtonIndex(Object) is called with the passed in data object. Then only the child handler with the returned index is set to true, all others are set to false.
        Parameters:
        data - the data object to be set
      • getUnselectedData

        protected T getUnselectedData()
        Returns the data to be returned by this handler if none of the radio buttons in this group is selected. This base implementation returns null. Derived classes that support a default value can override this method.
        Returns:
        the data to be returned if no radio button is selected
      • getUnselectedIndex

        protected int getUnselectedIndex()
        Returns the index of the radio button that should be selected if this handler does not contain any data. This method is called by setData() if null is passed in. This base implementation returns an invalid index (-1), which has the effect that no radio button is selected. Derived classes can override this method if one of the radio buttons should be selected per default.
        Returns:
        the index of the radio button to be selected if no data is available
      • getDataForButton

        protected abstract T getDataForButton​(int idx)
        Returns the data value that corresponds to the radio button with the given index. This method is called by getData(). A concrete implementation must return the data value that corresponds to the radio button with the given index.
        Parameters:
        idx - the index of the radio button
        Returns:
        the corresponding data value
      • getButtonIndex

        protected abstract int getButtonIndex​(T value)
        Returns the index of the radio button that corresponds to the specified data value. This method is called by setData(Object). setData() then sets the data of this radio button to true and all other to false.
        Parameters:
        value - the data value
        Returns:
        the index of the corresponding radio button