FileDocCategorySizeDatePackage
FileTransferHandler.javaAPI DocExample6430Sat Jan 24 10:44:38 GMT 2004je3.datatransfer

FileTransferHandler

public class FileTransferHandler extends TransferHandler
This TransferHandler subclass wraps another TransferHandler and delegates most of its operations to the wrapped handler. It adds the ability to to drop or paste files using the predefined DataFlavor.javaFileListFlavor. When a file list is pasted or dropped, it assumes the files are text, reads them in order, concatenates their contents, and then passes the resulting string to the wrapped handler for insertion.

Fields Summary
TransferHandler
wrappedHandler
static DataFlavor[]
stringFlavorArray
Constructors Summary
public FileTransferHandler(TransferHandler wrappedHandler)
Pass an existing TransferHandler to this constructor

    
            
       
	if (wrappedHandler == null)  	      // Fail immediately on null
	    throw new NullPointerException();
	this.wrappedHandler = wrappedHandler; // Remember wrapped handler
    
Methods Summary
public booleancanImport(javax.swing.JComponent c, java.awt.datatransfer.DataFlavor[] flavors)
This method returns true if the TransferHandler knows how to work with one of the specified flavors. This implementation first checks the superclass, then checks for fileListFlavor support

	// If the wrapped handler can import it, we're done
	if (wrappedHandler.canImport(c, flavors)) return true;
	
	// Otherwise, if the wrapped handler can handle string imports, then 
	// see if we are being offered a list of files that we can convert
	// to a string.
	if (wrappedHandler.canImport(c, stringFlavorArray)) {
	    for(int i = 0; i < flavors.length; i++) 
		if (flavors[i].equals(DataFlavor.javaFileListFlavor))
		    return true;
	}
	
	// Otherwise, we can't import any of the flavors.
	return false;
    
public voidexportAsDrag(javax.swing.JComponent c, java.awt.event.InputEvent e, int action)

	wrappedHandler.exportAsDrag(c, e, action);
    
public voidexportToClipboard(javax.swing.JComponent c, java.awt.datatransfer.Clipboard clip, int action)

	wrappedHandler.exportToClipboard(c, clip, action);
    
public intgetSourceActions(javax.swing.JComponent c)

	return wrappedHandler.getSourceActions(c);
    
public javax.swing.IcongetVisualRepresentation(java.awt.datatransfer.Transferable t)

	// This method is not currently (Java 1.4) used by Swing
	return wrappedHandler.getVisualRepresentation(t);
    
public booleanimportData(javax.swing.JComponent c, java.awt.datatransfer.Transferable t)
If the wrapped handler can import strings and the specified Transferable can provide its data as a List of File objects, then we read the files, and pass their contents as a string to the wrapped handler. Otherwise, we offer the Transferable to the wrapped handler to handle on its own.

	// See if we're offered a java.util.List of java.io.File objects.
	// We handle this case first because the Transferable is likely to
	// also offer the filenames as strings, and we want to import the
	// file contents, not their names!
	if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor) &&
	    wrappedHandler.canImport(c, stringFlavorArray)) {
	    try {
		List filelist =
		    (List)t.getTransferData(DataFlavor.javaFileListFlavor);
		
		// Loop through the files to determine total size
		int numfiles = filelist.size();
		int numbytes = 0;
		for(int i = 0; i < numfiles; i++) {
		    File f = (File)filelist.get(i);
		    numbytes += (int)f.length();
		}
		
		// There will never be more characters than bytes in the files
		char[] text = new char[numbytes]; // to hold file contents
		int p = 0;         // current position in the text[] array
		
		// Loop through the files again, reading their content as text
		for(int i = 0; i < numfiles; i++) {
		    File f = (File)filelist.get(i);
		    Reader r = new BufferedReader(new FileReader(f));
		    p += r.read(text, p, (int)f.length());
		}
		
		// Convert the character array to a string and wrap it
		// in a pre-defined Transferable class for transferring strings
		StringSelection selection =
		    new StringSelection(new String(text, 0, p));

		// Ask the wrapped handler to import the string
		return wrappedHandler.importData(c, selection);
	    }
	    // If anything goes wrong, just beep to tell the user
	    catch(UnsupportedFlavorException e) {
		c.getToolkit().beep(); // audible error
		return false;          // return failure code
	    }
	    catch(IOException e) {
		c.getToolkit().beep(); // audible error
		return false;          // return failure code
	    }
	}
	    
	// Otherwise let the wrapped class handle this transferable itself
	return wrappedHandler.importData(c, t);