com.alee.extended.drag
Class FileDragAndDropHandler

java.lang.Object
  extended by javax.swing.TransferHandler
      extended by com.alee.extended.drag.FileDragAndDropHandler
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
GalleryTransferHandler, WebFileChooserPanel.FilesLocateDropHandler

public class FileDragAndDropHandler
extends javax.swing.TransferHandler

Custom TransferHandler that provides easy and convenient way to support files DnD to/from any Swing component.

To provide drag functionality override filesDropped() method and enable drag operation through methods or constructor. You will also have to call "exportAsDrag" method of TransferHandler to initialize drag operation from where you want it to start. Usually that action is done from MouseMotionListener drag method.

To provide drop functionality imply override filesDropped() method and enable drop operation through methods or constructor.

Author:
Mikle Garin
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
protected  int dragAction
          Desired drag action.
protected  boolean dragEnabled
          Whether files drag is enabled or not.
protected  boolean dropEnabled
          Whether files drop is enabled or not.
 
Fields inherited from class javax.swing.TransferHandler
COPY, COPY_OR_MOVE, LINK, MOVE, NONE
 
Constructor Summary
FileDragAndDropHandler()
          Constructs new FileDragAndDropHandler that allows only file drop operations.
FileDragAndDropHandler(boolean dragEnabled, boolean dropEnabled)
          Constructs new FileDragAndDropHandler that might allow drag and/or drop operations.
 
Method Summary
 boolean canImport(javax.swing.TransferHandler.TransferSupport info)
          
protected  java.awt.datatransfer.Transferable createTransferable(javax.swing.JComponent c)
          
 java.io.File fileDragged()
          Informs that drag action has started and dragged file should be provided.
 java.util.List<java.io.File> filesDragged()
          Informs that drag action has started and list of dragged files should be provided.
 boolean filesDropped(java.util.List<java.io.File> files)
          Informs that specified files were dropped.
 int getDragAction()
          Returns desired drag action.
 int getSourceActions(javax.swing.JComponent c)
          
 boolean importData(java.awt.datatransfer.Transferable t)
          Performs data import checks and action.
 boolean importData(javax.swing.TransferHandler.TransferSupport info)
          
 boolean isDragEnabled()
          Returns whether drag is enabled or not.
 boolean isDropEnabled()
          Returns whether drop is enabled or not.
 void setDragAction(int action)
          Sets desired drag action.
 void setDragEnabled(boolean enabled)
          Sets whether drag is enabled or not.
 void setDropEnabled(boolean enabled)
          Sets whether drop is enabled or not.
 
Methods inherited from class javax.swing.TransferHandler
canImport, exportAsDrag, exportDone, exportToClipboard, getCopyAction, getCutAction, getPasteAction, getVisualRepresentation, importData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dragEnabled

protected boolean dragEnabled
Whether files drag is enabled or not.


dragAction

protected int dragAction
Desired drag action.


dropEnabled

protected boolean dropEnabled
Whether files drop is enabled or not.

Constructor Detail

FileDragAndDropHandler

public FileDragAndDropHandler()
Constructs new FileDragAndDropHandler that allows only file drop operations.


FileDragAndDropHandler

public FileDragAndDropHandler(boolean dragEnabled,
                              boolean dropEnabled)
Constructs new FileDragAndDropHandler that might allow drag and/or drop operations.

Parameters:
dragEnabled - whether drag operations are allowed
dropEnabled - whether drop operations are allowed
Method Detail

isDragEnabled

public boolean isDragEnabled()
Returns whether drag is enabled or not. If false is set any drag actions will be blocked.

Returns:
true if drag is enabled, false otherwise

setDragEnabled

public void setDragEnabled(boolean enabled)
Sets whether drag is enabled or not. If false is set any drag actions will be blocked.

Parameters:
enabled - whether drag is enabled or not

getDragAction

public int getDragAction()
Returns desired drag action. This is either TransferHandler.COPY or TransferHandler.MOVE constant.

Returns:
desired drag action

setDragAction

public void setDragAction(int action)
Sets desired drag action. This should be either TransferHandler.COPY or TransferHandler.MOVE constant.

Parameters:
action - new desired drag action

getSourceActions

public int getSourceActions(javax.swing.JComponent c)

Overrides:
getSourceActions in class javax.swing.TransferHandler

createTransferable

protected java.awt.datatransfer.Transferable createTransferable(javax.swing.JComponent c)

Overrides:
createTransferable in class javax.swing.TransferHandler

filesDragged

public java.util.List<java.io.File> filesDragged()
Informs that drag action has started and list of dragged files should be provided. You can override this method to provide custom list of dragged files.

Returns:
list of dragged files

fileDragged

public java.io.File fileDragged()
Informs that drag action has started and dragged file should be provided. You can override this method to provide custom dragged file.

Returns:
dragged file

isDropEnabled

public boolean isDropEnabled()
Returns whether drop is enabled or not. If false is returned any drop actions are blocked.

Returns:
true if drop is enabled, false otherwise

setDropEnabled

public void setDropEnabled(boolean enabled)
Sets whether drop is enabled or not. If false is set any drop actions will be blocked.

Parameters:
enabled - whether drop is enabled or not

canImport

public boolean canImport(javax.swing.TransferHandler.TransferSupport info)

Overrides:
canImport in class javax.swing.TransferHandler

importData

public boolean importData(javax.swing.TransferHandler.TransferSupport info)

Overrides:
importData in class javax.swing.TransferHandler

importData

public boolean importData(java.awt.datatransfer.Transferable t)
Performs data import checks and action. Returns whether drop was completed or not.

Parameters:
t - dropped transferable
Returns:
true if drop was completed, false otherwise

filesDropped

public boolean filesDropped(java.util.List<java.io.File> files)
Informs that specified files were dropped. You can override this method to perform desired actions.

Parameters:
files - list of dropped files
Returns:
true if drop was completed, false otherwise