be.ugent.caagt.swirl
Class SelectionGroup

java.lang.Object
  extended by be.ugent.caagt.swirl.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.

See Also:
GenericSelectionGroup

Constructor Summary
SelectionGroup()
          Create a selection group with a newly created single selection model and no buttons.
SelectionGroup(boolean clearable)
          Create a selection group with a newly created single selection model and no buttons.
SelectionGroup(javax.swing.SingleSelectionModel model, boolean clearable)
          Create a selection group with the given model.
 
Method Summary
 void add(javax.swing.AbstractButton button)
          Add the given button to the group.
 void addChangeListener(javax.swing.event.ChangeListener listener)
          Register the listener with the model.
 java.lang.String getActionCommand()
          Return the action command of the currently selected button, or null if none is selected.
 javax.swing.SingleSelectionModel getModel()
          The single selection model used by this group.
 javax.swing.AbstractButton getSelectedButton()
          Return the button which is currently selected.
 int getSelectedIndex()
          Return the current selection.
 void removeChangeListener(javax.swing.event.ChangeListener listener)
          Unregister the listener form the model.
 void setSelectedIndex(int index)
          Set the currently selected index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SelectionGroup

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


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.

SelectionGroup

public SelectionGroup(javax.swing.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.
Method Detail

getModel

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


addChangeListener

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


removeChangeListener

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


add

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


getSelectedButton

public javax.swing.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.

getActionCommand

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


setSelectedIndex

public void setSelectedIndex(int index)
Set the currently selected index.

Parameters:
index - Index to be selected, or -1 to clear the selection.