Example usage for java.awt.datatransfer Transferable getTransferData

List of usage examples for java.awt.datatransfer Transferable getTransferData

Introduction

In this page you can find the example usage for java.awt.datatransfer Transferable getTransferData.

Prototype

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException;

Source Link

Document

Returns an object which represents the data to be transferred.

Usage

From source file:DNDList.java

/**
 * a drop has occurred/* ww w  . j  a  v  a  2 s .  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:com.egangotri.transliteratorAsSwing.TransliteratorJFrame.java

public void copyToClipBoard(String source) {
    Clipboard clipboard;//from ww w  . j a va 2  s  .c  o m
    clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable content = clipboard.getContents(this);
    try {
        String data = (String) content.getTransferData(DataFlavor.stringFlavor);
        StringSelection contents;
        if ("cb1".equalsIgnoreCase(source))
            contents = new StringSelection(tb1.getText());
        else if ("cb2".equalsIgnoreCase(source))
            contents = new StringSelection(tb2.getText());
        else
            contents = new StringSelection(tb3.getText());
        clipboard.setContents(contents, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MyTree.java

public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof MyTree)) {
        return false;
    }//ww w .j  a  v a 2 s . c  o m
    if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        return false;
    }
    MyTree tree = (MyTree) comp;
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    try {
        List data = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
        Iterator i = data.iterator();
        while (i.hasNext()) {
            File f = (File) i.next();
            root.add(new DefaultMutableTreeNode(f.getName()));
        }
        model.reload();
        return true;
    } catch (Exception ioe) {
        System.out.println(ioe);
    }
    return false;
}

From source file:simplesqlformatter.formatter.SQLFormatterEditor.java

private void paste() {
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    final Transferable transferable = clipboard.getContents(null);
    try {//from   w ww. java  2 s .  c  o m
        textArea.setText((String) transferable.getTransferData(DataFlavor.stringFlavor));
    } catch (final IOException e) {
        LOGGER.log(Level.FINE, e.getMessage(), e);
    } catch (final UnsupportedFlavorException e) {
        LOGGER.log(Level.FINE, e.getMessage(), e);
    }
    statusBar.setText("Pasted new SQL statement over the old");
}

From source file:ColorSink.java

public void pastecolor() {
    // Get the clipboard, and read its contents
    Clipboard c = this.getToolkit().getSystemClipboard();
    Transferable t = c.getContents(this);
    if (t == null) { // If nothing to paste
        this.getToolkit().beep(); // then beep and do nothing
        return;//from   w  ww  .j av a2 s  .  c o  m
    }
    try {
        // If the clipboard contained a color, use it as the background color
        if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
            Color color = (Color) t.getTransferData(TransferableColor.colorFlavor);
            this.setBackground(color);
        }
        // If the clipboard contained text, insert it.
        else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String s = (String) t.getTransferData(DataFlavor.stringFlavor);
            this.replaceSelection(s);
        }
        // Otherwise, we don't know how to paste the data, so just beep.
        else
            this.getToolkit().beep();
    } catch (UnsupportedFlavorException ex) {
        this.getToolkit().beep();
    } catch (IOException ex) {
        this.getToolkit().beep();
    }
}

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 w  w  . ja va2  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:EditorDropTarget4.java

protected boolean dropFile(Transferable transferable)
        throws IOException, UnsupportedFlavorException, MalformedURLException {
    List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
    File transferFile = (File) fileList.get(0);
    final URL transferURL = transferFile.toURL();
    DnDUtils.debugPrintln("File URL is " + transferURL);

    pane.setPage(transferURL);//  www  .  j  av a 2  s .c  o m

    return true;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.ImageTransferable.java

@Override
public boolean importData(TransferSupport support) {
    Vector<File> fileList = new Vector<File>();

    try {/* w  w  w  .  j a va 2  s  . c o  m*/
        Transferable trans = support.getTransferable();
        DataFlavor[] flavors = trans.getTransferDataFlavors();
        for (DataFlavor df : flavors) {
            if (df.getHumanPresentableName().equals("text/uri-list")
                    && df.getRepresentationClass() == String.class) {
                String uris = (String) trans.getTransferData(df);
                String[] filePaths = StringUtils.split(uris, "\r\n");
                for (String path : filePaths) {
                    URI uri = URI.create(path);
                    File f = new File(uri);
                    if (filter.isImageFile(path)) {
                        fileList.add(f);
                    }
                }
                break;
            }
        }

    } catch (UnsupportedFlavorException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }

    if (fileList.size() > 0) {
        processImages(fileList);
        return true;
    }

    return false;
}

From source file:DropTest.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from  w  ww.j  av a  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:com.mirth.connect.client.ui.editors.JavaScriptEditorDialog.java

public void dragEnter(DropTargetDragEvent dtde) {
    try {// ww w  . j av  a 2  s . co  m
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);

            java.util.List fileList = (java.util.List) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator iterator = fileList.iterator();
            while (iterator.hasNext()) {
                iterator.next();
            }
        } else {
            dtde.rejectDrag();
        }
    } catch (Exception e) {
        dtde.rejectDrag();
    }
}