Example usage for java.awt.datatransfer Clipboard getContents

List of usage examples for java.awt.datatransfer Clipboard getContents

Introduction

In this page you can find the example usage for java.awt.datatransfer Clipboard getContents.

Prototype

public synchronized Transferable getContents(Object requestor) 

Source Link

Document

Returns a transferable object representing the current contents of the clipboard.

Usage

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

/**
 * @param parent the parent component/*from   ww  w.ja  va 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: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  . java  2s  .  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:org.apache.openmeetings.screenshare.job.RemoteJob.java

public String getClipboardText() {
    try {/*from   w ww  . j  a va 2s.c  o  m*/
        // get the system clipboard
        Clipboard systemClipboard = getDefaultToolkit().getSystemClipboard();
        // get the contents on the clipboard in a transferable object
        Transferable clipboardContents = systemClipboard.getContents(null);
        // check if clipboard is empty
        if (clipboardContents == null) {
            // Clipboard is empty!!!
        } else if (clipboardContents.isDataFlavorSupported(stringFlavor)) {
            // see if DataFlavor of DataFlavor.stringFlavor is supported
            // return text content
            String returnText = (String) clipboardContents.getTransferData(stringFlavor);
            return returnText;
        }
    } catch (Exception e) {
        log.error("Unexpected exception while getting clipboard text", e);
    }
    return "";
}

From source file:com.egangotri.transliteratorAsSwing.TransliteratorJFrame.java

public void copyToClipBoard(String source) {
    Clipboard clipboard;
    clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable content = clipboard.getContents(this);
    try {//from  ww w .  j a va  2s  .c o m
        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:simplesqlformatter.formatter.SQLFormatterEditor.java

private void paste() {
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    final Transferable transferable = clipboard.getContents(null);
    try {//from  w ww .  j  av  a 2  s . co 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:Samples.Advanced.GraphEditorDemo.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.  ja  v a  2 s .co  m*/
*/
public String getClipboardContents() {
    String result = "";
    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:de.mycrobase.jcloudapp.Main.java

@SuppressWarnings("unchecked")
public boolean doUploadClipboard() {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable t = cb.getContents(null);

    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        try {/*from   www  .ja va  2 s  .c  om*/
            List<File> data = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            uploadFilesFromClipboard(data);
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        return true;
    } else if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        try {
            Image data = (Image) t.getTransferData(DataFlavor.imageFlavor);
            BufferedImage bi = (BufferedImage) data;
            uploadImageFromClipboard(bi);
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        return true;
    } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String data = (String) t.getTransferData(DataFlavor.stringFlavor);
            if (isValidURL(data)) {
                createBookmarkFromClipboard(data);
            } else {
                uploadStringFromClipboard(data);
            }
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        return true;
    }
    return false;
}

From source file:com.jk.framework.util.FakeRunnable.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 . ja  v a 2 s .c  o  m
 */
public static String pasteFromClipBoard() {
    String result = "";
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    // odd: the Object param of getContents is not currently used
    final Transferable contents = clipboard.getContents(null);
    final boolean hasTransferableText = contents != null
            && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if (hasTransferableText) {
        try {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        } catch (final UnsupportedFlavorException ex) {
            // highly unlikely since we are using a standard DataFlavor
            ex.printStackTrace();
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
    }
    return result;
}

From source file:TransferableScribblePane.java

/** Get a PolyLine from the clipboard, if one exists, and display it */
public void paste() {
    // Get the system Clipboard and ask for its Transferable contents
    Clipboard c = this.getToolkit().getSystemClipboard();
    Transferable t = c.getContents(this);

    // See if we can extract a PolyLine from the Transferable object
    PolyLine line;/*from w  w  w .  j a va 2  s  . c o  m*/
    try {
        line = (PolyLine) t.getTransferData(TransferablePolyLine.FLAVOR);
    } catch (Exception e) { // UnsupportedFlavorException or IOException
        // If we get here, the clipboard doesn't hold a PolyLine we can use
        getToolkit().beep(); // So beep to indicate the error
        return;
    }

    lines.add(line); // We got a line from the clipboard, so add it to list
    repaint(); // And repaint to make the line appear
}

From source file:com.raddle.tools.ClipboardTransferMain.java

private void setRemoteClipboard(boolean alert) {
    if (!isProcessing) {
        isProcessing = true;/*from  w  ww .  j  a v a 2 s.com*/
        try {
            Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
            Transferable clipT = sysc.getContents(null);
            if (alert && !ClipboardUtils.isClipboardNotEmpty(clipT)) {
                updateMessage("?");
                return;
            }
            updateMessage("???");
            trayIcon.setImage(sendImage);
            final BooleanHolder success = new BooleanHolder();
            doInSocket(new SocketCallback() {

                @Override
                public Object connected(Socket socket) throws Exception {
                    ClipCommand cmd = new ClipCommand();
                    cmd.setCmdCode(ClipCommand.CMD_SET_CLIP);
                    cmd.setResult(ClipboardUtils.getClipResult());
                    // ??
                    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                    out.writeObject(cmd);
                    // 
                    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                    ClipResult result = (ClipResult) in.readObject();
                    if (result.isSuccess()) {
                        StringBuilder sb = new StringBuilder();
                        for (DataFlavor dataFlavor : cmd.getResult().getClipdata().keySet()) {
                            sb.append("\n");
                            sb.append(dataFlavor.getPrimaryType()).append("/").append(dataFlavor.getSubType());
                        }
                        iconQueue.add("send");
                        success.value = true;
                        updateMessage("????? " + sb);
                    } else {
                        updateMessage("???:" + result.getMessage());
                    }
                    in.close();
                    out.close();
                    return null;
                }
            });
            if (!success.value) {
                trayIcon.setImage(grayImage);
            }
        } catch (Exception e) {
            trayIcon.setImage(grayImage);
            updateMessage("???" + e.getMessage());
        } finally {
            isProcessing = false;
        }
    }
}