Class ExtObjListTransferHandler

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

public abstract class ExtObjListTransferHandler extends TransferHandler
Transfer handler for JLists of objects reference by URLs or file names. This transfer handler supports objects represented externally by A file name or URL. Subclasses must implement the method insertByURL. This is used when objects are loaded from an external source. If the list is being reordered, the objects in the list are not modified and insertByURL is not called.

In one use case, the objects will be subclasses of ImageIcon, which a JList can display graphically, with each object containing additional methods that the application needs. For example, the ImageIcon might contain a scaled "thumbnail" image of a larger image file, and store the original file's URL for later processing.

The following code is an example of how to set up a JList that supports reordering and displays icon images obtained from a URL or file name (the class OurImageIcon is a subclass of ImageIcon that provides an icon and some additional data):


  DefaultListModel<OurImageIcon> listModel =
      new DefaultListModel<OurImageIcon>();
  JList<OurImageIcon> list = new JList<OurImageIcon>(listModel);
  JScrollPane scrollPane = new JScrollPane(list);
  File dir = ...;
  TransferHandler th = new ExtObjListTransferHandler(list, dir) {
          public void insertByURL(URL url,
                                  DefaultListModel model,
                                  int index)
          {
            // Create OurIconImage
                OurIconImage image = ... ;
            // Make sure the image is fully loaded.
            ...
            // Add the image to the model at the specified index, or at
            // the end if no index was specified.
             if (index == -1) {
                model.addElement(image);
             } else {
               model.addElement(index, image);
             }
            }
       };
  list.setTranferHandler(th);
  list.setDragEnabled(true);
  list.setDropMode(DropMode.INSERT);
  DefaultListCellRenderer lcr = (DefaultListCellRenderer)
       list.getCellRenderer();
  lcr.setHorizontalAlignment(SwingConstants.CENTER);
  Dimension d = scrollPane.getHorizontalScrollBar().getPreferredSize();
  int h = d.height;
  d = scrollPane.getVerticalScrollBar().getPreferredSize();
  int w = d.width;
  int iconWidth = ...;
  int iconHeight = ...;
  list.setFixedCellHeight(iconHeight + h);
  rlist.setFixedCellWidth(iconWidth + w);
  rlist.setLayoutOrientation(JList.VERTICAL);
  rlist.setVisibleRowCount(5);
 
See Also:
  • Constructor Details

    • ExtObjListTransferHandler

      public ExtObjListTransferHandler(JList jlist, File currentDir)
      Constructor.
      Parameters:
      jlist - the JList that will be the drop target.
      currentDir - the current working directory to use for relative URLs or file names
  • Method Details

    • insertByURL

      protected abstract void insertByURL(URL url, DefaultListModel model, int index) throws Exception
      Insert an object given its URL. The implementation is responsible for creating the object given its URL, and the object must be one that the JList can display. The default JList implementation can handle objects of type String and ImageIcon. After an object OBJ is created, the implementation must call model.addElement(OBJ) when index is -1; otherwise it must call model.add(index, OBJ). Some implementations will create an object that must later be modified to be displayed correctly (e.g., a 'blank' image until the actual image has been loaded), with the object modified after it was added to the list model. The modification would typically be done in a separate thread. In this case, as a final step, the implementation may call model.indexOf(OBJ) to get the actual index INDEX and then call model.setElementAt(INDEX, OBJ), which will automatically force the JList to be repainted so that the modified object is displayed correctly.
      Parameters:
      url - the image's URL
      model - the list model for the JList in which the image will appear
      index - the index in the list model before which the image will be inserted; -1 if the entry is to be added to the end of the list
      Throws:
      Exception - an error occurred
    • 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-selection mode and 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
    • createTransferable

      protected Transferable createTransferable(JComponent c)
      Overrides:
      createTransferable in class TransferHandler
    • getSourceActions

      public int getSourceActions(JComponent c)
      Overrides:
      getSourceActions in class TransferHandler
    • importData

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

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

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

      protected void exportDone(JComponent src, Transferable data, int action)
      Overrides:
      exportDone in class TransferHandler