Hi all, I'm experimenting with the tree2 component, but I'm facing a problem: I
added a checkbox for each node, trying to follow the same method I would use for
a datatable. I then added a commandButton for submitting page data.

The problem is, when the commandButton action is performed on the server side, I
can't extract any node from the tree component.

Here is the code:

The backing bean:

public class MyBean {

    private UITreeData tree = null;
    private UISelectBoolean checkbox = null;

    //Creates the TreeModel to feed the tree
    public TreeModel getTreeData() {
        TreeNode root = this.addNode(new MyTreeNode("0", "root"), 0);
        return new TreeModelBase(root);
    }

    //Adds some fake nodes to the tree
    private TreeNode addNode(MyTreeNode node, int level) {

        TreeNodeBase node1 = null;

        if(level == 0)
            node1 = new TreeNodeBase("root",  node.getName(), node.getId(), 
false);
        else
            node1 = new TreeNodeBase("central-node", node.getName(),
node.getId(), false);

        Vector children = new Vector();

        if(level < 3) {
            children.add( new MyTreeNode(level + ".1", "node" + level + ".1") );
            children.add( new MyTreeNode(level + ".2", "node" + level + ".2") );
            children.add( new MyTreeNode(level + ".3", "node" + level + ".3") );

            for(int i = 0; i < children.size(); i++) {
                TreeNode child =
this.addNode((MyTreeNode)children.elementAt(i), level + 1);
                node1.getChildren().add(child);
            }
        }

        return node1;
    }

    public UITreeData getTree() {
        return tree;
    }

    public void setTree(UITreeData tree) {
        this.tree = tree;
    }

    public UISelectBoolean getCheckbox() {
        return checkbox;
    }

    public void setCheckbox(UISelectBoolean checkbox) {
        this.checkbox = checkbox;
    }

    public void submitTree(ActionEvent e) {

        TreeNode root = this.tree.getNode();

        if(root != null) {
           ...   //   *** ROOT IS ALWAYS NULL. THIS BLOCK IS NEVER EXECUTED ***
        }
    }
}

MyTreeNode is a simple bean:

class MyTreeNode {

   private String name;
   private String id;

   public MyTreeNode(String id, String name) {
        this.id = id;
        this.name = name;
   }

   public String getId() { return id; }
   public String getName() { return name; }
   public void setId(String id) { this.id = id; }
   public void setName(String name) { this.name = name; }
}

And here's the view:

<[EMAIL PROTECTED] uri="http://java.sun.com/jsf/html"; prefix="h"%>
<[EMAIL PROTECTED] uri="http://myfaces.apache.org/tomahawk"; prefix="t"%>
<[EMAIL PROTECTED] uri="http://java.sun.com/jsf/core"; prefix="f"%>

<f:view>
    <h:form>
    <t:tree2 binding="#{myBean.tree}"
             value="#{myBean.treeData}"
             var="node"
             varNodeToggler="t"
             clientSideToggle="false"
             showLines="true"
             showRootNode="true"
             preserveToggle="true"
             id="tree">
        <f:facet name="root">
            <h:panelGroup>
                    <h:outputText value="<strong>root</strong>"
styleClass="nodeFolder" escape="false"/>
            </h:panelGroup>
        </f:facet>
        <f:facet name="central-node">
            <h:panelGroup>
                <h:selectBooleanCheckbox id="relation"
binding="#{myBean.checkbox}" />
                <h:commandLink immediate="true" action="#{t.toggleExpanded}">
                    <h:outputText value="<strong>#{node.description}</strong>"
styleClass="nodeFolder" escape="false" />
                </h:commandLink>
            </h:panelGroup>
        </f:facet>
    </t:tree2>                            

    <h:commandButton value="do something" action="#{myBean.submitTree}" />

    </h:form>
</f:view>


The problem is in the submitTree() method: in fact the root node is always null!

begin:vcard
fn:Enrico Triolo
n:Triolo;Enrico
org:ITC-irst
adr:;;Via Sommarive, 18;Povo;Trento;;Italy
email;internet:[EMAIL PROTECTED]
version:2.1
end:vcard

Reply via email to