edu.northwestern.at.utils.swing
Class XDropTarget

java.lang.Object
  extended by edu.northwestern.at.utils.swing.XDropTarget
All Implemented Interfaces:
java.awt.dnd.DropTargetListener, java.util.EventListener
Direct Known Subclasses:
ListDropTargetOnItem, TreeDropTargetOnItem

public abstract class XDropTarget
extends java.lang.Object
implements java.awt.dnd.DropTargetListener

An abstract base class for drop targets.

This class implements a drop target listener for any AWT component. It handles the details of image dragging if the underlying OS does not support it and the details of autoscrolling scrollable targets.

Concrete subclasses must implement the getScrollIncrement and drop methods.

Autoscrolling is enabled only if the component is scrollable, and only if the drag and drop operation was started by the partner XDragSource class within the same JVM. We have implemented our own autoscrolling and do not use Swing's built-in facilities. Our autoscrolling starts when the user drags the mouse above or below the scrolling component, not when he moves the mouse near the top or bottom and pauses.

This class has an incestuous relationship with its partner XDragSource class. See that class overview for more details.


Constructor Summary
XDropTarget(javax.swing.JComponent component, javax.swing.JScrollPane scrollPane)
          Constructs a new drop target.
 
Method Summary
 void dragEnter(java.awt.dnd.DropTargetDragEvent dtde)
          Handles drag enter events.
 void dragExit(java.awt.dnd.DropTargetEvent dte)
          Handles drag exit events.
 void dragOver(java.awt.dnd.DropTargetDragEvent dtde)
          Handles drag over events.
abstract  void drop(java.awt.dnd.DropTargetDropEvent dtde)
          Handles drop events.
 void dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)
          Handles drop action changed events.
abstract  int getScrollIncrement(boolean up)
          Gets the scroll increment.
static void setGhost(java.awt.image.BufferedImage ghostImage, java.awt.Point ghostOffset)
          Sets the ghost image for manual image dragging.
static void stopCurrentTarget()
          Stops the current target at the end of a drag and drop operation.
 void stopDrag()
          Stops the current drag and drop operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XDropTarget

public XDropTarget(javax.swing.JComponent component,
                   javax.swing.JScrollPane scrollPane)
Constructs a new drop target.

Parameters:
component - The component.
scrollPane - The scroll pane if the component is scrollable, else null.
Method Detail

stopCurrentTarget

public static void stopCurrentTarget()
Stops the current target at the end of a drag and drop operation.

This method is invoked by XDragSouce.dragDropEnd when a drag and drop operation ends (when the user releases the mouse button).

Autoscrolling and ghost image dragging are both terminated for the currently active drop target, if any.


setGhost

public static void setGhost(java.awt.image.BufferedImage ghostImage,
                            java.awt.Point ghostOffset)
Sets the ghost image for manual image dragging.

This method is invoked by XDragSource.startDrag if the underlying OS does not support image dragging. In this case this class takes care of manually dragging the ghost image as the mouse enters and moves over drop targets.

Parameters:
ghostImage - The ghost image.
ghostOffset - The image offset.

stopDrag

public void stopDrag()
Stops the current drag and drop operation. Autoscrolling is stopped and the ghost image is cleared.

Subclasses may override this method, but they must remember to invoke super.stopDrag.


dragEnter

public void dragEnter(java.awt.dnd.DropTargetDragEvent dtde)
Handles drag enter events.

Any previously active drop target is stopped, the autoscroll timer is stopped, and the y coordinate of the mouse location is recorded.

Subclasses often override this method, but must remember to invoke super.dragEnter.

Specified by:
dragEnter in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - The event.

dragExit

public void dragExit(java.awt.dnd.DropTargetEvent dte)
Handles drag exit events.

The ghost image is cleared. If the last known y coordinate of the mouse is within 20 pixels of the top or bottom of the component's viewport the autoscrolling timer is started.

Subclasses may override this method, but must remember to invoke super.dragExit.

Specified by:
dragExit in interface java.awt.dnd.DropTargetListener
Parameters:
dte - The event.

dragOver

public void dragOver(java.awt.dnd.DropTargetDragEvent dtde)
Handles drag over events.

Any previously active different drop target is stopped, the autoscroll timer is stopped, the y coordinate of the mouse location is recorded, and the ghost image is cleared and redrawn.

Subclasses may override this method, but must remember to invoke super.dragOver.

Specified by:
dragOver in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - The event.

dropActionChanged

public void dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)
Handles drop action changed events.

The implementation does nothing. Subclasses may override this method if they wish.

Specified by:
dropActionChanged in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - The event.

getScrollIncrement

public abstract int getScrollIncrement(boolean up)
Gets the scroll increment.

Parameters:
up - True if scrolling up, false if down.

drop

public abstract void drop(java.awt.dnd.DropTargetDropEvent dtde)
Handles drop events.

Specified by:
drop in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - The event.