5 Replies Latest reply on Feb 2, 2012 11:57 AM by Lazy Guy

    Rich:tree is not populating...

    Lazy Guy Newbie

      Hi to all,

       

      Am new to this community. and am a newbie to jsf and to richfaces .

      but I need to populate rich:tree component...

       

      Jsf page:

      -----------

       

      1. <h:form>     
      2.         <h:panelGrid columns="2" width="100%" columnClasses="col1,col2"
      3.          
      4.             <rich:tree style="width:300px" nodeSelectListener="#{simpleTreeBean.processSelection}"  
      5.                 reRender="selectedNode" ajaxSubmitSelection="true"  switchType="client" 
      6.                 value="#{simpleTreeBean.treeNode}" var="item"
      7.             </rich:tree> 
      8.              
      9.             <h:outputText escape="false" value="Selected Node: #{simpleTreeBean.nodeTitle}" id="selectedNode" /> 
      10.          
      11.         </h:panelGrid> 

       

      Java Bean:

       

      1. import java.io.IOException; 
      2. import java.io.InputStream; 
      3. import java.util.ArrayList; 
      4. import java.util.Iterator; 
      5. import java.util.List; 
      6. import java.util.Map; 
      7. import java.util.Properties; 
      8.  
      9. import javax.faces.FacesException; 
      10. import javax.faces.component.UIComponent; 
      11. import javax.faces.context.ExternalContext; 
      12. import javax.faces.context.FacesContext; 
      13.  
      14. import org.richfaces.component.UITree; 
      15. import org.richfaces.component.html.HtmlTree; 
      16. import org.richfaces.event.NodeSelectedEvent; 
      17. import org.richfaces.model.TreeNode; 
      18. import org.richfaces.model.TreeNodeImpl; 
      19.  
      20. public class SimpleTreeBean { 
      21.      
      22.     private TreeNode rootNode = null
      23.     private List<String> selectedNodeChildren = new ArrayList<String>();     
      24.      
      25.     private String nodeTitle; 
      26.     private static final String DATA_PATH = "/simple-tree.properties"
      27.      
      28.     private void addNodes(String path, TreeNode node, Properties properties) { 
      29.         boolean end = false
      30.         int counter = 1
      31.          
      32.         while (!end) { 
      33.             String key = path != null ? path + '.' + counter : String.valueOf(counter); 
      34.  
      35.             String value = properties.getProperty(key); 
      36.             if (value != null) { 
      37.                 TreeNodeImpl nodeImpl = new TreeNodeImpl(); 
      38.                 nodeImpl.setData(value); 
      39.                 node.addChild(new Integer(counter), nodeImpl); 
      40.                 addNodes(key, nodeImpl, properties); 
      41.                 counter++; 
      42.             } else
      43.                 end = true
      44.             } 
      45.         } 
      46.     } 
      47.      
      48.     private void loadTree() { 
      49.         FacesContext facesContext = FacesContext.getCurrentInstance(); 
      50.         ExternalContext externalContext = facesContext.getExternalContext(); 
      51.         InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH); 
      52.         try
      53.             Properties properties = new Properties(); 
      54.             properties.load(dataStream); 
      55.              
      56.             rootNode = new TreeNodeImpl(); 
      57.             addNodes(null, rootNode, properties); 
      58.              
      59.         } catch (IOException e) { 
      60.             throw new FacesException(e.getMessage(), e); 
      61.         } finally
      62.             if (dataStream != null) { 
      63.                 try
      64.                     dataStream.close(); 
      65.                 } catch (IOException e) { 
      66.                     externalContext.log(e.getMessage(), e); 
      67.                 } 
      68.             } 
      69.         } 
      70.     } 
      71.      
      72.     public void processSelection(NodeSelectedEvent event) { 
      73.         HtmlTree tree = (HtmlTree) event.getComponent(); 
      74.         nodeTitle = (String) tree.getRowData(); 
      75.         selectedNodeChildren.clear(); 
      76.         TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey()); 
      77.         if (currentNode.isLeaf()){ 
      78.             selectedNodeChildren.add((String)currentNode.getData()); 
      79.         }else 
      80.         { 
      81.             Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren(); 
      82.             while (it!=null &&it.hasNext()) { 
      83.                 Map.Entry<Object, TreeNode> entry = it.next(); 
      84.                 selectedNodeChildren.add(entry.getValue().getData().toString());  
      85.             } 
      86.         } 
      87.     } 
      88.      
      89.     public TreeNode getTreeNode() { 
      90.         if (rootNode == null) { 
      91.             loadTree(); 
      92.         } 
      93.          
      94.         return rootNode; 
      95.     } 
      96.  
      97.  
      98.      
      99.     public String getNodeTitle() { 
      100.         return nodeTitle; 
      101.     } 
      102.  
      103.     public void setNodeTitle(String nodeTitle) { 
      104.         this.nodeTitle = nodeTitle; 
      105.     } 
      106.  
      107.      

       

       

      But am not getting tree component.

       

      What to do?

      It's Just printing the output : Selected Node: but not populating tree component.

      I have one more doubt..wher to keep this properties file..

      richfaces Jar version that i used:

       

      richfaces-api-3.2.2.SR1.jar

      richfaces-impl-3.2.2.SR1.jar

      richfaces-ui-3.2.2.SR1.jar

       

      Thanks in advance..and pretty urgent...

        • 2. Re: Rich:tree is not populating...
          Lazy Guy Newbie

          As u said.. I kept the treenode tag:

           

          <rich:treenode>

              <h:outputText value="#{item}"></h:outputText>

          </rich:treenode>

           

          But no result..

          I tried the code from the below url..

          http://www.mastertheboss.com/web-interfaces/270-richfaces-tree-example.html

           

          What is going wrong.. am not getting..

           

          any working code?

          • 3. Re: Rich:tree is not populating...
            Lazy Guy Newbie

            .............

            Help me... in populating tree structure from properties file...

            • 4. Re: Rich:tree is not populating...
              Mahesh Bonagiri Newbie

              Hi Ali,

               

              I am able to populate the tree. i copied the code from the url u gave.

               

              This is the working code,try to use as it is, then modify with your need

              i removed the tree binding from the jsp file and added switchType="client" ajaxSubmitSelection="true".

               

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

              <ui:composition xmlns="http://www.w3.org/1999/xhtml"

                        xmlns:ui="http://java.sun.com/jsf/facelets"

                        xmlns:h="http://java.sun.com/jsf/html"

                        xmlns:f="http://java.sun.com/jsf/core"

                        xmlns:a4j="http://richfaces.org/a4j"

                        xmlns:rich="http://richfaces.org/rich">

               

               

               

               

                        <head>

               

               

              <title>tree</title>

                        </head>

                        <body>

                                  <f:view>

                                            <h:form>

                                                      <h:panelGrid columns="2" width="100%" columnClasses="col1,col2">

               

               

                                                                <rich:tree style="width:300px"

                                                                          nodeSelectListener="#{simpleTreeBean.processSelection}"

                                                                          reRender="selectedNode" switchType="client" ajaxSubmitSelection="true"

               

                                                                          value="#{simpleTreeBean.treeNode}" var="item" ajaxKeys="#{null}">

                                                                </rich:tree>

               

               

                                                                <h:outputText escape="false"

                                                                          value="Selected Node: #{simpleTreeBean.nodeTitle}"

                                                                          id="selectedNode" />

               

               

                                                      </h:panelGrid>

               

               

                                            </h:form>

                                  </f:view>

                        </body>

              </ui:composition>

               

              import java.io.IOException;

              import java.io.InputStream;

              import java.util.ArrayList;

              import java.util.Iterator;

              import java.util.List;

              import java.util.Map;

              import java.util.Properties;

               

               

              import org.richfaces.component.html.HtmlTree;

              import org.richfaces.event.NodeSelectedEvent;

              import org.richfaces.model.TreeNode;

              import org.richfaces.model.TreeNodeImpl;

               

               

              public class SimpleTreeBean {

                        protected org.richfaces.component.UITree sampleTreeBinding;

               

               

                        public org.richfaces.component.UITree getSampleTreeBinding() {

                                  return sampleTreeBinding;

                        }

               

               

                        public void setSampleTreeBinding(

                                            org.richfaces.component.UITree sampleTreeBinding) {

                                  this.sampleTreeBinding = sampleTreeBinding;

                        }

               

               

                        private TreeNode rootNode = null;

                        private List<String> selectedNodeChildren = new ArrayList<String>();

               

               

                        private String nodeTitle;

               

               

                        private void addNodes(String path, TreeNode node, Properties properties) {

                                  boolean end = false;

                                  int counter = 1;

               

               

                                  while (!end) {

                                            String key = path != null ? path + '.' + counter : String

                                                                .valueOf(counter);

               

               

                                            String value = properties.getProperty(key);

                                            if (value != null) {

                                                      TreeNodeImpl nodeImpl = new TreeNodeImpl();

               

               

                                                      nodeImpl.setData(value);

                                                      node.addChild(new Integer(counter), nodeImpl);

                                                      addNodes(key, nodeImpl, properties);

                                                      counter++;

                                            } else {

                                                      end = true;

                                            }

                                  }

                        }

               

               

                        private void loadTree() {

                                  Properties props = new Properties();

                                  InputStream stream = this.getClass().getClassLoader()

                                                      .getResourceAsStream("com/xyz/resources/richtree.properties");

                                  try {

                                            props.load(stream);

                                  } catch (IOException e) {

                                            // TODO Auto-generated catch block

                                            e.printStackTrace();

                                  }

               

               

                                  rootNode = new TreeNodeImpl();

                                  addNodes(null, rootNode, props);

                        }

               

               

                        public void processSelection(NodeSelectedEvent event) {

                                  HtmlTree tree = (HtmlTree) event.getComponent();

                                  nodeTitle = (String) tree.getRowData();

                                  selectedNodeChildren.clear();

                                  TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());

                                  if (currentNode.isLeaf()) {

               

               

                                            selectedNodeChildren.add((String) currentNode.getData());

                                  } else {

                                            Iterator<Map.Entry<Object, TreeNode>> it = currentNode

                                                                .getChildren();

                                            while (it != null && it.hasNext()) {

                                                      Map.Entry<Object, TreeNode> entry = it.next();

                                                      selectedNodeChildren.add(entry.getValue().getData().toString());

                                            }

                                  }

                        }

               

               

                        public TreeNode getTreeNode() {

                                  if (rootNode == null) {

                                            loadTree();

                                  }

                                  return rootNode;

                        }

               

               

                        public String getNodeTitle() {

                                  return nodeTitle;

                        }

               

               

                        public void setNodeTitle(String nodeTitle) {

                                  this.nodeTitle = nodeTitle;

                        }

               

               

              }

              • 5. Re: Rich:tree is not populating...
                Lazy Guy Newbie

                Thank you soooooooooooooooooooooooooooooooo much...... Mahesh Bonagiri..