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
27  @summary Tests basic DnD functionality
28  @author Your Name: Alexey Utkin area=dnd
29  @run applet ImageDecoratedDnDInOut.html
30*/
31
32import java.applet.Applet;
33import java.awt.*;
34import java.awt.Robot;
35import java.awt.event.InputEvent;
36import java.awt.dnd.DragSource;
37
38
39public class ImageDecoratedDnDInOut extends Applet {
40    //Declare things used in the test, like buttons and labels here
41
42    public void init() {
43        //Create instructions for the user here, as well as set up
44        // the environment -- set the layout manager, add buttons,
45        // etc.
46        this.setLayout(new BorderLayout());
47
48        String[] instructions =
49                {
50                        "Automatic test.",
51                        "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
52                        "a red panel, will appear below. ",
53                        "1. The button would be clicked and dragged to the red panel. ",
54                        "2. When the mouse enters the red panel during the drag, the panel ",
55                        "should turn yellow. On the systems that supports pictured drag, ",
56                        "the image under the drag-cursor should appear (ancor is shifted ",
57                        "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
58                        "In WIN32 systems the image under cursor would be visible ONLY over ",
59                        "the drop targets with activated extended OLE D\'n\'D support (that are ",
60                        "the desktop and IE ).",
61                        "3. The mouse would be released.",
62                        "The panel should turn red again and a yellow button labeled ",
63                        "\"Drag ME!\" should appear inside the panel. "
64                };
65        Sysout.createDialogWithInstructions(instructions);
66
67    }//End  init()
68
69    public void start() {
70        Frame f = new Frame("Use keyboard for DnD change");
71        Panel mainPanel;
72        Component dragSource, dropTarget;
73
74        f.setBounds(0, 400, 200, 200);
75        f.setLayout(new BorderLayout());
76
77        mainPanel = new Panel();
78        mainPanel.setLayout(new BorderLayout());
79
80        mainPanel.setBackground(Color.blue);
81
82        dropTarget = new DnDTarget(Color.red, Color.yellow);
83        dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
84
85        mainPanel.add(dragSource, "North");
86        mainPanel.add(dropTarget, "Center");
87        f.add(mainPanel, BorderLayout.CENTER);
88
89        f.setVisible(true);
90        try {
91            Point sourcePoint = dragSource.getLocationOnScreen();
92            Dimension d = dragSource.getSize();
93            sourcePoint.translate(d.width / 2, d.height / 2);
94
95            Robot robot = new Robot();
96            robot.mouseMove(sourcePoint.x, sourcePoint.y);
97            robot.mousePress(InputEvent.BUTTON1_MASK);
98            Thread.sleep(2000);
99            for(int i = 0; i <100; ++i) {
100                robot.mouseMove(
101                    sourcePoint.x + d.width / 2 + 10,
102                    sourcePoint.y + d.height);
103                Thread.sleep(100);
104
105                robot.mouseMove(sourcePoint.x, sourcePoint.y);
106                Thread.sleep(100);
107
108                robot.mouseMove(
109                    sourcePoint.x,
110                    sourcePoint.y + d.height);
111                Thread.sleep(100);
112            }
113            sourcePoint.y += d.height;
114            robot.mouseMove(sourcePoint.x, sourcePoint.y);
115            Thread.sleep(100);
116
117            robot.mouseRelease(InputEvent.BUTTON1_MASK);
118            Thread.sleep(4000);
119        } catch( Exception e){
120        e.printStackTrace();
121            throw new RuntimeException("test failed: drop was not successful with exception " + e);
122        }
123
124    }// start()
125}// class DnDAcceptanceTest
126
127
128/**
129 * *************************************************
130 * Standard Test Machinery
131 * DO NOT modify anything below -- it's a standard
132 * chunk of code whose purpose is to make user
133 * interaction uniform, and thereby make it simpler
134 * to read and understand someone else's test.
135 * **************************************************
136 */
137class Sysout {
138    private static TestDialog dialog;
139
140    public static void createDialogWithInstructions(String[] instructions) {
141        dialog = new TestDialog(new Frame(), "Instructions");
142        dialog.printInstructions(instructions);
143        dialog.show();
144        println("Any messages for the tester will display here.");
145    }
146
147    public static void createDialog() {
148        dialog = new TestDialog(new Frame(), "Instructions");
149        String[] defInstr = {"Instructions will appear here. ", ""};
150        dialog.printInstructions(defInstr);
151        dialog.show();
152        println("Any messages for the tester will display here.");
153    }
154
155
156    public static void printInstructions(String[] instructions) {
157        dialog.printInstructions(instructions);
158    }
159
160
161    public static void println(String messageIn) {
162        dialog.displayMessage(messageIn);
163    }
164
165}// Sysout  class
166
167
168class TestDialog extends Dialog {
169
170    TextArea instructionsText;
171    TextArea messageText;
172    int maxStringLength = 80;
173
174    //DO NOT call this directly, go through Sysout
175    public TestDialog(Frame frame, String name) {
176        super(frame, name);
177        int scrollBoth = TextArea.SCROLLBARS_BOTH;
178        instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
179        add("North", instructionsText);
180
181        messageText = new TextArea("", 5, maxStringLength, scrollBoth);
182        add("South", messageText);
183
184        pack();
185
186        show();
187    }// TestDialog()
188
189    //DO NOT call this directly, go through Sysout
190    public void printInstructions(String[] instructions) {
191        //Clear out any current instructions
192        instructionsText.setText("");
193
194        //Go down array of instruction strings
195
196        String printStr, remainingStr;
197        for (int i = 0; i < instructions.length; i++) {
198            //chop up each into pieces maxSringLength long
199            remainingStr = instructions[i];
200            while (remainingStr.length() > 0) {
201                //if longer than max then chop off first max chars to print
202                if (remainingStr.length() >= maxStringLength) {
203                    //Try to chop on a word boundary
204                    int posOfSpace = remainingStr.
205                            lastIndexOf(' ', maxStringLength - 1);
206
207                    if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
208
209                    printStr = remainingStr.substring(0, posOfSpace + 1);
210                    remainingStr = remainingStr.substring(posOfSpace + 1);
211                }
212                //else just print
213                else {
214                    printStr = remainingStr;
215                    remainingStr = "";
216                }
217
218                instructionsText.append(printStr + "\n");
219
220            }// while
221
222        }// for
223
224    }//printInstructions()
225
226    //DO NOT call this directly, go through Sysout
227    public void displayMessage(String messageIn) {
228        messageText.append(messageIn + "\n");
229    }
230
231}// TestDialog  class
232
233