Previous Page
Next Page

5.1.8. TreeViewer class

The treeViewer class wraps the tree widget. A tree viewer displays a hierarchical list of objects in a parentchild relationship. This viewer needs to be configured with label and content providers. The CheckboxTreeViewer enhances this further by adding support for graying out individual items and toggling on and off an associated checkbox with each item. Useful APIs include:

add(Object, Object) Adds the given child element to this viewer as a child of the given parent element.

add(Object, Object[]) Adds the given child elements to this viewer as children of the given parent element.

addTreeListener(ITreeViewerListener) Adds a listener for expanding and collapsing events in this viewer.

collapseAll() Collapses all nodes of the viewer's tree, starting with the root.

collapseToLevel(Object, int) Collapses the subtree rooted at the given element to the given level.

expandAll() Expands all nodes of the viewer's tree, starting with the root.

expandToLevel(int) Expands the root of the viewer's tree to the given level.

expandToLevel(Object, int) Expands all ancestors of the given element so that the given element becomes visible in this viewer's tree control, and then expands the subtree rooted at the given element to the given level.

getExpandedElements() Returns a list of elements corresponding to expanded nodes in this viewer's tree, including currently hidden ones that are marked as expanded but are under a collapsed ancestor.

getExpandedState(Object) Returns whether the node corresponding to the given element is expanded or collapsed.

TRee getTree() Returns this tree viewer's tree control.

getVisibleExpandedElements() Gets the expanded elements that are visible to the user.

isExpandable(Object) Returns whether the tree node representing the given element can be expanded.

remove(Object) Removes the given element from the viewer.

remove(Object[]) Removes the given elements from this viewer.

reveal(Object) Ensures that the given element is visible, scrolling the viewer if necessary.

scrollDown(int, int) Scrolls the viewer's control down by one item from the given display-relative coordinates.

scrollUp(int, int) Scrolls the viewer's control up by one item from the given display-relative coordinates.

setAutoExpandLevel(int) Sets the auto-expand level.

setContentProvider(IContentProvider) The implementation, AbstractTreeViewer, of this method checks to ensure that the content provider is an ITreeContentProvider.

setExpandedElements(Object[]) Sets which nodes are expanded in this viewer's tree.

setExpandedState(Object, boolean) Sets whether the node corresponding to the given element is expanded or collapsed.

setLabelProvider(IBaseLabelProvider) The tree viewer implementation of this Viewer framework method ensures that the given label provider is an instance of ILabelProvider.

CheckboxTreeViewer adds the following useful APIs:

addCheckStateListener(ICheckStateListener) Adds a listener for changes to the checked state of elements in this viewer.

getChecked(Object) Returns the checked state of the given element.

getCheckedElements() Returns a list of checked elements in this viewer's tree, including currently hidden ones that are marked as checked but are under a collapsed ancestor.

getGrayed(Object) Returns the grayed state of the given element.

getGrayedElements() Returns a list of grayed elements in this viewer's tree, including currently hidden ones that are marked as grayed but are under a collapsed ancestor.

setChecked(Object, boolean) Sets the checked state for the given element in this viewer.

setCheckedElements(Object[]) Sets which elements are checked in this viewer's tree.

setGrayChecked(Object, boolean) Checks and grays the selection rather than calling both setGrayed and setChecked as an optimization.

setGrayed(Object, boolean) Sets the grayed state for the given element in this viewer.

setGrayedElements(Object[]) Sets which elements are grayed in this viewer's tree.

setParentsGrayed(Object, boolean) Sets the grayed state for the given element and its parents in this viewer.

setSubtreeChecked(Object, boolean) Sets the checked state for the given element and its visible children in this viewer.

The following example creates a tree viewer with a label provider and content provider (see Figure 5-10).

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TreeViewerExample {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setText("Tree Viewer Example");
      shell.setBounds(100, 100, 200, 200);
      shell.setLayout(new FillLayout());

      final TreeViewer treeViewer =
         new TreeViewer(shell, SWT.SINGLE);
      treeViewer.setLabelProvider(
         new PersonListLabelProvider());
      treeViewer.setContentProvider(
         new PersonTreeContentProvider());
      treeViewer.setInput(Person.example());

      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) display.sleep();
      }
      display.dispose();
   }
}

Figure 5-10. TreeViewer example.


After creating the tree viewer, the label provider is set using the setLabelProvider() method and the content provider with the setContentProvider() method. The content provider, PersonTreeContentProvider, returns the parent and children of each item. The class looks like this:

import org.eclipse.jface.viewers.*;

public class PersonTreeContentProvider
   extends ArrayContentProvider
   implements ITreeContentProvider {

   public Object[] getChildren(Object parentElement) {
      Person person = (Person) parentElement;
      return person.children;
   }

   public Object getParent(Object element) {
      Person person = (Person) element;
      return person.parent;
   }

   public boolean hasChildren(Object element) {
      Person person = (Person) element;
      return person.children.length > 0;
   }
}


Previous Page
Next Page