How do I access the actual objects in a RichFaces 4 tree?
davemulligan Feb 29, 2012 4:02 AMHi all,
I'm really struggling with what seems like a simple problem. I've built a rich:tree component, and it renders just fine. But, when I select a node, I cannot work out how to access the object that was originally put there. For example, I have a tree of Product objects, arranged into ProductFamilies. I'd like to work out how to get to the Product object when I click on it. Right now I am getting a reference to the SwingTreeNodeImpl object, which must be the runtime class of the node. I feel I must be missing something obvious, but I haven't worked out what it is, despite having tried almost every suggestion I can find.
Here is my tree definition (some node types left out for brevity):
{code}
<rich:tree
id="sku"
nodeType="#{node.type}"
var="node"
value="#{treeBean.rootNodes}"
toggleType="ajax"
selectionType="ajax"
selectionChangeListener="#{treeBean.selectionChanged}"
>
<rich:treeNode type="ProductFamily">
#{node.displayString}
</rich:treeNode>
<rich:treeNode type="Product">
#{node.displayString}
</rich:treeNode>
</rich:tree>
{code}
Here is the part of my TreeBean class that defines the root nodes:
{code}
private List<TreeNode> rootNodes = new ArrayList<TreeNode>();
public TreeBean() {
for (ProductFamily pf : ProductFamily.values()) {
rootNodes.add(pf);
}
}
{code}
and some parts of the ProductFamily class that seem relevant:
{code}
public enum ProductFamily implements TreeNode {
...
public List<Product> getProducts() {
return getDAO().getAllProductsinFamily(this);
}
...
@Override
public Enumeration<Product> children() {
return Collections.enumeration(getProducts());
}
@Override
public boolean getAllowsChildren() {
return true;
}
... and so on ...
{code}
But, when I run the code I plagiarized from here (http://richfaces-showcase.appspot.com/richfaces/component-sample.jsf?demo=tree&skin=blueSky), I end up with an exception that the SwingTreeNodeImpl class doesn't have the method I'm trying to call when I ask for "name". Which is, of course, perfectly correct, but how do I get my Product or ProductFamily out of the SwingTreeNodeImpl?
Thanks, and I really hope I haven't missed anything obvious: this is my first project working with Richfaces.
- Dave