be.ugent.caagt.swirl.dnd

Class LocalTransferHandler


public class LocalTransferHandler
extends 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(JComponent,JComponent), exportDone 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.

Constructor Summary

LocalTransferHandler()
Create a handler of this type.

Method Summary

void
addDropHandler(DropHandler handler)
Register a drop handler.
boolean
canImport(JComponent comp, DataFlavor[] transferFlavors)
Overrides the standard functionality of TransferHandler by delegating to the individual drop handlers registered with this object.
protected Transferable
createTransferable(JComponent comp)
Creates a transferable encapsulating the objects returned by getExportedObjects(JComponent) with a data flavor derived from the value of getExportedClass(JComponent).
protected void
exportDone(JComponent source, JComponent target, Object[] objects, Class type, int action)
Invoked after data have been dragged-and-dropped from a component managed by this handler.
protected void
exportDone(JComponent source, Transferable data, int action)
Delegates to exportDone.
DragHandler
getDragHandler()
Return the drag handler used by this transfer handler, or null when none is registered.
protected Class
getExportedClass(JComponent source)
Return the class of objects being exported.
protected Object
getExportedObjects(JComponent source)
Return the object(s) to be exported by a drag-and-drop or cut-and-paste operation.
int
getSourceActions(JComponent source)
Returns the type of transfer actions supported by the given source component.
boolean
importData(JComponent comp, Transferable t)
Overrides the standard functionality of TransferHandler by delegating to the individual drop handlers registered with this object.
protected boolean
isTransferAllowed(JComponent source, JComponent target)
Indicates whether a transfer from a given source to given destination is allowed.
void
setDragHandler(DragHandler dragHandler)
Install a drag handler.

Constructor Details

LocalTransferHandler

public LocalTransferHandler()
Create a handler of this type.

Method Details

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.

canImport

public boolean canImport(JComponent comp,
                         DataFlavor[] transferFlavors)

createTransferable

protected final Transferable createTransferable(JComponent comp)

exportDone

protected void exportDone(JComponent source,
                          JComponent target,
                          Object[] objects,
                          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:
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.
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

exportDone

protected final void exportDone(JComponent source,
                                Transferable data,
                                int action)
Delegates to exportDone.

getDragHandler

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

getExportedClass

protected Class getExportedClass(JComponent source)
Return the class of objects being exported. If this returns null, the class of the object returned by getExportedObjects(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

getExportedObjects

protected Object getExportedObjects(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

getSourceActions

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

importData

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

isTransferAllowed

protected boolean isTransferAllowed(JComponent source,
                                    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.

setDragHandler

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