Example usage for java.awt.datatransfer Transferable getTransferDataFlavors

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

Introduction

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

Prototype

public DataFlavor[] getTransferDataFlavors();

Source Link

Document

Returns an array of DataFlavor objects indicating the flavors the data can be provided in.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    System.out.println("Object Name: " + clip.getName());
    Transferable contents = clip.getContents(new MainClass().getClass());
    if (contents == null)
        System.out.println("\n\nThe clipboard is empty.");
    else {/*from ww w . j  a  v  a2 s .c om*/
        DataFlavor flavors[] = contents.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; ++i) {
            System.out.println("\n\n Name: " + flavors[i].getHumanPresentableName());
            System.out.println("\n MIME Type: " + flavors[i].getMimeType());
            Class cl = flavors[i].getRepresentationClass();
            if (cl == null)
                System.out.println("null");
            else
                System.out.println(cl.getName());
        }
    }
}

From source file:Main.java

public boolean importData(JComponent comp, Transferable t) {
    DataFlavor[] flavors = t.getTransferDataFlavors();
    System.out.println("Trying to import:" + t);
    for (int i = 0; i < flavors.length; i++) {
        DataFlavor flavor = flavors[i];
        try {/*from   www . j av a 2s. c o m*/
            if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                System.out.println("importData: FileListFlavor");
                List l = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
                Iterator iter = l.iterator();
                while (iter.hasNext()) {
                    File file = (File) iter.next();
                    System.out.println("GOT FILE: " + file.getCanonicalPath());
                }
                return true;
            } else if (flavor.equals(DataFlavor.stringFlavor)) {
                System.out.println("importData: String Flavor");
                String fileOrURL = (String) t.getTransferData(flavor);
                System.out.println("GOT STRING: " + fileOrURL);
                try {
                    URL url = new URL(fileOrURL);
                    System.out.println("Valid URL: " + url.toString());
                    return true;
                } catch (MalformedURLException ex) {
                    System.err.println("Not a valid URL");
                    return false;
                }
            } else {
                System.out.println("importData rejected: " + flavor);
            }
        } catch (IOException ex) {
            System.err.println("IOError getting data: " + ex);
        } catch (UnsupportedFlavorException e) {
            System.err.println("Unsupported Flavor: " + e);
        }
    }
    return false;
}

From source file:DropTest.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++) {
            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 {// w  w  w .  j  av 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.jhylafax.addressbook.AbstractContactTransferHandler.java

@Override
public boolean importData(JComponent component, Transferable transferable) {
    if (canImport(component, transferable.getTransferDataFlavors())) {
        try {// ww  w  .  j  a  va2s  .co m
            ContactIOFactory factory = Pim.getContactIOFactory();
            ContactUnmarshaller unmarshaller = factory.createContactUnmarshaller();
            InputStream in = (InputStream) transferable.getTransferData(ContactTransferable.VCARD_FLAVOR);
            unmarshaller.setEncoding("UTF-8");
            Contact[] contacts = unmarshaller.unmarshallContacts(in);
            if (contacts != null && contacts.length > 0) {
                importData(contacts);
            }
            return true;
        } catch (Exception e) {
            logger.debug("Error during import", e);
        }
    }
    return false;
}

From source file:DropTest.java

public void drop(DropTargetDropEvent dtde) {
    try {//  w  w w  .  j  av  a 2s.  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:com.codecrate.shard.ui.dragdrop.FileTransferHandler.java

public boolean importData(JComponent c, Transferable t) {
    if (!canImport(c, t.getTransferDataFlavors())) {
        return false;
    }/*from  ww w.ja va  2  s  .  c  om*/
    try {
        if (hasFileFlavor(t.getTransferDataFlavors())) {
            List files = (List) t.getTransferData(fileDataFlavor);
            for (int i = 0; i < files.size(); i++) {
                File file = (File) files.get(i);
                onFileDrop(file);
            }
        }
        if (hasUriListFlavor(t.getTransferDataFlavors())) {
            String fileNames = (String) t.getTransferData(uriListFlavor);
            File file = new File(new URI(fileNames.trim()));
            onFileDrop(file);
        }
        return true;
    } catch (Exception e) {
        LOG.error("Error handling drag/drop", e);
    }
    return false;
}

From source file:com.frostwire.gui.bittorrent.BTDownloadTransferHandler.java

public boolean importData(JComponent comp, Transferable t) {
    if (DNDUtils.contains(t.getTransferDataFlavors(), SearchResultTransferable.dataFlavor)) {
        try {/*from  ww w  .  j a  v a2s.  co  m*/
            SearchResultTransferable srt = (SearchResultTransferable) t
                    .getTransferData(SearchResultTransferable.dataFlavor);
            SearchResultMediator rp = srt.getResultPanel();
            SearchResultDataLine[] lines = srt.getTableLines();
            SearchMediator.downloadFromPanel(rp, lines);
            return true;
        } catch (Throwable e) {
            LOG.error("Error importing DnD data", e);
        }
    }
    return DNDUtils.DEFAULT_TRANSFER_HANDLER.importData(comp, t);
}

From source file:DropTest2.java

public void drop(DropTargetDropEvent dtde) {
    try {/*from   w w w.  java 2s.  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_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:Dropper.java

/**
 * Do the actual import.//  ww  w. j av  a  2  s .co m
 * 
 * @see javax.swing.TransferHandler#importData(javax.swing.JComponent,
 *      java.awt.datatransfer.Transferable)
 */
public boolean importData(JComponent comp, Transferable t) {
    DataFlavor[] flavors = t.getTransferDataFlavors();
    System.out.println("Trying to import:" + t);
    System.out.println("... which has " + flavors.length + " flavors.");
    for (int i = 0; i < flavors.length; i++) {
        DataFlavor flavor = flavors[i];
        try {
            if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                System.out.println("importData: FileListFlavor");

                List l = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
                Iterator iter = l.iterator();
                while (iter.hasNext()) {
                    File file = (File) iter.next();
                    System.out.println("GOT FILE: " + file.getCanonicalPath());
                    // Now do something with the file...
                }
                return true;
            } else if (flavor.equals(DataFlavor.stringFlavor)) {
                System.out.println("importData: String Flavor");
                String fileOrURL = (String) t.getTransferData(flavor);
                System.out.println("GOT STRING: " + fileOrURL);
                try {
                    URL url = new URL(fileOrURL);
                    System.out.println("Valid URL: " + url.toString());
                    // Do something with the contents...
                    return true;
                } catch (MalformedURLException ex) {
                    System.err.println("Not a valid URL");
                    return false;
                }
                // now do something with the String.

            } else {
                System.out.println("importData rejected: " + flavor);
                // Don't return; try next flavor.
            }
        } catch (IOException ex) {
            System.err.println("IOError getting data: " + ex);
        } catch (UnsupportedFlavorException e) {
            System.err.println("Unsupported Flavor: " + e);
        }
    }
    // If you get here, I didn't like the flavor.
    Toolkit.getDefaultToolkit().beep();
    return false;
}