views:

601

answers:

1

Hello I've a rich:tree in my JSF like so:

            <rich:tree value="#{MyBacking.treeNodes}" var="item"
                nodeFace="#{item.type}">
                <rich:treeNode type="folder"
                    <h:outputText value="#{item.folder}" />
                </rich:treeNode>
                <rich:treeNode type="file"
                    <h:outputText value="#{item.contfile}" />
                </rich:treeNode>
            </rich:tree>

and the java runtime error

'#{item.type}' Property 'type' not found on type java.lang.String

But I've looked at the javadoc for org.richfaces.model.TreeNodeImpl and there's no mention of a method to assign a type to a tree node. How is this done?

EDIT I have modified the code as per the supplied answer. Although it feels right, my tree is now being ignored. The java looks like this:

In my backing bean (MyBacking):

public TreeNodeImpl<LogTreeItem> getTreeNodes() {
    TreeNodeImpl<LogTreeItem> rootNode = new TreeNodeImpl<LogTreeItem>();
    LogTreeItem rootItem = new LogTreeItem();
    rootItem.setType("folder");
    rootItem.setName("folderName");
    rootNode.setData(rootItem);

    return rootNode;
}

item class:

public class LogTreeItem {
    private String type; 
    private String name; 

    public String getType() {
        return type;
    }

    public void setType(String t) {
        type = t;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }
}

JSF snippet thus is now:

            <rich:tree value="#{MyBacking.treeNodes}" var="item" nodeFace="#{item.type}">
                <rich:treeNode type="folder">
                    <h:outputText value="#{item.name}" />
                </rich:treeNode>
            </rich:tree>

All I'm trying to do at this point is get a tree recognised. As I'm only creating a tree with a rootNode, I was expecting a one node tree output, but instead I'm seeing nothing at all. I know I'm close but I just cannot see what tweak would bring it to life.

Thanks a lot

+1  A: 

It appears that your item is of type String. The nodeFace attribute determines which one of the nodes defined below will be rendered for each item.

You must populate your tree with nodes containing data of your custom type. Let's say your item looks like this:

public class Item {
    private String type; //getter+setter
    private String folder; //getter+setter
    private String contfile; //getter+setter
}

Then treeNodes should actually be rootNode, and must look like this:

rootNode = new TreeNodeImpl<Item>();
for (some loop here) {
    TreeNode<Item> node = new TreeNodeImpl<Item>();
    Item item = ..; //get or create the item;
    node.setData(item);
    node.setParent(rootNode);
    rootNode.addChild("someIdentifier", node);
}

I'd suggest checking out richfaces demo sources to see how it is implemented there.

Bozho
Thanks for the good suggestion - I think it's correct, but I've modified my question to include code based on your answer as it doesn't quite work. Could you suggest why this code doesn't output a tree?
Mark Lewis
@Mark Lewis : yup, I forgot the `rootNode.addChild(..);` :)
Bozho
Thanks. Interestingly, adding in the row with "1" as the identifier and `rootItem` as my node to add, caused eclipse to moan, `The method addChild(Object, TreeNode<LogTreeItem>) in the type TreeNodeImpl<LogTreeItem> is not applicable for the arguments (String, LogTreeItem)`; one of my options was to let `LogTreeItem` implement `TreeNode`; I had to then allow it to add the unimplemented methods to `LogTreeItem` in order for it to build. Does this sound right to you? It worked, but seemed a bit extreme.
Mark Lewis
I've just noticed that the jsf reference `#{item.name}` isn't picking up the name of the root node. sounds like i'm still slightly off. what should my identifier be? The javadoc says an object, but all the sites i've been to use ints.
Mark Lewis
the identifier has the be the unique identifier of the object you are using, using which you can later get the original object.
Bozho