Example usage for java.awt.dnd DropTargetDropEvent getLocation

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

Introduction

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

Prototype


public Point getLocation() 

Source Link

Document

This method returns a Point indicating the Cursor 's current location in the Component 's coordinates.

Usage

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();//  w w w  .  j  a  va  2  s .co 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:org.jas.dnd.MultiLayerDropTargetListener.java

@Override
public void drop(final DropTargetDropEvent dtde) {
    dtde.acceptDrop(DnDConstants.ACTION_COPY);
    initializeTransferable(dtde.getTransferable(), true);
    boolean success = getDragAction().drop(dtde.getLocation());
    if (success) {
        log.info("drop success");
    }/* www.  j ava2  s . c om*/
    lastDropSuccess = success;
}

From source file:de.tor.tribes.ui.windows.AbstractDSWorkbenchFrame.java

@Override
public void drop(DropTargetDropEvent dtde) {
    if (dtde.isDataFlavorSupported(VillageTransferable.villageDataFlavor)
            || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    } else {/*from ww w. jav  a 2 s  . c o m*/
        dtde.rejectDrop();
        return;
    }

    Transferable t = dtde.getTransferable();
    List<Village> v;
    MapPanel.getSingleton().setCurrentCursor(MapPanel.getSingleton().getCurrentCursor());
    try {
        v = (List<Village>) t.getTransferData(VillageTransferable.villageDataFlavor);
        fireVillagesDraggedEvent(v, dtde.getLocation());
    } catch (Exception ignored) {
    }
}

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();/*from   w ww . j  av a  2 s .  com*/
        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:com.qspin.qtaste.ui.TestCaseTree.java

public void drop(DropTargetDropEvent dtde) {
    //try/*from   ww  w.  j  av  a2  s .c om*/
    {
        try {
            TCTreeNode tcTreeNode = (TCTreeNode) dtde.getTransferable().getTransferData(localObjectFlavor);
            Point dropPoint = dtde.getLocation();
            // int index = locationToIndex (dropPoint);
            TreePath path = getPathForLocation(dropPoint.x, dropPoint.y);
            Object targetNode = path.getLastPathComponent();
            if (targetNode instanceof TCTreeNode) {
                // rename the dragged dir into the new target one
                TCTreeNode tcTargetNode = (TCTreeNode) targetNode;
                FileNode fn = (FileNode) tcTargetNode.getUserObject();
                if (fn.isTestcaseDir()) {
                    dtde.rejectDrop();
                    return;
                }

                FileNode draggedFileNode = (FileNode) tcTreeNode.getUserObject();
                draggedFileNode.getFile()
                        .renameTo(new File(fn.getFile() + "/" + draggedFileNode.getFile().getName()));
                // update target tree

                testCasePane.parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

                TCTreeNode parentTreeNode = (TCTreeNode) tcTargetNode.getParent();
                if (parentTreeNode != null) {
                    parentTreeNode.removeAllChildren();
                    FileNode parentFileNode = (FileNode) parentTreeNode.getUserObject();
                    addTreeToDir(parentFileNode.getFile(), parentTreeNode);
                    ((DefaultTreeModel) getModel()).reload(parentTreeNode);
                } else {
                    tcTargetNode.removeAllChildren();
                    FileNode targetFileNode = (FileNode) tcTargetNode.getUserObject();
                    addTreeToDir(targetFileNode.getFile(), tcTargetNode);
                    ((DefaultTreeModel) getModel()).reload(tcTargetNode);
                }
                // update source tree
                parentTreeNode = (TCTreeNode) tcTreeNode.getParent();
                if (parentTreeNode != null) {
                    parentTreeNode.removeAllChildren();
                    FileNode parentFileNode = (FileNode) parentTreeNode.getUserObject();
                    addTreeToDir(parentFileNode.getFile(), parentTreeNode);
                    ((DefaultTreeModel) getModel()).reload(parentTreeNode);
                } else {
                    tcTreeNode.removeAllChildren();
                    FileNode targetFileNode = (FileNode) tcTreeNode.getUserObject();
                    addTreeToDir(targetFileNode.getFile(), tcTreeNode);
                    ((DefaultTreeModel) getModel()).reload(tcTreeNode);
                }
                testCasePane.parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                dtde.getDropTargetContext().dropComplete(true);
            } else {
                dtde.rejectDrop();
            }
        } catch (UnsupportedFlavorException ex) {
            logger.error(ex.getMessage());
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }

    }
}

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;/*from  w  ww.j  a  va  2 s.  c o m*/
        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: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 {// ww w.ja v  a2 s.  com
            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:com.igormaznitsa.sciareto.ui.editors.MMDEditor.java

@Override
public void drop(@Nonnull final DropTargetDropEvent dtde) {

    dtde.acceptDrop(DnDConstants.ACTION_MOVE);
    File detectedFileObject = null;

    for (final DataFlavor df : dtde.getCurrentDataFlavors()) {
        final Class<?> representation = df.getRepresentationClass();
        if (FileTransferable.class.isAssignableFrom(representation)) {
            final FileTransferable t = (FileTransferable) dtde.getTransferable();
            final List<File> listOfFiles = t.getFiles();
            detectedFileObject = listOfFiles.isEmpty() ? null : listOfFiles.get(0);
            break;
        } else if (df.isFlavorJavaFileListType()) {
            try {
                final List list = (List) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                if (list != null && !list.isEmpty()) {
                    detectedFileObject = (File) list.get(0);
                }/* w  ww  . j  av a 2s  .  c  o  m*/
                break;
            } catch (Exception ex) {
                LOGGER.error("Can't extract file from DnD", ex);
            }
        }
    }

    if (detectedFileObject != null) {
        addFileToElement(detectedFileObject, this.mindMapPanel.findTopicUnderPoint(dtde.getLocation()));
    }
}

From source file:ScribbleDragAndDrop.java

/**
 * This is the key method of DropTargetListener. It is invoked when the user
 * drops something on us.// www  . ja  va2  s.co m
 */
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: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 ww.j  a  v  a 2  s  .co m*/

    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);
}