Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying a tree with all its nodes?  RSS feed

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers

I need suggestions on copying a tree

To elaborate more:

I have a tree with some nodes; I need the exact tree in a new dialog where the user can perform some operations;

What is the best approach to copy the tree?

1> Enumerate the tree; get the nodes and add it to a new root?

2> Is there any api's which vectorizes the tree nodes automatically?

Has anybody attempted this? Can you please suggest approaches

Any links, examples which could provide more details.

TIA
Ravi
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call newTree.setModel(oldTree.getModel()) the new tree and old tree will have exactly the same contents. Any changes to the contents of one will be reflected in the other, however the node selection and expansion will be independent.

If you don't expect the old tree to change or don't mind reflecting some of those changes in the new tree you could write a custom TableModel that does lazy loading of the original TableModel contents. That is, if there's a call to getChildCount() or getChild() the new model reads the child information of the old tree and caches it. This is similar to how file dialogs are often implemented in Java: when a user expands a directory, read the contents of the directory into memory and cache it there. For file choosers people typically also listen for re-expansion of previously cached paths and check to see whether directory contents changed. For an example of this, see the l2fprod JDirectoryChooser .

If you really need a "snapshot" of the original tree you're going to have to write a copyTreeModel(TreeModel function) that returns a DefaultTreeModel. The simplest implementation of this method would call a recursive function copyTreeNode(TreeModel model, Object node) which returns a TreeNode.
 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David,

As you mentioned; I am successfully using a single TreeModel in 2 seperate Trees and so far I do not have any issues with that....i have a requirement where in I always build subnodes of the node selected by making a query to backend... and hence using same TreeModel works;

I also need a copy or as you call it "snapshot" of the tree.... For which
you suggested:


If you really need a "snapshot" of the original tree you're going to have to write a copyTreeModel(TreeModel function) that returns a DefaultTreeModel. The simplest implementation of this method would call a recursive function copyTreeNode(TreeModel model, Object node) which returns a TreeNode.


Do you think deep copying through recursion 1000's of nodes is a good idea?

TIA
Ravi
[ March 14, 2005: Message edited by: Ravi S Sathish ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think deep copying through recursion 1000's of nodes is a good idea?

If there's a simple solution, it's usually best to implement it because it may surprise you and meet your performance specs. If it doesn't, though, there's still hope.

It would be possible to write a DeltaTreeModel such that if deltaModel = new DeltaTreeModel(originalModel) then deltaModel only stores information about what makes the deltaModel different from the original. This can't work if the originalModel can change since you get notified about removed or changed tree nodes too late to actually record their state. You could then say

TreeModel originalModel = originalTree.getModel();
originalTree.setModel(new DeltaTreeModel(originalModel));
newTree.setModel(new DeltaTreeModel(originalModel));

The DeltaTreeModel would be similar to a lazy loading file chooser as I described above except it would allow you to modify the cached information that's been copied from the original model. It would have to make a copy of every TreeNode that gets accessed. If only a small subset of the nodes are accessed then it should perform decently.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!