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 February 23, 2025 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 * @param image The image to be transferred. 045 */ 046 public ImageSelection(Image image) { 047 this.image = image; 048 } 049 050 /** 051 * Returns an array of flavors in which this <code>Transferable</code> 052 * can provide the data. <code>DataFlavor.imageFlavor</code> is properly supported. 053 * 054 * @return an array of length one, whose only element is <code>DataFlavor.imageFlavor</code> 055 */ 056 public synchronized DataFlavor[] getTransferDataFlavors() { 057 return new DataFlavor[]{DataFlavor.imageFlavor}; 058 } 059 060 /** 061 * Returns whether the requested flavor is supported by this <code>Transferable</code>. 062 * 063 * @param flavor the requested flavor for the data 064 * @return true if <code>flavor</code> is equal to <code>DataFlavor.imageFlavor</code>; 065 * false if <code>flavor</code> is any other flavor. 066 */ 067 public boolean isDataFlavorSupported(DataFlavor flavor) { 068 return DataFlavor.imageFlavor.equals(flavor); 069 } 070 071 /** 072 * Returns the <code>Transferable</code>'s data in the requested <code>DataFlavor</code> if possible. 073 * If the desired flavor is <code>DataFlavor.imageFlavor</code>, or an equivalent flavor, the 074 * <code>Image</code> representing the selection is returned. 075 * 076 * @param flavor the requested flavor for the data 077 * @return the data in the requested flavor, as outlined above 078 * @throws UnsupportedFlavorException if the requested data flavor is not equivalent to DataFlavor.imageFlavor 079 * @throws IOException if the data is no longer available in the requested flavor. 080 * @throws NullPointerException if flavor is <code>null</code> 081 */ 082 public synchronized Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 083 if (DataFlavor.imageFlavor.equals(flavor)) 084 return image; 085 else 086 throw new UnsupportedFlavorException(flavor); 087 } 088 089 /** 090 * Notifies this object that it is no longer the clipboard owner. 091 * This method will be called when another application or another object within 092 * this application asserts ownership of the clipboard. 093 */ 094 public void lostOwnership(Clipboard clipboard, Transferable contents) { } 095 096}