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