Java Reference
In-Depth Information
Example 13−4: ScribbleDragAndDrop.java (continued)
/**
* This method, and the four unused methods that follow it implement the
* DragSourceListener interface. dragDropEnd() is invoked when the user
* drops the scribble she was dragging. If the drop was successful, and
* if the user did a "move" rather than a "copy", then we delete the
* dragged scribble from the list of scribbles to draw.
**/
public void dragDropEnd(DragSourceDropEvent e) {
if (!e.getDropSuccess()) return;
int action = e.getDropAction();
if (action == DnDConstants.ACTION_MOVE) {
scribbles.remove(beingDragged);
beingDragged = null;
repaint();
}
}
// These methods are also part of DragSourceListener.
// They are invoked at interesting points during the drag, and can be
// used to perform "drag over" effects, such as changing the drag cursor
// or drag image.
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
public void dragOver(DragSourceDragEvent e) {}
// The next five methods implement DropTargetListener
/**
* This method is invoked when the user first drags something over us.
* If we understand the data type being dragged, then call acceptDrag()
* to tell the system that we're receptive. Also, we change our border
* as a "drag under" effect to signal that we can accept the drop.
**/
public void dragEnter(DropTargetDragEvent e) {
if (e.isDataFlavorSupported(Scribble.scribbleDataFlavor) ||
e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
this.setBorder(dropBorder);
}
}
/** The user is no longer dragging over us, so restore the border */
public void dragExit(DropTargetEvent e) { this.setBorder(normalBorder); }
/**
* This is the key method of DropTargetListener. It is invoked when the
* user drops something on us.
**/
public void drop(DropTargetDropEvent e) {
this.setBorder(normalBorder);
// Restore the default border
// First, check whether we understand the data that was dropped.
// If we supports our data flavors, accept the drop, otherwise reject.
if (e.isDataFlavorSupported(Scribble.scribbleDataFlavor) ||
e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
}
Search WWH ::




Custom Search