T
- the type of the data supported by this ComponentHandler
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.
Implementation note: This class is not thread-safe.
Modifier | Constructor and Description |
---|---|
protected |
AbstractRadioButtonHandler(Class<T> dataType)
Creates a new instance of
AbstractRadioButtonHandler . |
Modifier and Type | Method and 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 this
ComponentHandler . |
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.
|
addHandler, getChildHandler, getChildHandlerCount, getChildHandlerIndex, getChildHandlerNameAt, getChildHandlerNames, getChildHandlers, getComponent, getOuterComponent, getType, isEnabled, setEnabled
public T getData()
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.public void setData(T data)
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.data
- the data object to be setprotected T getUnselectedData()
protected int getUnselectedIndex()
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.protected abstract T getDataForButton(int idx)
getData()
. A concrete
implementation must return the data value that corresponds to the radio
button with the given index.idx
- the index of the radio buttonprotected abstract int getButtonIndex(T value)
setData(Object)
. setData()
then sets the data of this radio button to true and all
other to false.value
- the data valueCopyright © 2016 The JGUIraffe Team. All rights reserved.