001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009package imagingbook.lib.util;
010
011import java.awt.Toolkit;
012import java.awt.datatransfer.DataFlavor;
013import java.awt.datatransfer.StringSelection;
014import java.awt.datatransfer.Transferable;
015
016/** 
017 * Methods from
018 * http://examples.javacodegeeks.com/desktop-java/awt/datatransfer/getting-and-setting-text-on-the-system-clipboard/
019 * @author wilbur
020 */
021
022public abstract class Clipboard {
023        
024        // This method writes a string to the clipboard. 
025        public static void copyStringToClipboard(String text) {
026                StringSelection stringSelection = new StringSelection(text);
027                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);  
028        } 
029
030        // If a string is on the system clipboard, this method returns it; otherwise it returns null.
031        public static String getStringFromClipboard() { 
032                String text = null;
033                Transferable trf = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
034                if (trf != null && trf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
035                        try {
036                                text = (String) trf.getTransferData(DataFlavor.stringFlavor);
037                        }  catch (Exception e) {};
038                }
039                return text; 
040        }
041
042}