ImageDecoratedDnDInOut.java revision 8729:0242fce0f717
119304Speter/*
219304Speter * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
319304Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
419304Speter *
519304Speter * This code is free software; you can redistribute it and/or modify it
619304Speter * under the terms of the GNU General Public License version 2 only, as
719304Speter * published by the Free Software Foundation.
819304Speter *
919304Speter * This code is distributed in the hope that it will be useful, but WITHOUT
1019304Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1119304Speter * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1219304Speter * version 2 for more details (a copy is included in the LICENSE file that
13254225Speter * accompanied this code).
1419304Speter *
1519304Speter * You should have received a copy of the GNU General Public License version
1619304Speter * 2 along with this work; if not, write to the Free Software Foundation,
1719304Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1819304Speter *
1919304Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2019304Speter * or visit www.oracle.com if you need additional information or have any
2119304Speter * questions.
2219304Speter */
2319304Speter
2419304Speter/*
2519304Speter  test %W% %E%
2619304Speter  @bug 4874070
2719304Speter  @summary Tests basic DnD functionality
2819304Speter  @author Your Name: Alexey Utkin area=dnd
2919304Speter  @run applet ImageDecoratedDnDInOut.html
3019304Speter*/
3119304Speter
3219304Speterimport java.applet.Applet;
3319304Speterimport java.awt.*;
34281373Sbaptimport java.awt.Robot;
3519304Speterimport java.awt.event.InputEvent;
3619304Speterimport java.awt.dnd.DragSource;
37254225Speter
3819304Speter
3919304Speterpublic class ImageDecoratedDnDInOut extends Applet {
4019304Speter    //Declare things used in the test, like buttons and labels here
4119304Speter
4219304Speter    public void init() {
4319304Speter        //Create instructions for the user here, as well as set up
4419304Speter        // the environment -- set the layout manager, add buttons,
4519304Speter        // etc.
4619304Speter        this.setLayout(new BorderLayout());
4719304Speter
4819304Speter        String[] instructions =
4919304Speter                {
5019304Speter                        "Automatic test.",
5119304Speter                        "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
5219304Speter                        "a red panel, will appear below. ",
5319304Speter                        "1. The button would be clicked and dragged to the red panel. ",
5419304Speter                        "2. When the mouse enters the red panel during the drag, the panel ",
5519304Speter                        "should turn yellow. On the systems that supports pictured drag, ",
5619304Speter                        "the image under the drag-cursor should appear (ancor is shifted ",
5719304Speter                        "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
5819304Speter                        "In WIN32 systems the image under cursor would be visible ONLY over ",
5919304Speter                        "the drop targets with activated extended OLE D\'n\'D support (that are ",
60254225Speter                        "the desktop and IE ).",
61254225Speter                        "3. The mouse would be released.",
62254225Speter                        "The panel should turn red again and a yellow button labeled ",
63254225Speter                        "\"Drag ME!\" should appear inside the panel. "
64254225Speter                };
6519304Speter        Sysout.createDialogWithInstructions(instructions);
6619304Speter
6719304Speter    }//End  init()
6819304Speter
6919304Speter    public void start() {
7019304Speter        Frame f = new Frame("Use keyboard for DnD change");
7119304Speter        Panel mainPanel;
7219304Speter        Component dragSource, dropTarget;
7319304Speter
7419304Speter        f.setBounds(0, 400, 200, 200);
7519304Speter        f.setLayout(new BorderLayout());
7619304Speter
7719304Speter        mainPanel = new Panel();
7819304Speter        mainPanel.setLayout(new BorderLayout());
7919304Speter
8019304Speter        mainPanel.setBackground(Color.blue);
8119304Speter
82281373Sbapt        dropTarget = new DnDTarget(Color.red, Color.yellow);
8319304Speter        dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
8419304Speter
85254225Speter        mainPanel.add(dragSource, "North");
8619304Speter        mainPanel.add(dropTarget, "Center");
8719304Speter        f.add(mainPanel, BorderLayout.CENTER);
8819304Speter
8919304Speter        f.setVisible(true);
9019304Speter        try {
9119304Speter            Point sourcePoint = dragSource.getLocationOnScreen();
9219304Speter            Dimension d = dragSource.getSize();
9319304Speter            sourcePoint.translate(d.width / 2, d.height / 2);
9419304Speter
95254225Speter            Robot robot = new Robot();
96254225Speter            robot.mouseMove(sourcePoint.x, sourcePoint.y);
9719304Speter            robot.mousePress(InputEvent.BUTTON1_MASK);
9819304Speter            Thread.sleep(2000);
9919304Speter            for(int i = 0; i <100; ++i) {
10019304Speter                robot.mouseMove(
10119304Speter                    sourcePoint.x + d.width / 2 + 10,
10219304Speter                    sourcePoint.y + d.height);
10319304Speter                Thread.sleep(100);
10419304Speter
10519304Speter                robot.mouseMove(sourcePoint.x, sourcePoint.y);
10619304Speter                Thread.sleep(100);
10719304Speter
10819304Speter                robot.mouseMove(
10919304Speter                    sourcePoint.x,
11019304Speter                    sourcePoint.y + d.height);
11119304Speter                Thread.sleep(100);
11219304Speter            }
113281373Sbapt            sourcePoint.y += d.height;
11419304Speter            robot.mouseMove(sourcePoint.x, sourcePoint.y);
11519304Speter            Thread.sleep(100);
116254225Speter
11719304Speter            robot.mouseRelease(InputEvent.BUTTON1_MASK);
11819304Speter            Thread.sleep(4000);
119254225Speter        } catch( Exception e){
120254225Speter        e.printStackTrace();
12119304Speter            throw new RuntimeException("test failed: drop was not successful with exception " + e);
12219304Speter        }
12319304Speter
12419304Speter    }// start()
12519304Speter}// class DnDAcceptanceTest
12619304Speter
12719304Speter
12819304Speter/**
12919304Speter * *************************************************
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