* Adds a drag and drop listener to a tree.
* @param tree the tree to which the listener is added.
*/
private void addDragAndDropListener(JTree tree)
{
final DragSource dragSource = DragSource.getDefaultDragSource();
final DragSourceListener dragSourceListener = new DragSourceListener()
{
/**
* {@inheritDoc}
*/
public void dragDropEnd(DragSourceDropEvent dsde)
{
}
/**
* {@inheritDoc}
*/
public void dragEnter(DragSourceDragEvent dsde)
{
DragSourceContext context = dsde.getDragSourceContext();
int dropAction = dsde.getDropAction();
if ((dropAction & DnDConstants.ACTION_COPY) != 0)
{
context.setCursor(DragSource.DefaultCopyDrop);
}
else if ((dropAction & DnDConstants.ACTION_MOVE) != 0)
{
context.setCursor(DragSource.DefaultMoveDrop);
}
else
{
context.setCursor(DragSource.DefaultCopyNoDrop);
}
}
/**
* {@inheritDoc}
*/
public void dragOver(DragSourceDragEvent dsde)
{
}
/**
* {@inheritDoc}
*/
public void dropActionChanged(DragSourceDragEvent dsde)
{
}
/**
* {@inheritDoc}
*/
public void dragExit(DragSourceEvent dsde)
{
}
};
final DragGestureListener dragGestureListener = new DragGestureListener()
{
/**
* {@inheritDoc}
*/
public void dragGestureRecognized(DragGestureEvent e)
{
//Get the selected node
JTree tree = treePane.getTree();
TreePath[] paths = tree.getSelectionPaths();
if (paths != null)
{
BrowserNodeInfo[] nodes = new BrowserNodeInfo[paths.length];
DndBrowserNodes dndNodes = new DndBrowserNodes();
for (int i=0; i<paths.length; i++)
{
BrowserNodeInfo node = controller.getNodeInfoFromPath(paths[i]);
nodes[i] = node;
}
dndNodes.setParent(tree);
dndNodes.setNodes(nodes);
//Select the appropriate cursor;
Cursor cursor = DragSource.DefaultCopyNoDrop;
// begin the drag
dragSource.startDrag(e, cursor, dndNodes, dragSourceListener);
}
}
};
dragSource.createDefaultDragGestureRecognizer(tree, //DragSource
DnDConstants.ACTION_COPY_OR_MOVE, //specifies valid actions
dragGestureListener
);
}