1/*
2 * Copyright (c) 2009, 2013, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25  test %W% %E%
26  @bug 4874070 7146550
27  @summary Tests basic DnD functionality
28  @author Your Name: Alexey Utkin area=dnd
29  @run applet ImageDecoratedDnDNegative.html
30*/
31
32import java.applet.Applet;
33import java.awt.*;
34import java.awt.Robot;
35import java.awt.event.InputEvent;
36import java.awt.event.KeyEvent;
37import java.awt.geom.Point2D;
38
39
40import java.awt.dnd.DragSource;
41
42
43public class ImageDecoratedDnDNegative extends Applet {
44    //Declare things used in the test, like buttons and labels here
45
46    public void init() {
47        //Create instructions for the user here, as well as set up
48        // the environment -- set the layout manager, add buttons,
49        // etc.
50        this.setLayout(new BorderLayout());
51
52        String[] instructions =
53                {
54                        "Automatic test.",
55                        "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
56                        "a red panel, will appear below. ",
57                        "1. The button would be clicked and dragged to the red panel. ",
58                        "2. When the mouse enters the red panel during the drag, the panel ",
59                        "should turn yellow. On the systems that supports pictured drag, ",
60                        "the image under the drag-cursor should appear (ancor is shifted ",
61                        "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
62                        "In WIN32 systems the image under cursor would be visible ONLY over ",
63                        "the drop targets with activated extended OLE D\'n\'D support (that are ",
64                        "the desktop and IE ).",
65                        "3. The mouse would be released.",
66                        "The panel should turn red again and a yellow button labeled ",
67                        "\"Drag ME!\" should appear inside the panel. You should be able ",
68                        "to repeat this operation multiple times."
69                };
70        Sysout.createDialogWithInstructions(instructions);
71
72    }//End  init()
73
74    public void moveTo(
75        Robot r,
76        Point b,
77        Point e)
78    {
79        Point2D.Double ee = new Point2D.Double(e.getX(), e.getY());
80        Point2D.Double bb = new Point2D.Double(b.getX(), b.getY());
81        final int count = (int)(ee.distance(bb));
82        Point2D.Double c = new Point2D.Double(bb.getX(), bb.getY());
83        for(int i=0; i<count; ++i){
84            c.setLocation(
85                    bb.getX() + (ee.getX()-bb.getX())*i/count,
86                    bb.getY() + (ee.getY()-bb.getY())*i/count);
87            r.mouseMove(
88                    (int)c.getX(),
89                    (int)c.getY());
90            r.delay(5);
91        }
92        r.mouseMove(
93                (int)ee.getX(),
94                (int)ee.getY());
95        r.delay(5);
96    }
97
98    public void start() {
99        Frame f = new Frame("Use keyboard for DnD change");
100        Panel mainPanel;
101        Component dragSource, dropTarget;
102
103        f.setBounds(0, 400, 200, 200);
104        f.setLayout(new BorderLayout());
105
106        mainPanel = new Panel();
107        mainPanel.setLayout(new BorderLayout());
108
109        mainPanel.setBackground(Color.blue);
110
111        dropTarget = new DnDTarget(Color.red, Color.yellow);
112        dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
113
114        mainPanel.add(dragSource, "North");
115        mainPanel.add(dropTarget, "Center");
116        f.add(mainPanel, BorderLayout.CENTER);
117
118        f.setVisible(true);
119
120        Point sourcePoint = dragSource.getLocationOnScreen();
121        Dimension d = dragSource.getSize();
122        sourcePoint.translate(d.width / 2, d.height / 2);
123
124        try {
125            Robot robot = new Robot();
126            robot.mouseMove(sourcePoint.x, sourcePoint.y);
127            Point start = new Point(
128                    sourcePoint.x,
129                    sourcePoint.y);
130            Point out = new Point(
131                    sourcePoint.x + d.width / 2 + 10,
132                    sourcePoint.y + d.height);
133
134            Point cur = start;
135            for(int i = 2; i < 5; ++i){
136                moveTo(robot, cur, start);
137                robot.delay(500);
138                robot.mousePress(InputEvent.BUTTON1_MASK);
139                robot.delay(500);
140                moveTo(robot, start, out);
141                robot.keyPress(KeyEvent.VK_CONTROL);
142                Point drop = new Point(
143                        (int)start.getX(),
144                        (int)start.getY() + (d.height + 5) * i );
145                moveTo(robot, out, drop);
146
147                robot.mouseRelease(InputEvent.BUTTON1_MASK);
148                robot.delay(10);
149                robot.keyRelease(KeyEvent.VK_CONTROL);
150                robot.delay(1000);
151
152                cur = drop;
153            }
154        } catch( Exception e){
155            e.printStackTrace();
156            throw new RuntimeException("test failed: drop was not successful with exception " + e);
157        }
158    }// start()
159}// class DnDAcceptanceTest
160
161
162/**
163 * *************************************************
164 * Standard Test Machinery
165 * DO NOT modify anything below -- it's a standard
166 * chunk of code whose purpose is to make user
167 * interaction uniform, and thereby make it simpler
168 * to read and understand someone else's test.
169 * **************************************************
170 */
171class Sysout {
172    private static TestDialog dialog;
173
174    public static void createDialogWithInstructions(String[] instructions) {
175        dialog = new TestDialog(new Frame(), "Instructions");
176        dialog.printInstructions(instructions);
177        dialog.show();
178        println("Any messages for the tester will display here.");
179    }
180
181    public static void createDialog() {
182        dialog = new TestDialog(new Frame(), "Instructions");
183        String[] defInstr = {"Instructions will appear here. ", ""};
184        dialog.printInstructions(defInstr);
185        dialog.show();
186        println("Any messages for the tester will display here.");
187    }
188
189
190    public static void printInstructions(String[] instructions) {
191        dialog.printInstructions(instructions);
192    }
193
194
195    public static void println(String messageIn) {
196        dialog.displayMessage(messageIn);
197    }
198
199}// Sysout  class
200
201
202class TestDialog extends Dialog {
203
204    TextArea instructionsText;
205    TextArea messageText;
206    int maxStringLength = 80;
207
208    //DO NOT call this directly, go through Sysout
209    public TestDialog(Frame frame, String name) {
210        super(frame, name);
211        int scrollBoth = TextArea.SCROLLBARS_BOTH;
212        instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
213        add("North", instructionsText);
214
215        messageText = new TextArea("", 5, maxStringLength, scrollBoth);
216        add("South", messageText);
217
218        pack();
219
220        show();
221    }// TestDialog()
222
223    //DO NOT call this directly, go through Sysout
224    public void printInstructions(String[] instructions) {
225        //Clear out any current instructions
226        instructionsText.setText("");
227
228        //Go down array of instruction strings
229
230        String printStr, remainingStr;
231        for (int i = 0; i < instructions.length; i++) {
232            //chop up each into pieces maxSringLength long
233            remainingStr = instructions[i];
234            while (remainingStr.length() > 0) {
235                //if longer than max then chop off first max chars to print
236                if (remainingStr.length() >= maxStringLength) {
237                    //Try to chop on a word boundary
238                    int posOfSpace = remainingStr.
239                            lastIndexOf(' ', maxStringLength - 1);
240
241                    if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
242
243                    printStr = remainingStr.substring(0, posOfSpace + 1);
244                    remainingStr = remainingStr.substring(posOfSpace + 1);
245                }
246                //else just print
247                else {
248                    printStr = remainingStr;
249                    remainingStr = "";
250                }
251
252                instructionsText.append(printStr + "\n");
253
254            }// while
255
256        }// for
257
258    }//printInstructions()
259
260    //DO NOT call this directly, go through Sysout
261    public void displayMessage(String messageIn) {
262        messageText.append(messageIn + "\n");
263    }
264
265}// TestDialog  class
266
267