001/**
002*   Please feel free to use any fragment of the code in this file that you need
003*   in your own work. As far as I am concerned, it's in the public domain. No
004*   permission is necessary or required. Credit is always appreciated if you
005*   use a large chunk or base a significant product on one of my examples,
006*   but that's not required either.
007*
008*   This code is distributed in the hope that it will be useful,
009*   but WITHOUT ANY WARRANTY; without even the implied warranty of
010*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011*
012*      --- Joseph A. Huwaldt
013**/
014package jahuwaldt.swing;
015
016import java.awt.Image;
017import java.awt.datatransfer.Clipboard;
018import java.awt.datatransfer.ClipboardOwner;
019import java.awt.datatransfer.Transferable;
020import java.awt.datatransfer.DataFlavor;
021import java.awt.datatransfer.UnsupportedFlavorException;
022import java.io.IOException;
023
024
025/**
026*  A <code>Transferable</code> which implements the capability required
027*  to transfer an <code>Image</code>. This <code>Transferable</code> properly supports
028*  <code>DataFlavor.imageFlavor</code> and all equivalent flavors.
029*  No other DataFlavors are supported.
030*
031*  <p>  Modified by:  Joseph A. Huwaldt   </p>
032*
033*  @author   Joseph A. Huwaldt   Date:  March 5, 2009
034*  @version  March 5, 2009
035**/
036public class ImageSelection implements Transferable, ClipboardOwner {
037
038        //      The image being transferred.
039    private Image image;
040
041        /**
042        *  Creates a <code>Transferable</code> capable of transferring the specified <code>Image</code>.
043        **/
044    public ImageSelection(Image image) {
045        this.image = image;
046    }
047
048    /**
049        *  Returns an array of flavors in which this <code>Transferable</code>
050        *  can provide the data. <code>DataFlavor.imageFlavor</code> is properly supported.
051        *
052        *  @return an array of length one, whose only element is <code>DataFlavor.imageFlavor</code>
053        **/
054    public synchronized DataFlavor[] getTransferDataFlavors() {
055        return new DataFlavor[]{DataFlavor.imageFlavor};
056    }
057
058    /**
059        *  Returns whether the requested flavor is supported by this <code>Transferable</code>.
060        *
061        *  @param flavor  the requested flavor for the data
062        *  @return true if <code>flavor</code> is equal to <code>DataFlavor.imageFlavor</code>;
063        *          false if <code>flavor</code> is any other flavor.
064        **/
065    public boolean isDataFlavorSupported(DataFlavor flavor) {
066        return DataFlavor.imageFlavor.equals(flavor);
067    }
068
069    /**
070        *  Returns the <code>Transferable</code>'s data in the requested <code>DataFlavor</code> if possible.
071        *  If the desired flavor is <code>DataFlavor.imageFlavor</code>, or an equivalent flavor, the 
072        *  <code>Image</code> representing the selection is returned.
073        *
074        *  @param flavor  the requested flavor for the data
075        *  @return the data in the requested flavor, as outlined above
076        *  @throws UnsupportedFlavorException if the requested data flavor is not equivalent to DataFlavor.imageFlavor
077        *  @throws IOException if the data is no longer available in the requested flavor.
078        *  @throws NullPointerException if flavor is <code>null</code>
079        **/
080    public synchronized Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
081        if (DataFlavor.imageFlavor.equals(flavor))
082                        return image;
083                else
084            throw new UnsupportedFlavorException(flavor);
085    }
086        
087        /**
088        * Notifies this object that it is no longer the clipboard owner.
089        * This method will be called when another application or another object within
090        * this application asserts ownership of the clipboard.
091        **/
092        public void lostOwnership(Clipboard clipboard, Transferable contents) { }
093        
094}