1 /* 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import java.awt.*; 27 import java.awt.datatransfer.*; 28 import java.awt.image.BufferedImage; 29 import java.io.IOException; 30 import java.util.Vector; 31 32 /* 33 * @test 34 * @summary To check Whether java.awt.Image can be transferred through the 35 * system clipboard 36 * @author Jitender(jitender.singh@eng.sun.com) area=AWT 37 * @author dmitriy.ermashov@oracle.com 38 * @library ../../../../lib/testlibrary 39 * @build ExtendedRobot 40 * @run main ImageTransferTest 41 */ 42 43 public class ImageTransferTest { 44 45 TestFrame frame1, frame2; 46 Image image1, image2; 47 DataFlavor imageFlavor; 48 49 public static void main(String[] args) throws Exception { 50 new ImageTransferTest().doTest(); 51 } 52 53 class TestFrame extends Frame { 54 boolean show; 55 Image img; 56 public TestFrame(boolean show, Image img){ 57 this.show = show; 58 this.img = img; 59 } 60 61 public void paint( Graphics g ){ 62 if (show) 63 g.drawImage( this.img, 30, 30, this ); 64 } 65 } 66 67 class TransferableObject implements Transferable, ClipboardOwner { 68 69 Object data; 70 DataFlavor[] dfs; 71 72 public TransferableObject(Object data) throws Exception { 73 Vector v = new Vector(); 74 imageFlavor = new DataFlavor("image/x-java-Image;class=java.awt.Image"); 75 imageFlavor.setHumanPresentableName("Java Image Flavor Object"); 76 77 if( data instanceof java.awt.Image ) 78 v.addElement(imageFlavor); 79 80 dfs = new DataFlavor[v.size()]; 81 v.copyInto(dfs); 82 83 for (int j = 0; j < dfs.length; j++) 84 System.out.println(" in constructor flavor : " + dfs[j]); 85 86 this.data = data; 87 } 88 89 public Object getTransferData(DataFlavor flavor) 90 throws UnsupportedFlavorException,IOException { 91 92 System.out.println(" **************"); 93 System.out.println(" The flavor passed to retrive the data :-" + flavor); 94 System.out.println(" The flavors supported"); 95 for (int j = 0; j < dfs.length; j++) 96 System.out.println(" jitu flavor : " + dfs[j]); 97 98 System.out.println(" **************"); 99 100 if(!isDataFlavorSupported(flavor)) 101 throw new UnsupportedFlavorException(flavor); 102 103 if(imageFlavor.equals(flavor)){ 104 if( data instanceof java.awt.Image ) 105 return data; 106 } 107 return null; 108 109 } 110 111 public DataFlavor[] getTransferDataFlavors() { return dfs; } 112 113 public boolean isDataFlavorSupported(DataFlavor flavor) { 114 for (int i = 0 ; i < dfs.length; i++) 115 if (dfs[i].match(flavor)) return true; 116 return false; 117 } 118 119 public void lostOwnership(Clipboard clip,Transferable contents) { 120 System.out.println(" LostOwnership is invoked"); 121 } 122 } 123 124 ImageTransferTest() throws Exception { 125 BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); 126 Graphics g = img.createGraphics(); 127 g.setColor(Color.WHITE); 128 g.fillRect(0, 0, 100, 100); 129 g.setColor(Color.RED); 130 g.fillRect(20, 20, 60, 60); 131 g.dispose(); 132 133 image1 = img; 134 frame1 = new TestFrame(true, image1); 135 frame2 = new TestFrame(false, null); 136 137 System.out.println("Inside contrucor..."); 138 139 imageFlavor = new DataFlavor("image/x-java-Image;class=java.awt.Image"); 140 imageFlavor.setHumanPresentableName("Java Image Flavor Object"); 141 142 frame1.setBounds(10,350,200,200); 143 frame2.setBounds(250,350,200,200); 144 frame1.setVisible(true); 145 frame2.setVisible(true); 146 frame1.repaint(); 147 } 148 149 public void compareImages() { 150 if (image2 == image1) 151 System.out.println("Pasted Image is same as that one copied !"); 152 else 153 if (image2 != image1) 154 throw new RuntimeException("Image retreived from the Clipboard is not the same " + 155 "image that was set to the Clipboard"); 156 } 157 158 159 public void doTest() throws Exception { 160 ExtendedRobot robot = new ExtendedRobot(); 161 robot.waitForIdle(2000); 162 TransferableObject imagedata = new TransferableObject(image1); 163 164 Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); 165 System.out.println("copying image..."); 166 if (imagedata != null) { 167 clip.setContents(imagedata, imagedata); 168 } else { 169 System.out.println("Transferable object is null"); 170 } 171 172 Transferable content = clip.getContents(this); 173 if (content != null && (content.isDataFlavorSupported(imageFlavor))) { 174 System.out.println("jitu after paste" + content); 175 image2 = (Image) content.getTransferData(imageFlavor); 176 } else { 177 System.out.println("Contents of the clipboard is null"); 178 } 179 180 frame2.img = image2; 181 frame2.show = true; 182 frame2.repaint(); 183 184 robot.waitForIdle(2000); 185 186 compareImages(); 187 } 188 } 189