Package util

Source Code of util.ClipboardManager$ImageSelection

package util;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class ClipboardManager
{
    // If an image is on the system clipboard, this method returns it;
    // otherwise it returns null.
    public static Image getClipboard() {
     
      BufferedImage i = null;   
        try {
          i = (BufferedImage)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.imageFlavor);
          return i;
         
        } catch (Exception e) {
          return null;
        }
    }

   
    // This method writes a image to the system clipboard.
    // otherwise it returns null.
    public static void setClipboard(Image image) {
        ImageSelection imgSel = new ImageSelection(image);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
    }
   
   
    // This class is used to hold an image while on the clipboard.
    public static class ImageSelection implements Transferable {
        private Image image;
   
        public ImageSelection(Image image) {
            this.image = image;
        }
   
        // Returns supported flavors
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.imageFlavor};
        }
   
        // Returns true if flavor is supported
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.imageFlavor.equals(flavor);
        }
   
        // Returns image
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            if (!DataFlavor.imageFlavor.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return image;
        }
    }

   
}
TOP

Related Classes of util.ClipboardManager$ImageSelection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.