Handling the selection

The selection model requirements for an IlpTree are defined by the interface IlpTreeSelectionModel. This model extends the Swing TreeSelectionModel to provide convenience operations when working with business objects ( IlpObject).
The selection model is responsible for setting, modifying, and retrieving the objects selected in the IlpTree.
The IlpTreeSelectionModel interface provides the following basic methods:
  • modifying or retrieving the selected IlpObject instances,
  • setting or retrieving the selection mode,
  • registering or unregistering selection listeners.
This interface also has a number of advanced features described in Architecture of the tree component of this section.
IlpDefaultTreeSelectionModel is the default implementation of IlpTreeSelectionModel.
The following code shows you how to access the IlpTreeSelectionModel of an IlpTree.

How to access the selection model of a tree component

IlpTree tree = ...
//...
// Retrieve the selection model
IlpTreeSelectionModel selectionModel = tree.getSelectionModel();

How to retrieve selected objects from the selection model

The following code sample shows you how to retrieve the selected IlpObject instances from the selection model.
Collection selection = selectionModel.getSelectedObjects();
Iterator it = selection.iterator();
while (it.hasNext()) {
   IlpObject node = (IlpObject)it.next();
      // Do what you want with the selected node
      // ...
}
The sample <installdir> /samples/tree/basic demonstrates the use of the selection model.

Setting the selection look and feel

The IlpTree allows you to choose between the look-and-feel for the selection:
  • HIGHLIGHT_SELECTION_LOOK_AND_FEEL : the standard mode, where a selected cell appears highlighted.
  • CHECKBOX_SELECTION_LOOK_AND_FEEL : in this mode, a check box is displayed next to each tree node. A check mark indicates that the corresponding node is selected. Clicking a check box or a cell switches the selection state of the corresponding node. In this mode, the highlighting only indicates which cell has the focus.
    Note
    In this mode, collapsing a node does not deselect its descendants.
selectionCheckBox.gif
Sample tree using the check box selection look and feel
The following code shows you how to activate the CHECKBOX_SELECTION_LOOK_AND_FEEL .

How to activate a selection look and feel

IlpTree tree = ...
// Activate the CHECKBOX_SELECTION_LOOK_AND_FEEL
tree.setSelectionLookAndFeel
  (IlpTreeView.CHECKBOX_SELECTION_LOOK_AND_FEEL);