Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Drag and Drop from JTree to Diagram Node - is this possible? (Read 8765 times)
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Drag and Drop from JTree to Diagram Node - is this possible?
Oct 4th, 2016 at 2:26pm
Print Post  
Our diagram sits next to a JTree, and we would like to be able to select an item or items from the JTree and drag them onto a Diagram Node.

Note that the items in the JTree are not JDiagram items, but would need to be converted to items which the Diagram Nodes do support/contain, however we do currently have functioning code to perform this conversion.

Is this theoretically possible? Do Diagram Nodes support classic drag and drop functionality? If not, might it be possible to use the clipboard functionality to 'simulate' drag and drop functionality?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #1 - Oct 4th, 2016 at 5:06pm
Print Post  
You can use Java standard Transferrable and TransferHandler APIs for drag and drop -
https://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html

You can find these used in FormEditor, Flowcharter and Demo sample projects. JDiagram.jar also contains public DraggedNode and DraggedNodeTransferable classes used by NodeListView, to which DiagramView responds automatically by cloning the dragged node, so you could as well return these from the JTree's transfer handler.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #2 - Oct 5th, 2016 at 3:18pm
Print Post  
All your examples show 'drop' onto the DiagramView and create a new DiagramNode.

What we actually want to do is 'drop' onto an existing DiagramNode (actually a CompositeNode) to add data to it. I can't find 'addTransferHandler()' on DiagramNode, so I assume this isn't directly supported.

Is it possible to, easily, detect which DiagramNode (if any) is in the position being 'dropped' on in the DiagramView, to enable us to make it look as if the 'drop' was onto the DiagramNode itself?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #3 - Oct 5th, 2016 at 5:27pm
Print Post  
Call the getNodeAt method -

Code
Select All
Point mousePosition = view.getMousePosition(true);
Point2D diagPoint = view.deviceToDoc(mousePosition);
if (diagram.getNodeAt(diagPoint) instanceof CompositeNode)
    // ....
 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #4 - Oct 6th, 2016 at 12:46pm
Print Post  
I've tried your suggestion but the call to

Code
Select All
diagramView.getMousePosition(true) 



is returning Null.

The diagramView object seems to be okay as far as I can tell. If I look on its diagram object I can see the 3 composite nodes which are on the particular diagram I am testing with.

Any idea why getMousePosition(true) might be returning null? [Have also tried getMousePosition() and getMousePosition(false) but they also return null]

We're using version 4.1.3 of JDiagram with Java 7.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #5 - Oct 6th, 2016 at 4:46pm
Print Post  
It's supposed to return null if the mouse pointer is not directly over the view -
http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getMousePositio...

The built-in importData used by NodeListView checks for null, so I guess Java also calls it in situations where the pointer isn't over the view and you should check for null too -

Code
Select All
Point mousePosition = view.getMousePosition(true);
if (mousePosition == null)
    return false; 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #6 - Oct 7th, 2016 at 8:49am
Print Post  
Initially I was always getting null, even when I 'dropped' on a CompositeNode but now I do get a Point value - unfortunately it doesn't seem to match the CompositeNode I am dropping over.

For example, on a very simple diagram with a single CompositeNode the drop values I get are:

Point x = 19, y = 118

which converts to

Point2D x = 9.75, y = 84.0

(after this I get a number of 'null' drop points as part of the same 'drag and drop' motion)

but the bounds of the single CompositeNode are:

x = 464.25
y = 168.0
width = 100.0
height = 100.0

From running in debug mode, and observing when I get the 'drop' value, this seems to come as the mouse moves from the JTree onto the Diagram, and when I actually 'drop' the item I get a 'null' position. Is there anything I can do about this?
« Last Edit: Oct 7th, 2016 at 10:01am by AR »  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #7 - Oct 7th, 2016 at 1:18pm
Print Post  
Are you sure you are calling getMousePosition on DiagramView instance? You can find attached modified FormEditor that either creates a new node or adds the dragged string to existing one at current mouse location -

Code
Select All
public boolean importData(JComponent c, Transferable t)
{
	if (canImport(c, t.getTransferDataFlavors()))
	{
		// Can only drop on the flow chart control
		if (!(c instanceof DiagramView))
			return false;

		if (t.isDataFlavorSupported(DataFlavor.stringFlavor))
		{
			try
			{
				String text = (String)t.getTransferData(DataFlavor.stringFlavor);

				DiagramView view = (DiagramView)c;
				Diagram diagram = view.getDiagram();

				Point mousePosition = view.getMousePosition(true);
				Point2D diagPoint = view.deviceToDoc(mousePosition);
				DiagramNode node = diagram.getNodeAt(diagPoint);
				if (node instanceof TableNode)
				{
					TableNode table = (TableNode)node;
					int lastRow = table.addRow();
					table.getCell(0,  lastRow).setText(text);
				}
				else
				{
					diagram.getFactory().createTableNode(
						diagPoint.getX(), diagPoint.getY(), 50, 80, 1, 0);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	return false;
} 



It doesn't seem to return null at any point, which might be related to the (c instanceof DiagramView) check.
  

drag_test.zip ( 6 KB | 264 Downloads )
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #8 - Oct 7th, 2016 at 2:48pm
Print Post  
I ran your example code and it worked fine - thanks for supplying that it was very helpful.

I noticed you were using :
Code
Select All
importData( JComponent comp, Transferable transferable )
canImport( JComponent comp, DataFlavor[] transferFlavors ) 



whereas I was using the versions which take a single TransferSupport parameter.

I have now changed to using the same methods as your example and it is working fine Smiley

Do you have any idea why one method would work and the other not?

All I need to do now is 'refresh' the view/diagram so I can see the new item on my CompositeNode.
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #9 - Oct 7th, 2016 at 4:25pm
Print Post  
Got the refresh working too now.  Smiley Smiley
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Drag and Drop from JTree to Diagram Node - is this possible?
Reply #10 - Oct 10th, 2016 at 6:48am
Print Post  
Quote:
Do you have any idea why one method would work and the other not?


A similar single-parameter version works for me, I'm guessing that's related to your old code not checking if target is a DiagramView before converting mouse position -

Code
Select All
@Override
public boolean importData(TransferSupport support)
{
	if (canImport(support))
	{
		java.awt.Component c = support.getComponent();
		Transferable t = support.getTransferable();

		// Can only drop on the flow chart control
		if (!(c instanceof DiagramView))
			return false;

		if (t.isDataFlavorSupported(DataFlavor.stringFlavor))
		{
			try
			{
				String text = (String)t.getTransferData(DataFlavor.stringFlavor);

				DiagramView view = (DiagramView)c;
				Diagram diagram = view.getDiagram();

				Point mousePosition = view.getMousePosition(true);
				Point2D diagPoint = view.deviceToDoc(mousePosition);
				DiagramNode node = diagram.getNodeAt(diagPoint);
				if (node instanceof TableNode)
				{
					TableNode table = (TableNode)node;
					int lastRow = table.addRow();
					table.getCell(0,  lastRow).setText(text);
				}
				else
				{
					diagram.getFactory().createTableNode(
						diagPoint.getX(), diagPoint.getY(), 50, 80, 1, 0);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	return false;
}

@Override
public boolean canImport(TransferSupport support)
{
	DataFlavor[] flavors = support.getDataFlavors();

	for (int i = 0; i < flavors.length; i++)
	{
		if (DataFlavor.stringFlavor.equals(flavors[i]))
			return true;
	}

	return false;
} 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint