/** * Dumps a given text (either String or StringBuffer) into the Clipboard with a specified MIME type */ public static void putText(TextType type, Charset charset, TransferType transferType, CharSequence data) throws Exception { String mimeType = type + "; charset=" + charset + "; class=" + transferType; TextTransferable transferable = new TextTransferable(mimeType, data.toString()); getSystemClipboard().setContents(transferable, transferable); }
/** * evaluates the current textual content of the system clipboard * * @return the textual content or empty string if not possible */ public static String getClipboard() { Transferable content = null; try { content = Clipboard.getSystemClipboard().getContents(null); } catch (Exception ex) { Debug.error("Env.getClipboard: %s", ex.getMessage()); } if (content != null) { try { if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) { return (String) content.getTransferData(DataFlavor.stringFlavor); } } catch (UnsupportedFlavorException ex) { Debug.error("Env.getClipboard: UnsupportedFlavorException: " + content); } catch (IOException ex) { Debug.error("Env.getClipboard: %s", ex.getMessage()); } } return ""; }
/** * sets the current textual content of the system clipboard to the given text * * @param text text */ public static void setClipboard(String text) { try { Clipboard.putText(Clipboard.PLAIN, Clipboard.UTF8, Clipboard.CHAR_BUFFER, text); } catch (Exception ex) { Debug.error("Env.setClipboard: %s", ex.getMessage()); } }
/** * Dumps a given text (either String or StringBuffer) into the Clipboard, with a default MIME type */ public static void putText(CharSequence data) throws Exception { StringSelection copy = new StringSelection(data.toString()); getSystemClipboard().setContents(copy, copy); }