Example usage for java.awt.datatransfer Transferable isDataFlavorSupported

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

Introduction

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

Prototype

public boolean isDataFlavorSupported(DataFlavor flavor);

Source Link

Document

Returns whether or not the specified data flavor is supported for this object.

Usage

From source file:corelyzer.util.StringUtility.java

public static String getClipboard() {
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

    try {/*from  w ww .j  a  va 2s . co m*/
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) t.getTransferData(DataFlavor.stringFlavor);
            return text;
        }
    } catch (UnsupportedFlavorException e) {
    } catch (IOException e) {
    }

    return "";
}

From source file:Main.java

public static String getClipboardData() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    Transferable contents = clipboard.getContents(null);
    DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
    DataFlavor dfTxt = DataFlavor.stringFlavor;

    boolean hasTransferableRTFText = (contents != null) && contents.isDataFlavorSupported(dfRTF);

    boolean hasTransferableTxtText = (contents != null) && contents.isDataFlavorSupported(dfTxt);

    if (hasTransferableRTFText) {
        try {//from w w w .ja v  a  2s  .  c o  m
            result = streamToString((InputStream) contents.getTransferData(dfRTF));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (hasTransferableTxtText) {
        try {
            result = (String) contents.getTransferData(dfTxt);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return result;
}

From source file:org.feistymeow.dragdrop.ListTransferable.java

/**
 * a helper method that can process transfer data from either a java file list or a URI list.
 *///from w w w  .j  a v a 2 s  .com
@SuppressWarnings("unchecked")
static public List<Object> extractData(Transferable tran) {
    if (tran == null)
        return null;
    if (tran.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        logger.debug("extractData seeing java files flavor.");
        try {
            return (List<Object>) tran.getTransferData(DataFlavor.javaFileListFlavor);
        } catch (Throwable cause) {
            logger.error("extractData caught exception for java file list.", cause);
            return null;
        }
    } else if (tran.isDataFlavorSupported(ListTransferable.getURIListFlavor1())
            || tran.isDataFlavorSupported(ListTransferable.getURIListFlavor2())) {
        logger.debug("extractData seeing uri list flavor.");
        try {
            return textURIListToFileList((String) tran.getTransferData(getURIListFlavor1()));
        } catch (Throwable cause) {
            logger.error("extractData caught exception for URI list.", cause);
            return null;
        }
    }
    logger.error("extractData: Transferable did not support known data flavor.");
    return null;
}

From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java

public static void pasteSpec(RuntimeBean runtime)
        throws IOException, URISyntaxException, UnsupportedFlavorException, ParseException {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(null);
    if ((contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        String json = (String) contents.getTransferData(DataFlavor.stringFlavor);
        JSONObject jspec = (JSONObject) mParser.parse(json);
        AppSpecBean spec = FromJSONLogic.fromJSONAppSpec(jspec);
        selectMRU(runtime, spec);//from  w w w.j  a  va2  s.  c  o  m
    }
}

From source file:de.cebitec.readXplorer.util.GeneralUtils.java

/**
 * @param parent the parent component//from  w w w.  j  ava 2 s  .c  o  m
 * @return Any text found in the clipboard. If none is found, an empty
 * String is returned.
 */
public static String getClipboardContents(Component parent) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    String result = "";
    Transferable contents = clipboard.getContents(null);
    final boolean hasTransferableText = (contents != null)
            && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if (hasTransferableText) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException ex) {
            JOptionPane.showMessageDialog(parent, "Unsupported DataFlavor for clipboard copying.",
                    "Paste Error", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(parent,
                    "IOException occured during recovering of text from clipboard.", "Paste Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
    return result;
}

From source file:pkgrenamer.Main.java

static void onDropFiles(DropTargetDropEvent dtde, FileDropListener onDrop) {
    try {/*from   w w w. j  ava2  s  .com*/
        Transferable transferable = dtde.getTransferable();

        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
            java.util.List<?> files = (java.util.List<?>) transferable
                    .getTransferData(DataFlavor.javaFileListFlavor);
            File[] fa = new File[files.size()];
            for (int i = 0; i < fa.length; i++) {
                fa[i] = (File) files.get(i);
            }
            onDrop.onDropFile(fa);
            dtde.getDropTargetContext().dropComplete(true);

        } else {
            if (sNixFileDataFlavor == null) {
                sNixFileDataFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
            }
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            String data = (String) transferable.getTransferData(sNixFileDataFlavor);
            if (data != null) {
                ArrayList<File> fs = new ArrayList<>();
                for (StringTokenizer st = new StringTokenizer(data, "\r\n"); st.hasMoreTokens();) {
                    String token = st.nextToken().trim();
                    if (token.startsWith("#") || token.isEmpty()) {
                        continue;
                    }
                    try {
                        fs.add(new File(new URI(token)));
                    } catch (Exception e) {
                    }
                }
                onDrop.onDropFile(fs.toArray(new File[0]));
                dtde.getDropTargetContext().dropComplete(true);
            } else {
                dtde.rejectDrop();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.limegroup.gnutella.gui.search.MagnetClipboardListener.java

public static String extractStringContentFromClipboard(Object requestor) {
    Transferable data = null;
    try {/*  w  w w. ja  va2 s. c  o  m*/
        //check if there's anything in the clipboard
        data = CLIPBOARD.getContents(requestor);
    } catch (IllegalStateException isx) {
        //we can't use the clipboard, give up.
        return null;
    }

    //is there anything in the clipboard?
    if (data == null)
        return null;

    //then, check if the data in the clipboard is text
    if (!data.isDataFlavorSupported(DataFlavor.stringFlavor))
        return null;

    //next, extract the content into a string
    String contents = null;

    try {
        contents = (String) data.getTransferData(DataFlavor.stringFlavor);
    } catch (IOException iox) {
        LOG.info("problem occured while trying to parse clipboard, do nothing", iox);
    } catch (UnsupportedFlavorException ufx) {
        LOG.error("UnsupportedFlavor??", ufx);
    }

    return contents;
}

From source file:Clipboard.java

/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an
* empty String./*from   w  w  w .j  av  a 2s. c  om*/
*/
public String getClipboardContents() {
    String result = "";
    java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    //odd: the Object param of getContents is not currently used
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if (hasTransferableText) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException ex) {
            //highly unlikely since we are using a standard DataFlavor
            System.out.println(ex);
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println(ex);
            ex.printStackTrace();
        }
    }
    return result;
}

From source file:MyTree.java

public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof MyTree)) {
        return false;
    }//from w  w w.j  a  va2s  . co 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:MainClass.java

public boolean importData(JComponent comp, Transferable t) {
    if (comp instanceof JLabel) {
        JLabel label = (JLabel) comp;
        if (t.isDataFlavorSupported(flavors[0])) {
            try {
                image = (Image) t.getTransferData(flavors[0]);
                ImageIcon icon = new ImageIcon(image);
                label.setIcon(icon);/*  w  w  w  .j a  v  a2 s. co  m*/
                return true;
            } catch (UnsupportedFlavorException ignored) {
            } catch (IOException ignored) {
            }
        }
    } else if (comp instanceof AbstractButton) {
        AbstractButton button = (AbstractButton) comp;
        if (t.isDataFlavorSupported(flavors[0])) {
            try {
                image = (Image) t.getTransferData(flavors[0]);
                ImageIcon icon = new ImageIcon(image);
                button.setIcon(icon);
                return true;
            } catch (UnsupportedFlavorException ignored) {
            } catch (IOException ignored) {
            }
        }
    }
    return false;
}