Example usage for java.awt.dnd DragGestureEvent startDrag

List of usage examples for java.awt.dnd DragGestureEvent startDrag

Introduction

In this page you can find the example usage for java.awt.dnd DragGestureEvent startDrag.

Prototype

public void startDrag(Cursor dragCursor, Transferable transferable) throws InvalidDnDOperationException 

Source Link

Document

Starts the drag operation given the Cursor for this drag operation and the Transferable representing the source data for this drag operation.

Usage

From source file:DragGesture.java

public void dragGestureRecognized(DragGestureEvent event) {
    Cursor cursor = null;//ww w .ja va  2 s  . c  o  m
    if (event.getDragAction() == DnDConstants.ACTION_COPY) {
        cursor = DragSource.DefaultCopyDrop;
    }
    event.startDrag(cursor, this);
}

From source file:ComplexExample.java

public void dragGestureRecognized(DragGestureEvent event) {
    Cursor cursor = null;/*from   w  w  w  .  ja v  a  2 s  . c om*/
    JPanel panel = (JPanel) event.getComponent();

    Color color = panel.getBackground();
    if (event.getDragAction() == DnDConstants.ACTION_COPY) {
        cursor = DragSource.DefaultCopyDrop;
    }
    event.startDrag(cursor, new TransferableColor(color));
}

From source file:unikn.dbis.univis.navigation.tree.VTree.java

/**
 * A <code>DragGestureRecognizer</code> has detected
 * a platform-dependent drag initiating gesture and
 * is notifying this listener//from  ww  w.j  av a  2 s  . c  o m
 * in order for it to initiate the action for the user.
 * <p/>
 *
 * @param dge the <code>DragGestureEvent</code> describing
 *            the gesture that has just occurred
 */
public void dragGestureRecognized(DragGestureEvent dge) {

    // Gets the selected tree node.
    Object o = getLastSelectedPathComponent();

    if (o instanceof DefaultMutableTreeNode) {

        // Gets the user object of the tree node.
        Object userObject = ((DefaultMutableTreeNode) o).getUserObject();

        if (userObject instanceof VDimension) {

            final VDimension dimension = (VDimension) userObject;

            // Whether the selected dimension allows drag and drop
            // or not.
            if (dimension.isDragable() && !dimension.isDropped()) {
                dge.startDrag(DragSource.DefaultMoveDrop, new Transferable() {

                    /**
                     * Returns an array of DataFlavor objects indicating the flavors the data
                     * can be provided in.  The array should be ordered according to preference
                     * for providing the data (from most richly descriptive to least descriptive).
                     *
                     * @return an array of data flavors in which this data can be transferred
                     */
                    public DataFlavor[] getTransferDataFlavors() {
                        return new DataFlavor[] { VDataReferenceFlavor.COMBINATION_FLAVOR,
                                VDataReferenceFlavor.DIMENSION_FLAVOR };
                    }

                    /**
                     * Returns whether or not the specified data flavor is supported for
                     * this object.
                     *
                     * @param flavor the requested flavor for the data
                     * @return boolean indicating whether or not the data flavor is supported
                     */
                    public boolean isDataFlavorSupported(DataFlavor flavor) {
                        for (DataFlavor dataFlavor : getTransferDataFlavors()) {
                            if (dataFlavor.match(flavor)) {
                                return true;
                            }
                        }
                        return false;
                    }

                    /**
                     * Returns an object which represents the data to be transferred.  The class
                     * of the object returned is defined by the representation class of the flavor.
                     *
                     * @param flavor the requested flavor for the data
                     * @throws java.awt.datatransfer.UnsupportedFlavorException
                     *          if the requested data flavor is
                     *          not supported.
                     * @see java.awt.datatransfer.DataFlavor#getRepresentationClass
                     */
                    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                        if (VDataReferenceFlavor.COMBINATION_FLAVOR.match(flavor)) {

                            VCube cube = null;
                            for (VDataReference dataReference = dimension; dataReference != null; dataReference = dataReference
                                    .getParent()) {
                                if (dataReference instanceof VCube) {
                                    //                                        cube = (VCube) dataReference;
                                    break;
                                }
                            }

                            VCombination combination = new VCombinationImpl();
                            combination.setCube(cube);
                            combination.setDimension(dimension);
                            combination.setMeasure(measure);
                            combination.setFunction(function);

                            return combination;
                        } else if (VDataReferenceFlavor.DIMENSION_FLAVOR.match(flavor)) {
                            return dimension;
                        }
                        throw new UnsupportedFlavorException(flavor);
                    }
                });
            } else {
                if (LOG.isInfoEnabled()) {
                    LOG.info("The dimension node isn't summable or has been drag and dropped in the past.");
                }
            }
        } else if (userObject instanceof VMeasure) {

            final VMeasure measure = (VMeasure) userObject;

            dge.startDrag(DragSource.DefaultMoveDrop, new Transferable() {

                /**
                 * Returns an array of DataFlavor objects indicating the flavors the data
                 * can be provided in.  The array should be ordered according to preference
                 * for providing the data (from most richly descriptive to least descriptive).
                 *
                 * @return an array of data flavors in which this data can be transferred
                 */
                public DataFlavor[] getTransferDataFlavors() {
                    return new DataFlavor[] { VDataReferenceFlavor.MEASURE_FLAVOR };
                }

                /**
                 * Returns whether or not the specified data flavor is supported for
                 * this object.
                 *
                 * @param flavor the requested flavor for the data
                 * @return boolean indicating whether or not the data flavor is supported
                 */
                public boolean isDataFlavorSupported(DataFlavor flavor) {
                    for (DataFlavor dataFlavor : getTransferDataFlavors()) {
                        if (dataFlavor.match(flavor)) {
                            return true;
                        }
                    }
                    return false;
                }

                /**
                 * Returns an object which represents the data to be transferred.  The class
                 * of the object returned is defined by the representation class of the flavor.
                 *
                 * @param flavor the requested flavor for the data
                 * @throws java.awt.datatransfer.UnsupportedFlavorException
                 *          if the requested data flavor is
                 *          not supported.
                 * @see java.awt.datatransfer.DataFlavor#getRepresentationClass
                 */
                public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                    if (VDataReferenceFlavor.MEASURE_FLAVOR.match(flavor)) {
                        return measure;
                    }
                    throw new UnsupportedFlavorException(flavor);
                }
            });
        }
    }
}