File: ImageTransferTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (190 lines) | stat: -rw-r--r-- 6,374 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Vector;

/*
 * @test
 * @key headful
 * @summary To check Whether java.awt.Image can be transferred through the
 *          system clipboard
 * @author Jitender(jitender.singh@eng.sun.com) area=AWT
 * @author dmitriy.ermashov@oracle.com
 * @library ../../../../lib/testlibrary
 * @build ExtendedRobot
 * @run main ImageTransferTest
 */

public class ImageTransferTest  {

    TestFrame frame1, frame2;
    Image image1, image2;
    DataFlavor imageFlavor;

    public static void main(String[] args) throws Exception {
        new ImageTransferTest().doTest();
    }

    class TestFrame extends Frame {
        boolean show;
        Image img;
        public TestFrame(boolean show, Image img){
            this.show = show;
            this.img = img;
        }

        public void paint( Graphics g ){
            if (show)
                g.drawImage( this.img, 30, 30, this );
        }
    }

    class TransferableObject implements Transferable, ClipboardOwner {

        Object data;
        DataFlavor[] dfs;

        public TransferableObject(Object data) throws Exception {
            Vector v = new Vector();
            imageFlavor = new DataFlavor("image/x-java-Image;class=java.awt.Image");
            imageFlavor.setHumanPresentableName("Java Image Flavor Object");

            if( data instanceof java.awt.Image )
                v.addElement(imageFlavor);

            dfs = new DataFlavor[v.size()];
            v.copyInto(dfs);

            for (int j = 0; j < dfs.length; j++)
                System.out.println(" in constructor flavor : " + dfs[j]);

            this.data = data;
        }

        public Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException,IOException {

            System.out.println(" **************");
            System.out.println(" The flavor passed to retrive the data :-" + flavor);
            System.out.println(" The flavors supported");
            for (int j = 0; j < dfs.length; j++)
                System.out.println(" jitu flavor : " + dfs[j]);

            System.out.println(" **************");

            if(!isDataFlavorSupported(flavor))
                throw new UnsupportedFlavorException(flavor);

            if(imageFlavor.equals(flavor)){
                if( data instanceof java.awt.Image )
                    return data;
            }
            return null;

        }

        public DataFlavor[] getTransferDataFlavors() { return dfs; }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            for (int i = 0 ; i < dfs.length; i++)
                if (dfs[i].match(flavor)) return true;
            return false;
        }

        public void lostOwnership(Clipboard clip,Transferable contents) {
            System.out.println(" LostOwnership is invoked");
        }
    }

    ImageTransferTest() throws Exception {
        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 100, 100);
        g.setColor(Color.RED);
        g.fillRect(20, 20, 60, 60);
        g.dispose();

        image1 = img;
        frame1 = new TestFrame(true, image1);
        frame2 = new TestFrame(false, null);

        System.out.println("Inside contrucor...");

        imageFlavor = new DataFlavor("image/x-java-Image;class=java.awt.Image");
        imageFlavor.setHumanPresentableName("Java Image Flavor Object");

        frame1.setBounds(10,350,200,200);
        frame2.setBounds(250,350,200,200);
        frame1.setVisible(true);
        frame2.setVisible(true);
        frame1.repaint();
    }

    public void compareImages() {
        if (image2 == image1)
            System.out.println("Pasted Image is same as that one copied !");
        else
            if (image2 != image1)
                throw new RuntimeException("Image retreived from the Clipboard is not the same " +
                        "image that was set to the Clipboard");
    }


    public void doTest() throws Exception {
        ExtendedRobot robot = new ExtendedRobot();
        robot.waitForIdle(2000);
        TransferableObject imagedata = new TransferableObject(image1);

        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        System.out.println("copying image...");
        if (imagedata != null) {
            clip.setContents(imagedata, imagedata);
        } else {
            System.out.println("Transferable object is null");
        }

        Transferable content = clip.getContents(this);
        if (content != null && (content.isDataFlavorSupported(imageFlavor))) {
            System.out.println("jitu after paste" + content);
            image2 = (Image) content.getTransferData(imageFlavor);
        } else {
            System.out.println("Contents of the clipboard is null");
        }

        frame2.img = image2;
        frame2.show = true;
        frame2.repaint();

        robot.waitForIdle(2000);

        compareImages();
    }
}