Mp3dings 
20100427

net.sourceforge.mp3dings.datatransfer
Class DataTransferHandler

java.lang.Object
  extended by javax.swing.TransferHandler
      extended by net.sourceforge.mp3dings.datatransfer.DataTransferHandler
All Implemented Interfaces:
Serializable

public class DataTransferHandler
extends TransferHandler

A TransferHandler that manages Copy/Paste for Mp3dingsTables. In principle I would like to use it for Drag and Drop as well but there is a problem when activating Swing Drag and Drop support:

  1. You can only register one TransferHandler with JComponent.setTransferHandler(javax.swing.TransferHandler), you cannot register one for DnD and one for Copy/Paste.
  2. The TransferHandler has a method getSourceActions(JComponent c) which takes neither the type of operation (DnD or C/P) nor the DataFlavor (text or files) as an argument.
    If our TransferHandler returns COPY we cannot use 'Cut' on the contents of the JTables. So this won't work.
    If on the other hand it returns COPY_OR_MOVE the default drag and drop operation will be MOVE and in the case of files this will (sometimes) lead to moving the files, which is also unacceptable.

Author:
ringler
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.TransferHandler
TransferHandler.DropLocation, TransferHandler.TransferSupport
 
Field Summary
protected  boolean cpSupported
          Whether this TransferHandler supports Copy/Paste operations.
protected  boolean dndSupported
          Whether this TransferHandler supports Drag and Drop operations.
static int PASTE_APPEND
          Append imported data to current table cell contents on paste.
static int PASTE_APPEND_WITH_SPACE
          Append a blank character and imported data to current table cell contents on paste.
static int PASTE_OVERWRITE
          Overwrite current table cell contents on paste.
 
Fields inherited from class javax.swing.TransferHandler
COPY, COPY_OR_MOVE, LINK, MOVE, NONE
 
Constructor Summary
DataTransferHandler(boolean isDnDHandler, boolean isCpHandler)
          Constructs a new DataTransferHandler that allows the COPY sourceAction and all export flavors returned by getDefaultFlavors().
DataTransferHandler(boolean isDnDHandler, boolean isCpHandler, int sourceActions)
          Constructs a new DataTransferHandler that supports the specified source actions and all export flavors returned by getDefaultFlavors().
DataTransferHandler(boolean isDnDHandler, boolean isCpHandler, int sourceActions, DataFlavor[] initialExportFlavors)
          Constructs a new DataTransferHandler that supports the specified source actions and export flavors.
 
Method Summary
 boolean canImport(JComponent c, DataFlavor[] flavors)
          Description of the Method
protected  Transferable createTransferable(JComponent c)
          Creates a Transferable from the current selection in an Mp3dingsTable.
 void exportAsDrag(JComponent comp, InputEvent e, int action)
           
protected  void exportDone(JComponent src, Transferable data, int action)
          Description of the Method
 void exportToClipboard(JComponent comp, Clipboard clip, int action)
           
static DataFlavor[] getDefaultFlavors()
          Returns the DataFlavors that this class can im- and export by default.
 int getSourceActions(JComponent c)
          Gets the sourceActions attribute of the TransferHandler object
 boolean importData(JComponent c, Transferable t)
          Description of the Method
 boolean importData(JComponent c, Transferable t, int pasteMode)
           
protected  void importFiles(MainFrame mf, File[] files)
           
protected  void importObjectArray(Mp3dingsTable target, Object[][] obj, int pasteMode)
          Description of the Method
protected  void importString(Mp3dingsTable target, String strg, int pasteMode)
          Description of the Method
 boolean isExportEnabled(DataFlavor flavor)
          Returns whether export of the specified flavor is currently enabled or disabled.
 void setExportEnabled(boolean b)
          Disables or enables the export of all default flavors.
 void setExportEnabled(DataFlavor flavor, boolean b)
          Disables or enables the export of a specific flavor and its linked flavors.
 void setSourceActions(int actions)
          Sets the source actions that this TransferHandler should support.
 
Methods inherited from class javax.swing.TransferHandler
canImport, 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

PASTE_OVERWRITE

public static final int PASTE_OVERWRITE
Overwrite current table cell contents on paste.

See Also:
Constant Field Values

PASTE_APPEND

public static final int PASTE_APPEND
Append imported data to current table cell contents on paste.

See Also:
Constant Field Values

PASTE_APPEND_WITH_SPACE

public static final int PASTE_APPEND_WITH_SPACE
Append a blank character and imported data to current table cell contents on paste.

See Also:
Constant Field Values

dndSupported

protected boolean dndSupported
Whether this TransferHandler supports Drag and Drop operations.

See Also:
exportAsDrag(javax.swing.JComponent, java.awt.event.InputEvent, int)

cpSupported

protected boolean cpSupported
Whether this TransferHandler supports Copy/Paste operations.

See Also:
exportToClipboard(javax.swing.JComponent, java.awt.datatransfer.Clipboard, int)
Constructor Detail

DataTransferHandler

public DataTransferHandler(boolean isDnDHandler,
                           boolean isCpHandler)
Constructs a new DataTransferHandler that allows the COPY sourceAction and all export flavors returned by getDefaultFlavors().

Parameters:
isDnDHandler - whether this TransferHandler should handle Drag and Drop operations
isCpHandler - whether this TransferHandler should handle Copy/Paste operations

DataTransferHandler

public DataTransferHandler(boolean isDnDHandler,
                           boolean isCpHandler,
                           int sourceActions)
Constructs a new DataTransferHandler that supports the specified source actions and all export flavors returned by getDefaultFlavors().

Parameters:
isDnDHandler - whether this TransferHandler should handle Drag and Drop operations
isCpHandler - whether this TransferHandler should handle Copy/Paste operations
sourceActions - the source actions that this TransferHandler will support, one of TransferHandler.COPY, COPY_OR_MOVE, MOVE, NONE
See Also:
getSourceActions(javax.swing.JComponent)

DataTransferHandler

public DataTransferHandler(boolean isDnDHandler,
                           boolean isCpHandler,
                           int sourceActions,
                           DataFlavor[] initialExportFlavors)
Constructs a new DataTransferHandler that supports the specified source actions and export flavors.

Parameters:
isDnDHandler - whether this TransferHandler should handle Drag and Drop operations
isCpHandler - whether this TransferHandler should handle Copy/Paste operations
sourceActions - the source actions that this TransferHandler will support, one of TransferHandler.COPY, COPY_OR_MOVE, MOVE, NONE
initialExportFlavors - an array of DataFlavors that this TransferHandler should export.
See Also:
getSourceActions(javax.swing.JComponent)
Method Detail

getDefaultFlavors

public static DataFlavor[] getDefaultFlavors()
Returns the DataFlavors that this class can im- and export by default.

Returns:
the DataFlavors supported by this class

setExportEnabled

public void setExportEnabled(boolean b)
Disables or enables the export of all default flavors.

Parameters:
b - whether export is enabled (true) or disabled (false)
See Also:
getDefaultFlavors(), isExportEnabled(java.awt.datatransfer.DataFlavor)

setExportEnabled

public void setExportEnabled(DataFlavor flavor,
                             boolean b)
Disables or enables the export of a specific flavor and its linked flavors.

Parameters:
b - whether export is enabled (true) or disabled (false)
flavor - the flavor whose export should be enabled or disabled
See Also:
isExportEnabled(java.awt.datatransfer.DataFlavor)

isExportEnabled

public boolean isExportEnabled(DataFlavor flavor)
Returns whether export of the specified flavor is currently enabled or disabled.

Parameters:
flavor - the flavor whose export should be enabled or disabled
Returns:
whether export of the specified flavor is currently enabled or disabled
See Also:
setExportEnabled(DataFlavor, boolean)

setSourceActions

public void setSourceActions(int actions)
Sets the source actions that this TransferHandler should support.

Parameters:
actions - the source actions that this TransferHandler will support, one of TransferHandler.COPY, COPY_OR_MOVE, MOVE, NONE
See Also:
getSourceActions(javax.swing.JComponent)

createTransferable

protected Transferable createTransferable(JComponent c)
Creates a Transferable from the current selection in an Mp3dingsTable. The transferable will support those default flavors whose export is currently enabled.

Overrides:
createTransferable in class TransferHandler
Parameters:
c - the Mp3dingsTable whose selection should be exported
Returns:
a transferable containing the data in the current selection in c if c is an Mp3dingsTable, null otherwise.
See Also:
getDefaultFlavors(), isExportEnabled(java.awt.datatransfer.DataFlavor)

getSourceActions

public int getSourceActions(JComponent c)
Gets the sourceActions attribute of the TransferHandler object

Overrides:
getSourceActions in class TransferHandler
Parameters:
c - Description of the Parameter
Returns:
The sourceActions value

importData

public boolean importData(JComponent c,
                          Transferable t,
                          int pasteMode)

importData

public boolean importData(JComponent c,
                          Transferable t)
Description of the Method

Overrides:
importData in class TransferHandler
Parameters:
c - Description of the Parameter
t - Description of the Parameter
Returns:
Description of the Return Value

exportAsDrag

public void exportAsDrag(JComponent comp,
                         InputEvent e,
                         int action)
Overrides:
exportAsDrag in class TransferHandler

exportToClipboard

public void exportToClipboard(JComponent comp,
                              Clipboard clip,
                              int action)
Overrides:
exportToClipboard in class TransferHandler

exportDone

protected void exportDone(JComponent src,
                          Transferable data,
                          int action)
Description of the Method

Overrides:
exportDone in class TransferHandler
Parameters:
src - Description of the Parameter
data - Description of the Parameter
action - Description of the Parameter

canImport

public boolean canImport(JComponent c,
                         DataFlavor[] flavors)
Description of the Method

Overrides:
canImport in class TransferHandler
Parameters:
c - Description of the Parameter
flavors - Description of the Parameter
Returns:
Description of the Return Value

importObjectArray

protected void importObjectArray(Mp3dingsTable target,
                                 Object[][] obj,
                                 int pasteMode)
Description of the Method

Parameters:
target - Description of the Parameter
obj - Description of the Parameter
pasteMode - Description of the Parameter

importFiles

protected void importFiles(MainFrame mf,
                           File[] files)

importString

protected void importString(Mp3dingsTable target,
                            String strg,
                            int pasteMode)
Description of the Method

Parameters:
target - Description of the Parameter
strg - Description of the Parameter
pasteMode - Description of the Parameter

Mp3dings 
20100427