TreeViewer in RCP

This tutorial is about creating a Tree using RCP and displaying it. I intend to create a small tree in RCP here which will look like -

Root
        Child 1
                   Child 1_1
                   Child 1_2
        Child 2

So, let' start step by step.

Step 1.

Create a Simple RCP application and add a view to it. If you don't know how to do it, please refer to these tutorials  - Creating a simple RCP application and Adding a view to RCP application. In this example, I have named the view class TreeViewClass.java



Step 2.

Create some TreeItems. For example , for the root node we can create the node like this -

 
TreeItem root = new TreeItem(tree, 0);
root.setText("Root");

Again, for non-root node, we need to create the node like this -

 
TreeItem child1 = new TreeItem(root, 0);
child1.setText("Child 1");

Please note, while creating the new item, the first parameter, we passing is the parent node. That's why, while creating root node, we used the Tree Object there, while for non-root nodes, we used the name of the parent node.

Well, that's it...!
Now, the code looks like -

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;

public class TreeViewClass extends ViewPart {

 public static final String ID = "TreeViewerDemo.treeView";

 Tree tree;
 public TreeViewClass() {
  // TODO Auto-generated constructor stub
 }

 @Override
 public void createPartControl(Composite parent) {
  tree = new Tree(parent, SWT.BORDER);
  
  TreeItem root = new TreeItem(tree, 0);
  root.setText("Root");
  
  TreeItem child1 = new TreeItem(root, 0);
  child1.setText("Child 1");
  
  TreeItem childOfChild1 = new TreeItem(child1, 0);
  childOfChild1.setText("Child 1_1");
  
  TreeItem childOfChild2 = new TreeItem(child1, 0);
  childOfChild2.setText("Child 1_2");
  
  TreeItem child2 = new TreeItem(root, 0);
  child2.setText("Child 2");
  
 }


 @Override
 public void setFocus() {
  // TODO Auto-generated method stub

 }

}

And, the output looks like this -


On expanding the whole tree, the tree will look like this -



Please note that, with this code, the tree will not be expanded. If we want to expand all the levels of the tree, we need to traverse the tree and expand them manually.
For that, we can simply, add a method which takes a TreeItem as a parameter.
private void traverseAllElements(TreeItem treeItem) {
  int length = treeItem.getItemCount();
  System.out.println(treeItem);
  for (int j = 0; j < length; j++) {
    traverseAllElements(treeItem.getItem(j));
  }
 }

This method traverses through all of the elements one by one and when it finds that an element has a child, it calls itself on iteration, thus creating a recursive function call.
But, we need to expand all the nodes. For that, we will modify the code a little.

private void traverseAllElements(TreeItem treeItem) {
  int length = treeItem.getItemCount();
  System.out.println(treeItem);
  if(treeItem.getItemCount()>0) {
   treeItem.setExpanded(true);
  }
  for (int j = 0; j < length; j++) {
   traverseAllElements(treeItem.getItem(j));
  }
 }


Now call this method from the createPartControl method.
traverseAllElements(tree.getItem(0));
So, that's it!
Now, the output will look like -




Happy learning! Don't forget to share!