Example usage for java.awt.dnd DragSource DefaultMoveDrop

List of usage examples for java.awt.dnd DragSource DefaultMoveDrop

Introduction

In this page you can find the example usage for java.awt.dnd DragSource DefaultMoveDrop.

Prototype

Cursor DefaultMoveDrop

To view the source code for java.awt.dnd DragSource DefaultMoveDrop.

Click Source Link

Document

The default Cursor to use with a move operation indicating that a drop is currently allowed.

Usage

From source file:DNDList.java

/**
 * a drag gesture has been initiated/*  w w w. j  a va2 s  . co  m*/
 * 
 */

public void dragGestureRecognized(DragGestureEvent event) {

    Object selected = getSelectedValue();
    if (selected != null) {
        StringSelection text = new StringSelection(selected.toString());

        // as the name suggests, starts the dragging
        dragSource.startDrag(event, DragSource.DefaultMoveDrop, text, this);
    } else {
        System.out.println("nothing was selected");
    }
}

From source file:ColorSource.java

public void dragGestureRecognized(DragGestureEvent e) {
    // Create an image we can drag along with us.
    // Not all systems support this, but it doesn't hurt to try.
    Image colorblock = this.createImage(25, 25);
    Graphics g = colorblock.getGraphics();
    g.setColor(color);/*from www.  j a v  a  2 s . c o m*/
    g.fillRect(0, 0, 25, 25);

    // Start dragging our transferable color object.
    e.startDrag(DragSource.DefaultMoveDrop, // The initial drag cursor
            colorblock, new Point(0, 0), // The image to drag
            tcolor, // The data being dragged
            this); // Who to notify during drag
}

From source file:ScribbleDragAndDrop.java

/**
 * This method implements the DragGestureListener interface. It will be
 * invoked when the DragGestureRecognizer thinks that the user has initiated
 * a drag. If we're not in drawing mode, then this method will try to figure
 * out which Scribble object is being dragged, and will initiate a drag on
 * that object./* w ww . jav a 2s.c  om*/
 */
public void dragGestureRecognized(DragGestureEvent e) {
    // Don't drag if we're not in drag mode
    if (!dragMode)
        return;

    // Figure out where the drag started
    MouseEvent inputEvent = (MouseEvent) e.getTriggerEvent();
    int x = inputEvent.getX();
    int y = inputEvent.getY();

    // Figure out which scribble was clicked on, if any by creating a
    // small rectangle around the point and testing for intersection.
    Rectangle r = new Rectangle(x - LINEWIDTH, y - LINEWIDTH, LINEWIDTH * 2, LINEWIDTH * 2);
    int numScribbles = scribbles.size();
    for (int i = 0; i < numScribbles; i++) { // Loop through the scribbles
        Scribble s = (Scribble) scribbles.get(i);
        if (s.intersects(r)) {
            // The user started the drag on top of this scribble, so
            // start to drag it.

            // First, remember which scribble is being dragged, so we can
            // delete it later (if this is a move rather than a copy)
            beingDragged = s;

            // Next, create a copy that will be the one dragged
            Scribble dragScribble = (Scribble) s.clone();
            // Adjust the origin to the point the user clicked on.
            dragScribble.translate(-x, -y);

            // Choose a cursor based on the type of drag the user initiated
            Cursor cursor;
            switch (e.getDragAction()) {
            case DnDConstants.ACTION_COPY:
                cursor = DragSource.DefaultCopyDrop;
                break;
            case DnDConstants.ACTION_MOVE:
                cursor = DragSource.DefaultMoveDrop;
                break;
            default:
                return; // We only support move and copys
            }

            // Some systems allow us to drag an image along with the
            // cursor. If so, create an image of the scribble to drag
            if (dragSource.isDragImageSupported()) {
                Rectangle scribbleBox = dragScribble.getBounds();
                Image dragImage = this.createImage(scribbleBox.width, scribbleBox.height);
                Graphics2D g = (Graphics2D) dragImage.getGraphics();
                g.setColor(new Color(0, 0, 0, 0)); // transparent background
                g.fillRect(0, 0, scribbleBox.width, scribbleBox.height);
                g.setColor(Color.black);
                g.setStroke(linestyle);
                g.translate(-scribbleBox.x, -scribbleBox.y);
                g.draw(dragScribble);
                Point hotspot = new Point(-scribbleBox.x, -scribbleBox.y);

                // Now start dragging, using the image.
                e.startDrag(cursor, dragImage, hotspot, dragScribble, this);
            } else {
                // Or start the drag without an image
                e.startDrag(cursor, dragScribble, this);
            }
            // After we've started dragging one scribble, stop looking
            return;
        }
    }
}

From source file:org.orbisgis.core.ui.plugins.views.geocatalog.Catalog.java

@Override
public void dragGestureRecognized(DragGestureEvent dge) {
    Transferable dragData = getDragData(dge);
    if (dragData != null) {
        dragSource.startDrag(dge, DragSource.DefaultMoveDrop, dragData, this);
    }/*from  w  ww .  ja v a 2  s.c  o m*/
}

From source file:org.rdv.ui.DataPanelContainer.java

public void dragGestureRecognized(DragGestureEvent e) {
    e.startDrag(DragSource.DefaultMoveDrop, new StringSelection(""), this);
}

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   w  w  w .ja va 2s.c  om*/
 * 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);
                }
            });
        }
    }
}