| Graphic Components > Tree Component > Tree Component Services > Sorting the Tree Nodes |
Sorting the Tree Nodes |
INDEX
PREVIOUS
NEXT
|
The tree component allows you to sort the displayed nodes. By default, the tree adapter sorts nodes using an arbitrary order (see IlpAbstractTreeAdapter.ARBITRARY_COMPARATOR) in which the same set of objects is always displayed in the same order. You can specify your own order by providing an object that implements the Comparator interface before the nodes are actually created. The compare() method of the comparator object will be called for pairs of IlpObject instances in the data source to establish a sort order. Objects are ordered locally, that is, within their parent node.
For convenient sorting based on the attribute values of the business objects, JViews TGO provides the predefined IlpAttributeComparator class as an implementation of the Comparator interface. This class sorts objects based on the value of one or more of their attributes.
The following sample code shows you how to use this class to implement the sorting of tree nodes. The entire sample is available in:
<installdir>/samples/framework/datasource-explorer
where <installdir> is the directory where you have installed JViews TGO.
// Create a comparator to sort all the nodes in the tree
// Use the predefined class IlpAttributeComparator to sort
// The first sort parameter is directory, so directories appears before files
// then the 'name' attribute
// then the 'path' attribute, as name is sometimes null (for roots)
IlpAttributeComparator sorter =
new IlpAttributeComparator(dataSource.fileClass.getAttribute("directory"),
false);
sorter.setDirectionAndOrder(dataSource.fileClass.getAttribute("name"),2,true);
sorter.setDirectionAndOrder(dataSource.fileClass.getAttribute("path"),3,true);
// Set the sorter to the tree BEFORE setting the data source
treeComponent.getAdapter().setComparator(sorter);
All the objects are sorted again when a new comparator is set.
To retrieve the current sort order, use the method getSortComparator.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. All rights reserved. Legal terms. | PREVIOUS NEXT |