be.ugent.caagt.swirl.dnd
Class LocalTransferHandler

java.lang.Object
  extended by javax.swing.TransferHandler
      extended by be.ugent.caagt.swirl.dnd.LocalTransferHandler
All Implemented Interfaces:
java.io.Serializable

public class LocalTransferHandler
extends javax.swing.TransferHandler

Transfer handler for drag and drop of objects within the same virtual machine. Differs from the standard TransferHandler in the following ways:

Typical usage is as follows: create an object of this class, install a drag handler, add one or more drop handlers, enable the parent component for dragging and register the object as a transfer handler with the parent:
    LocalTransferHandler handler = new LocalTransferHandler ();
    handler.setDragHandler (someDragHandler);
    handler.addDropHandler (handlerForSomeClass);
    handler.addDropHandler (handlerForSomeOtherClass);
    ...
    parent.setDragEnabled (true);
    parent.setTransferHandler (handler);
 
Clients may choose to register handlers as part of the constructor of an extension of this class.

Sources and targets

Methods isTransferAllowed(javax.swing.JComponent, javax.swing.JComponent), exportDone(javax.swing.JComponent, javax.swing.JComponent, java.lang.Object[], java.lang.Class, int) provide both a source and target parameter which can be used by client implementations. Because Swing support for this kind of information is poor (at least in versions of Java prior to 6.0), source and target references are stored as static variables of this class. As a consequence, they are only reliable when the transfer handler for both source and target are of type LocalTransferHandler. Note Like TransferHandler, objects of this class can be shared by different components.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.TransferHandler
javax.swing.TransferHandler.DropLocation, javax.swing.TransferHandler.TransferSupport
 
Field Summary
 
Fields inherited from class javax.swing.TransferHandler
COPY, COPY_OR_MOVE, LINK, MOVE, NONE
 
Constructor Summary
LocalTransferHandler()
          Create a handler of this type.
 
Method Summary
 void addDropHandler(DropHandler handler)
          Register a drop handler.
 boolean canImport(javax.swing.JComponent comp, java.awt.datatransfer.DataFlavor[] transferFlavors)
          Overrides the standard functionality of TransferHandler by delegating to the individual drop handlers registered with this object.
protected  java.awt.datatransfer.Transferable createTransferable(javax.swing.JComponent comp)
          Creates a transferable encapsulating the objects returned by getExportedObjects(javax.swing.JComponent) with a data flavor derived from the value of getExportedClass(javax.swing.JComponent).
protected  void exportDone(javax.swing.JComponent source, javax.swing.JComponent target, java.lang.Object[] objects, java.lang.Class<?> type, int action)
          Invoked after data have been dragged-and-dropped from a component managed by this handler.
protected  void exportDone(javax.swing.JComponent source, java.awt.datatransfer.Transferable data, int action)
          Delegates to exportDone(javax.swing.JComponent, javax.swing.JComponent, java.lang.Object[], java.lang.Class, int).
 DragHandler getDragHandler()
          Return the drag handler used by this transfer handler, or null when none is registered.
protected  java.lang.Class getExportedClass(javax.swing.JComponent source)
          Return the class of objects being exported.
protected  java.lang.Object getExportedObjects(javax.swing.JComponent source)
          Return the object(s) to be exported by a drag-and-drop or cut-and-paste operation.
 int getSourceActions(javax.swing.JComponent source)
          Returns the type of transfer actions supported by the given source component.
 boolean importData(javax.swing.JComponent comp, java.awt.datatransfer.Transferable t)
          Overrides the standard functionality of TransferHandler by delegating to the individual drop handlers registered with this object.
protected  boolean isTransferAllowed(javax.swing.JComponent source, javax.swing.JComponent target)
          Indicates whether a transfer from a given source to given destination is allowed.
 void setDragHandler(DragHandler dragHandler)
          Install a drag handler.
 
Methods inherited from class javax.swing.TransferHandler
canImport, exportAsDrag, exportToClipboard, getCopyAction, getCutAction, getPasteAction, getVisualRepresentation, importData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalTransferHandler

public LocalTransferHandler()
Create a handler of this type.

Method Detail

addDropHandler

public void addDropHandler(DropHandler handler)
Register a drop handler. If an object is dropped that can be handled by more than one handler, the last handler takes precedence. If an array of objects is dropped, the class first searches for a handler for the array type, and if not found, for a handler of the element type. In the latter case, a drop will result in multiple calls to handler.acceptDrop.


setDragHandler

public void setDragHandler(DragHandler dragHandler)
Install a drag handler. If no drag handler is installed, dragging is effectively disabled.


getDragHandler

public DragHandler getDragHandler()
Return the drag handler used by this transfer handler, or null when none is registered.


isTransferAllowed

protected boolean isTransferAllowed(javax.swing.JComponent source,
                                    javax.swing.JComponent target)
Indicates whether a transfer from a given source to given destination is allowed. This check is performed before the list of drop handlers is consulted for a supported data type.

Delegates to the drag handler, or returns true when no drag handler is registered.

Parameters:
source - The component that is the source of the data.
target - The component that is the target of the data, or null if the target transfer handler is not of this type.

canImport

public boolean canImport(javax.swing.JComponent comp,
                         java.awt.datatransfer.DataFlavor[] transferFlavors)
Overrides the standard functionality of TransferHandler by delegating to the individual drop handlers registered with this object.

Note: At this point it is not always possible to recognize a multiple drag onto a drop target that only accepts single elements. This special case is handled by a subsequent call to importData(javax.swing.JComponent, java.awt.datatransfer.Transferable).

Overrides:
canImport in class javax.swing.TransferHandler

importData

public boolean importData(javax.swing.JComponent comp,
                          java.awt.datatransfer.Transferable t)
Overrides the standard functionality of TransferHandler by delegating to the individual drop handlers registered with this object.

Overrides:
importData in class javax.swing.TransferHandler

getSourceActions

public int getSourceActions(javax.swing.JComponent source)
Returns the type of transfer actions supported by the given source component. Should return one TransferHandler.COPY, TransferHandler.MOVE, TransferHandler.COPY_OR_MOVE, TransferHandler.LINK or TransferHandler.NONE. This implementation delegates to the drag handler if it exists or otherwise returns NONE.

Overrides:
getSourceActions in class javax.swing.TransferHandler

exportDone

protected void exportDone(javax.swing.JComponent source,
                          javax.swing.JComponent target,
                          java.lang.Object[] objects,
                          java.lang.Class<?> type,
                          int action)
Invoked after data have been dragged-and-dropped from a component managed by this handler. Typically, when the action is MOVE and source and target are not the same (or are not views of the same model), the objects need to be removed from the source. When the action is COPY or LINK, nothing needs to be done.

This implementation delegates to the drag handler if one is registered and otherwise does nothing.

Parameters:
objects - Array of objects which have been exported
type - Element type of this array
action - the actual action that was performed, will never be NONE
source - The component that is the source of the data.
target - The component that was the target of the data, or null if the source transfer handler is not of this type.

exportDone

protected final void exportDone(javax.swing.JComponent source,
                                java.awt.datatransfer.Transferable data,
                                int action)
Delegates to exportDone(javax.swing.JComponent, javax.swing.JComponent, java.lang.Object[], java.lang.Class, int).

Overrides:
exportDone in class javax.swing.TransferHandler

getExportedObjects

protected java.lang.Object getExportedObjects(javax.swing.JComponent source)
Return the object(s) to be exported by a drag-and-drop or cut-and-paste operation. If multiple objects are exported at the same time, this should return an array.

This implementation delegates to the drag handler if one is registered.

Parameters:
source - component from which data should be exported

getExportedClass

protected java.lang.Class getExportedClass(javax.swing.JComponent source)
Return the class of objects being exported. If this returns null, the class of the object returned by getExportedObjects(javax.swing.JComponent) is used, or the element type if an array is returned.

This implementation delegates to drag handler if one is registered and returns null otherwise. This implementation delegates to the drag handler if one is registered.

Parameters:
source - component from which data should be exported

createTransferable

protected final java.awt.datatransfer.Transferable createTransferable(javax.swing.JComponent comp)
Creates a transferable encapsulating the objects returned by getExportedObjects(javax.swing.JComponent) with a data flavor derived from the value of getExportedClass(javax.swing.JComponent).

Overrides:
createTransferable in class javax.swing.TransferHandler