Class ExtObjTransferHandler

java.lang.Object
javax.swing.TransferHandler
org.bzdev.swing.ExtObjTransferHandler
All Implemented Interfaces:
Serializable

public abstract class ExtObjTransferHandler extends TransferHandler
TransferHandler for external objects referenced by file name or URL. This class supports drag and drop operations in which the objects transferred are identified by file names or URLs.

At a minimum, users of this class must implement the methods clear(boolean) and addURL(URL). It may be desirable to also implement setFile() addFile(FILE), and setURL(URL) for efficiency. These methods affect the target of a D&D operation and may do more than simply copy a URL and store it.

As an example, suppose a class named InputPane is used to provide input and can be used as a D&D target when the boolean field dndInput has the value true. The class definition of InputPane could include the following inner class:


    class FilenameTransferHandler extends ExtObjTransferHandler {
      @Override
      protected void clear(boolean all) {
          InputPane.this.clear(all);
      }

      @Override
      protected void addFile(File f) {
          InputPane.this.addFile(f);
      }

      @Override
      protected void addURL(URL url) {
          InputPane.this.addURL(url);
      }

      FilenameTransfer(File cdir) {
          super(cdir);
      }

      @Override
      public boolean importData(TransferHandler.TransferSupport support) {
          Component comp = support.getComponent();
          if (!(comp instanceof InputPane) &&
              !(comp.getParent() instanceof InputPane)) {
              return false;
          }
          return super.importData(support);
      }

      @Override
      public boolean canImport(TransferHandler.TransferSupport support) {
          Component comp = support.getComponent();
          if ((comp instanceof InputPane ||
               comp.getParent() instanceof InputPane)) {
              if (!dndInput) return false;
              return super.canImport(support);
          }
          return false;
      }
  }
 
This class uses methods of InputPane to store the objects transferred via a D&D operation and additionally overrides importData and canImport to test the the target is the desired one: either an InputPane or a component (e.g., an Icon) of an InputPane. The constructor for InputPane includes the following statements:

      fnt = new FilenameTransferHandler(currentDirectory);
      setTransferHandler(fnt);
 

The behavior of this class can be modified by calling setMultiEntryMode(boolean) or setMultiEntryMode(boolean,boolean). If not called, the default behavior is to respond to a D&D request by keeping all existing entries and accepting all the new entries the D&D operation provides. For some specialized cases, other options may be desirable, but the default is what one would use in nearly all cases.

See Also:
  • Constructor Details

    • ExtObjTransferHandler

      public ExtObjTransferHandler(File currentDir)
      Constructor.
      Parameters:
      currentDir - the current working directory to use for resolving a relative file name referencing an object being transferred.
  • Method Details

    • setFile

      protected void setFile(File file)
      Transfer an object and clear existing objects. The default implementation converts the argument to a URL passes that URL to setURL.
      Parameters:
      file - a file representing the object to be transferred.
    • addFile

      protected void addFile(File file)
      Transfer an object and add it to a collection of existing objects. The default implementation converts the argument to a URL passes that URL to addURL.
      Parameters:
      file - a file representing the object to be transferred.
    • clear

      protected abstract void clear(boolean all)
      Remove existing objects.
      Parameters:
      all - true to clear all entries; false to keep the first entry
    • setURL

      protected void setURL(URL url)
      Transfer an object referenced by a URL and clear existing objects. The default implementation calls clear() and then addURL(url).
      Parameters:
      url - the URL representing the object to be transferred.
    • addURL

      protected abstract void addURL(URL url)
      Transfer an object referenced by a URL and add it to the collection of existing objects.
      Parameters:
      url - the URL representing the object to be transferred.
    • setMultiEntryMode

      public void setMultiEntryMode(boolean mode)
      Set multi-entry mode. Equivalent to setMultiEntryMode(mode, false). When this class is initialized, its state is equivalent to calling setMultiEntryMode(true).
      Parameters:
      mode - true if multiple entries are allowed; false otherwise
      See Also:
    • setMultiEntryMode

      public void setMultiEntryMode(boolean mode, boolean cmode)
      Set multi-entry mode with the replacement option. The behavior depends on the combination of arguments provided:
       
      modecmodeDescription
      true(ignored)A drag & drop operation may insert multiple values, with new entries added to the target
      falsetrueA drag & drop operation may insert a single value, even if multiple values were selected, and previous values will be cleared from the JList.
      falsefalseA drag & drop operation may insert multiple values and previous values will be cleared from the JList.
      Parameters:
      mode - true if images from multiple D&D entries are kept; false if they are replaced
      cmode - true if only one value is to be inserted in a D&D operation when mode has the value false; false if multiple values can be inserted in a D&D operation when mode has the value false; ignored if mode has the value true
    • importData

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

      public boolean importData(TransferHandler.TransferSupport support)
      Overrides:
      importData in class TransferHandler
    • canImport

      public boolean canImport(TransferHandler.TransferSupport support)
      Overrides:
      canImport in class TransferHandler