FileDocCategorySizeDatePackage
TreeDragSource.javaAPI DocExample2481Thu Oct 24 20:14:28 BST 2002None

TreeDragSource

public class TreeDragSource extends Object implements DragGestureListener, DragSourceListener

Fields Summary
DragSource
source
DragGestureRecognizer
recognizer
TransferableTreeNode
transferable
DefaultMutableTreeNode
oldNode
JTree
sourceTree
Constructors Summary
public TreeDragSource(JTree tree, int actions)

    sourceTree = tree;
    source = new DragSource();
    recognizer = source.createDefaultDragGestureRecognizer(
		 sourceTree, actions, this);
  
Methods Summary
public voiddragDropEnd(java.awt.dnd.DragSourceDropEvent dsde)

    /*
     * to support move or copy, we have to check which occurred:
     */
    System.out.println("Drop Action: " + dsde.getDropAction());
    if (dsde.getDropSuccess() &&
        (dsde.getDropAction() == DnDConstants.ACTION_MOVE)) {
      ((DefaultTreeModel)sourceTree.getModel()).removeNodeFromParent(oldNode);
    }

    /*
     * to support move only...
    if (dsde.getDropSuccess()) {
      ((DefaultTreeModel)sourceTree.getModel()).removeNodeFromParent(oldNode);
    }
    */
  
public voiddragEnter(java.awt.dnd.DragSourceDragEvent dsde)

 
public voiddragExit(java.awt.dnd.DragSourceEvent dse)

 
public voiddragGestureRecognized(java.awt.dnd.DragGestureEvent dge)

    TreePath path = sourceTree.getSelectionPath();
    if ((path == null) || (path.getPathCount() <= 1)) {
      // We can't move the root node or an empty selection
      return;
    }
    oldNode = (DefaultMutableTreeNode)path.getLastPathComponent();
    transferable = new TransferableTreeNode(path);
    source.startDrag(dge, DragSource.DefaultMoveNoDrop, transferable, this);

    // If you support dropping the node anywhere, you should probably
    // start with a valid move cursor:
    //source.startDrag(dge, DragSource.DefaultMoveDrop, transferable, this);
  
public voiddragOver(java.awt.dnd.DragSourceDragEvent dsde)

 
public voiddropActionChanged(java.awt.dnd.DragSourceDragEvent dsde)

 
    System.out.println("Action: " + dsde.getDropAction());
    System.out.println("Target Action: " + dsde.getTargetActions());
    System.out.println("User Action: " + dsde.getUserAction());