0 Replies Latest reply on May 4, 2007 6:51 AM by Thomas Tretli

    Problems with tree nodes in myfaces

    Thomas Tretli Newbie

      I have made a menu tree using Richfaces TreeNode.
      The code works fine with suns implementaion of JSF.
      But with Myfaces the output text written at each nodelevel are never printed on the website. Example: "<h:outputText value="This is a test"></h:outputText>", this text is never printed in the tree.

      <%@ page language="java" pageEncoding="ISO-8859-1"%>
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://richfaces.ajax4jsf.org/rich" prefix="rich"%>
      <%
       String path = request.getContextPath();
       String basePath = request.getScheme() + "://" + request.getServerName()
       + ":" + request.getServerPort() + path + "/";
      %>
      
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
       <head>
       <base href="<%=basePath%>">
      
       <title>My JSF 'Tree.jsp' starting page</title>
      
       <meta http-equiv="pragma" content="no-cache">
       <meta http-equiv="cache-control" content="no-cache">
       <meta http-equiv="expires" content="0">
       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
       <meta http-equiv="description" content="This is my page">
       <!--
       <link rel="stylesheet" type="text/css" href="styles.css">
       -->
      
       </head>
      
       <body>
       <f:view>
       <h:form>
       <div class="sample-container">
       <p>
       This is exactly the same tree, but now it uses "client" switch
       type. Note, that all nodes actually rendered up-front and
       expand/collapse now do not requre server call, søpple gutta på tur
       </p>
       <rich:tree switchType="client" style="width:300px"
       value="#{menu.data}" var="item" nodeFace="#{item.type}">
       <rich:treeNode type="menu">
       <f:verbatim>sss</f:verbatim>
       </rich:treeNode>
       <rich:treeNode type="folder">
       <f:verbatim>sss</f:verbatim>
       </rich:treeNode>
       <rich:treeNode type="leaf">
       <h:outputText value="This is a test"></h:outputText>
       </rich:treeNode>
       </rich:tree>
       </div>
       </h:form>
       </f:view>
       </body>
      </html>
      
      
      My nodes:
      
      
      package no.menu;
      
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Map;
      import java.util.Set;
      import java.util.Map.Entry;
      
      import no.menu.logic.MenuSort;
      
      import org.richfaces.component.TreeNode;
      
      
      
      public class Menu implements TreeNode {
       /**
       *
       */
       private static final long serialVersionUID = -3530085227471752526L;
       private Map<Object, TreeNode> menumap = null;
      
       public Menu() {
       super();
       menumap = new HashMap<Object, TreeNode>();
       /*MenuBuilder menuBuilder = new MenuBuilder();
       menuBuilder.buildRecord(this);
       */
       Folder node1 = new Folder(31, "test node 1", this, 3);
       Folder node2 = new Folder(32, "w test node 2", this, 2);
       Folder node3 = new Folder(33, "test node 3", this, 1);
       Link link4 = new Link(34, "test link 4", this,4,"dsa");
       Link link5 = new Link(35, "test link 5", this,5,"dsadas");
       Link link6 = new Link(36, "test link 6", this,6,"sdfdsaf");
      
       node2.addFolder(node3);
       node2.addLink(link5);
       node1.addLink(link6);
      
       menumap.put(link4.getId(), link4);
       menumap.put(node1.getId(), node1);
       menumap.put(node2.getId(), node2);
      
       Iterator iterator = menumap.keySet().iterator();
       while (iterator.hasNext()) {
       Object key = (Object) iterator.next();
       System.out.println(key.toString());
       }
       }
      
       public void addNode(Folder node) {
       addChild(node.getId(), node);
       node.setParent(this);
       }
      
       public void addChild(Object identifier, TreeNode child) {
       menumap.put(identifier, child);
       }
      
       public TreeNode getChild(Object id) {
       System.out.println("menu getChild " + id);
       return (TreeNode) menumap.get(id);
       }
      
       public Iterator getChildren() {
       System.out.println("menu getChildren ");
       Set<Entry<Object, TreeNode>> set = menumap.entrySet();
       List<Entry<Object, TreeNode>> list = new ArrayList<Entry<Object,TreeNode>>(set);
       MenuSort sortAlg = new MenuSort();
       Collections.sort(list,sortAlg);
       return list.iterator();
       //return menumap.entrySet().iterator();
       }
      
       public Object getData() {
       return this;
       }
      
       public TreeNode getParent() {
       return null;
       }
      
       public boolean isLeaf() {
       return menumap.isEmpty();
       }
      
       public void removeChild(Object id) {
       menumap.remove(id);
       }
      
       public void setData(Object data) {
       }
      
       public void setParent(TreeNode parent) {
       }
      
       public String getType() {
       return "menu";
       }
      
       public int getSortOrder() {
       // TODO Auto-generated method stub
       return 0;
       }
      }
      
      
      
      FOLDER
      
      package no.menu;
      
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Map;
      import java.util.Set;
      import java.util.Map.Entry;
      
      
      
      import no.menu.logic.MenuNodeInterface;
      import no.menu.logic.MenuSort;
      
      import org.richfaces.component.TreeNode;
      
      public class Folder implements TreeNode, MenuNodeInterface{
      
       private String name;
       private Map<Object, TreeNode> nodeMap;
       private TreeNode parent;
       private Integer id;
       private int sortOrder;
      
       public Folder() {
       super();
       nodeMap = new HashMap<Object, TreeNode>();
       }
      
       public Folder(Integer id, String name, TreeNode parent, int sort) {
       super();
       nodeMap = new HashMap<Object, TreeNode>();
       this.id = id;
       this.name = name;
       this.parent = parent;
       this.sortOrder = sort;
       }
       public Folder(Integer id, String name, int sort) {
       super();
       nodeMap = new HashMap<Object, TreeNode>();
       this.id = id;
       this.name = name;
       this.parent = null;
       this.sortOrder = sort;
       }
      
       public void addFolder(Folder folder) {
       addChild(folder.getId(), folder);
       }
      
       public void addLink(Link link) {
       addChild(link.getId(), link);
       }
      
       public void addChild(Object arg0, TreeNode arg1) {
       nodeMap.put(arg0, arg1);
       }
      
       public TreeNode getChild(Object arg0) {
       System.out.println(id + " - folder getChild");
       return nodeMap.get(arg0);
       }
      
       public Iterator getChildren() {
       System.out.println(id + " - folder getChildren");
      
       Set<Entry<Object, TreeNode>> set = nodeMap.entrySet();
       List<Entry<Object, TreeNode>> list = new ArrayList<Entry<Object,TreeNode>>(set);
       MenuSort sortAlg = new MenuSort();
       Collections.sort(list,sortAlg);
       return list.iterator();
       //return nodeMap.entrySet().iterator();
       }
      
       public Object getData() {
       System.out.println(id + " - folder getData");
       return this;
       }
      
       public TreeNode getParent() {
       System.out.println(id + " - folder getParent");
       return parent;
       }
      
       public boolean isLeaf() {
       System.out.println(id + " - folder isLeaf");
       return nodeMap.isEmpty();
       }
      
       public void removeChild(Object arg0) {
       nodeMap.remove(arg0);
       }
      
       public void setData(Object arg0) {
      
       }
      
       public void setParent(TreeNode arg0) {
       parent = arg0;
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public String getType() {
       return "folder";
       }
      
       public Integer getId() {
       return id;
       }
      
       public void setId(Integer id) {
       this.id = id;
       }
      
       public int getSortOrder() {
       return sortOrder;
       }
      
       public void setSortOrder(int sortOrder) {
       this.sortOrder = sortOrder;
       }
      
      }
      
      
      
      
      LINK:
      
      
      
      package no.carrot.menu;
      
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.Map;
      
      import org.richfaces.component.TreeNode;
      import no.menu.logic.MenuNodeInterface;
      
      public class Link implements TreeNode, MenuNodeInterface {
      
       private String name;
       private String path;
       private Map<Object, TreeNode> nodeMap;
       private TreeNode parent;
       private Integer id;
       private int sortOrder;
      
       public Link() {
       super();
       nodeMap = new HashMap<Object, TreeNode>();
       }
      
       public Link(Integer id, String name, TreeNode parent, int sort, String path) {
       super();
       nodeMap = new HashMap<Object, TreeNode>();
       this.id = id;
       this.name = name;
       this.parent = parent;
       this.sortOrder = sort;
       this.path = path;
       }
      
       public Link(Integer id, String name, int sort, String path) {
       super();
       nodeMap = new HashMap<Object, TreeNode>();
       this.id = id;
       this.name = name;
       this.parent = null;
       this.sortOrder = sort;
       this.path = path;
       }
      
       public void addLink(Link link) {
       addChild(link.getId(), link);
       }
      
       public void addChild(Object arg0, TreeNode arg1) {
       nodeMap.put(arg0, arg1);
       }
      
       public TreeNode getChild(Object arg0) {
       return nodeMap.get(arg0);
       }
      
       public Iterator getChildren() {
       return nodeMap.entrySet().iterator();
       }
      
       public Object getData() {
       return this;
       }
      
       public TreeNode getParent() {
       return parent;
       }
      
       public boolean isLeaf() {
       return nodeMap.isEmpty();
       }
      
       public void removeChild(Object arg0) {
       nodeMap.remove(arg0);
       }
      
       public void setData(Object arg0) {
      
       }
      
       public void setParent(TreeNode arg0) {
       parent = arg0;
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public String getType() {
       return "leaf";
       }
      
       public Integer getId() {
       return id;
       }
      
       public void setId(Integer id) {
       this.id = id;
       }
      
       public int getSortOrder() {
       return sortOrder;
       }
      
       public void setSortOrder(int sortOrder) {
       this.sortOrder = sortOrder;
       }
      
       public String getPath() {
       System.out.println("Link, getting path: "+path);
       return path;
       }
      
       public void setPath(String path) {
       this.path = path;
       }
      
      }