Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

maintain action history of drag and drop in jtree

 
jenita brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-). 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic