Example usage for java.awt.dnd DropTargetDropEvent acceptDrop

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

Introduction

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

Prototype


public void acceptDrop(int dropAction) 

Source Link

Document

accept the drop, using the specified action.

Usage

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 {// w ww  . j a  va 2s. c o  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  . j av  a  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:org.eclipse.swt.snippets.Snippet319.java

public void go() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 319");
    shell.setBounds(10, 10, 600, 200);//ww w .  j  a v  a2  s. c  o  m

    /* Create SWT controls and add drag source */
    final Label swtLabel = new Label(shell, SWT.BORDER);
    swtLabel.setBounds(10, 10, 580, 50);
    swtLabel.setText("SWT drag source");
    DragSource dragSource = new DragSource(swtLabel, DND.DROP_COPY);
    dragSource.setTransfer(new MyTypeTransfer());
    dragSource.addDragListener(new DragSourceAdapter() {
        @Override
        public void dragSetData(DragSourceEvent event) {
            MyType object = new MyType();
            object.name = "content dragged from SWT";
            object.time = System.currentTimeMillis();
            event.data = object;
        }
    });

    /* Create AWT/Swing controls */
    Composite embeddedComposite = new Composite(shell, SWT.EMBEDDED);
    embeddedComposite.setBounds(10, 100, 580, 50);
    embeddedComposite.setLayout(new FillLayout());
    Frame frame = SWT_AWT.new_Frame(embeddedComposite);
    final JLabel jLabel = new JLabel("AWT/Swing drop target");
    frame.add(jLabel);

    /* Register the custom data flavour */
    final DataFlavor flavor = new DataFlavor(MIME_TYPE, "MyType custom flavor");
    /*
     * Note that according to jre/lib/flavormap.properties, the preferred way to
     * augment the default system flavor map is to specify the AWT.DnD.flavorMapFileURL
     * property in an awt.properties file.
     *
     * This snippet uses the alternate approach below in order to provide a simple
     * stand-alone snippet that demonstrates the functionality.  This implementation
     * works well, but if the instanceof check below fails for some reason when used
     * in a different context then the drop will not be accepted.
     */
    FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
    if (map instanceof SystemFlavorMap) {
        SystemFlavorMap systemMap = (SystemFlavorMap) map;
        systemMap.addFlavorForUnencodedNative(MIME_TYPE, flavor);
    }

    /* add drop target */
    DropTargetListener dropTargetListener = new DropTargetAdapter() {
        @Override
        public void drop(DropTargetDropEvent dropTargetDropEvent) {
            try {
                dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY);
                ByteArrayInputStream inStream = (ByteArrayInputStream) dropTargetDropEvent.getTransferable()
                        .getTransferData(flavor);
                int available = inStream.available();
                byte[] bytes = new byte[available];
                inStream.read(bytes);
                MyType object = restoreFromByteArray(bytes);
                String string = object.name + ": " + new Date(object.time).toString();
                jLabel.setText(string);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    new DropTarget(jLabel, dropTargetListener);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Main.java

public void drop(DropTargetDropEvent dtde) {
    try {//ww w .j a  v a 2 s  . co 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.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 www .  j  a  va 2  s .co  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: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");
    }// w  w w  .j a  v  a  2s.  c o  m
    lastDropSuccess = success;
}

From source file:DropTest.java

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

/**
 * a drop has occurred/*w  w w  . j  a va2 s.  c  o  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: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 w w w  .j a va2  s. com*/
        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:com.mirth.connect.client.ui.editors.JavaScriptEditorDialog.java

public void drop(DropTargetDropEvent dtde) {
    try {//  w ww. ja v a  2  s .co  m
        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();
    }
}