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: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);/*from  www .j  ava  2  s .co  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: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");
    }//from  w w  w  .j a v a 2s . c  om
    lastDropSuccess = success;
}

From source file:net.rptools.tokentool.ui.TokenCompositionPanel.java

public void drop(DropTargetDropEvent dtde) {

    Transferable transferable = dtde.getTransferable();
    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

    try {//  ww  w  . ja v  a 2 s.  co m
        if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            List<URL> urls = new FileTransferableHandler().getTransferObject(transferable);

            for (URL url : urls) {
                String baseName = java.net.URLDecoder.decode(FilenameUtils.getBaseName(url.getFile()), "UTF-8");
                TokenTool.getFrame().getControlPanel().setNamePrefixField(baseName);
            }
        }
    } catch (UnsupportedFlavorException | IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        Image image = new ImageTransferableHandler().getTransferObject(transferable);
        if (!(image instanceof BufferedImage)) {
            // Convert to buffered image
            image = ImageUtil.createCompatibleImage(image);
        }

        setToken((BufferedImage) image);
    } catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    fireCompositionChanged();
}

From source file:com.mirth.connect.client.ui.TemplatePanel.java

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

public void drop(DropTargetDropEvent dtde) {
    try {//from w w w .  j  a  v  a2  s .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:HostPanel.java

/**
 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
 *//*from  w w  w  . j  a v  a2 s.c  om*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

    dropTargetScroll = new javax.swing.JScrollPane();
    dropTargetArea = new javax.swing.JTextArea();
    dropTargetTitle = new javax.swing.JLabel();
    globalProgress = new javax.swing.JProgressBar();
    currentProgress = new javax.swing.JProgressBar();
    transferButton = new javax.swing.JButton();
    dropTargetValue = new javax.swing.JLabel();
    dropTargetClear = new javax.swing.JLabel();
    statusDisplay1 = new javax.swing.JLabel();
    statusDisplay2 = new javax.swing.JLabel();

    setFocusable(false);

    dropTargetScroll.setFocusable(false);

    dropTargetArea.setDropTarget(new DropTarget() {
        @Override
        public synchronized void drop(DropTargetDropEvent evt) {
            if (transfer == null || transfer.isDone()) {
                evt.acceptDrop(DnDConstants.ACTION_LINK);
                Transferable transferable = evt.getTransferable();

                try {
                    if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                        ProcessDropTarget pdt = new ProcessDropTarget(
                                (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor));
                        pdt.execute();
                    }
                } catch (UnsupportedFlavorException | IOException ex) {
                    Logger.getLogger(HostPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    dropTargetArea.setEditable(false);
    dropTargetArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
    dropTargetArea.setFocusable(false);
    dropTargetScroll.setViewportView(dropTargetArea);

    dropTargetTitle.setText("Drag files to host onto box below");
    dropTargetTitle.setFocusable(false);

    globalProgress.setFocusable(false);
    globalProgress.setRequestFocusEnabled(false);
    globalProgress.setStringPainted(true);

    currentProgress.setFocusable(false);
    currentProgress.setRequestFocusEnabled(false);
    currentProgress.setStringPainted(true);

    transferButton.setText("Host");
    transferButton.setActionCommand("host");
    transferButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            transferButtonActionPerformed(evt);
        }
    });

    dropTargetValue.setText("Files to Share: 0");
    dropTargetValue.setFocusable(false);

    dropTargetClear.setText("Clear");
    dropTargetClear.setFocusable(false);
    dropTargetClear.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            dropTargetClearMouseReleased(evt);
        }
    });

    statusDisplay1.setText("");

    statusDisplay2.setText("");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dropTargetScroll)
                            .addGroup(layout.createSequentialGroup().addGroup(
                                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(globalProgress, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    165, Short.MAX_VALUE)
                                            .addComponent(currentProgress, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(statusDisplay1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    359, Short.MAX_VALUE)
                                            .addComponent(statusDisplay2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGap(18, 18, 18)
                                    .addComponent(transferButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
                                            javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup().addComponent(dropTargetTitle)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                            javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(dropTargetValue, javax.swing.GroupLayout.PREFERRED_SIZE, 136,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18).addComponent(dropTargetClear)))
                    .addContainerGap()));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            javax.swing.GroupLayout.Alignment.TRAILING,
            layout.createSequentialGroup().addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dropTargetTitle).addComponent(dropTargetValue)
                            .addComponent(dropTargetClear))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(dropTargetScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup().addGroup(layout
                                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(currentProgress, javax.swing.GroupLayout.PREFERRED_SIZE,
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(statusDisplay1))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(globalProgress,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(statusDisplay2)))
                            .addComponent(transferButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap()));
}

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  v a2  s .  com*/
            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();
    }
}