ImageTransferTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 1999, 2016, 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
26import java.awt.*;
27import java.awt.datatransfer.*;
28import java.awt.image.BufferedImage;
29import java.io.IOException;
30import java.util.Vector;
31
32/*
33 * @test
34 * @key headful
35 * @summary To check Whether java.awt.Image can be transferred through the
36 *          system clipboard
37 * @author Jitender(jitender.singh@eng.sun.com) area=AWT
38 * @author dmitriy.ermashov@oracle.com
39 * @library ../../../../lib/testlibrary
40 * @build ExtendedRobot
41 * @run main ImageTransferTest
42 */
43
44public class ImageTransferTest  {
45
46    TestFrame frame1, frame2;
47    Image image1, image2;
48    DataFlavor imageFlavor;
49
50    public static void main(String[] args) throws Exception {
51        new ImageTransferTest().doTest();
52    }
53
54    class TestFrame extends Frame {
55        boolean show;
56        Image img;
57        public TestFrame(boolean show, Image img){
58            this.show = show;
59            this.img = img;
60        }
61
62        public void paint( Graphics g ){
63            if (show)
64                g.drawImage( this.img, 30, 30, this );
65        }
66    }
67
68    class TransferableObject implements Transferable, ClipboardOwner {
69
70        Object data;
71        DataFlavor[] dfs;
72
73        public TransferableObject(Object data) throws Exception {
74            Vector v = new Vector();
75            imageFlavor = new DataFlavor("image/x-java-Image;class=java.awt.Image");
76            imageFlavor.setHumanPresentableName("Java Image Flavor Object");
77
78            if( data instanceof java.awt.Image )
79                v.addElement(imageFlavor);
80
81            dfs = new DataFlavor[v.size()];
82            v.copyInto(dfs);
83
84            for (int j = 0; j < dfs.length; j++)
85                System.out.println(" in constructor flavor : " + dfs[j]);
86
87            this.data = data;
88        }
89
90        public Object getTransferData(DataFlavor flavor)
91                throws UnsupportedFlavorException,IOException {
92
93            System.out.println(" **************");
94            System.out.println(" The flavor passed to retrive the data :-" + flavor);
95            System.out.println(" The flavors supported");
96            for (int j = 0; j < dfs.length; j++)
97                System.out.println(" jitu flavor : " + dfs[j]);
98
99            System.out.println(" **************");
100
101            if(!isDataFlavorSupported(flavor))
102                throw new UnsupportedFlavorException(flavor);
103
104            if(imageFlavor.equals(flavor)){
105                if( data instanceof java.awt.Image )
106                    return data;
107            }
108            return null;
109
110        }
111
112        public DataFlavor[] getTransferDataFlavors() { return dfs; }
113
114        public boolean isDataFlavorSupported(DataFlavor flavor) {
115            for (int i = 0 ; i < dfs.length; i++)
116                if (dfs[i].match(flavor)) return true;
117            return false;
118        }
119
120        public void lostOwnership(Clipboard clip,Transferable contents) {
121            System.out.println(" LostOwnership is invoked");
122        }
123    }
124
125    ImageTransferTest() throws Exception {
126        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
127        Graphics g = img.createGraphics();
128        g.setColor(Color.WHITE);
129        g.fillRect(0, 0, 100, 100);
130        g.setColor(Color.RED);
131        g.fillRect(20, 20, 60, 60);
132        g.dispose();
133
134        image1 = img;
135        frame1 = new TestFrame(true, image1);
136        frame2 = new TestFrame(false, null);
137
138        System.out.println("Inside contrucor...");
139
140        imageFlavor = new DataFlavor("image/x-java-Image;class=java.awt.Image");
141        imageFlavor.setHumanPresentableName("Java Image Flavor Object");
142
143        frame1.setBounds(10,350,200,200);
144        frame2.setBounds(250,350,200,200);
145        frame1.setVisible(true);
146        frame2.setVisible(true);
147        frame1.repaint();
148    }
149
150    public void compareImages() {
151        if (image2 == image1)
152            System.out.println("Pasted Image is same as that one copied !");
153        else
154            if (image2 != image1)
155                throw new RuntimeException("Image retreived from the Clipboard is not the same " +
156                        "image that was set to the Clipboard");
157    }
158
159
160    public void doTest() throws Exception {
161        ExtendedRobot robot = new ExtendedRobot();
162        robot.waitForIdle(2000);
163        TransferableObject imagedata = new TransferableObject(image1);
164
165        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
166        System.out.println("copying image...");
167        if (imagedata != null) {
168            clip.setContents(imagedata, imagedata);
169        } else {
170            System.out.println("Transferable object is null");
171        }
172
173        Transferable content = clip.getContents(this);
174        if (content != null && (content.isDataFlavorSupported(imageFlavor))) {
175            System.out.println("jitu after paste" + content);
176            image2 = (Image) content.getTransferData(imageFlavor);
177        } else {
178            System.out.println("Contents of the clipboard is null");
179        }
180
181        frame2.img = image2;
182        frame2.show = true;
183        frame2.repaint();
184
185        robot.waitForIdle(2000);
186
187        compareImages();
188    }
189}
190
191