2 Replies Latest reply on Mar 26, 2011 5:26 PM by codo

    RF 4: How to show root node (using org.richfaces.model.TreeNode model)

    codo Newbie

      Hello,

       

      I'm trying to make simple sample of use of rich:tree. I implement org.richfaces.model.TreeNode. I created root node and added to it new nodes (children). When the tree is rendered I can see only the children nodes, not the root. How to show the root node? Here I'll show part of my code:

       

      My model:

      public class MyRFTreeNode implements TreeNode, Serializable {

       

          private Picture data;

          private Map<Object, TreeNode> children = new HashMap<Object, TreeNode>();

          private TreeNode parent;

       

          public MyRFTreeNode() {

              this.data = new Picture("Root", null);

          }

       

          public MyRFTreeNode(Picture data) {

              this.data = data;

          }

       

          @Override

          public TreeNode getChild(Object o) {

              return children.get(o);

          }

       

          @Override

          public int indexOf(Object o) {

              return 0;

          }

       

          @Override

          public Iterator<Object> getChildrenKeysIterator() {

              return children.keySet().iterator();

          }

       

          @Override

          public boolean isLeaf() {

              return false;

          }

       

          @Override

          public void addChild(Object o, TreeNode tn) {

              children.put(o, tn);

              ((MyRFTreeNode) tn).setParent(this);

          }

       

          @Override

          public void insertChild(int i, Object o, TreeNode tn) {

              addChild(o, tn);

          }

       

          @Override

          public void removeChild(Object o) {

              MyRFTreeNode treeNode = (MyRFTreeNode) children.remove(o);

              if (treeNode != null) {

                  treeNode.setParent(null);

              }

          }

       

          public void setParent(MyRFTreeNode parent) {

              this.parent = parent;

          }

       

          public Picture getData() {

              return data;

          }

       

          public TreeNode getParent() {

              return parent;

          }

      }

       

      My managed bean:

       

      @ManagedBean

      @RequestScoped

      public class TreeBeanRFTreeNode {

       

          private MyRFTreeNode root;

       

          public TreeBeanRFTreeNode() {

              root = new MyRFTreeNode();

       

              MyRFTreeNode node1 = new MyRFTreeNode(new Picture("picture1", "Description1"));

              MyRFTreeNode node2 = new MyRFTreeNode(new Picture("picture2", "Description2"));

              MyRFTreeNode node3 = new MyRFTreeNode(new Picture("picture3", "Description3"));

       

              root.addChild(1, node1);

              root.addChild(2, node2);

              root.addChild(3, node3);

       

              MyRFTreeNode node11 = new MyRFTreeNode(new Picture("picture11", "Description11"));

              MyRFTreeNode node12 = new MyRFTreeNode(new Picture("picture12", "Description12"));

              MyRFTreeNode node13 = new MyRFTreeNode(new Picture("picture13", "Description13"));

       

              node1.addChild(11, node11);

              node1.addChild(12, node12);

              node1.addChild(13, node13);

       

          }

       

          public MyRFTreeNode getRoot() {

              return root;

          }

      }

       

      My xhtml:

       

                      <h:form id="form_main_tree">

                          <rich:tree value="#{treeBeanRFTreeNode.root}" var="item">

                              <rich:treeNode>

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

                              </rich:treeNode>

                          </rich:tree>

                      </h:form>

       

      The result:

       

      tree_sample.png

       

      I'm using RichFaces 4, nightly build from 22.03.2011.

       

      Greetings

        • 1. RF 4: How to show root node (using org.richfaces.model.TreeNode model)
          Rocky S Master

          If i understood you correctly, you wana show top picture folder as the root node and all other pictures beneath it.

           

          try this-

           

          public TreeBeanRFTreeNode() {

                  root = new MyRFTreeNode();

           

           

                  MyRFTreeNode nodeRoot = new MyRFTreeNode(new Picture("root", "Root"));

                   nodeRoot.addChild(1, node1);

                  nodeRoot.addChild(2, node2);

                  nodeRoot.addChild(3, node3);

            

                  root.addChild(1, nodeRoot);

           

                  MyRFTreeNode node1 = new MyRFTreeNode(new Picture("picture1", "Description1"));

                  MyRFTreeNode node2 = new MyRFTreeNode(new Picture("picture2", "Description2"));

                  MyRFTreeNode node3 = new MyRFTreeNode(new Picture("picture3", "Description3"));

           

                  //root.addChild(1, node1);

                  //root.addChild(2, node2);

                  //root.addChild(3, node3);

           

                  MyRFTreeNode node11 = new MyRFTreeNode(new Picture("picture11", "Description11"));

                  MyRFTreeNode node12 = new MyRFTreeNode(new Picture("picture12", "Description12"));

                  MyRFTreeNode node13 = new MyRFTreeNode(new Picture("picture13", "Description13"));

           

                  node1.addChild(11, node11);

                  node1.addChild(12, node12);

                  node1.addChild(13, node13);

           

              }

          • 2. RF 4: How to show root node (using org.richfaces.model.TreeNode model)
            codo Newbie

            Yeah it works that way

             

            I just was surrprised that the node with parent null is not shown (because when I use javax.swing.tree.TreeNode model in the same way, like the code above, it is shown).

             

            Thank you!