Copyright 2012-04-26 Werner Randelshofer

org.monte.media.gui.datatransfer
Class DropFileTransferHandler

java.lang.Object
  extended by javax.swing.TransferHandler
      extended by org.monte.media.gui.datatransfer.DropFileTransferHandler
All Implemented Interfaces:
java.io.Serializable

public class DropFileTransferHandler
extends javax.swing.TransferHandler

The DropFileTransferHandler can be used to add drag and drop support. When a file is dropped, the supplied ActionListner is invoked. The filename is passed in the action command.

Version:
1.2 2010-10-03 Adds support for file filter.
1.1 2008-12-03 Added file selection mode.
1.0 September 8, 2007 Created.
Author:
Werner Randelshofer
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
DropFileTransferHandler()
          Creates a new instance.
DropFileTransferHandler(int fileSelectionMode)
          Creates a new instance.
DropFileTransferHandler(int fileSelectionMode, java.io.FileFilter filter)
          Creates a new instance.
 
Method Summary
 boolean canImport(javax.swing.JComponent comp, java.awt.datatransfer.DataFlavor[] transferFlavors)
           
protected  java.awt.datatransfer.Transferable createTransferable(javax.swing.JComponent comp)
           
protected  void exportDone(javax.swing.JComponent comp, java.awt.datatransfer.Transferable data, int action)
          This method is called after data has been exported.
 java.io.FileFilter getFileFilter()
           
protected  java.awt.datatransfer.DataFlavor getImportFlavor(java.awt.datatransfer.DataFlavor[] flavors, javax.swing.JComponent c)
          Try to find a flavor that can be used to import a Transferable.
 int getSourceActions(javax.swing.JComponent comp)
          This is the type of transfer actions supported by the source.
 boolean importData(javax.swing.JComponent c, java.awt.datatransfer.Transferable t)
           
 void setActionListener(java.awt.event.ActionListener l)
           
 void setFileFilter(java.io.FileFilter fileFilter)
           
 
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

DropFileTransferHandler

public DropFileTransferHandler()
Creates a new instance.


DropFileTransferHandler

public DropFileTransferHandler(int fileSelectionMode)
Creates a new instance.

Parameters:
fileSelectionMode - JFileChooser file selection mode.

DropFileTransferHandler

public DropFileTransferHandler(int fileSelectionMode,
                               java.io.FileFilter filter)
Creates a new instance.

Parameters:
fileSelectionMode - JFileChooser file selection mode.
Method Detail

setActionListener

public void setActionListener(java.awt.event.ActionListener l)

importData

public boolean importData(javax.swing.JComponent c,
                          java.awt.datatransfer.Transferable t)
Overrides:
importData in class javax.swing.TransferHandler

createTransferable

protected java.awt.datatransfer.Transferable createTransferable(javax.swing.JComponent comp)
Overrides:
createTransferable in class javax.swing.TransferHandler

canImport

public boolean canImport(javax.swing.JComponent comp,
                         java.awt.datatransfer.DataFlavor[] transferFlavors)
Overrides:
canImport in class javax.swing.TransferHandler

getImportFlavor

protected java.awt.datatransfer.DataFlavor getImportFlavor(java.awt.datatransfer.DataFlavor[] flavors,
                                                           javax.swing.JComponent c)
Try to find a flavor that can be used to import a Transferable. The set of usable flavors are tried in the following order:
  1. First, an attempt to find a text/plain flavor is made.
  2. Second, an attempt to find a flavor representing a String reference in the same VM is made.
  3. Lastly, DataFlavor.stringFlavor is searched for.


getSourceActions

public int getSourceActions(javax.swing.JComponent comp)
This is the type of transfer actions supported by the source. Some models are not mutable, so a transfer operation of COPY only should be advertised in that case.

Overrides:
getSourceActions in class javax.swing.TransferHandler
Parameters:
comp - The component holding the data to be transfered. This argument is provided to enable sharing of TransferHandlers by multiple components.
Returns:
This is implemented to return NONE if the component is a JPasswordField since exporting data via user gestures is not allowed. If the text component is editable, COPY_OR_MOVE is returned, otherwise just COPY is allowed.

exportDone

protected void exportDone(javax.swing.JComponent comp,
                          java.awt.datatransfer.Transferable data,
                          int action)
This method is called after data has been exported. This method should remove the data that was transfered if the action was MOVE.

Overrides:
exportDone in class javax.swing.TransferHandler
Parameters:
comp - The component that was the source of the data.
data - The data that was transferred or possibly null if the action is NONE.
action - The actual action that was performed.

getFileFilter

public java.io.FileFilter getFileFilter()
Returns:
the fileFilter

setFileFilter

public void setFileFilter(java.io.FileFilter fileFilter)
Parameters:
fileFilter - the fileFilter to set

Copyright 2012-04-26 Werner Randelshofer