First of all, sorry for the spam!!!
I figured out why my last post was st00pid...
I just thought I would share what I did to resolve this situation...
I am manually handling the expand/collapse using a Hashtable. There is
probably a better way, but, this works for me.
Here is my code:
package util;
import java.util.Hashtable;
import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;
import org.apache.myfaces.custom.tree2.*;
import com.nov.www.cetgws.services.CETGServices.*;
public class TreeBacker {
private TreeModelBase _treeModel;
private TreeNode selectedNode;
private CETGServicesProxy proxy=null;
private CredentialsInfo cred=null;
private Hashtable expansions = new Hashtable();
public TreeBacker() {
// Initialize the tree with the root node
init();
}
private void init() {
try {
cred = FacesUtil.getSession().getCredentials();
proxy = FacesUtil.getSession().getProxy();
String rootUID =
FacesUtil.getSession().getRootUID();
TreeNode node = new MyTreeNode("Root", "Home",
false);
node.setIdentifier(rootUID);
_treeModel = new TreeModelBase(node);
FolderInfo[] fi =
proxy.getFolderContents(cred,rootUID);
boolean leaf = true;
for (int i=0;i<fi.length;i++) {
if
(fi[i].getType().equalsIgnoreCase("novweb")) continue;
TreeNode tnb = new
MyTreeNode(fi[i].getType(),fi[i].getName(),leaf);
tnb.setIdentifier(fi[i].getPuid());
node.getChildren().add(tnb);
expansions.put(fi[i].getPuid(),new Boolean(false));
}
}
catch (Exception e) {
}
}
public TreeModel getTreeModel() {
return _treeModel;
}
public void selectedNode() {
this.selectedNode = this.getTreeModel().getNode();
loadSelectedNode();
TreeState ts = _treeModel.getTreeState();
ts.toggleExpanded(selectedNode.getIdentifier());
}
public void loadSelectedNode() {
try {
FolderInfo[] fi =
proxy.getFolderContents(cred,selectedNode.getIdentifier());
if (fi.length <= 0) return;
selectedNode.getChildren().removeAll(selectedNode.getChildren());
selectedNode.setLeaf(false);
boolean leaf = true;
for (int i=0;i<fi.length;i++) {
if
(fi[i].getType().equalsIgnoreCase("novweb")) continue;
TreeNode tnb = new
MyTreeNode(fi[i].getType(),fi[i].getName(),leaf);
tnb.setIdentifier(fi[i].getPuid());
selectedNode.getChildren().add(tnb);
expansions.put(fi[i].getPuid(),new Boolean(false));
}
}
catch (Exception e) {
}
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void handleClick(ActionEvent arg0) {
UIComponent component = (UIComponent) arg0.getSource();
while (!(component != null && component instanceof HtmlTree)) {
component = component.getParent();
}
if (component != null) {
HtmlTree tree = (HtmlTree) component;
TreeNode node = tree.getNode();
if (tree.isNodeExpanded()) {
if
(((Boolean)expansions.get(node.getIdentifier())).booleanValue()) {
tree.toggleExpanded();
expansions.put(node.getIdentifier(),new
Boolean(false));
}
else {
tree.toggleExpanded();
tree.toggleExpanded();
expansions.put(node.getIdentifier(),new
Boolean(true));
}
}
else {
if
(((Boolean)expansions.get(node.getIdentifier())).booleanValue()) {
tree.toggleExpanded();
}
else {
tree.toggleExpanded();
expansions.put(node.getIdentifier(),new
Boolean(true));
}
}
}
}
}
-----Original Message-----
From: Hughes, Stan [mailto:[EMAIL PROTECTED]
Sent: Sunday, June 18, 2006 5:41 PM
To: MyFaces Discussion
Subject: RE: Deleting Nodes/Branch from Tree
Andrew,
Thanks for the response, but, if I could ask just one other thing...
I have tried a couple of things to collapse the node as I re-add it,
but, it doesn't seem to do anything. Can you give some hint as to how I
do this?
This is what I tried (n00b alert)
TreeNodeBase tnb = new
TreeNodeBase(fi[i].getType(),fi[i].getName(),true);
tnb.setIdentifier(fi[i].getPuid());
TreeModelBase tm = new TreeModelBase(tnb);
TreeState ts = tm.getTreeState();
if (ts.isNodeExpanded(fi[i].getPuid()))
ts.toggleExpanded(fi[i].getPuid());
selectedNode.getChildren().add(tnb);
I know this is wrong, but, I am not sure why...
Looking at the issue you posted, you refer to a TreeNode.getNodeId(). I
am using Tomahawk 1.1.1 and I do not have this function...
Also, I have looked at the Wiki, the FAQ, the javadocs and this list but
find myself still needing some explanation on some of the Tomahawk
functions. Are there any other resources you can point me to?
Thanks,
Stan
-----Original Message-----
From: Andrew Robinson [mailto:[EMAIL PROTECTED]
Sent: Saturday, June 17, 2006 7:57 PM
To: MyFaces Discussion
Subject: Re: Deleting Nodes/Branch from Tree
Rows state is by row index not row ID unfortunately (I sumbitted an
issue on this). As a result you will have that issue. Best thing you
can do is write a custom tree state object for your model or manually
expand/collapse the paths when you add/remove nodes.
BTW - You shouln't have to rebuild the branch, but simply remove the
nodes you need to (unless you really need to rebuild the nodes for
some reason).
-Andrew
On 6/17/06, Hughes, Stan <[EMAIL PROTECTED]> wrote:
>
>
>
>
> This may seem counter-productive to some, but, I need to be able to
> dynamically delete and rebuild a tree structure. The thing is, I need
the
> data to always be real-time...
>
>
>
> So, given the tree following:
>
>
>
> A
>
> | A1
>
> | A1.1
>
> B
>
>
>
> How do I, when the user clicks the A node, delete everything beneath
it so
> that I can rebuild it?
>
>
>
> I have tried various things, but, nothing seems to work. The only way
I can
> see to do it is,
>
>
>
> HtmlTree tree = (HtmlTree) component;
>
> selectedNode = (TreeNodeBase) tree.getNode();
>
>
> selectedNode.getChildren().removeAll(selectedNode.getChildren());
>
>
>
> But, this does not work. When the tree is rebuilt, the nodes seem to
keep
> the state of the node that previously occupied that position.
>
> Here is what happens: if the user selects A, then selects A1 (to
expand
> them), and then collapses A by selecting it again, everything looks
normal.
> But if he then selects A to expand it (again), the code is supposed to
> delete everything below A and rebuild the first level below A (like
lazy
> loading). This appears to work also, but, A1, though it has been
deleted and
> newly created, has the state of being expanded - forcing you to select
it
> twice to expand it...
>
>
>
> Is there some way to COMPLETELY remove everything below a node? Or, is
my
> description not clear enough of what is going on?
>
>
>
> Thanks for any help...
>
>
>
>