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: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.jav  a2 s  .  c o m
    }
}

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.
 *//*  ww w. j  av  a2s. c om*/
@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:com.hp.alm.ali.idea.ui.editor.field.HTMLAreaField.java

public static void enableCapability(final JTextPane desc, Project project, String value, boolean editable,
        boolean navigation) {
    value = removeSmallFont(value);// w  ww . j a v  a  2 s . c  o  m
    HTMLEditorKit kit = new HTMLLetterWrappingEditorKit();
    desc.setEditorKit(kit);
    desc.setDocument(kit.createDefaultDocument());
    if (!editable && navigation) {
        value = NavigationDecorator.explodeHtml(project, value);
    }
    desc.setText(value);
    if (!editable) {
        desc.setCaret(new NonAdjustingCaret());
    }
    desc.addCaretListener(new BodyLimitCaretListener(desc));
    if (editable) {
        String element = checkElements(desc.getDocument().getDefaultRootElement());
        if (element != null) {
            desc.setToolTipText("Found unsupported element '" + element + "', editing is disabled.");
            editable = false;
        }
    }
    desc.setEditable(editable);

    if (editable && SpellCheckerManager.isAvailable()
            && ApplicationManager.getApplication().getComponent(AliConfiguration.class).spellChecker) {
        desc.getDocument().addDocumentListener(new SpellCheckDocumentListener(project, desc));
    }

    Font font = UIManager.getFont("Label.font");
    String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize()
            + "pt; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(bodyRule);

    // AGM uses plain "p" to create lines, we need to avoid excessive spacing this by default creates
    String paragraphRule = "p { margin-top: 0px; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(paragraphRule);

    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // following is needed to make copy work in the IDE
            try {
                StringSelection selection = new StringSelection(desc.getText(desc.getSelectionStart(),
                        desc.getSelectionEnd() - desc.getSelectionStart()));
                CopyPasteManager.getInstance().setContents(selection);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_COPY)), desc);
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // avoid pasting non-supported HTML markup by always converting to plain text
            Transferable contents = CopyPasteManager.getInstance().getContents();
            try {
                desc.getActionMap().get(DefaultEditorKit.cutAction).actionPerformed(null);
                desc.getDocument().insertString(desc.getSelectionStart(),
                        (String) contents.getTransferData(DataFlavor.stringFlavor), null);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_PASTE)), desc);
    installNavigationShortCuts(desc);

}

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

/**
 * @param parent the parent component/*from w  ww . j  a v  a 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  av  a2 s .  c  o m
        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  . java 2 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:Main.java

public Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);
    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;//from  w  w w .ja  v a2s.c  o m
    try {
        dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
        System.out.println(dstData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);

    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;//from  w  ww  .j  av a2  s  .c  o m
    try {
        dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
        System.out.println(clipboard.getData(DataFlavor.stringFlavor));
        System.out.println(dstData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);
    System.out.println(clipboard.getName());

    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;//from   ww  w .j  av  a 2s .  co  m
    try {
        dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
        System.out.println(dstData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);
    System.out.println(clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor));

    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;//w ww.  j  a v  a  2 s  . c  o  m
    try {
        dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
        System.out.println(dstData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}