Example usage for java.awt.dnd DropTargetDropEvent getTransferable

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

Introduction

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

Prototype


public Transferable getTransferable() 

Source Link

Document

This method returns the Transferable object associated with the drop.

Usage

From source file:DropTest.java

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

public void drop(DropTargetDropEvent dtde) {
    try {/*from ww w.  ja v a2 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.jabref.gui.entryeditor.SimpleUrlDragDrop.java

@Override
public void drop(DropTargetDropEvent event) {
    Transferable tsf = event.getTransferable();
    event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    //try with an URL
    DataFlavor dataFlavor = null;
    try {/*from www  .ja  va2  s  . co  m*/
        dataFlavor = new DataFlavor("application/x-java-url; class=java.net.URL");
    } catch (ClassNotFoundException e) {
        LOGGER.warn("Could not find DropTargetDropEvent class", e);
    }
    try {
        URL url = (URL) tsf.getTransferData(dataFlavor);
        //insert URL
        editor.setText(url.toString());
        storeFieldAction.actionPerformed(new ActionEvent(editor, 0, ""));
    } catch (UnsupportedFlavorException nfe) {
        // if not an URL
        JOptionPane.showMessageDialog((Component) editor, Localization.lang("Operation not supported"),
                Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE);
        LOGGER.warn("Could not perform drage and drop", nfe);
    } catch (IOException ioex) {
        LOGGER.warn("Could not perform drage and drop", ioex);
    }
}

From source file:Main.java

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

    try {/*  w ww  .  ja  v a  2 s. 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:DropTest.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from   w w  w.  j a  va 2 s  .  c  o  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);
                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: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 {// w  w w . j  av a 2  s  .  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:DropTest2.java

public void drop(DropTargetDropEvent dtde) {
    try {//  w ww  .j  a  v a  2 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_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:DNDList.java

/**
 * a drop has occurred//  w ww  .  ja va 2s.co m
 * 
 */

public void drop(DropTargetDropEvent event) {

    try {
        Transferable transferable = event.getTransferable();

        // we accept only Strings
        if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {

            event.acceptDrop(DnDConstants.ACTION_MOVE);
            String s = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            addElement(s);
            event.getDropTargetContext().dropComplete(true);
        } else {
            event.rejectDrop();
        }
    } catch (Exception exception) {
        System.err.println("Exception" + exception.getMessage());
        event.rejectDrop();
    }
}

From source file:net.sf.jabref.gui.UrlDragDrop.java

@Override
public void drop(DropTargetDropEvent dtde) {
    Transferable tsf = dtde.getTransferable();
    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    //try with an URL
    DataFlavor dtURL = null;/*from  w  w  w . j  a va  2s  . c o m*/
    try {
        dtURL = new DataFlavor("application/x-java-url; class=java.net.URL");
    } catch (ClassNotFoundException e) {
        LOGGER.warn("Could not find DropTargetDropEvent class.", e);
    }
    try {
        URL url = (URL) tsf.getTransferData(dtURL);
        JOptionChoice res = (JOptionChoice) JOptionPane.showInputDialog(editor, "",
                Localization.lang("Select action"), JOptionPane.QUESTION_MESSAGE, null,
                new JOptionChoice[] { new JOptionChoice(Localization.lang("Insert URL"), 0),
                        new JOptionChoice(Localization.lang("Download file"), 1) },
                new JOptionChoice(Localization.lang("Insert URL"), 0));
        if (res != null) {
            switch (res.getId()) {
            //insert URL
            case 0:
                feditor.setText(url.toString());
                editor.updateField(feditor);
                break;
            //download file
            case 1:
                try {
                    //auto filename:
                    File file = new File(new File(Globals.prefs.get("pdfDirectory")),
                            editor.getEntry().getCiteKey() + ".pdf");
                    frame.output(Localization.lang("Downloading..."));
                    MonitoredURLDownload.buildMonitoredDownload(editor, url).downloadToFile(file);
                    frame.output(Localization.lang("Download completed"));
                    feditor.setText(file.toURI().toURL().toString());
                    editor.updateField(feditor);

                } catch (IOException ioex) {
                    LOGGER.error("Error while downloading file.", ioex);
                    JOptionPane.showMessageDialog(editor, Localization.lang("File download"),
                            Localization.lang("Error while downloading file:" + ioex.getMessage()),
                            JOptionPane.ERROR_MESSAGE);
                }
                break;
            default:
                LOGGER.warn("Unknown selection (should not happen)");
                break;
            }
        }
        return;
    } catch (UnsupportedFlavorException nfe) {
        // not an URL then...
        LOGGER.warn("Could not parse URL.", nfe);
    } catch (IOException ioex) {
        LOGGER.warn("Could not perform drag and drop.", ioex);
    }

    try {
        //try with a File List
        @SuppressWarnings("unchecked")
        List<File> filelist = (List<File>) tsf.getTransferData(DataFlavor.javaFileListFlavor);
        if (filelist.size() > 1) {
            JOptionPane.showMessageDialog(editor, Localization.lang("Only one item is supported"),
                    Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        File fl = filelist.get(0);
        feditor.setText(fl.toURI().toURL().toString());
        editor.updateField(feditor);

    } catch (UnsupportedFlavorException nfe) {
        JOptionPane.showMessageDialog(editor, Localization.lang("Operation not supported"),
                Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE);
        LOGGER.warn("Could not perform drag and drop.", nfe);
    } catch (IOException ioex) {
        LOGGER.warn("Could not perform drag and drop.", ioex);
    }

}

From source file:com.mirth.connect.client.ui.editors.JavaScriptEditorDialog.java

public void drop(DropTargetDropEvent dtde) {
    try {/*  w  w w  .  j a  v a  2s.  c om*/
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

            java.util.List fileList = (java.util.List) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = (File) iterator.next();

                scriptContent.setText(
                        scriptContent.getText() + FileUtils.readFileToString(file, UIConstants.CHARSET));
            }
        }
    } catch (Exception e) {
        dtde.rejectDrop();
    }
}