Package DisplayProject

Source Code of DisplayProject.ClipboardUtils$ImageSelection

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import Framework.ImageData;
import Framework.TextData;

/**
* This class provides standard system-clipboard functionality to mirror the functionality available
* in UDS. Note at the moment only the following types of data are supported (As opposed to the types
* supported by UDS):
* <table>
*   <tr><th>Value</th><th>Definition</th></tr>
*   <tr><td>SD_TEXT</td><td>Text data returnable as a TextData object.</td></tr>
*   <tr><td>SD_IMAGE</td><td>Image data returnable as an ImageData object.</td></tr>
*   <tr><td>SD_NONE</td><td>No data in the clipboard usable.</td></tr>
* </table>
* @author tfaulkes
*
*/
public class ClipboardUtils {
    /**
     * ImageSelection class is used to hold image data that has been copied to the system clipboard
     * @author tfaulkes
     *
     */
    private static class ImageSelection implements Transferable {
        /**
         * The Image object which will be housed by this ImageSelection
         */
        private Image image;

        public ImageSelection(Image image) {
            this.image = image;
        }

        /**
         * Returns the supported data flavours of our implementation
         */
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[] {DataFlavor.imageFlavor};
        }

        /**
         * Returns true if the flavour is supported
         */
        public boolean isDataFlavorSupported(DataFlavor flavour) {
            return DataFlavor.imageFlavor.equals(flavour);
        }

        /**
         * Returns Image object housed by this Transferable object
         */
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            if (!DataFlavor.imageFlavor.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return image;
        }
    }

    /**
     * Place the passed string into the clipboard and set this application
     * as the clipboard owner.
     * @param aString
     */
    public static void copyToClipboard(Object pObject) {
        if (pObject instanceof TextData || pObject instanceof String) {
            StringSelection stringSelection = new StringSelection(pObject.toString());
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
        }
        else if (pObject instanceof Image) {
            ImageSelection imageSelection = new ImageSelection((Image)pObject);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null);
        }
        else if (pObject instanceof ImageData) {
            ImageSelection imageSelection = new ImageSelection(((ImageData)pObject).getValue());
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null);
        }
    }

    /**
     * The getClipboardDataType method gets the data type of the information on the window system clipboard.<p>
     *
     * GetClipboardDataType queries the window system to determine the type of data in the clipboard,
     * classifying the data into categories represented as integers by the following values:<p>
     * <table>
     *   <tr><th>Value</th><th>Definition</th></tr>
     *   <tr><td>SD_TEXT</td><td>Text data returnable as a TextData object.</td></tr>
     *   <tr><td>SD_IMAGE</td><td>Image data returnable as an ImageData object.</td></tr>
     *   <tr><td>SD_NONE</td><td>No data in the clipboard usable.</td></tr>
     * </table>
     */
    public static int getClipboardDataType() {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        if (contents != null) {
            if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return Constants.SD_TEXT;
            }
            else if (contents.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                return Constants.SD_IMAGE;
            }
        }
        return Constants.SD_NONE;
    }

    /**
     * The copyFromClipboard method gets the information on the window system clipboard, providing that
     * the data on the clipboard is of the appropriate type passed as a parameter.<p>
     *
     * copyFromClipboard queries the window system to determine the type of data in the clipboard,
     * returning the information only if it matches the passed type:<p>
     * <table>
     *   <tr><th>Value</th><th>Definition</th></tr>
     *   <tr><td>SD_TEXT</td><td>Text data returnable as a TextData object.</td></tr>
     *   <tr><td>SD_IMAGE</td><td>Image data returnable as an ImageData object.</td></tr>
     * </table>
     */
    public static Object copyFromClipboard(int pObjectDataType) {
        Object result = null;
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        if (contents != null) {
            try {
                if (pObjectDataType == Constants.SD_IMAGE && contents.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    result = new ImageData((Image)contents.getTransferData(DataFlavor.imageFlavor));
                }
                else if (pObjectDataType == Constants.SD_TEXT && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    result = new TextData((String)contents.getTransferData(DataFlavor.stringFlavor));
                }
            }
            catch (IOException ex) {}
            catch (UnsupportedFlavorException ex) {}
        }
        return result;
    }

    /**
     * The copyFromClipboard method gets the information on the window system clipboard,
     * returning that information as the appropriate object<p>
     */
    public static Object copyFromClipboard() {
        return copyFromClipboard(getClipboardDataType());
    }
}
TOP

Related Classes of DisplayProject.ClipboardUtils$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.