|
Spec-Zone .ru
спецификации, руководства, описания, API
|
The
TransferSupport class, one of the inner classes of the TransferHandler class introduced in JDK 6, serves two functions. As the name suggests, its first function is to support the transfer process and for that purpose it provides several utility methods used to access the details of the data transfer. The following list shows the methods that can be used to obtain information from the TransferHandler. Several of these methods are related to drop actions, which will be discussed in
Component getComponent()— This method returns the target component of the transfer.COPY, MOVE or LINK) when the transfer is a drop. If the transfer is not a drop, this method throws an exception.ACTION_LINK action. For more information on user drop actions, see the API for
. If the transfer is not a drop, this method throws an exception.DataFlavor is supported. The
indicates the type of data represented, such as an image (imageFlavor), a string (stringFlavor), a list of files (javaFileListFlavor), and so on.Transferable data for this transfer. It is more efficient to use one of these methods to query information about the transfer than to fetch the transferable and query it, so this method is not recommended unless you cannot get the information another way.JList component returns the index in the list where the drop occurred. If the transfer is not a drop, this method throws an exception.Now that you are familiar with the TransferSupport utility methods, let us look at sample canImport and importData methods:
public boolean canImport(TransferSupport supp) {
// Check for String flavor
if (!supp.isDataFlavorSupported(stringFlavor)) {
return false;
}
// Fetch the drop location
DropLocation loc = supp.getDropLocation();
// Return whether we accept the location
return shouldAcceptDropLocation(loc);
}
public boolean importData(TransferSupport supp) {
if (!canImport(sup)) {
return false;
}
// Fetch the Transferable and its data
Transferable t = supp.getTransferable();
String data = t.getTransferData(stringFlavor);
// Fetch the drop location
DropLocation loc = supp.getDropLocation();
// Insert the data at this location
insertAt(loc, data);
return true;
}
Next we look at how you can set the drop mode for selected components.