Example usage for javax.swing TransferHandler exportAsDrag

List of usage examples for javax.swing TransferHandler exportAsDrag

Introduction

In this page you can find the example usage for javax.swing TransferHandler exportAsDrag.

Prototype

public void exportAsDrag(JComponent comp, InputEvent e, int action) 

Source Link

Document

Causes the Swing drag support to be initiated.

Usage

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

protected void handleDnDDragEvent(MouseEvent e) {
    //Don't bother to drag if no photo is selected
    if (selection.isEmpty()) {
        return;/* ww w.j  av  a  2 s . c o m*/
    }

    if (firstMouseEvent != null) {
        log.debug("considering drag");
        e.consume();

        //If they are holding down the control key, COPY rather than MOVE
        int ctrlMask = InputEvent.CTRL_DOWN_MASK;
        int action = e.isControlDown() ? TransferHandler.COPY : TransferHandler.MOVE;

        int dx = Math.abs(e.getX() - firstMouseEvent.getX());
        int dy = Math.abs(e.getY() - firstMouseEvent.getY());
        //Arbitrarily define a 5-pixel shift as the
        //official beginning of a drag.
        if (dx > 5 || dy > 5) {
            log.debug("Start a drag");
            //This is a drag, not a click.
            JComponent c = (JComponent) e.getSource();
            //Tell the transfer handler to initiate the drag.
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, firstMouseEvent, action);
            firstMouseEvent = null;
        }
    }
}