Class AbstractRadioButtonHandler<T>
- java.lang.Object
-
- net.sf.jguiraffe.gui.builder.components.AbstractCompositeComponentHandler<T,Boolean>
-
- net.sf.jguiraffe.gui.builder.components.model.AbstractRadioButtonHandler<T>
-
- Type Parameters:
T
- the type of the data supported by thisComponentHandler
- 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 bygetData()
.getData()
determines the index of the selected button in the group. ThengetDataForButton()
has to return a corresponding data object.getButtonIndex()
is called bysetData(Object)
.setData()
passes the value object to be set togetButtonIndex()
method in order to find out which radio button in the group must be selected.
Implementation note: This class is not thread-safe.
- Version:
- $Id: AbstractRadioButtonHandler.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractRadioButtonHandler(Class<T> dataType)
Creates a new instance ofAbstractRadioButtonHandler
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract int
getButtonIndex(T value)
Returns the index of the radio button that corresponds to the specified data value.T
getData()
Returns the data of thisComponentHandler
.protected abstract T
getDataForButton(int idx)
Returns the data value that corresponds to the radio button with the given index.protected T
getUnselectedData()
Returns the data to be returned by this handler if none of the radio buttons in this group is selected.protected int
getUnselectedIndex()
Returns the index of the radio button that should be selected if this handler does not contain any data.void
setData(T data)
Sets the data of this handler.-
Methods inherited from class net.sf.jguiraffe.gui.builder.components.AbstractCompositeComponentHandler
addHandler, getChildHandler, getChildHandlerCount, getChildHandlerIndex, getChildHandlerNameAt, getChildHandlerNames, getChildHandlers, getComponent, getOuterComponent, getType, isEnabled, setEnabled
-
-
-
-
Method Detail
-
getData
public T getData()
Returns the data of thisComponentHandler
. This implementation determines which radio button in the associated group is selected. If none is selected, the result ofgetUnselectedData()
is returned. Otherwise, the object returned bygetDataForButton(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 callinggetUnselectedIndex()
. (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 bysetData()
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 bygetData()
. 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 bysetData(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
-
-