That last class is supposed to be CustomTreeNode.

Then, just have a TreeModel as a member variable of you backing-bean and return that (or create a new one each time, up to you). If you keep it around as a variable, it will keep the expanded state across calls.


On 4/19/06, Andrew Robinson <[EMAIL PROTECTED]> wrote:
I have a static tree model with one root node. Then, I override the node interface. Something like this:

  public class CustomTreeRootNode
    implements TreeNode, Serializable {
...
    public List getChildren() {
      MyObject parent = rootObject;
      List<MyObject> myChildren = parent.getChildren();
      List<TreeNode> nodes = new ArrayList<TreeNode>();
      for (MyObject child : myChildren)
      {
         nodes.add(new CustomTreeNode(child.getId()));
      }
    }
...
  }

  public class CustomRootNode
    implements TreeNode, Serializable {
    private String identifier;
    public CustomRootNode(String id) { this.identifier = id; }
...
    public List getChildren() {
      MyObject parent = myHashMap.get(this.identifier);
      List<MyObject> myChildren = parent.getChildren();
      List<TreeNode> nodes = new ArrayList<TreeNode>();
      for (MyObject child : myChildren)
      {
         nodes.add(new CustomTreeNode(child.getId()));
      }
    }
...

  }





On 4/19/06, sunilskulkarni < [EMAIL PROTECTED]> wrote:

Thanks Sean, Andrew.

I am trying to construct tree2 from following DB table. KaMenu object
represents the following table.
+-------+--------+------+------+--------+
| child | parent | name | link | locale |
+-------+--------+------+------+--------+
|     1 |      1 | n1   | l1   |      0 |
|     2 |      1 | n2   | l2   |      0 |
|     3 |      1 | n3   | l3   |      0 |
+-------+--------+------+------+--------+

Here is my algorithm to construct the TreeNode for tree2. I tried server
side tree2 without any help. It is recursive. nodeLeafRef is a hashmap that
returns TreeNode for rootNode 1. When the function is called, it is filled
with information about the root node. In this example nodeLeafRef.size() is
3, but I still get the Java heap space exception.

Any help is really appreciated. Thank you.

public void buildTreeData(KaMenu kaMenu) {

                //List list = kaMenuModel.findChildren(kaMenu.getChild());
                List list = (ArrayList)parentChild.get(kaMenu.getChild());
                // If the list is empty, then its a leaf else it is a node

                if(list != null)
                {
                        int n = list.size();
                        if (n > 0) {
                                // Add the parent to the Hashtable
                                TreeNode nodeRef = new TreeNodeBase("foo-folder",
kaMenu.getName(),false);

                                if (!nodeLeafRef.containsKey(kaMenu.getChild())) {
                                         nodeLeafRef.put(kaMenu.getChild(), nodeRef);
                                } else {
                                        log.severe("Navigation has duplicate node ids");
                                }

                                // Check, if each child has its own children
                                Iterator itr = list.iterator();
                                for (int i = 0; i < n; i++) {
                                        Integer child = (Integer)itr.next();
                                        KaMenu kaChildMenu = (KaMenu) nodeInfo.get(child);
                                        buildTreeData(kaChildMenu);
                                }

                                // Add the node to its parent
                                // if(parent != 1){
                                TreeNode treeNode = (TreeNode) nodeLeafRef.get(kaMenu.getParent());
                                if (treeNode != null) {
                                        nodeRef.getChildren().add(nodeRef);
                                        nodeLeafRef.remove (kaMenu.getParent());
                                        nodeLeafRef.put(kaMenu.getParent(), treeNode);
                                }
                                treeNode = null;
                                nodeRef = null;
                                // }
                        }
                } else {
                        TreeNodeBase leaf = new TreeNodeBase("document", kaMenu.getName(),
                                         kaMenu.getLink(), true);

                        // Add the leaf to its node
                        TreeNode treeNode = null;
                        TreeNodeBase treeNodeBase = null;

                        // If the parent is root node(1), then add the TreeNodeBase
                        if (kaMenu.getParent().intValue() != 1) {
                                treeNode = (TreeNode) nodeLeafRef.get(kaMenu.getParent());
                                if (treeNode != null) {
                                        treeNode.getChildren().add(leaf);
                                        nodeLeafRef.remove(kaMenu.getParent());
                                        nodeLeafRef.put( kaMenu.getParent (), treeNode);
                                        treeNode = null;
                                }
                        } else {
                                treeNodeBase = (TreeNodeBase) nodeLeafRef.get (kaMenu.getParent());
                                if (treeNodeBase != null) {
                                        treeNodeBase.getChildren().add(leaf);
                                        nodeLeafRef.remove (kaMenu.getParent());
                                        nodeLeafRef.put(kaMenu.getParent(), treeNodeBase);
                                        treeNodeBase = null;
                                }
                        }

                        /*
                         * // Add leaf reference to node if
                         * (!nodeLeafRef.containsKey(parent)) nodeLeafRef.put(parent, leaf);
                         * else { log.severe("Navigation has duplicate node ids"); }
                         */
                        leaf = null;
                }
        }
--
View this message in context: http://www.nabble.com/TreeModelBase-HtmlTree-%3A-Java-heap-space-t1461744.html#a3991308
Sent from the MyFaces - Users forum at Nabble.com.



Reply via email to