6 Replies Latest reply on Jan 7, 2008 3:11 AM by Nicklas Karlsson

    Richfaces Tree

    Richard Mokua Newbie

      Hi everyone
      I have tried using rich-faces tree, but quite frankly
      am not able to display a tree.
      Note that other rich-faces components are being displayed correctly.
      I followed this
      http://www.jboss.com/index.html?op=loginscreen&module=user
      and this http://www.jboss.com/index.html?op=loginscreen&module=user
      but still got no-where!

      Is there anyone using rich-faces tree?I will greatly appreciate any help.

      Environment:
      seam 1.2.1 GA
      jboss-4.0.5 GA
      richfaces-3.0.1-SNAPSHOT.jar

      Here is the Node :

      package com.triad.treeNodes;
      
      import java.util.Iterator;
      import java.util.LinkedHashMap;
      import java.util.Map;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.richfaces.component.TreeNode;
      
      @Name("tree")
      @Scope(ScopeType.PAGE)
      public class RichTreeNode implements TreeNode {
      
       /**
       *
       */
       private static final long serialVersionUID = 1L;
      
       // private TreeNode treeNode;
       private String name;
      
       private String type;
      
       private Map<Object, TreeNode> childrenMap = new LinkedHashMap<Object, TreeNode>();
      
       public Object getData() {
       // return treeNode;
       return this;
       }
      
       public void setData(Object arg0) {
      
       }
      
       public boolean isLeaf() {
       return childrenMap.size() == 0;
       }
      
       public Iterator getChildren() {
       return childrenMap.entrySet().iterator();
       }
      
       public TreeNode getChild(Object identifier) {
       return childrenMap.get(identifier);
       }
      
       public void addChild(Object identifier, TreeNode treeNode) {
       childrenMap.put(identifier, treeNode);
      
       }
      
       public void removeChild(Object arg0) {
       // TODO Auto-generated method stub
      
       }
      
       public TreeNode getParent() {
      
       return null;
      
       }
      
       public RichTreeNode() {
      
       this.name = "parent-node";
       this.type = "root";
      
       }
      
       public void setParent(TreeNode arg0) {
       // TODO Auto-generated method stub
      
      
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public String getType() {
       return type;
       }
      
       public void setType(String type) {
       this.type = type;
       }
      
      }
      



      Manager bean:
      package com.triad.sessionBeans.tree;
      
      import java.io.Serializable;
      import java.util.Iterator;
      
      import javax.faces.component.UIComponent;
      import javax.faces.component.html.HtmlOutputText;
      import javax.faces.component.html.HtmlPanelGrid;
      import javax.faces.component.html.HtmlPanelGroup;
      
      import javax.faces.context.FacesContext;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.log.Log;
      import org.richfaces.component.html.HtmlPanel;
      import org.richfaces.component.html.HtmlTree;
      import org.richfaces.component.html.HtmlTreeNode;
      
      import com.triad.treeNodes.RichTreeNode;
      
      
      
      @Name("menu")
      @Scope(ScopeType.SESSION)
      public class MenuBean implements Serializable{
      
       /**
       *
       */
       private static final long serialVersionUID = 1L;
       @Logger
       private Log log;
      
       private RichTreeNode richTree;
      
       @Create
       public void init(){
       log.info("********* init **********************");
       richTree=new RichTreeNode();
       richTree.setName("Ka-Name");
       richTree.setType("root");
      
       log.info("has created the tree==>"+richTree);
       RichTreeNode node1=new RichTreeNode();
       node1.setName("Node 1");
       node1.setType("leaf");
      
       richTree.addChild("node1",node1);
       log.info("the tree with children *********");
       Iterator it=richTree.getChildren();
       while(it.hasNext()){
       log.info("***** the node ==>"+it.next());
       }
       }
      
       public RichTreeNode getRichTree() {
      
      
       return richTree;
       }
      
      
       public void setRichTree(RichTreeNode richTree) {
       this.richTree = richTree;
       }
      
      
       public MenuBean() {
       }
      
      
      
      
      }
      


      Now display:
      <!DOCTYPE composition 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:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:rich="http://richfaces.ajax4jsf.org/rich"
       xmlns:a="https://ajax4jsf.dev.java.net/ajax"
       template="layout/template.xhtml">
      
       <ui:define name="body">
      
       <h:messages globalOnly="true" styleClass="message" />
      
       <rich:panel>
       <h:form >
       <h:outputText value="Node - Test"/>
      
       <a:outputPanel ajaxRendered="true">
       <rich:tree id="directoryTree" switchType="ajax"
       value="#{menu.richTree}" var="d"
       style="margin: 10px;width:300px"
      
       >
       <rich:treeNode>
       <h:outputText value="Node - ** #{d.name}"/>
       </rich:treeNode>
       </rich:tree>
       </a:outputPanel>
       </h:form>
       </rich:panel>
      
      
      
       </ui:define>
      </ui:composition>
      


        • 1. Re: Richfaces Tree
          Damian Harvey Master

          The only thing I noticed was that your addChild identifier is a String:

          richTree.addChild("node1",node1);
          Whereas the examples and mine use an Integer. I recall having some problems with this as well so try:
          int myIndex = 0;
          richTree.addChild(new Integer(myIndex++),node1);


          It's also probably worth using the TreeNodeImpl rather than rolling your own until you get it going.

          Cheers,

          Damian.

          • 2. Re: Richfaces Tree
            Juan Rodriguez Newbie

            mokua_ombati:

            Could you display the tree?

            if you could display the tree...could you please show me the code

            Regards

            Jaime

            • 3. Re: Richfaces Tree
              fanto black Newbie

              I have met the same issue, could any one give me an example?
              thanks.

              • 4. Re: Richfaces Tree
                Nicklas Karlsson Master

                Does this work:

                <f:view>
                 <h:form>
                 <rich:panel header="Tree">
                 <rich:tree value="#{treebean.tree}" var="node">
                 <rich:treeNode>
                 <h:outputText value="#{node}"/>
                 </rich:treeNode>
                 </rich:tree>
                 </rich:panel>
                 </h:form>
                </f:view>
                


                @Name("treebean")
                @Scope(ScopeType.SESSION)
                public class TreeBean {
                 private TreeNode tree = new TreeNodeImpl();
                
                 @Create
                 public void createTree() {
                 TreeNodeImpl fooNode = new TreeNodeImpl();
                 fooNode.setData("foo!");
                 TreeNodeImpl barNode = new TreeNodeImpl();
                 barNode.setData("bar!");
                 TreeNodeImpl tarNode = new TreeNodeImpl();
                 tarNode.setData("tar!");
                 tree.addChild("foo", fooNode);
                 fooNode.addChild("bar", barNode);
                 barNode.addChild("tar", tarNode);
                 }
                
                
                 public TreeNode getTree() {
                 return tree;
                 }
                
                 public void setTree(TreeNode tree) {
                 this.tree = tree;
                 }
                
                }
                


                It has a simple string as node data and uses strings for ids

                • 5. Re: Richfaces Tree
                  fanto black Newbie

                  thanks,nickarls.
                  I have got the tree. and one more thing i want to know, does richface tree support vertical dispaly? Not jusft from left to right, what about from top to bottom?

                  • 6. Re: Richfaces Tree
                    Nicklas Karlsson Master

                    I'm having a hard time imagining a "top to bottom" tree. Check the documentation and try asking in the RichFaces forum...