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