TransferHandler subclass wraps another TransferHandler and delegates most of its operations to the wrapped handler : Drag Drop « Swing JFC « Java






TransferHandler subclass wraps another TransferHandler and delegates most of its operations to the wrapped handler

  
 
/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.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.
 */
public class FileTransferHandler extends TransferHandler {
  TransferHandler wrappedHandler; // The handler that we wrap

  // We use this array to test the wrapped handler
  static DataFlavor[] stringFlavorArray = new DataFlavor[] { DataFlavor.stringFlavor };

  /** Pass an existing TransferHandler to this constructor */
  public FileTransferHandler(TransferHandler wrappedHandler) {
    if (wrappedHandler == null) // Fail immediately on null
      throw new NullPointerException();
    this.wrappedHandler = wrappedHandler; // Remember wrapped handler
  }

  /**
   * 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
   */
  public boolean canImport(JComponent c, DataFlavor[] flavors) {
    // 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;
  }

  /**
   * 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.
   */
  public boolean importData(JComponent c, Transferable t) {
    // 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);
  }

  /*
   * The following methods just delegate to the wrapped TransferHandler
   */
  public void exportAsDrag(JComponent c, InputEvent e, int action) {
    wrappedHandler.exportAsDrag(c, e, action);
  }

  public void exportToClipboard(JComponent c, Clipboard clip, int action) {
    wrappedHandler.exportToClipboard(c, clip, action);
  }

  public int getSourceActions(JComponent c) {
    return wrappedHandler.getSourceActions(c);
  }

  public Icon getVisualRepresentation(Transferable t) {
    // This method is not currently (Java 1.4) used by Swing
    return wrappedHandler.getVisualRepresentation(t);
  }

  /**
   * This class demonstrates the FileTransferHandler by installing it on a
   * JTextArea component and providing a JFileChooser to drag and cut files.
   */

  public static void main(String[] args) {
    // Here's the text area. Note how we wrap our TransferHandler
    // around the default handler returned by getTransferHandler()
    JTextArea textarea = new JTextArea();
    TransferHandler defaultHandler = textarea.getTransferHandler();
    textarea.setTransferHandler(new FileTransferHandler(defaultHandler));
    // Here's a JFileChooser, with dragging explicitly enabled.
    JFileChooser filechooser = new JFileChooser();
    filechooser.setDragEnabled(true);

    // Display them both in a window
    JFrame f = new JFrame("File Transfer Handler Test");
    f.getContentPane().add(new JScrollPane(textarea), "Center");
    f.getContentPane().add(filechooser, "South");
    f.setSize(400, 600);
    f.setVisible(true);
  }

}

   
    
  








Related examples in the same category

1.Tree: Drag and DropTree: Drag and Drop
2.Drag and dropDrag and drop
3.Adding Image-Dragging Behavior
4.Dropper - show File Drop Target from Drag-n-DropDropper - show File Drop Target from Drag-n-Drop
5.Demonstrate various aspects of Swing data transferDemonstrate various aspects of Swing data transfer
6.File Tree Drop TargetFile Tree Drop Target
7.File Tree Drag SourceFile Tree Drag Source
8.Editor Drop Target 4Editor Drop Target 4
9.Drag Drop Tree ExampleDrag Drop Tree Example
10.JLabel Drag Source JLabel Drag Source
11.Panel Drop TargetPanel Drop Target
12.Editor Drop Target 3Editor Drop Target 3
13.Editor Drop TargetEditor Drop Target
14.Editor Drop Target 2Editor Drop Target 2
15.JTextArea subclass allows TransferableColor objects toJTextArea subclass allows TransferableColor objects to
16.Transferable Color
17.Color Drag Source
18.A sample component for dragging and dropping a collection of files into a tree.A sample component for dragging and dropping a collection of files into a tree.
19.Test of the DragGesture classes and JList to see if weTest of the DragGesture classes and JList to see if we
20.A TransferHandler and JTextArea that will accept any drop at allA TransferHandler and JTextArea that will accept any drop at all
21.Drag and drop: TextAreaDrag and drop: TextArea
22.Drag capabilities: JListDrag capabilities: JList
23.A simple drop tester application for JDK 1.4 Swing componentsA simple drop tester application for JDK 1.4 Swing components
24.Drag and Drop:JList and ListDrag and Drop:JList and List
25.DnD (drag and drop)JTree code DnD (drag and drop)JTree code
26.Drop: TextAreaDrop: TextArea
27.Drag and drop: TextArea 2Drag and drop: TextArea 2
28.Label DnD (Drag and Drop) Label DnD (Drag and Drop)
29.LabelDnD2 allows dropping color onto the foreground of the JLabelLabelDnD2 allows dropping color onto the foreground of the JLabel
30.Drag List Demo Drag List Demo
31.Drag Picture Demo
32.Extended DnD (Drag and Drop) DemoExtended DnD (Drag and Drop) Demo
33.Drag Color DemoDrag Color Demo
34.Drag File DemoDrag File Demo
35.Drag Picture Demo 2
36.Drag Color TextField DemoDrag Color TextField Demo
37.BasicDnD (Drag and Drop)BasicDnD (Drag and Drop)
38.Drag and drop icons: use an icon property.
39.Implement drag & drop functionality in your application
40.Detect a drag initiating gesture in your application
41.Making a Component Draggable
42.Getting and Setting Text on the System Clipboard
43.Use drag and drop to reorder a list
44.Create a drag source a drop target and a transferable object.
45.implements DragGestureListener, Transferable
46.Comma separated text will be inserted into two or more rows.
47.Setting text drag in a JTextArea
48.Built-in drag and drop support: utilize a TransferHandler class
49.This program demonstrates drag and drop in an image list.
50.This program demonstrates the basic Swing support for drag and drop.