How to create ADF TreeTable programmatically?

Using ADF-BC to create ADF TreeTable is simple and straightforward, but doing the same without using ADF-BC involves a bit of work. Its not that its quite difficult but we would have to build the data in the format expected by the ADF TreeTable. In this post I will show you how the data structure for the ADF TreeTable should be with a simple example.

Consider the scenario:

We need to show the hierarchical structure of Managers and their directs using a ADF TreeTable.

Every Manager is an Employee and all directs under the Manager are also Employee. The only difference is that if the Employee is not a Manager then his directs are empty. Lets consider the Employee class as shown:

public class Employee {
  public Employee(String name, String loc){
    this.name = name;
    this.location = loc;
    directs = new ArrayList<Employee>();
  }
  private String name;
  private String location;
  private List<Employee> directs;

  public String getName() {
    return name;
  }

  public String getLocation() {
    return location;
  }

  public List<Employee> getDirects() {
    return directs;
  }
  
  public void addDirect(Employee emp){
    directs.add(emp);
  }
}

Go ahead and create this Employee class in your ViewController or Model project. Ideal place would be your Model project.

Lets create a list of Managers and Employees with each Manager having a list of his directs. But before that lets create a Page Model bean for our JSP which will hold the data for the ADF TreeTable and this Page Model bean is declared to be available in the view scope. The data for the ADF TreeTable is nothing but the list of Managers we are going to create.

We can create TreeTablePageModel (our managed bean holding the data for JSP) in the ViewController project and create this list of Managers and Employees in our TreeTablePageModel constructor. But the ADF TreeTable cannot consume the list of Managers and Employees as is. It has to be wrapped into another class which adapts out list to the data model expected by ADF TreeTable. And that class is the ChildPropertyTreeModel(org.apache.myfaces.trinidad.model.ChildPropertyTreeModel).

The ChildPropertyTreeModel accepts the list of Managers and Employees and along with it accepts the name of the attribute which contains the children, in this case the directs. So for each element in the list the ChildPropertyTreeModel invokes the get to retrieve its children. Lets look at this in action:

public class TreeTablePageModel {
  
  List<Employee> allEmployees = new ArrayList<Employee>();
  
  ChildPropertyTreeModel employeeTree;
  
  public TreeTablePageModel() {
    super();
    
    Employee manager1 = new Employee("John","London");
    Employee emp = new Employee("Jack", "London");
    manager1.addDirect(emp);
    emp = new Employee("Ken","New York");
    manager1.addDirect(emp);
    
    Employee manager2 = new Employee("Ravi","Bangalore");
    emp = new Employee("Ramesh","Bangalore");
    manager2.addDirect(emp);
    Employee manager3 = new Employee("Raju","Pune");
    emp = new Employee("Rakesh","Pune");
    manager3.addDirect(emp);
    manager2.addDirect(manager3);
    
    emp = new Employee("Jamie","California");
    
    allEmployees.add(manager1);
    allEmployees.add(manager2);
    allEmployees.add(emp);
    
    //Wrapping the list in to a class used by ADF TreeTable. 
    employeeTree = 
        new ChildPropertyTreeModel(allEmployees,"directs");
  }


  public ChildPropertyTreeModel getEmployeeTree() {
    return employeeTree;
  }
}

And our JSP which includes the ADF TreeTable would be like:

<af:treeTable id="tt1" var="row"
    value="#{viewScope.treeTableModel.employeeTree}">
  <f:facet name="nodeStamp">
    <af:column id="nCol" headerText="Name">
      <af:outputText id="nOt" value="#{row.name}"/>
    </af:column>
  </f:facet>
  <af:column id="lCol" headerText="Location">
    <af:outputText id="lOt" value="#{row.location}"/>
  </af:column>
</af:treeTable>

Our ADF TreeTable looks like:
AdfTreeTable
If we wrap our ADF TreeTable within ADF PanelCollection, it looks a much better with ADF PanelCollection providing extra controls for working with ADF TreeTable.
AdfTreeTable2
The JDeveloper project can be downloaded from here.

PS: This post was an extension to my answer on Stackoverflow.

2 thoughts on “How to create ADF TreeTable programmatically?”

  1. Hello, I followed your example successfully, but I have a problem and do not know if you can help. I need to make a filter with a inputtext to show me only the nodes that contain the search string characters long. Wear a week working and I can not do it. thank you very much

    Reply

Leave a Reply

Discover more from Experiences Unlimited

Subscribe now to keep reading and get access to the full archive.

Continue reading