private void copyOverviewToClipboard() throws InsufficientDataException { String overview = generateOverviewText(); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents( new StringSelection(overview), new ClipboardOwner() { @Override public void lostOwnership(Clipboard c, Transferable t) {} }); }
/** * Returns the clipboard text. * * @return text */ private static String clip() { // copy selection to clipboard final Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); final Transferable tr = clip.getContents(null); if (tr != null) { final ArrayList<Object> contents = BaseXLayout.contents(tr); if (!contents.isEmpty()) return contents.get(0).toString(); } else { Util.debug("Clipboard has no contents."); } return null; }
/** * @return the clipboard content as a String (DataFlavor.stringFlavor) Code snippet adapted from * jEdit (Registers.java), http://www.jedit.org. Returns null if clipboard is empty. */ public static String getClipboardStringContent(Clipboard clipboard) { // Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); try { String selection = (String) (clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor)); if (selection == null) return null; boolean trailingEOL = (selection.endsWith("\n") || selection.endsWith(System.getProperty("line.separator"))); // Some Java versions return the clipboard contents using the native line separator, // so have to convert it here , see jEdit's "registers.java" BufferedReader in = new BufferedReader(new StringReader(selection)); StringBuffer buf = new StringBuffer(); String line; while ((line = in.readLine()) != null) { buf.append(line); buf.append('\n'); } // remove trailing \n if (!trailingEOL) buf.setLength(buf.length() - 1); return buf.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Get the String residing on the clipboard. * * @return any text found on the Clipboard; if none found, return an empty String. */ public void openClipboardContents() { 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) { TextDocument entry_document = new TextDocument(); final InputStreamProgressListener progress_listener = getInputStreamProgressListener(); entry_document.addInputStreamProgressListener(progress_listener); final EntryInformation artemis_entry_information = Options.getArtemisEntryInformation(); final uk.ac.sanger.artemis.io.Entry new_embl_entry = EntryFileDialog.getEntryFromFile(this, entry_document, artemis_entry_information, false); if (new_embl_entry == null) // the read failed return; try { final Entry entry = new Entry(new_embl_entry); EntryEdit last_entry_edit = makeEntryEdit(entry); addEntryEdit(last_entry_edit); getStatusLabel().setText(""); last_entry_edit.setVisible(true); } catch (OutOfRangeException e) { new MessageDialog( this, "read failed: one of the features in " + " cut and paste has an out of range " + "location: " + e.getMessage()); } catch (NoSequenceException e) { new MessageDialog(this, "read failed: " + " cut and paste contains no sequence"); } } }
/** * Paste the contents of the clipboard into the console buffer * * @return true if clipboard contents pasted */ public boolean paste() throws IOException { Clipboard clipboard; try { // May throw ugly exception on system without X clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); } catch (Exception e) { return false; } if (clipboard == null) { return false; } Transferable transferable = clipboard.getContents(null); if (transferable == null) { return false; } try { Object content = transferable.getTransferData(DataFlavor.plainTextFlavor); /* * This fix was suggested in bug #1060649 at * http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056 * to get around the deprecated DataFlavor.plainTextFlavor, but it * raises a UnsupportedFlavorException on Mac OS X */ if (content == null) { try { content = new DataFlavor().getReaderForText(transferable); } catch (Exception e) { } } if (content == null) { return false; } String value; if (content instanceof Reader) { // TODO: we might want instead connect to the input stream // so we can interpret individual lines value = ""; String line = null; for (BufferedReader read = new BufferedReader((Reader) content); (line = read.readLine()) != null; ) { if (value.length() > 0) { value += "\n"; } value += line; } } else { value = content.toString(); } if (value == null) { return true; } putString(value); return true; } catch (UnsupportedFlavorException ufe) { if (debugger != null) debug(ufe + ""); return false; } }