be.ugent.caagt.swirl

Class SelectionGroup


public class SelectionGroup
extends java.lang.Object

Acts like a button group with associated single selection model. Of all toggle buttons added to a group of this kind, at most one can be selected at the same time. Every time a new selection is made, this is reported to the selection model. Buttons are given an internal index as they are added to the group (starting at 0). When the selection model selects a new index, the corresponding button will be selected and the old button will be deselected.

Provides convenience methods to add listeners to the associated selection model and to determine what is the currently selected button, either as an index, an action command or a button object.

A group like this is especially useful when the same set of choices must be available at the same time in different forms: as a set of radio button menu items and a set of toggle buttons in a tool bar, say. Implementing both groups as selection groups with the same model makes both groups automatically synchronized.

In the above example, the group of radio menu items would for instance be created as follows:

    SelectionGroup menuGroup = new SelectionGroup ();
    JRadioButtonMenuItem item0 = new JRadioButtonMenuItem (...);
    JRadioButtonMenuItem item1 = new JRadioButtonMenuItem (...);
    JRadioButtonMenuItem item2 = new JRadioButtonMenuItem (...);
    menuGroup.add (item0);
    menuGroup.add (item1);
    menuGroup.add (item2);
 
and the group of toggle buttons
    SelectionGroup buttonGroup = new SelectionGroup (menuGroup.getModel(), true);
    JToggleButton button0 = new JToggleButton (...);
    JToggleButton button1 = new JToggleButton (...);
    JToggleButton button2 = new JToggleButton (...);
    buttonGroup.add(item0);
    buttonGroup.add(item1);
    buttonGroup.add(item2);
 
To query which of the three buttons in each group is currently selected, use menuGroup.getSelectedIndex() or buttonGroup.getSelectedIndex(), which will both yield the same answer. Alternatively, the selection model can be queried directly, or a change listener may be registered with the selection model.

Constructor Summary

SelectionGroup()
Create a selection group with a newly created single selection model and no buttons.
SelectionGroup(SingleSelectionModel model, boolean clearable)
Create a selection group with the given model.
SelectionGroup(boolean clearable)
Create a selection group with a newly created single selection model and no buttons.

Method Summary

void
add(AbstractButton button)
Add the given button to the group.
void
addChangeListener(ChangeListener listener)
Register the listener with the model.
String
getActionCommand()
Return the action command of the currently selected button, or null if none is selected.
SingleSelectionModel
getModel()
The single selection model used by this group.
AbstractButton
getSelectedButton()
Return the button which is currently selected.
int
getSelectedIndex()
Return the current selection.
void
removeChangeListener(ChangeListener listener)
Unregister the listener form the model.
void
setSelectedIndex(int index)
Set the currently selected index.

Constructor Details

SelectionGroup

public SelectionGroup()
Create a selection group with a newly created single selection model and no buttons. Short for Selectiongroup (true).

SelectionGroup

public SelectionGroup(SingleSelectionModel model,
                      boolean clearable)
Create a selection group with the given model.
Parameters:
clearable - indicates whether buttons can be cleared by clicking on them. If false the group behaves like a button group.

SelectionGroup

public SelectionGroup(boolean clearable)
Create a selection group with a newly created single selection model and no buttons.
Parameters:
clearable - indicates whether buttons can be cleared by clicking on them. If false the group behaves like a button group.

Method Details

add

public void add(AbstractButton button)
Add the given button to the group.

addChangeListener

public void addChangeListener(ChangeListener listener)
Register the listener with the model.

getActionCommand

public String getActionCommand()
Return the action command of the currently selected button, or null if none is selected.

getModel

public SingleSelectionModel getModel()
The single selection model used by this group.

getSelectedButton

public AbstractButton getSelectedButton()
Return the button which is currently selected.
Returns:
the currently selected button or null if none is selected

getSelectedIndex

public int getSelectedIndex()
Return the current selection.
Returns:
the current selection index or -1 if none.

removeChangeListener

public void removeChangeListener(ChangeListener listener)
Unregister the listener form the model.

setSelectedIndex

public void setSelectedIndex(int index)
Set the currently selected index.
Parameters:
index - Index to be selected, or -1 to clear the selection.