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:Main.java

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

    try {//from www  .  j a  va 2s  .  c  om
        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:Main.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from  w w w  .j  av a  2s  .  c om*/
        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: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 {//from  w  w  w  .  j a  va2  s.co m
            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 {/*ww  w .jav  a 2  s.  co  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: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 {/*ww  w .  j  a  v  a2  s. co  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:DropTest.java

public void drop(DropTargetDropEvent dtde) {
    try {//from  ww w. j  av a  2  s  . c om
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            System.out.println("Possible flavor: " + flavors[i].getMimeType());
            if (flavors[i].isFlavorJavaFileListType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
                ta.setText("Successful file list drop.\n\n");

                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");
                }
                dtde.dropComplete(true);
                return;
            }
        }
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        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 {//from   w w w  .j ava2  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.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 a  2  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:DropTest.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from  w  w w.j a va  2  s.  com*/
        // 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:javazoom.jlgui.player.amp.skin.DropTargetAdapter.java

public void drop(DropTargetDropEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;/*from  www .j  a va 2s  . c o m*/
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        } else if (DataFlavor.stringFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Data Flavor available ?
    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);
            processDrop(data);
        } 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);
    }
}