This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes maintain action history of drag and drop in jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "maintain action history of drag and drop in jtree" Watch "maintain action history of drag and drop in jtree" New topic
Author

maintain action history of drag and drop in jtree

jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
Hello,

I have made an applet which creates a tree from a hashmap. I then use the awt dnd api to add drag and drop functionality to my tree. The user can drag drop stuff around and change the hierarchy structure. I am maintaining a list of the nodes that changed and use this for updating the DB. I want to add undo functionality to my drag drop operations. I tried to store the defaulttreemodel before every drag drop operation in a list and try to reload the last model at undo. This however does not work. I can also not think of a way to get the corresponding node in the tree based on the node i have saved in my list of changes. Can someone please me out?

example ..
<pre>
before change
Root
|_n1
| |_n2
| |_n3
|_n4

after change 1
Root
|_n1
| |_n2
|
|_n4
|_n3
change list -> (node n3, parent n4)

after change 2
Root
|_n1
|
|
|_n4
|_n3
|_n2

change list -> (node n3, parent n4),(node n2, parent n4)

when i undo once , I want to get the tree back to
Root
|_n1
| |_n2
|
|_n4
|_n3
</pre>
And so on ....

Thank you.

Regards,



Jenal
SCJP 1.4
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

You don't say whether you are using the classes in the javax.swing.undo package. You might find things easier if you did -- once you got over the hurdle of understanding how they worked, that is.

Anyway what you should store is an "undo" action which restores the tree to its state before the change, and a "redo" action which replicates the change. Trying to store a copy of the entire tree before and after the change is way overkill.

For example if you removed a node from position 7 of node X and added it at position 4 of node Y, your "redo" action would contain code which did exactly that, and your "undo" action would contain code which removed the node from position 4 of node Y and added it at position 7 of node X.
jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
No, I am not using the undo package yet. I just read about it yesterday and was about to explore. So you saying the undo manager will store the state of the tree (actually just the commands) for me and i just need to use the package and set up the methods correctly. I found a usage example of undo with a normal Jtree, will the package be just as effective with the dnd api used with the JTree?

Thank you for the reply.

Regards,

Jenal
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

I don't think it matters what the GUI is doing. You should be logging changes made to the tree model for your undo/redo facility.

And yes, the undo manager will save the edits (which as you say are really commands) and allow you to apply them. I should say that it took me a while to get used to the undo package, but once I understood what it was doing then it was no problem. Although I was trying to get it to work so that I didn't have to undo and redo my typing one character at a time, which is kind of a messy problem. The tree edits are a lot simpler. You do have to treat moving within one branch differently than moving from one branch to another, but I will let you deal with all the messy details when you get there.
jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
I have already got the drag drop logic sorted out. I looked at a usage example of the undo package. It just supports basic expand, collapse operations. There is no method in the package for drag drop actions. There is nothing called undo drop or undo drag that you can call on a node of a tree. I dont know this will help. Could you point me to some usage examples?

Regards,

Jenal
jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
Hi Paul,

I tried a couple of things with the undo class. JTree expand and collapse are straight forward with undo. The problem is that the drag drop event dont trigger the class (don't get caught by the class). So there is no way to undo these actions.

Any ideas?

Regards,

Jenal
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

I don't understand what it means to "trigger a class".

In my JTree drag-and-drop implementation, it's my code which is moving nodes from one tree to another. Surely your nodes don't get moved by magic, there's some code doing it.
jenita brown
Ranch Hand

Joined: Feb 20, 2007
Posts: 51
:-). Yes there are two classes which imperilment the DropTargetListener, DropsourceListner, etc listeners which handle drag and drop. The undo button is in my applet and has action listener. The tree has the UndoableEditListener. When I expand or collapse the nodes the editable event gets fired. But when i do a drag drop it is not fire an editable event so it is not added by the undo manager in its undo list. How do I get the drag drop to fire this event. What should i put in the event object?

Regards,

Jenal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: maintain action history of drag and drop in jtree