Example usage for java.awt.dnd DropTargetDropEvent rejectDrop

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

Introduction

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

Prototype


public void rejectDrop() 

Source Link

Document

reject the Drop.

Usage

From source file:ScribbleDragAndDrop.java

/**
 * This is the key method of DropTargetListener. It is invoked when the user
 * drops something on us./*from  ww  w . j  av a 2s.c  o 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: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 ava 2 s  . co 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:DropTest.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from   ww w  .jav a2  s  .  c om*/
        // 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);
                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;
            }
        }
        // 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:com.mirth.connect.client.ui.EditMessageDialog.java

public void drop(DropTargetDropEvent dtde) {
    try {//  ww  w.  j a  va  2s.  com
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

            List<File> fileList = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator<File> iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = iterator.next();
                messageContent.setText(
                        messageContent.getText() + FileUtils.readFileToString(file, UIConstants.CHARSET));
            }
        }
    } catch (Exception e) {
        dtde.rejectDrop();
    }
}

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 {//ww w.  j  ava2 s.c  om
            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:Main.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from w w w . j  a  va2  s  .  c  o m*/
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            if (flavors[i].isFlavorJavaFileListType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                List list = (List) tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size(); j++) {
                    ta.append(list.get(j) + "\n");
                }
                dtde.dropComplete(true);
                return;
            } else if (flavors[i].isFlavorSerializedObjectType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                Object o = tr.getTransferData(flavors[i]);
                ta.append("Object: " + o);
                dtde.dropComplete(true);
                return;
            } else if (flavors[i].isRepresentationClassInputStream()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.read(new InputStreamReader((InputStream) tr.getTransferData(flavors[i])),
                        "from system clipboard");
                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 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 w  w.ja  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:com.mirth.connect.client.ui.TemplatePanel.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from w  w w  . jav  a 2s . co  m*/
        Transferable tr = dtde.getTransferable();

        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            File file = ((List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)).get(0);

            String dataType = PlatformUI.MIRTH_FRAME.displayNameToDataType.get(getDataType());
            DataTypeClientPlugin dataTypePlugin = LoadedExtensions.getInstance().getDataTypePlugins()
                    .get(dataType);
            if (dataTypePlugin.isBinary()) {
                byte[] content = FileUtils.readFileToByteArray(file);

                // The plugin should decide how to convert the byte array to string
                pasteBox.setText(dataTypePlugin.getTemplateString(content));
            } else {
                pasteBox.setText(FileUtils.readFileToString(file, UIConstants.CHARSET));
            }

            parent.modified = true;
        }
    } catch (Exception e) {
        dtde.rejectDrop();
    }
}

From source file:ColorSink.java

/** This method is invoked when the user drops something on us */
public void drop(DropTargetDropEvent e) {
    this.setBorder(null); // Restore the default border
    Transferable t = e.getTransferable(); // Get the data that was dropped

    // Check for types of data that we support
    if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
        // If it was a color, accept it, and use it as the background color
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {//from w  ww.  jav a2s  .  c  o m
            Color c = (Color) t.getTransferData(TransferableColor.colorFlavor);
            this.setBackground(c);
            e.dropComplete(true);
        } catch (Exception ex) {
            e.dropComplete(false);
        }
    } else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        // If it was a file list, accept it, read the first file in the list
        // and display the file contents
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {
            List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
            File f = (File) files.get(0);
            BufferedReader in = new BufferedReader(new FileReader(f));
            String s;
            this.setText("");
            while ((s = in.readLine()) != null)
                this.append(s);
            e.dropComplete(true);
        } catch (Exception ex) {
            e.dropComplete(false);
        }
    } else { // If it wasn't a color or a file list, reject it.
        e.rejectDrop();
        return;
    }
}

From source file:com.qspin.qtaste.ui.TestCaseTree.java

public void drop(DropTargetDropEvent dtde) {
    //try/*from  w  w w . j a  v  a 2 s . c o m*/
    {
        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());
        }

    }
}