Building your own tree in ADF does not seem that easy at first sight but in fact it is not that hard if you know what to do. On the forum at OTN you often find questions about building your own model.
In this post you will find the steps needed to build your own model. In the example i just use some static data but you can rewrite it to use whatever you want.
Attached to this post you can find the TreeExample.zip which contains the example application with a very simple tree. We will build a tree that has 2 nodes in the root and the first node has 2 children.
Our TreeModel contains objects of the type TreeItem. TreeItem is not a build in class. I call it TreeItem but you can name it whatever you want. It is a regular bean with getters and setters. In order to build a tree, the class should have a method that returns its children.
This is the TreeItem class we will be using:
As you can see, the TreeItem has 3 properties:
You are free to add more properties. Every property you add to the TreeItem you can use it in the tree itself.
Next we need to create our managed bean. This will contain the TreeModel itself.
In fact we just create the rootNode and use a default TreeModel from ADF. This is the ChildPropertyTreeModel.
We will build the tree in the constructor of the bean:
The af:tree component requires a TreeModel as value so we need to create such a property as well:
In this methode we will create the actual TreeModel based upon the root we created in the constructor. As you can see we also need to specify the name of the method that will return the children. By specifying "children" as a second parameter of the ChildPropertyTreeModel constructor, we tell the framework to call getChildren() in order to retreive the list of children.
When this is done, we need to register the bean to the faces-config.xml:
As a final step we need to add the tree to the page:
When you run the application, the tree should be there with some nodes.