Pages

Thursday, November 19, 2009

Find and Expand all nodes of an ADF Tree

I want to find and expand all nodes of an ADF Tree and I saw an Oracle Forum post of Kenyatta which gave me a nice solution.
First some usefull methods.

private void expandTreeChildrenNode( RichTree rt
, FacesCtrlHierNodeBinding node
, List<Key> parentRowKey) {
ArrayList children = node.getChildren();
List<Key> rowKey;

if ( children != null ) {
for (int i = 0; i < children.size(); i++) {
rowKey = new ArrayList<Key>();
rowKey.addAll(parentRowKey);
rowKey.add(((FacesCtrlHierNodeBinding)children.get(i)).getRowKey());
rt.getDisclosedRowKeys().add(rowKey);
if (((FacesCtrlHierNodeBinding)(children.get(i))).getChildren() == null)
continue;
expandTreeChildrenNode(rt
,(FacesCtrlHierNodeBinding)(node.getChildren().get(i))
, rowKey);
}
}
}

// find a jsf component
private UIComponent getUIComponent(String name) {
FacesContext facesCtx = FacesContext.getCurrentInstance();
return facesCtx.getViewRoot().findComponent(name) ;
}

private UIComponent getUIComponent(UIComponent component,String name ){
List<UIComponent> items = component.getChildren();
for ( UIComponent item : items ) {
UIComponent found = getUIComponent(item,name);
if ( found != null ) {
return found;
}
if ( item.getId().equalsIgnoreCase(name) ) {
return item;
};
}
return null;
}

Now find the ADF tree in a region and expand the main and child nodes of this tree

// get the dymamic region of the main page
RichRegion region = (RichRegion)getUIComponent("dynam1");

if ( region != null) {
// find tree 2 and expand this tree
RichTree rt = (RichTree)getUIComponent(region,"t2");
if ( rt != null ) {
int rowCount = rt.getRowCount();
List<Key> rowKey;
for (int j = 0; j < rowCount; j++) {
// expand the main nodes
FacesCtrlHierNodeBinding node = (FacesCtrlHierNodeBinding)rt.getRowData(j);
rowKey = new ArrayList<Key>();
rowKey.add(node.getRowKey());
rt.getDisclosedRowKeys().add(rowKey);
rt.setRowKey(rowKey);
// expand the child nodes of the main nodes
expandTreeChildrenNode(rt , node, rowKey);
}
}
}

3 comments:

  1. Edwin - a little caution - expanding all nodes can be dangerous... What if the tree has 50000 nodes? I would probably put some sort of limiting logic on the loop.

    I mean, it's a sweet and tight recursive method but ...

    ReplyDelete
  2. Hi Edwin - long time no see!

    I used this code in a project and worked like a charm, but for some reason the following line caused the tree(table) to not show any records sometimes:

    rt.setRowKey(rowKey);

    Removing this line did the trick; although I must admit our tree only has one root-node.

    ReplyDelete
  3. Hi,

    I have to access parent node of current node in a tree with 4 level of nodes.Do you have any idea how to do this from jsff page?........something like #{node.ResponseCode=='EGO_COMPLETED'}.

    Please suggest how to do this.
    Thanks in adavance

    ReplyDelete