9 Replies Latest reply on Nov 6, 2007 9:32 PM by jvinson

    Simplest Example of RichFaces Tree

      hi all, i found tree example and it works fine, but i want to write my own tree.
      here is my example :
      1.faces-config.xml

       <managed-bean>
       <managed-bean-name>database</managed-bean-name> <managed-bean-class>com.liliko.billing.businesslayer.beans.common.DataBases</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>
       </managed-bean>
      

      2.DataBases.java
      public class DataBases implements TreeNode {
       private Map databases = null;
       public Map getDatabases() {
       if (databases==null) {
       initData();
       }
       return this.databases;
       }
       public void setData(Object object) {
       }
       public void removeChild(Object object) {
       databases.remove(object);
       }
       public TreeNode getChild(Object id) {
       return (TreeNode) getDatabases().get(id);
       }
       public void addDatabase(DataBase database)
       {
       addChild(Long.toString(database.getId()),database);
       database.setParent(this);
       }
       public void addChild(Object identifier, TreeNode child) {
       getDatabases().put(identifier,child);
       }
       public void setParent(TreeNode treeNode) {
       }
       public boolean isLeaf() {
       return true;
       }
       public TreeNode getParent() {
       return null;
       }
       public Object getData() {
       return this;
       }
       public Iterator getChildren() {
       return null;
       }
       private long nextId = 0;
       private long getNextId() {
       return nextId++;
       }
       public void initData(){
       databases = new HashMap();
      
       DataBase oracle = new DataBase(getNextId());
       oracle.setName("Oracle");
      
       DataBase iBMDB2 = new DataBase(getNextId());
       iBMDB2.setName("IBM DB2");
      
       DataBase msSql = new DataBase(getNextId());
       msSql.setName("MS SQL Server");
      
       DataBase postgreSQL = new DataBase(getNextId());
       postgreSQL.setName("Postgre SQL");
      
       DataBase mySQL = new DataBase(getNextId());
       mySQL.setName("My SQL");
      
       addDatabase(oracle);
       addDatabase(iBMDB2);
       addDatabase(msSql);
       addDatabase(postgreSQL);
       addDatabase(mySQL);
       }
       public String getType() {
       return "metadata";
       }
      }
      

      3.Database.java
      public class DataBase implements TreeNode{
      
       private static final long serialVersionUID = 7155620465939481885L;
       private long id;
       private String name;
       private DataBases databases;
       public DataBase(long id) {
       this.id = id;
       }
       public void setData(Object object) {
       }
       public void removeChild(Object object) {
       throw new UnsupportedOperationException("Database do not have children");
       }
       public TreeNode getChild(Object object) {
       throw new UnsupportedOperationException("Database do not have children");
       }
       public void addChild(Object object, TreeNode treeNode) {
       throw new UnsupportedOperationException("Database do not have children");
       }
       public void setParent(TreeNode treeNode) {
       this.databases = (DataBases) treeNode;
       }
       public boolean isLeaf() {
       return true;
       }
       public TreeNode getParent() {
       return databases;
       }
       public Object getData() {
       return this;
       }
       public Iterator getChildren() {
       return new ArrayList().iterator();
       }
       public String getName() {
       return name;
       }
       public void setName(String name) {
       this.name = name;
       }
       public long getId() {
       return id;
       }
      }
      

      3.client code
       <rich:tree rendered = "true" style="width:300px" value="#{database.data}" var="item" nodeFace="#{item.type}">
       <rich:treeNode type="metadata">
       <h:outputText value="#{item.type}" />
       </rich:treeNode>
       <rich:treeNode type="database">
       <h:outputText value="#{item.name}" />
       </rich:treeNode>
       </rich:tree>
      


      but tree is not visible :(,
      what i did incorrect ??


      Regards,
      Paata.


        • 1. Re: Simplest Example of RichFaces Tree
          kyuss

          try this:

          <rich:tree rendered = "true" style="width:300px" value="#{databases.data}" var="item" nodeFace="#{item.type}">
          


          • 2. Re: Simplest Example of RichFaces Tree
            ratondeau

            Hello,

            I did not see it as well, maybe it is because of the z-Index I set for each layer (header, navigation, content).

            Tried to set z-index to a higher number than the divs but still no tree...

            • 3. Re: Simplest Example of RichFaces Tree
              ratondeau

              Helllo Paata,

              did you solve the problem by any chance?

              Regards

              Matt

              • 4. Re: Simplest Example of RichFaces Tree

                Children of TreeNode must implement Map.Entry interface. You method getChildren() in Database is wrong.
                And don't throw exceptions. You can use logging and do nothing. The work will be more stable.
                Your code #{database.data} is right.

                It would be more right to set parent node in addChild() method of DataBases.
                Can you say why you return null in getChildren() method of DataBases? And ArrayList.iterator() in child nodes?

                After correction all mistakes you will see child nodes of DataBase class. You will not see instanse of DataBases class, because root node is not dispayed in tree.

                • 5. Re: Simplest Example of RichFaces Tree

                  hi AlexanderBelov
                  great thanks for your post,
                  i tried to what you say, after all my example looks like :
                  1.DataBases.java

                  public class DataBases implements TreeNode {
                  
                   private Map databases = null;
                   public Map getDatabases() {
                   if (databases==null) {
                   initData();
                   }
                   return this.databases;
                   }
                   public void setData(Object object) {
                   }
                   public void removeChild(Object object) {
                   databases.remove(object);
                   }
                   public TreeNode getChild(Object id) {
                   return (TreeNode) getDatabases().get(id);
                   }
                   public void addDatabase(DataBase database) {
                   addChild(Long.toString(database.getId()),database);
                   }
                   public void addChild(Object identifier, TreeNode child) {
                   getDatabases().put(identifier,child);
                   child.setParent(this);
                   }
                   public void setParent(TreeNode treeNode) {
                   }
                   public boolean isLeaf() {
                   return true;
                   }
                   public TreeNode getParent() {
                   return null;
                   }
                   public Object getData() {
                   return this;
                   }
                   public Iterator getChildren() {
                   return getDatabases().entrySet().iterator();
                   }
                   private long nextId = 0;
                   private long getNextId() {
                   return nextId++;
                   }
                   public void initData(){
                   databases = new HashMap();
                  
                   DataBase oracle = new DataBase(getNextId());
                   oracle.setName("Oracle");
                  
                   DataBase iBMDB2 = new DataBase(getNextId());
                   iBMDB2.setName("IBM DB2");
                  
                   DataBase msSql = new DataBase(getNextId());
                   msSql.setName("MS SQL Server");
                  
                   DataBase postgreSQL = new DataBase(getNextId());
                   postgreSQL.setName("Postgre SQL");
                  
                   DataBase mySQL = new DataBase(getNextId());
                   mySQL.setName("My SQL");
                  
                   addDatabase(oracle);
                   addDatabase(iBMDB2);
                   addDatabase(msSql);
                   addDatabase(postgreSQL);
                   addDatabase(mySQL);
                   }
                   public String getType() {
                   return "metadata";
                   }
                  }
                  

                  2.DataBase.java
                  public class DataBase implements TreeNode{
                  
                   private static final long serialVersionUID = 7155620465939481885L;
                   private long id;
                   private String name;
                   private DataBases databases;
                  
                   public DataBase(long id) {
                   this.id = id;
                   }
                  
                   public void setData(Object object) {
                   }
                  
                   public void removeChild(Object object) {
                   throw new UnsupportedOperationException("Database do not have children");
                   }
                  
                   public TreeNode getChild(Object object) {
                   throw new UnsupportedOperationException("Database do not have children");
                   }
                  
                   public void addChild(Object object, TreeNode treeNode) {
                   throw new UnsupportedOperationException("Database do not have children");
                   }
                  
                   public void setParent(TreeNode treeNode) {
                   this.databases = (DataBases) treeNode;
                   }
                  
                   public boolean isLeaf() {
                   return true;
                   }
                  
                   public TreeNode getParent() {
                   return databases;
                   }
                  
                   public Object getData() {
                   return this;
                   }
                  
                   public Iterator getChildren() {
                   throw new UnsupportedOperationException("Database do not have children");
                   }
                  
                   public String getName() {
                   return name;
                   }
                  
                   public void setName(String name) {
                   this.name = name;
                   }
                  
                   public long getId() {
                   return id;
                   }
                   public String getType(){
                   return "database";
                   }
                  }
                  

                  3.webView.xhtml
                  <rich:tree rendered = "true" style="width:300px" value="#{database.data}" var="item" nodeFace="#{item.type}">
                   <rich:treeNode type="metadata">
                   <h:outputText value="#{item.type}" />
                   </rich:treeNode>
                   <rich:treeNode type="database">
                   <h:outputText value="#{item.name}" />
                   </rich:treeNode>
                   </rich:tree>
                  



                  What is incorrect here ?
                  Can you help me , i can't resolve this problem yet :(

                  Regards,
                  Paata.


                  • 6. Re: Simplest Example of RichFaces Tree

                    and how i can use logging ?

                    • 7. Re: Simplest Example of RichFaces Tree

                      i got this error on client side (JavaScript Error):

                      this.elements.contentTd has no properties
                      initialize()tree.js (line 2)
                      initialize("j_id77:j_id120", "j_id77:j_id120:input", "ajax", Object, function(), "")tree.js (line 2)
                      create()prototype.js (line 3)
                      [Break on this error] Richfaces.TreeSelectEvent="Richfaces.TreeSelectEvent";Richfaces.TreeExpandEvent=...

                      i did not understand what does it mean :(

                      • 8. Re: Simplest Example of RichFaces Tree
                        danielnp

                        I have the same problem..... Help us!

                        • 9. Re: Simplest Example of RichFaces Tree
                          jvinson

                          Did you ever get this to work? If so, can you post so I can see how? I'm starting to do the same and would appreciate some help!