This content has been marked as final.
Show 1 reply
-
1. Re: Only top-level rich:treeNodes commit their commandLink-a
beligum Jun 17, 2007 4:27 PM (in response to beligum)Oh, and here's the backing-bean:
@Name("fileSystemTreeNode") public class FileSystemTreeNode implements TreeNode, Serializable { protected Inode inode; protected Map<Inode, FileSystemTreeNode> cachedChildren; public FileSystemTreeNode() { } public FileSystemTreeNode(Inode inode) { this.inode = inode; this.cachedChildren = null; reloadChildren(); } public boolean equals(Object o) { if ( this == o ) return true; if ( o == null || getClass() != o.getClass() ) return false; final FileSystemTreeNode node = (FileSystemTreeNode) o; //this could go wrong, type-wise try { Inode inode = (Inode)node.getData(); /* * For two nodes to be equals, their inode must be the same */ return inode.getId()==this.inode.getId(); } catch (Exception e) { return false; } } public void addChild(Object entityInstance, TreeNode child) { //TODO: not implemented } public void removeChild(Object entityInstance) { //TODO: not implemented } public Object getData() { return inode; } public void setData(Object data) { this.inode = (Inode)data; } public TreeNode getParent() { Inode parent = this.inode.getParentInode(); if (parent!=null) { return new FileSystemTreeNode(this.inode.getParentInode()); } else { return null; } } public void setParent(TreeNode parent) { //TODO: not implemented } public TreeNode getChild(Object obj) { if (cachedChildren==null) { reloadChildren(); } return cachedChildren.get((Inode)obj); } public Iterator getChildren() { if (cachedChildren==null) { reloadChildren(); } return cachedChildren.entrySet().iterator(); } public boolean isLeaf() { if (DirectoryInode.class.isAssignableFrom(this.inode.getClass())) { if (cachedChildren==null) { reloadChildren(); } return cachedChildren.isEmpty(); } //a FileInode is always a leaf else { return true; } } private void reloadChildren() { //init the cachedChildren here, this way, we won't do this again if not necessary this.cachedChildren = new HashMap<Inode, FileSystemTreeNode>(); //check if we are a directory if (DirectoryInode.class.isAssignableFrom(this.inode.getClass())) { DirectoryInode directory = (DirectoryInode)this.inode; List<Inode> directoryChildren = directory.getImmediateChildrenInodes(); for (Inode child : directoryChildren) { cachedChildren.put(child, new FileSystemTreeNode(child)); } } else { return; } } }