-1

I have a Tree primefaces component that i customized in my code, here is the code of this jsf page that contain my treeNodes :

<h:form id="formNode">
                        <p:commandButton value="+Add"  oncomplete="PF('w_addRoot').show();" />

                        <p:tree value="#{treeManagedBean.root}" var="node" animate="true" dynamic="true"
                                selection="#{treeManagedBean.selectedNode}" selectionMode="single"
                                 >
                            <p:treeNode expandedIcon="ui-icon ui-icon-folder-open"
                                        collapsedIcon="ui-icon ui-icon-folder-collapsed">
                                <h:panelGrid style="margin-top: -9px;" columns="2" >
                                    <h:outputText value="#{node}"/>
                                    <p:commandButton action="#{treeManagedBean.createNodeHere}" icon="iconForkTree" style="margin-top: 5px; margin-left:30px; max-width: 15px; max-height: 15px;">
                                        <f:setPropertyActionListener target="#{treeManagedBean.currNodeName}" value="#{node}" />
                                    </p:commandButton>    

                                </h:panelGrid>

                            </p:treeNode>
                            <p:treeNode type="document" icon="ui-icon ui-icon-document">
                                <h:link value="#{node}"/>
                            </p:treeNode>

                            <p:ajax event="select" listener="#{treeManagedBean.onNodeSelect}"></p:ajax>
                            <p:ajax event="unselect" listener="#{treeManagedBean.onNodeUnSelect}"></p:ajax>
                            <p:ajax event="expand" listener="#{treeManagedBean.onNodeExpand}"></p:ajax>
                            <p:ajax event="collapse" listener="#{treeManagedBean.onNodeCollapse}"></p:ajax>
                        </p:tree>
                    </h:form>

here is the function from the managedBean that should create the child node :

public void createNodeHere(){
        System.out.println("current node Name :  "+currNodeName);
        // get the current node from the giving name currNodeName
        currNodeName="";
    }

So i want to create a child treeNode when the user click on the commandButton (with plus icon ) ,and the parent of this child treeNode should be the current node . The problem is that i can't get the current Node ( i get only its name ). so can i get the treeNode object from its name ? if not , there is a way to do that without using event ( select , unselect ... )

Note that using f:ajax tag with event attribut(select ,expand ,collapse ,unselect) i can get the current node ,but its not my goal.

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39

1 Answers1

1

I founded the solution , so what I have done is that I pass the name of the current node to my managedBean ,then I get the liste of all node ( listeOFNoeud list of all existe node ) and in the function that create the node :

public void createNewRoot(ActionEvent event) {

        System.out.println("---------------------- Create Node -----------------------");
        System.out.println("name of the child " + nameOfTreeNode + "\n Name of the parent : " + nameOfParent);
        TreeNode t=new DefaultTreeNode();
        for (TreeNode treeNode : listeOFNoeud) {
            if(nameOfParent.equals(treeNode.getData().toString())){
                t=new DefaultTreeNode(nameOfTreeNode, treeNode);
            }
        }
        listeOFNoeud.add(t);
        System.out.println("Node Added");
    }

nameOfTreeNode : the name of the child node that will be created.

nameOfParent: the name of the current node (the parent of the child node ).

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39