This is a follow-up on a post “ADF Faces RC: Expanding an af:tree node by clicking onto its label” by Frank Nimphius.
The problem is to be able to expand/collapse a Tree node by clicking on it’s label. Frank’s solution is based on JavaScript. The solution bellow is a server side and based on a session backing bean.
Here’s a session bean to manage Tree state:
package electro;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ActionEvent;
import oracle.adf.view.faces.component.core.data.CoreTree;
import oracle.adf.view.faces.model.PathSet;
/**
* Bean to manage tree state - open/close it's nodes.
*
* @author Viktoras Agejevas
*/
public class TreeState {
private CoreTree tree;
private Object clickedNodeRowKey;
/**
* Manage clicked tree node event
*
* @param actionEvent
*/
public void clickTree(ActionEvent actionEvent) {
PathSet pathSet = getTree().getTreeState();
List<List> clickedNodePath = (List<List>) clickedNodeRowKey;
Iterator i = getTree().getTreeState().getKeySet().iterator();
boolean closedNode = false;
while (i.hasNext()) {
List openNodePath = (List) i.next();
// close already open node
if (openNodePath.equals(clickedNodeRowKey)) {
pathSet.getKeySet().remove(clickedNodePath);
closedNode = true;
}
}
// open clicked node
if (!closedNode) {
pathSet.getKeySet().add(clickedNodePath);
}
}
public void setTree(CoreTree tree) {
this.tree = tree;
}
public CoreTree getTree() {
return tree;
}
public void setClickedNodeRowKey(Object clickedNodeRowKey) {
this.clickedNodeRowKey = clickedNodeRowKey;
}
public Object getClickedNodeRowKey() {
return clickedNodeRowKey;
}
}
And the Tree definition looks like this:
<af:tree value="#{bindings.RegionsView.treeModel}" var="node" binding="#{treeState.tree}">
<f:facet name="nodeStamp">
<af:commandLink text="#{node}" actionListener="#{treeState.clickTree}">
<af:setActionListener from="#{treeState.tree.rowKey}" to="#{treeState.clickedNodeRowKey}"/>
</af:commandLink>
</f:facet>
</af:tree>
What we do here is bind tree component to a backing bean, on title click we pass current row and call clickTree method which expands/collapses tree node by current row value.
This solution has a little bit more code but I think it’s more bulletproof than the JavaScript one.
Thanks for posting this, Viktoras. That was exactly the example that I was looking for. There’s hardly any documentation from Oracle on programatic use of ADF Faces.
I’m working on ADF Faces Rich Client tree in 11g, do you know what class can be used instead of PathSet of Trinidad? thanks
Sorry, can’t help you much, since I don’t work with ADF anymore. This example worked with 10g; maybe in 11g it should be done in some different way.
You can try searching for an answer or even asking in ADF forum: http://forums.oracle.com/forums/forum.jspa?forumID=83.