Example usage for java.awt.dnd DropTargetDropEvent dropComplete

List of usage examples for java.awt.dnd DropTargetDropEvent dropComplete

Introduction

In this page you can find the example usage for java.awt.dnd DropTargetDropEvent dropComplete.

Prototype


public void dropComplete(boolean success) 

Source Link

Document

This method notifies the DragSource that the drop transfer(s) are completed.

Usage

From source file:DropTest2.java

public void drop(DropTargetDropEvent dtde) {
    try {//from ww w . j  ava 2s .  co  m
        // Ok, get the dropped object and try to figure out what it is
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            System.out.println("Possible flavor: " + flavors[i].getMimeType());
            // Check for file lists specifically
            if (flavors[i].isFlavorJavaFileListType()) {
                // Great! Accept copy drops...
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.setText("Successful file list drop.\n\n");

                // And add the list of file names to our text area
                java.util.List list = (java.util.List) tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size(); j++) {
                    ta.append(list.get(j) + "\n");
                }

                // If we made it this far, everything worked.
                dtde.dropComplete(true);
                return;
            }
            // Ok, is it another Java object?
            else if (flavors[i].isFlavorSerializedObjectType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.setText("Successful text drop.\n\n");
                Object o = tr.getTransferData(flavors[i]);
                ta.append("Object: " + o);
                dtde.dropComplete(true);
                return;
            }
            // How about an input stream?
            else if (flavors[i].isRepresentationClassInputStream()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.setText("Successful text drop.\n\n");
                ta.read(new InputStreamReader((InputStream) tr.getTransferData(flavors[i])),
                        "from system clipboard");
                dtde.dropComplete(true);
                return;
            }
        }
        // Hmm, the user must not have dropped a file list
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}

From source file:TreeDragTest.java

public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) parentpath.getLastPathComponent();
    if (parent.isLeaf()) {
        dtde.rejectDrop();/*from w  w  w. j  a  v  a  2  s .c  o m*/
        return;
    }

    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            if (tr.isDataFlavorSupported(flavors[i])) {
                dtde.acceptDrop(dtde.getDropAction());
                TreePath p = (TreePath) tr.getTransferData(flavors[i]);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent();
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                model.insertNodeInto(node, parent, 0);
                dtde.dropComplete(true);
                return;
            }
        }
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}

From source file:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void copyMoveModules(DropTargetDropEvent dtde) {
    DataFlavor chosen = PDragDrop.ModuleSelectionFlavor;
    Transferable transfer = dtde.getTransferable();
    Object data = null;/*from  w  w w.  j av a2s.  c om*/

    try {
        // Get the data
        data = transfer.getTransferData(chosen);

        dtde.acceptDrop(dtde.getDropAction()
                & (DnDConstants.ACTION_MOVE | DnDConstants.ACTION_COPY | DnDConstants.ACTION_LINK));
    } catch (Throwable t) {
        if (log.isWarnEnabled()) {
            log.warn("copyMoveModules(DropTargetDropEvent=" + dtde + ") failed", t);
        }
        dtde.dropComplete(false);
        return;
    }

    if (data != null && data instanceof PModuleTransferData) {
        // Cast the data and create a nice module.
        PModuleTransferData tdata = ((PModuleTransferData) data);
        boolean isSamePatch = false;
        if (tdata.getSourcePatch() == getModuleContainer().getPatch())
            isSamePatch = true;

        //Point p = dtde.getLocation();

        int action = dtde.getDropAction();

        if ((action & DnDConstants.ACTION_MOVE) != 0 && isSamePatch) {
            MoveOperation op = tdata.getSourceModuleContainer().createMoveOperation();
            op.setDestination(getModuleContainer());
            executeOperationOnSelection(tdata, dtde.getLocation(), op);
        } else {
            copyModules(tdata, dtde.getLocation(), ((dtde.getDropAction() & DnDConstants.ACTION_LINK) != 0));
        }

    }
    dtde.dropComplete(true);
}

From source file:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void dropPatchFile(DropTargetDropEvent dtde) {
    Transferable transfer = dtde.getTransferable();
    PPatch patch = getModuleContainer().getPatch();
    DataFlavor fileFlavor = FileDnd.getFileFlavor(transfer.getTransferDataFlavors());
    List<File> files = FileDnd.getTransferableFiles(fileFlavor, transfer);
    if (files.size() == 1) {
        PPatch newPatch;/* w  ww  .  j  a  v a  2  s  .com*/
        try {
            newPatch = patch.createFromFile(files.get(0));
        } catch (Exception e) {
            newPatch = null;
        }
        if (newPatch != null) {
            if (dropPatch(newPatch, dtde.getLocation())) {
                dtde.dropComplete(true);
            } else {
                dtde.rejectDrop();
                dtde.dropComplete(false);
            }
        } else {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }
    } else {
        dtde.rejectDrop();
        dtde.dropComplete(false);
    }
}

From source file:ScribbleDragAndDrop.java

/**
 * This is the key method of DropTargetListener. It is invoked when the user
 * drops something on us.//from  w  w w.  jav  a 2 s  . com
 */
public void drop(DropTargetDropEvent e) {
    this.setBorder(normalBorder); // Restore the default border

    // First, check whether we understand the data that was dropped.
    // If we supports our data flavors, accept the drop, otherwise reject.
    if (e.isDataFlavorSupported(Scribble.scribbleDataFlavor)
            || e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    } else {
        e.rejectDrop();
        return;
    }

    // We've accepted the drop, so now we attempt to get the dropped data
    // from the Transferable object.
    Transferable t = e.getTransferable(); // Holds the dropped data
    Scribble droppedScribble; // This will hold the Scribble object

    // First, try to get the data directly as a scribble object
    try {
        droppedScribble = (Scribble) t.getTransferData(Scribble.scribbleDataFlavor);
    } catch (Exception ex) { // unsupported flavor, IO exception, etc.
        // If that doesn't work, try to get it as a String and parse it
        try {
            String s = (String) t.getTransferData(DataFlavor.stringFlavor);
            droppedScribble = Scribble.parse(s);
        } catch (Exception ex2) {
            // If we still couldn't get the data, tell the system we failed
            e.dropComplete(false);
            return;
        }
    }

    // If we get here, we've got the Scribble object
    Point p = e.getLocation(); // Where did the drop happen?
    droppedScribble.translate(p.getX(), p.getY()); // Move it there
    scribbles.add(droppedScribble); // add to display list
    repaint(); // ask for redraw
    e.dropComplete(true); // signal success!
}

From source file:DragDropTreeExample.java

public void drop(DropTargetDropEvent dtde) {
    DnDUtils.debugPrintln("DropTarget drop, drop action = " + DnDUtils.showActions(dtde.getDropAction()));

    // Check the drop action
    if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
        // Accept the drop and get the transfer data
        dtde.acceptDrop(dtde.getDropAction());
        Transferable transferable = dtde.getTransferable();
        boolean dropSucceeded = false;

        try {/*from   ww  w . j  av  a  2  s .  c  om*/
            tree.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            // Save the user's selections
            saveTreeSelection();

            dropSucceeded = dropFile(dtde.getDropAction(), transferable, dtde.getLocation());

            DnDUtils.debugPrintln("Drop completed, success: " + dropSucceeded);
        } catch (Exception e) {
            DnDUtils.debugPrintln("Exception while handling drop " + e);
        } finally {
            tree.setCursor(Cursor.getDefaultCursor());

            // Restore the user's selections
            restoreTreeSelection();
            dtde.dropComplete(dropSucceeded);
        }
    } else {
        DnDUtils.debugPrintln("Drop target rejected drop");
        dtde.dropComplete(false);
    }
}

From source file:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void dropNewModule(DropTargetDropEvent dtde) {
    PModuleContainer mc = getModuleContainer();
    PModuleDescriptor md = PDragDrop.getModuleDescriptor(dtde.getTransferable());
    if (md == null || mc == null) {
        dtde.rejectDrop();//  w  w w  .  ja  v a 2 s .  c om
        return;
    }

    Point l = dtde.getLocation();

    PModule module;
    try {
        module = mc.createModule(md);
        module.setScreenLocation(l.x, l.y);
    } catch (InvalidDescriptorException e) {
        if (log.isErrorEnabled()) {
            log.error("could not create module: " + md, e);
        }
        dtde.rejectDrop();
        return;
    }
    boolean moduleAdded = mc.add(module);

    if (!moduleAdded) {

        dtde.rejectDrop();
        return;
    }

    // TODO short after dropping a new module and then moving it
    // causes a NullPointerException in the next line
    PModuleContainer parent = module.getParentComponent();
    if (parent != null) {
        JTCableManager cm = getCableManager();
        try {
            cm.setAutoRepaintDisabled();
            MoveOperation move = parent.createMoveOperation();
            move.setScreenOffset(0, 0);
            move.add(module);
            move.move();
        } finally {
            cm.clearAutoRepaintDisabled();
        }
    } else {
        // XXX concurrency problems probably ?!
        throw new RuntimeException("Drop problem on illegal modules: for example 2 midi globals");
    }

    dtde.acceptDrop(DnDConstants.ACTION_COPY);

    // compute dimensions of container
    revalidate();
    repaint();
    dtde.dropComplete(true);
}

From source file:javazoom.jlgui.player.amp.Player.java

/**
 * DnD : Drop implementation.//from   ww  w . j ava2s .  c  o m
 * Adds all dropped files to the playlist.
 */
public void drop(DropTargetDropEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Is file list ?
    if (tdf != null) {
        // Accept COPY DnD only.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
        } else
            return;
        try {
            Transferable t = e.getTransferable();
            Object data = t.getTransferData(tdf);
            // How many files ?
            if (data instanceof java.util.List) {
                java.util.List al = (java.util.List) data;
                // Read the first File.
                if (al.size() > 0) {
                    File file = null;
                    // Stops the player if needed.
                    if ((playerState == PLAY) || (playerState == PAUSE)) {
                        theSoundPlayer.stop();
                        playerState = STOP;
                    }
                    // Clean the playlist.
                    playlist.removeAllItems();
                    // Add all dropped files to playlist.
                    ListIterator li = al.listIterator();
                    while (li.hasNext()) {
                        file = (File) li.next();
                        PlaylistItem pli = null;
                        if (file != null) {

                            pli = new PlaylistItem(file.getName(), file.getAbsolutePath(), -1, true);
                            if (pli != null)
                                playlist.appendItem(pli);
                        }
                    }
                    // Start the playlist from the top.
                    playlist.nextCursor();
                    fileList.initPlayList();
                    this.setCurrentSong(playlist.getCursor());
                }
            } else {
                log.info("Unknown dropped objects");
            }
        } catch (IOException ioe) {
            log.info("Drop error", ioe);
            e.dropComplete(false);
            return;
        } catch (UnsupportedFlavorException ufe) {
            log.info("Drop error", ufe);
            e.dropComplete(false);
            return;
        } catch (Exception ex) {
            log.info("Drop error", ex);
            e.dropComplete(false);
            return;
        }
        e.dropComplete(true);
    }
}

From source file:org.apache.jmeter.gui.MainFrame.java

/**
 * Handler of Top level Dnd//w  ww  . ja v a2  s .co m
 */
@Override
public void drop(DropTargetDropEvent dtde) {
    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (DataFlavor flavor : flavors) {
            // Check for file lists specifically
            if (flavor.isFlavorJavaFileListType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                try {
                    openJmxFilesFromDragAndDrop(tr);
                } finally {
                    dtde.dropComplete(true);
                }
                return;
            }
        }
    } catch (UnsupportedFlavorException | IOException e) {
        log.warn("Dnd failed", e);
    }

}

From source file:org.kuali.test.ui.dnd.RepositoryDropTargetAdapter.java

@Override
public void drop(DropTargetDropEvent dtde) {
    boolean success = false;
    try {/*from   w ww  .  j ava2s  .  c o m*/
        if (canDrop(dtde)) {
            Transferable t = dtde.getTransferable();
            Object data = t.getTransferData(t.getTransferDataFlavors()[0]);
            DefaultMutableTreeNode dropNode = getNodeAtPoint(dtde.getLocation());
            if ((dropNode != null) && (data != null) && (data instanceof RepositoryTransferData)) {
                repositoryTree.handleDataDrop(t.getTransferDataFlavors()[0], (RepositoryTransferData) data,
                        dropNode);
                success = true;
                dtde.dropComplete(true);
            }
        }
    }

    catch (Exception e) {
        LOG.error(e.toString(), e);
    }

    if (!success) {
        dtde.rejectDrop();
    }
}