Class PaletteDragSupport

java.lang.Object
edu.cnu.mdi.transfer.PaletteDragSupport

public final class PaletteDragSupport extends Object
Installs and manages the drag side of a palette drag-and-drop interaction.

Purpose

Every palette panel that lets the user drag items onto a canvas needs the same Swing DnD boilerplate: create a DragSource gesture recogniser, start the drag with an optional drag image, and clean up the selection when the drag ends. PaletteDragSupport encapsulates that ceremony so that palette panels only have to supply two things:

  1. A PaletteDragSupport.PaletteDragSource callback that decides, for a given drag origin point, what Transferable to carry — or null to cancel the drag.
  2. Optionally, a PaletteDragSupport.DragImageProvider callback that builds a BufferedImage to display under the cursor during the drag.

Usage

 // In a palette panel constructor:
 new PaletteDragSupport(myTable, origin -> {
     int row = myTable.rowAtPoint(origin);
     int col = myTable.columnAtPoint(origin);
     if (row < 0 || col < 1) return null;           // cancel drag
     MyDescriptor d = buildDescriptor(row, col);
     return d != null ? new MyTransferable(d) : null;
 });
 

To supply a drag image in addition to the transferable:

 new PaletteDragSupport(myTable,
     origin -> buildTransferable(origin),
     origin -> buildDragImage(origin),
     32);   // drag image size in pixels
 

Cleanup callback

PaletteDragSupport calls PaletteDragSupport.PaletteDragSource.onDragEnd() on the EDT after every drag attempt — regardless of whether the drop succeeded or was cancelled, and regardless of whether the drag was even started (i.e. the callback returned null). Palette panels use this to clear their selection highlight.

Thread safety

All Swing DnD callbacks arrive on the EDT. PaletteDragSupport does not introduce any additional threading.

Design notes

Only the drag side of DnD is abstracted here. Drop targets are domain-specific: a map canvas and a network-layout canvas need completely different logic to unpack a payload and create the right item type. Keeping drop-target logic in the canvas implementations avoids forcing unrelated domains into a shared type hierarchy.

See Also:
  • Constructor Details

  • Method Details

    • scaleToDragImage

      public static BufferedImage scaleToDragImage(Image source, int sidePixels)
      Scale a Image into a new square BufferedImage suitable for use as a drag image.

      This is provided as a convenience for PaletteDragSupport.DragImageProvider implementations that already have a loaded image and only need to scale it. Rendering hints are set for bilinear interpolation, quality rendering, and anti-aliasing.

      Parameters:
      source - the source image to scale; must not be null
      sidePixels - the side length in pixels of the output square image; must be positive
      Returns:
      a new BufferedImage of size sidePixels × sidePixels
      Throws:
      IllegalArgumentException - if source is null or sidePixels is not positive