Friday 21 October 2011

ADF Tree : Row Disclosure Listener which expands below the disclosed node

I recently had the scenario where more than one level of a tree node had to be expanded on the disclosure of a particular tree node.

For example, say you have the below tree:
-A
-B
  - B1
  - B2
    - B2a
    - B2b
  - B3
-C
then, on the disclosure of tree node B you would want all the children (B1, B2, B3, B2a, B2b) of tree node B to be expanded.

I've done it as follow. I've added the following property to my ADF Tree:
rowDisclosureListener="#{treeBean.discloseTreeNodeChildren}" 

Then I implemented the referenced method in my "treeBean" backing bean. It assumes a tree binding called: "tree." See the listing below:
public void discloseTreeNodeChildren(RowDisclosureEvent event) {
    RowKeySet addedKeys = event.getAddedSet();
    Iterator addedIt = addedKeys.iterator();
    if(addedIt.hasNext()) {
        Object addedKey = addedIt.next();
        TreeModel treeModel = (TreeModel) this.tree.getValue();
        JUCtrlHierNodeBinding nodeBinding = (JUCtrlHierNodeBinding)
               treeModel.getRowData(addedKey);
        String isLeaf = (String) nodeBinding.getAttribute("isLeaf");
       
          // you have to have a condition to break the series of disclosure events!!
        if(!(new Boolean(isLeaf))) {
            TreeUtility.expandAllNodes(nodeBinding, this.tree.getDisclosedRowKeys(), 0, 1);
            AdfFacesContext.getCurrentInstance().addPartialTarget(this.tree);
        }
    }
}

Now the utility method called: "expandAllNodes()" I've implemented as such:
protected static void expandAllNodes(JUCtrlHierNodeBinding nodeBinding, RowKeySet
          disclosedKeys) {   
    List<JUCtrlHierNodeBinding> childNodes = (List<JUCtrlHierNodeBinding>)
        nodeBinding.getChildren();     
    ArrayList newKeys = new ArrayList();
    if(childNodes != null) {
        for(JUCtrlHierNodeBinding node : childNodes) {           
            newKeys.add(node.getKeyPath());
            // recursive call to the method expandAllNodes()         
            expandAllNodes(node, disclosedKeys);
        }
    }
    disclosedKeys.addAll(newKeys);     

}
A handy addition to the utility method above is to add currentLevel and targetLevel integers in order to apply a boundary to the expansion of nodes. This gives the ability to only expand n levels below the disclosed node...comes in handy in those "big oak trees!!" :-)

5 comments:

  1. I have requirement that expand of parent node should expand the child node also of that Parent

    Department->Employee->Job History

    If user expand the Department then it should expand both Department and Employee.

    I was tried to achieve this last one week but could not able to do..
    Please any one help on this.

    ReplyDelete
  2. Hi Karvendan,

    Have you tried the approach in my post above? It does work!!

    Regards,

    ReplyDelete
  3. Hi,
    Is there anyway to disable this expand and collapse functionality of tree?
    I just tweaked discloseTreeNodeChildren() function and get the removedKey set from the event and add that in disclosedRowKeys()

    public void treeDisclosureListener(RowDisclosureEvent rowDisclosureEvent)
    {
    // Add event code here...
    RichTreeTable component = (RichTreeTable)rowDisclosureEvent.getComponent();
    RowKeySet keySet = rowDisclosureEvent.getRemovedSet();
    if(keySet != null && keySet.size() > 0)
    {
    // add event as expanding tree
    RowKeySet disclosedRowKeys = component.getDisclosedRowKeys();
    disclosedRowKeys.addAll(keySet);
    }
    }

    it doesn't collapse the tree node but it is showing intermediate cursor.
    So what i want is it shouldn't do anything while clicking on the collapse/expand link.

    regards,

    ReplyDelete
  4. Is there any way to disable expand/collapse of tree?

    ReplyDelete
  5. Tree Utility is not found! can you please tell what is it

    ReplyDelete