Example usage for java.awt.dnd DropTargetDropEvent getDropAction

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

Introduction

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

Prototype

public int getDropAction() 

Source Link

Document

This method returns the user drop action.

Usage

From source file:EditorDropTarget.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();

        try {/*w w w.j av  a2s. c om*/
            boolean result = dropFile(transferable);

            dtde.dropComplete(result);
            DnDUtils.debugPrintln("Drop completed, success: " + result);
        } catch (Exception e) {
            DnDUtils.debugPrintln("Exception while handling drop " + e);
            dtde.dropComplete(false);
        }
    } else {
        DnDUtils.debugPrintln("Drop target rejected drop");
        dtde.rejectDrop();
    }
}

From source file:PanelDropTarget.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();

        try {//  www .  j  a v a  2  s  .  c o m
            boolean result = dropComponent(transferable);

            dtde.dropComplete(result);
            DnDUtils.debugPrintln("Drop completed, success: " + result);
        } catch (Exception e) {
            DnDUtils.debugPrintln("Exception while handling drop " + e);
            dtde.dropComplete(false);
        }
    } else {
        DnDUtils.debugPrintln("Drop target rejected drop");
        dtde.rejectDrop();
    }
}

From source file:Main.java

public void drop(DropTargetDropEvent e) {
    System.out.println("Dropping");

    try {//ww w  .  ja v a2 s .  c o  m
        Transferable t = e.getTransferable();

        if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            e.acceptDrop(e.getDropAction());

            String s;
            s = (String) t.getTransferData(DataFlavor.stringFlavor);

            target.setText(s);

            e.dropComplete(true);
        } else
            e.rejectDrop();
    } catch (java.io.IOException e2) {
    } catch (UnsupportedFlavorException e2) {
    }
}

From source file:EditorDropTarget3.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();

        try {//from w  w  w .ja  va 2s . c o m
            boolean result = false;

            if (draggingFile) {
                result = dropFile(transferable);
            } else {
                result = dropContent(transferable, dtde);
            }

            dtde.dropComplete(result);
            DnDUtils.debugPrintln("Drop completed, success: " + result);
        } catch (Exception e) {
            DnDUtils.debugPrintln("Exception while handling drop " + e);
            dtde.dropComplete(false);
        }
    } else {
        DnDUtils.debugPrintln("Drop target rejected drop");
        dtde.rejectDrop();
    }
}

From source file:EditorDropTarget4.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();

        try {/*ww w .  j  a v a2  s . c o  m*/
            boolean result = false;

            if (draggingFile) {
                result = dropFile(transferable);
            } else {
                result = dropContent(transferable, dtde);
            }

            dtde.dropComplete(result);
            DnDUtils.debugPrintln("Drop completed, success: " + result);
        } catch (Exception e) {
            DnDUtils.debugPrintln("Exception while handling drop " + e);
            dtde.rejectDrop();
        }
    } else {
        DnDUtils.debugPrintln("Drop target rejected drop");
        dtde.dropComplete(false);
    }
}

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 v a2 s.com*/
        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;/*  w ww  . ja v  a  2s . com*/

    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: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   w w  w .j  a  v a 2s  .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:com.declarativa.interprolog.gui.ListenerWindow.java

void handlePrologInputDnD(DropTargetDropEvent dtde) {
    //System.out.println("drop:"+dtde);
    try {/*w  w w.j  a  va  2s  .  com*/
        Transferable transferable = dtde.getTransferable();
        /*
         DataFlavor[] flavors = transferable.getTransferDataFlavors();
         for (int f=0;f<flavors.length;f++)
         System.out.println("Flavor:"+flavors[f]);*/
        int action = dtde.getDropAction();
        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            if (engine.isIdle()) {
                dtde.acceptDrop(action);
                final java.util.List files = (java.util.List) transferable
                        .getTransferData(DataFlavor.javaFileListFlavor);
                dtde.getDropTargetContext().dropComplete(true);
                boolean allPs = true;
                for (int f = 0; f < files.size(); f++) {
                    String filename = ((File) files.get(f)).getName();
                    int dot = filename.lastIndexOf('.');
                    if (!filename.endsWith(".P")) {
                        allPs = false;
                        break;
                    }
                }
                if (!allPs) {
                    errorMessage("All dragged files must be Prolog source files (with a .P extension)");
                } else {
                    prologOutput.append("\nReconsulting " + ((files.size() > 1 ? files.size() + " files...\n"
                            : files.size() + " file...\n")));
                    Runnable r = new Runnable() {
                        public void run() {
                            boolean crashed = false;
                            Toolkit.getDefaultToolkit().sync();
                            for (int f = 0; f < files.size() && !crashed; f++) {
                                File file = (File) files.get(f);
                                if (!processDraggedFile(file)) {
                                    crashed = true;
                                }
                            }
                            if (crashed) {
                                prologOutput.append("...terminated with errors.\n");
                            } else {
                                prologOutput.append("...done.\n");
                            }
                        }
                    };
                    SwingUtilities.invokeLater(r);
                }
            } else {
                dtde.rejectDrop();
                errorMessage("You can not consult files while Prolog is working");
            }
        } else {
            dtde.rejectDrop();
        }
    } catch (Exception e) {
        throw new IPException("Problem dropping:" + e);
    }
}

From source file:org.rdv.datapanel.AbstractDataPanel.java

@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent e) {
    try {//from  w ww.ja v a2 s .  c  om
        int dropAction = e.getDropAction();
        if (dropAction == DnDConstants.ACTION_LINK) {
            DataFlavor channelListDataFlavor = new ChannelListDataFlavor();
            Transferable tr = e.getTransferable();
            if (e.isDataFlavorSupported(channelListDataFlavor)) {
                e.acceptDrop(DnDConstants.ACTION_LINK);
                e.dropComplete(true);

                final List<String> channels = (List) tr.getTransferData(channelListDataFlavor);

                new Thread() {
                    public void run() {
                        for (int i = 0; i < channels.size(); i++) {
                            String channel = channels.get(i);
                            boolean status;
                            if (supportsMultipleChannels()) {
                                status = addChannel(channel);
                            } else {
                                status = setChannel(channel);
                            }

                            if (!status) {
                                // TODO display an error in the UI
                            }
                        }
                    }
                }.start();
            } else {
                e.rejectDrop();
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
    }
}