aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problems with D&D and CCP for custom TransferHandler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems with D&D and CCP for custom TransferHandler" Watch "Problems with D&D and CCP for custom TransferHandler" New topic
Author

Problems with D&D and CCP for custom TransferHandler

Elma Jestro
Greenhorn

Joined: Jul 06, 2014
Posts: 5
Hi there.

I have a JTree and my own custom TreeModel implementation. I now want to add both Drag&Drop, as well as Copy, Cut & Paste support to my JTree.
My TreeModel, however, is not very simple. It is incredibly important, that no item may exist in the model twice. Think of it as a Set!

But as far as I understood, the TransferHandler in java works like this:
1) Call importData to insert data into model
2) Then call exportDone to remove data from model if action was a move

As you can see, this goes against the limitations of my model.
I have made a little hack to get the Drag & Drop to work. I abuse the importData method to both remove items and insert them afterwards. This works like a charm although it is ugly.
But on the other hand this brings a completely new problem with Copy, Cut & Paste support. When an Item is cut I need the exportDone method to remove it from the model. But there is no way to find out whether the exportDone method was called after a Drag & Drop or after a Cut.

Furthermore I need to actually clone my data with a Copy but keep the old data with a Drag & Drop or Cut.
This has become incredibly frustrating with Swing.

I have a MCVE for this problem, although admittedly, its huge and not small:
We have:
1) A custom TreeModel
2) A custom Node implementation for the TreeModel
3) A TransferHandler
4) A Transferable implementation used by the TransferHandler
5) A utility class that was recommended to be used by the oracle tutorials
6) The main class to create the GUI and test everything













I also posted this question at:
http://www.javaprogrammingforums.com/awt-java-swing/38417-problem-custom-transferhandler.html
and
https://stackoverflow.com/questions/24590606/problems-with-dd-and-ccp-for-custom-transferhandler

but nobody replied.

I would greatly appreciate any kind of help. Thank you all very much.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Hi Elma, welcome to the Ranch!

My guess is, nobody replied to your posts on those other forums because you posted far too much code for anybody to understand.

However, I have written drag-and-drop code which refuses to drop an object in a JTree if there is already a matching entry in the tree; the TransferHandler's canImport() method is what I use for that. If the incoming object matches an existing object in the tree then I return false. I didn't look at your code to see if you did that because as I said, there was far too much of it, but hopefully that helps.
Elma Jestro
Greenhorn

Joined: Jul 06, 2014
Posts: 5
Paul Clapham wrote:Hi Elma, welcome to the Ranch!

My guess is, nobody replied to your posts on those other forums because you posted far too much code for anybody to understand.

Unfortunately I can not make it any smaller and still be functional to show the problem.
The problem description should be enough actually, but on Stack-Overflow somebody asked me for the MCVE, and thats why I made it. Now that I have it I can just as well post it.

I know its much, and I know its verbose, but thats swing unfortunately.


Paul Clapham wrote:However, I have written drag-and-drop code which refuses to drop an object in a JTree if there is already a matching entry in the tree; the TransferHandler's canImport() method is what I use for that. If the incoming object matches an existing object in the tree then I return false. I didn't look at your code to see if you did that because as I said, there was far too much of it, but hopefully that helps.

That would not be what I want. The drag & drop should be possible, but the data should first be removed and then be inserted.
But thank you for the time anyways.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Elma Jestro wrote:
Paul Clapham wrote:However, I have written drag-and-drop code which refuses to drop an object in a JTree if there is already a matching entry in the tree; the TransferHandler's canImport() method is what I use for that. If the incoming object matches an existing object in the tree then I return false. I didn't look at your code to see if you did that because as I said, there was far too much of it, but hopefully that helps.

That would not be what I want. The drag & drop should be possible, but the data should first be removed and then be inserted.
But thank you for the time anyways.


Then that's what you should do in the importData() method.

... Rereading your post, I see that is actually what you do in the importData() method. I don't see the problem. I don't see why you call it a "hack" either.
Elma Jestro
Greenhorn

Joined: Jul 06, 2014
Posts: 5
Paul Clapham wrote:
Elma Jestro wrote:
Paul Clapham wrote:However, I have written drag-and-drop code which refuses to drop an object in a JTree if there is already a matching entry in the tree; the TransferHandler's canImport() method is what I use for that. If the incoming object matches an existing object in the tree then I return false. I didn't look at your code to see if you did that because as I said, there was far too much of it, but hopefully that helps.

That would not be what I want. The drag & drop should be possible, but the data should first be removed and then be inserted.
But thank you for the time anyways.


Then that's what you should do in the importData() method.

... Rereading your post, I see that is actually what you do in the importData() method. I don't see the problem. I don't see why you call it a "hack" either.


The problem is, that in the exportDone method I would need to remove the data if the action was NOT a drag & drop but instead a cut.
But I have no way to find out whether it was drag & drop or a cut. The only information I get is whether it was a MOVE or COPY and, of course, the Transferable.

Furthermore, I also need to find out whether the Action was a cut & paste or a copy & paste within the importData method because I got to know whether I should clone the data or not.
The second problem is not as severe, I could simply always clone regardless of whether it was a copy & paste or a cut & paste, but it would be a nice to have.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Well, I have to say that I don't have that problem because I haven't figured out how to make the drag-and-drop be anything other than a MOVE as far as JTrees are concerned.

In my case I consider it a copy if the source tree is different from the target tree, and a move if the source tree is the same as the target tree, ignoring what the TransferHandler thinks. I don't know you'd find out whether a cut or a copy was intended either; are you doing something in the drag process which makes that distinction, or what?
Elma Jestro
Greenhorn

Joined: Jul 06, 2014
Posts: 5
Paul Clapham wrote:Well, I have to say that I don't have that problem because I haven't figured out how to make the drag-and-drop be anything other than a MOVE as far as JTrees are concerned.

In my case I consider it a copy if the source tree is different from the target tree, and a move if the source tree is the same as the target tree, ignoring what the TransferHandler thinks. I don't know you'd find out whether a cut or a copy was intended either; are you doing something in the drag process which makes that distinction, or what?


I dont want to make the Drag & Drop something else but a MOVE. I want to add Copy, Cut and Paste functionality through Ctrl+C, Ctrl+X and Ctrl+V hotkeys as well as through menubar items.
The Drag & Drop functionality works fine with the solution I am using right now, but the CCP is impossible with this.

The problem comes down to this:
Either CCP or DnD, but I cant get both to work.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Sorry, I haven't tried to use copy/paste via keystrokes like that. So I can't help you with that.
Elma Jestro
Greenhorn

Joined: Jul 06, 2014
Posts: 5
Thank you for the effort though.

But somebody must have done this at some point in time, right? I mean, this should not be something completely new to have D&D as well as CCP with a custom TreeModel, right?
 
 
subject: Problems with D&D and CCP for custom TransferHandler