Expand tree node all the way down in the ADF Faces Tree Component

1

We are still very much busy developing our ADF Faces application around the Tree Component. In previous articles we have discussed creating the tree in the first place, using various Tables as underlying data sources, adding intermediate nodes with node category labels, correctly displaying the node-as-folder vs node-without-children icon and generating the Tree based application using JHeadstart. In a next post I will discuss how to navigate the tree using keyboard keys instead of the mouse. This post addresses another tree-related issue: how to expand (and collapse) a node with all its off-spring, all the way down to the lowliest details.

 

It turns out it is quite difficult to do this in a very precise way, but very easy in a more general, just as effective fashion.

I will assume a simple application with ADF Faces Tree Component. It can be generated using JHeadstart – as discussed in Building an ADF Faces Tree Component with mixed (DEPT and EMP) nodes in 5 minutes using JHeadstart 10.1.3 and   Generating an Advanced ADF Faces Tree based application with JHeadstart 10.1.3 – or built directly with drag & drop from the ADF Data Control palette – as illustrated in Creating Multi-Type Node Children and Child Node labels in ADF Faces Tree Component – or even without any ADF binding with only the ADF Faces components and simple POJO style managed beans.  

....
 

We assume that the tree is bound to a backing bean, the selection of a node is registered with a backing bean and we use a command button to initiate the ‘expand currently selected node’ operation.

After creating the application (Web Technology Template ADF BC and ADF Faces) and developing the Business Components from Tables (EMP and DEPT in this example), I have created a new JSF JSP page, dragged the DeptView collection as Tree from the Data Control Palette to the JSF page and created the tree model that links in EmpView for the detail nodes.

Now it is time for a few simple steps:

  1. bind the tree to a backing bean
  2. make the nodes clickable and tell the backing bean which node was selected
  3. highlight the currently selected node by linking the tree’s focus to the backing bean
  4. add a command button (++) that initiates the expansion of the currently selected node
  5. link the button’s action to the backing bean 
  6. add a command button (–) that initiates the collapse of the currently selected node
  7. link the buttons action to the backing bean

1. Bind the tree to the backing bean:

&lt;af:tree value=&quot;#{bindings.DeptView1.treeModel}&quot; var=&quot;node&quot;<br />         binding=&quot;#{MyTreeBean.tree}&quot;&gt;<br />  &lt;f:facet name=&quot;nodeStamp&quot;&gt;<br />    &lt;af:outputText value=&quot;#{node}&quot;/&gt;<br />  &lt;/f:facet&gt;<br />&lt;/af:tree&gt;<br /><br />

The tree component is bound to the tree property in the backing bean MyTreeBean. This bean is auto-created when this binding was made by the JDeveloper IDE. We will inspect the code shortly.

2. Make the nodes clickable and tell the backing bean which node was selected

To make the nodes clickable/selectable , we change their definition from outputText to commandLink:

&lt;af:tree value=&quot;#{bindings.DeptView1.treeModel}&quot; var=&quot;node&quot;<br />         binding=&quot;#{MyTreeBean.tree}&quot;&gt;<br />   &lt;f:facet name=&quot;nodeStamp&quot;&gt;<br />     &lt;af:commandLink text=&quot;#{node}&quot;&gt;<br />       &lt;af:setActionListener from=&quot;#{MyTreeBean.tree.rowKey}&quot;<br />                             to=&quot;#{MyTreeBean.focusRowKey}&quot;/&gt;<br />     &lt;/af:commandLink&gt;<br />   &lt;/f:facet&gt;<br />&lt;/af:tree&gt;&nbsp;

I have added the focusRowKey property to the TreeBean – with accessors. When we run the application as it sits right now, we can click on the nodes of the tree – and see nothing happen. Only if you run in Debug Mode and create a breakpoint in the setFocusRowKey() method can you see that the method is invoked when the node is selected. 

3. highlight the currently selected node by linking the tree’s focus to the backing bean

The tree will display the selected node in a highlighted fashion, if it knows which node is the selected one. This is specified through the tree’s focusRowKey attribute. We can associate this attribute with the focusRowKey property of our tree bean:

&lt;af:tree value=&quot;#{bindings.DeptView1.treeModel}&quot; var=&quot;node&quot;<br />         focusRowKey=&quot;#{MyTreeBean.focusRowKey}&quot;<br />         binding=&quot;#{MyTreeBean.tree}&quot;&gt;<br />   &lt;f:facet name=&quot;nodeStamp&quot;&gt;<br />     &lt;af:commandLink text=&quot;#{node}&quot;&gt;<br />       &lt;af:setActionListener from=&quot;#{MyTreeBean.tree.rowKey}&quot;<br />                             to=&quot;#{MyTreeBean.focusRowKey}&quot;/&gt;<br />     &lt;/af:commandLink&gt;<br />   &lt;/f:facet&gt;<br />&lt;/af:tree&gt;&nbsp;<br />

Now when we run the application, we will see the selected node as the highlighted one. 

4. add a command button (++) that initiates the expansion of the currently selected node

&lt;h:form&gt;<br />  &lt;h:panelGrid columns=&quot;2&quot;&gt;<br />    &lt;af:panelGroup&gt;<br />      &lt;af:commandButton immediate=&quot;true&quot;<br />                        actionListener=&quot;#{MyTreeBean.expandCurrent}&quot;<br />                        text=&quot;++&quot;&gt;<br />      &lt;/af:commandButton&gt;<br />    &lt;/af:panelGroup&gt;<br />    &lt;af:panelGroup&gt;<br />      &lt;af:tree value=&quot;#{bindings.DeptView1.treeModel}&quot; var=&quot;node&quot;<br />                focusRowKey=&quot;#{MyTreeBean.focusRowKey}&quot;&nbsp;

Our button invokes the expandCurrent() method – see next section.

5. link the button’s action to the backing bean 

 

Here is the heart of this article. The ADF Tree Component has a TreeState – a collection that describes which nodes in the tree are expanded and which ones are not. The TreeState collection contains List elements. Each List Elements specifies a node, by specifying a tree path. The treepath of a node is something like [0,0,1] for the second child of the first child of the first root-node or [0] for the root-node itself or [4,2,1,5] for the 6th child of the 2nd child of the 3rd child of the 5th root-node.

We can programmatically expand nodes by adding them to the TreeState of the tree.  

public void expandCurrent(ActionEvent event) {<br />  List currentFocusRowKey = new ArrayList();<br />  currentFocusRowKey.addAll(getFocusRowKey());<br />  int currentLevel = currentFocusRowKey.size();<br />  getTree().getTreeState().getKeySet().add(currentFocusRowKey);<br />  expandNode(currentLevel, currentFocusRowKey, 10); // 10 levels deep<br />}<br /><br />private void expandNode(int currentLevel, List currentFocusRowKey, <br />                        int numberOfLevels) {<br />  if (numberOfLevels &gt; 0) {<br />    currentLevel++;<br />    f
or (int i = 0; i &lt; 10; i++)
{ // no more than 10 children per node<br />      currentFocusRowKey.add(Integer.toString(i));<br />      getTree().getTreeState().getKeySet().add(currentFocusRowKey);<br />      expandNode(currentLevel, currentFocusRowKey, --numberOfLevels);<br />      currentFocusRowKey.remove(currentLevel - 1);<br />    } //for<br />    currentLevel--;<br />    }<br />}<br />&nbsp;

Note: when using the ADF Faces Tree component together with the ADF Binding Tree element (FacesCtrlHierBinding), it turns out to be not so very simple to determine which nodes have children and how many of them. It is far easier to just add a lot of potential tree paths to the tree state of the tree – including many treepaths that do not refer to real nodes. Since the tree state merely serves the tree renderer to see whether or not a node should be displayed as expanded – and whether its children must be displayed – it does not hurt to have these non-existent tree paths in there.

6. add a command button (–) that initiates the collapse of the currently selected node

From where we are standing now, adding the Collapse Current Node functionality is a piece of cake. First add the button:

&lt;h:panelGrid columns=&quot;2&quot;&gt;<br />  &lt;af:panelGroup layout=&quot;vertical&quot;&gt;<br />    &lt;af:commandButton immediate=&quot;true&quot;<br />                      actionListener=&quot;#{MyTreeBean.expandCurrent}&quot;<br />                      text=&quot;++&quot;&gt;<br />    &lt;/af:commandButton&gt;<br />    &lt;af:objectSpacer height=&quot;10&quot; /&gt;<br />    &lt;af:commandButton immediate=&quot;true&quot;<br />                      actionListener=&quot;#{MyTreeBean.collapseCurrent}&quot;<br />                      text=&quot;--&quot;&gt;<br />    &lt;/af:commandButton&gt;<br />  &lt;/af:panelGroup&gt;<br />  &lt;af:panelGroup&gt;<br />&nbsp;

and then

7. Link the button’s action to the backing bean

We need to implement the method collapseCurrent in our Tree Bean:

    public void collapseCurrent(ActionEvent event) {<br />        List currentFocusRowKey = new ArrayList();<br />        currentFocusRowKey.addAll(getFocusRowKey());<br />        getTree().getTreeState().getKeySet().remove(currentFocusRowKey);<br />        Iterator keys = getTree().getTreeState().getKeySet().iterator();<br />        while (keys.hasNext()) {<br />            List rowKey = (List)keys.next();<br />            if (listStartsWith(rowKey, currentFocusRowKey)) {<br />               getTree().getTreeState().getKeySet().remove(rowKey);<br />            }<br />        }<br />    }<br />    <br />    private boolean listStartsWith(List check, List against) {<br />        if (check.size()&gt; against.size()) {<br />          for (int i = 0;i &lt; Math.min(check.size(),against.size());i++ ) {<br />            if (!((String)check.get(i)).equalsIgnoreCase((String)against.get(i)))<br />              return false; <br />          }// for<br />          return true;<br />        }// if        <br />        return false;<br />    }<br />&nbsp;

The essence here is that the tree-paths of the current node and all of its offspring are removed from the TreeState collection of the tree. So taking the current node’s tree path which is the same as the current focusRowKey and removing it from the TreeState is enough to close or collapse the current node. However, if we re-open the node, all of its children are potentially still expanded. So collapseCurrent needs to remove all entries in the TreeState that begin with focusRowKey – since all those entries are offspring of the current node.

JHeadstart and generating the Tree Application

Much of the inspiration for this approach comes from the JHeadstart team. The added value over JHeadstart presented in this article is the ability to expand and collapse a specific node – rather than the entire tree, which is a built-in feature of generated JHeadstart trees. 

If you are using JHeadstart, all you have to do is extend the JHeadstart TreeBean and create the expandCurrent() and collapseCurrent() methods with their helpers. And of course add the buttons to the generated pages.

Resources

Download the JDeveloper 10.1.3 Application with the HRM Tree implementation: TreeExpandCollapse.zip (4Mb)

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. You need to override the isContainer method in your ChildPropertyTreeModel so that you actually display the leaf nodes. At the moment you are only displaying containers.

    // create the list of root nodes:
    List nodes = new ArrayList();
    nodes.add(zoo);
    zooTreeModel = new ChildPropertyTreeModel(nodes, “children”){
    public boolean isContainer(){
    return ((GenericZooNode)getRowData()).getChildCount() > 0;
    }

    };