7 Replies Latest reply on May 7, 2008 7:39 PM by rajarchu

    Dynamic Tree

    thetoad

      Good evening!

      I'm trying to make a dynamic richTree, like the tree on the iceFaces (http://component-showcase.icefaces.org/component-showcase/index.jsp?rvn=1#). But i'm not very succesfull, after a whole day of attempts...

      How can I make the creation of a new treeNode using a commandButton and a inputText at runtime?

      This is the code I'm using:

      package paco.ufscar;
      
      import org.richfaces.component.UITree;
      import org.richfaces.event.NodeSelectedEvent;
      import org.richfaces.model.TreeNode;
      import org.richfaces.model.TreeNodeImpl;
      
      public class BeanTree {
       private static TreeNode rootNode = null;
       private String nodeTitle;
       private String txtTopico;
       private static int index = 1;
       private static int noSelecionado;
      
       public void addNode()
       {
       TreeNodeImpl nodeImpl = new TreeNodeImpl();
       nodeImpl.setData(txtTopico);
       rootNode.addChild(new Integer(index), nodeImpl);
       index++;
       }
      
       public void removeNode()
       {
       rootNode.removeChild(???????????);
       }
      
       public TreeNode getTreeNode() {
       if (rootNode == null) {
       rootNode = new TreeNodeImpl();
       }
       return rootNode;
       }
      
       public void processSelection(NodeSelectedEvent event) {
       UITree tree = (UITree) event.getComponent();
       nodeTitle = (String) tree.getRowData();
       }
      
       public String getNodeTitle() {
       return nodeTitle;
       }
      
       public void setNodeTitle(String nodeTitle) {
       this.nodeTitle = nodeTitle;
       }
      
       public String getTxtTopico() {
       return txtTopico;
       }
      
       public void setTxtTopico(String txtTopico) {
       this.txtTopico = txtTopico;
       }
      }
      


      <rich:tree id="RichTree" style="width:300px" nodeSelectListener="#{BeanTree.processSelection}"
      reRender="selectedNode" ajaxSubmitSelection="true" switchType="ajax"
      value="#{BeanTree.treeNode}" var="item">
      </rich:tree>
      
      <h:outputText escape="false" value="Selected Node: #{BeanTree.nodeTitle}" id="selectedNode" /> 
      <h:commandButton value="Exclude" action="#{BeanTree.removeNode}"/>
      


        • 1. Re: Dynamic Tree
          thetoad

          up

          • 2. Re: Dynamic Tree

            Do you mean it up and running?

            • 3. Re: Dynamic Tree
              thetoad

              Oh no, sorry, I only wanted to put my topic on the first page... however, I haven't found any solutions yet.

              Please, any hint will be really welcome.

              Thanks!

              • 4. Re: Dynamic Tree
                nbelaevski

                Hello,

                Sorry, but I couldn't reproduce the issue. My code:

                /**
                 * License Agreement.
                 *
                 * JBoss RichFaces - Ajax4jsf Component Library
                 *
                 * Copyright (C) 2007 Exadel, Inc.
                 *
                 * This library is free software; you can redistribute it and/or
                 * modify it under the terms of the GNU Lesser General Public
                 * License version 2.1 as published by the Free Software Foundation.
                 *
                 * This library is distributed in the hope that it will be useful,
                 * but WITHOUT ANY WARRANTY; without even the implied warranty of
                 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
                 * Lesser General Public License for more details.
                 *
                 * You should have received a copy of the GNU Lesser General Public
                 * License along with this library; if not, write to the Free Software
                 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
                 */
                
                package org.richfaces;
                
                import java.util.UUID;
                
                import org.richfaces.component.UITree;
                import org.richfaces.event.NodeSelectedEvent;
                import org.richfaces.model.TreeNode;
                import org.richfaces.model.TreeNodeImpl;
                
                /**
                 * @author $Autor$
                 *
                 */
                public class Bean {
                 private static TreeNode rootNode = null;
                 private String nodeTitle;
                 private String txtTopico;
                 private static int index = 1;
                 private static int noSelecionado;
                
                 public void addNode()
                 {
                 TreeNodeImpl nodeImpl = new TreeNodeImpl();
                 nodeImpl.setData(nodeTitle + " " + UUID.randomUUID().toString());
                 rootNode.addChild(new Integer(index), nodeImpl);
                 index++;
                 }
                
                 public void removeNode()
                 {
                 //rootNode.removeChild(???????????);
                 }
                
                 public TreeNode getTreeNode() {
                 if (rootNode == null) {
                 rootNode = new TreeNodeImpl();
                 }
                 return rootNode;
                 }
                
                 public void processSelection(NodeSelectedEvent event) {
                 UITree tree = (UITree) event.getComponent();
                 nodeTitle = (String) tree.getRowData();
                 }
                
                 public String getNodeTitle() {
                 return nodeTitle;
                 }
                
                 public void setNodeTitle(String nodeTitle) {
                 this.nodeTitle = nodeTitle;
                 }
                
                 public String getTxtTopico() {
                 return txtTopico;
                 }
                
                 public void setTxtTopico(String txtTopico) {
                 this.txtTopico = txtTopico;
                 }
                }


                <!-- DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                 "http://www.w3.org/TR/html4/loose.dtd"-->
                
                <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
                <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
                <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
                <%@ taglib uri="http://labs.jboss.com/jbossrichfaces/ui/drag-drop" prefix="dnd" %>
                <%@ taglib uri="http://labs.jboss.com/jbossrichfaces/ui/tree" prefix="rich"%>
                <html>
                 <body>
                 <f:view>
                 <h:form>
                 <rich:tree id="RichTree" style="width:300px" reRender="selectedNode"
                
                 ajaxSubmitSelection="true" switchType="ajax"
                 value="#{bean.treeNode}" var="item">
                 </rich:tree>
                
                 <h:inputText value="#{bean.nodeTitle}" />
                 <h:commandButton value="Add node" action="#{bean.addNode}" />
                 </h:form>
                 </f:view>
                 </body>
                </html>


                Could you please check what's your bean scope?

                • 5. Re: Dynamic Tree
                  nbelaevski

                  BTW, tried 3.2.1/3.1.5 SNAPSHOTs

                  • 6. Re: Dynamic Tree
                    thetoad

                    I already solved this problem; this is the solution i've got:
                    (bean scope - session; richFaces3.1.3; tomcat6):

                    Description: a dynamic richTree that creates and excludes nodes at runtime

                    
                    package paco.ufscar;
                    
                    import org.richfaces.component.UITree;
                    import org.richfaces.event.NodeSelectedEvent;
                    import org.richfaces.model.TreeNode;
                    import org.richfaces.model.TreeNodeImpl;
                    import paco.ufscar.SelectedNodeIndex;
                    
                    public class BeanTree {
                     private TreeNode rootNode = null;
                     private TreeNode selectedNode = null;
                     private String nodeTitle;
                     private String txtTopic;
                     private int selectedNodeIndex;
                     private int index = 1;
                    
                     public void addNode()
                     {
                     TreeNodeImpl nodeImpl = new TreeNodeImpl();
                     if(txtTopic=="") txtTopic="(empty)";
                     nodeImpl.setData(txtTopic);
                     if(selectedNode==null) selectedNode = rootNode;
                     selectedNode.addChild(new Integer(index), nodeImpl);
                     index++;
                     txtTopic = "";
                     }
                    
                     public void removeNode()
                     {
                     if(selectedNode!=null){
                     selectedNode.getParent().removeChild(selectedNodeIndex);
                     selectedNode = rootNode;
                     selectedNodeIndex = 0;
                     nodeTitle = "";
                     }
                     }
                    
                     public void processSelection(NodeSelectedEvent event){
                     UITree tree = (UITree) event.getComponent();
                    
                     nodeTitle = (String) tree.getRowData();
                     selectedNode = tree.getTreeNode();
                     selectedNodeIndex = new SelectedNodeIndex(tree.getRowKey().toString()).getValue();
                    /*
                     TreeNode node = new TreeNodeImpl();
                     node.setData("Works!");
                     tree.getTreeNode().addChild(new Integer (1), node); //Adiciona um childNode no nó selecionado
                    */
                    /* Nenhum desses funcionou
                     System.out.println(tree.getData());
                     System.out.println(tree.getId());
                     System.out.println(tree.getRowCount());
                     System.out.println(tree.getRowIndex());
                    
                     Em Teste
                     System.out.println(tree.getRowData());
                     System.out.println(tree.getRowKey()); Retorna o indice completo do no selecionado, desde o rootNode até ele (Ex: 3:1:2)
                     System.out.println(tree.getRowKeyVar());
                     System.out.println(tree.getRows());
                     System.out.println(tree.getStatus());
                     System.out.println(tree.getFacet(nodeTitle));
                    */
                     }
                    
                     public TreeNode getTreeNode() {
                     if (rootNode == null) {
                     rootNode = new TreeNodeImpl();
                     }
                     return rootNode;
                     }
                    
                     public String getNodeTitle() {
                     return nodeTitle;
                     }
                    
                     public String getTxtTopic() {
                     return txtTopic;
                     }
                    
                     public void setTxtTopic(String txtTopic) {
                     this.txtTopic = txtTopic;
                     }
                    }
                    
                    


                    package paco.ufscar;
                    
                    public class SelectedNodeIndex {
                     private int value;
                    
                     public SelectedNodeIndex(String name){
                     int lastPos = 0;
                     value = 0;
                     lastPos = name.lastIndexOf(':') + 1;
                     value = Integer.parseInt(name.substring(lastPos));
                     }
                    
                     public int getValue() {
                     return value;
                     }
                    }
                    


                    <h:form>
                     <p><strong>Topic: </strong> (<a href="">example</a>)</p>
                     <h:inputText value="#{BeanTree.txtTopic}"/>
                     <h:commandButton type="submit" value="Add" action="#{BeanTree.addNode}" style="margin-left: 2px">
                     <a4j:support event="onsubmit" reRender="RichTree"/>
                     </h:commandButton>
                     <br />
                     <br />
                    
                     <rich:tree id="RichTree" style="width:300px" nodeSelectListener="#{BeanTree.processSelection}"
                     reRender="selectedNode" ajaxSubmitSelection="true" switchType="ajax"
                     value="#{BeanTree.treeNode}" var="item" icon="/img/tree.gif" iconLeaf="/img/tree.gif">
                     </rich:tree>
                     <h:outputText escape="false" value="Selected node: #{BeanTree.nodeTitle}" id="selectedNode" /> 
                     <h:commandButton value="Exclude" action="#{BeanTree.removeNode}"/>
                     </h:form>
                    


                    But now, I'm facing a new one, how can I persist this tree??? Any suggestion will be really helpfull!

                    Thanks for the interest,
                    Raphael.

                    • 7. Re: Dynamic Tree
                      rajarchu

                      Hi am rajesh and i need help from you.. am sorry i was not able to answer ur question.

                      my question is..
                      am not able to select the node. i am able to generate tree from database and i want to display the selected node . but am not able to do it. can you please tell me how to do it. and what i ve to do in web.xml any actionlistener i ve to mention or in code i ve to do anything.
                      thanks in advance