Class FileTextFieldTransferHandler

java.lang.Object
javax.swing.TransferHandler
org.monte.media.swing.datatransfer.FileTextFieldTransferHandler
All Implemented Interfaces:
Serializable

public class FileTextFieldTransferHandler extends TransferHandler
The FileTextFieldTransferHandler can be used to add drag and drop support for JTextFields, which contain the path to a file.
Author:
Werner Randelshofer
See Also:
  • Constructor Details

    • FileTextFieldTransferHandler

      public FileTextFieldTransferHandler()
      Creates a new instance.
    • FileTextFieldTransferHandler

      public FileTextFieldTransferHandler(int fileSelectionMode)
      Creates a new instance.
      Parameters:
      fileSelectionMode - JFileChooser file selection mode.
    • FileTextFieldTransferHandler

      public FileTextFieldTransferHandler(int fileSelectionMode, FileFilter filter)
      Creates a new instance.
      Parameters:
      fileSelectionMode - JFileChooser file selection mode.
  • Method Details

    • importData

      public boolean importData(JComponent comp, Transferable t)
      Overrides:
      importData in class TransferHandler
    • createTransferable

      protected Transferable createTransferable(JComponent comp)
      Overrides:
      createTransferable in class TransferHandler
    • canImport

      public boolean canImport(JComponent comp, DataFlavor[] transferFlavors)
      Overrides:
      canImport in class TransferHandler
    • getImportFlavor

      protected DataFlavor getImportFlavor(DataFlavor[] flavors, JTextComponent 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.
    • handleReaderImport

      protected void handleReaderImport(Reader in, JTextComponent c, boolean useRead) throws BadLocationException, IOException
      Import the given stream data into the text component.
      Throws:
      BadLocationException
      IOException
    • getSourceActions

      public int getSourceActions(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 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(JComponent comp, 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 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 FileFilter getFileFilter()
      Returns:
      the fileFilter
    • setFileFilter

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