2129N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2129N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2129N/A *
2129N/A * This code is free software; you can redistribute it and/or modify it
2129N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
2129N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
2129N/A *
2129N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2129N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2129N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2129N/A * version 2 for more details (a copy is included in the LICENSE file that
2129N/A * accompanied this code).
2129N/A *
2129N/A * You should have received a copy of the GNU General Public License version
2129N/A * 2 along with this work; if not, write to the Free Software Foundation,
2129N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2129N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2129N/A */
2129N/A
2129N/A/*
2129N/A test %W% %E%
5037N/A @bug 4874070 7146550
2129N/A @summary Tests basic DnD functionality
2129N/A @author Your Name: Alexey Utkin area=dnd
2129N/A @run applet ImageDecoratedDnDNegative.html
2129N/A*/
2129N/A
2129N/Aimport java.applet.Applet;
2129N/Aimport java.awt.*;
2129N/Aimport java.awt.Robot;
2129N/Aimport java.awt.event.InputEvent;
2129N/Aimport java.awt.event.KeyEvent;
2129N/Aimport java.awt.geom.Point2D;
2129N/A
2129N/A
2129N/Aimport java.awt.dnd.DragSource;
2129N/A
2129N/A
2129N/Apublic class ImageDecoratedDnDNegative extends Applet {
2129N/A //Declare things used in the test, like buttons and labels here
2129N/A
2129N/A public void init() {
2129N/A //Create instructions for the user here, as well as set up
2129N/A // the environment -- set the layout manager, add buttons,
2129N/A // etc.
2129N/A this.setLayout(new BorderLayout());
2129N/A
2129N/A String[] instructions =
2129N/A {
2129N/A "Automatic test.",
2129N/A "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
2129N/A "a red panel, will appear below. ",
2129N/A "1. The button would be clicked and dragged to the red panel. ",
2129N/A "2. When the mouse enters the red panel during the drag, the panel ",
2129N/A "should turn yellow. On the systems that supports pictured drag, ",
2129N/A "the image under the drag-cursor should appear (ancor is shifted ",
2129N/A "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
2129N/A "In WIN32 systems the image under cursor would be visible ONLY over ",
2129N/A "the drop targets with activated extended OLE D\'n\'D support (that are ",
2129N/A "the desktop and IE ).",
2129N/A "3. The mouse would be released.",
2129N/A "The panel should turn red again and a yellow button labeled ",
2129N/A "\"Drag ME!\" should appear inside the panel. You should be able ",
2129N/A "to repeat this operation multiple times."
2129N/A };
2129N/A Sysout.createDialogWithInstructions(instructions);
2129N/A
2129N/A }//End init()
2129N/A
2129N/A public void moveTo(
2129N/A Robot r,
2129N/A Point b,
2129N/A Point e)
2129N/A {
2129N/A Point2D.Double ee = new Point2D.Double(e.getX(), e.getY());
2129N/A Point2D.Double bb = new Point2D.Double(b.getX(), b.getY());
2129N/A final int count = (int)(ee.distance(bb));
2129N/A Point2D.Double c = new Point2D.Double(bb.getX(), bb.getY());
2129N/A for(int i=0; i<count; ++i){
2129N/A c.setLocation(
2129N/A bb.getX() + (ee.getX()-bb.getX())*i/count,
2129N/A bb.getY() + (ee.getY()-bb.getY())*i/count);
2129N/A r.mouseMove(
2129N/A (int)c.getX(),
2129N/A (int)c.getY());
2129N/A r.delay(5);
2129N/A }
2129N/A r.mouseMove(
2129N/A (int)ee.getX(),
2129N/A (int)ee.getY());
2129N/A r.delay(5);
2129N/A }
2129N/A
2129N/A public void start() {
2129N/A Frame f = new Frame("Use keyboard for DnD change");
2129N/A Panel mainPanel;
2129N/A Component dragSource, dropTarget;
2129N/A
2129N/A f.setBounds(0, 400, 200, 200);
2129N/A f.setLayout(new BorderLayout());
2129N/A
2129N/A mainPanel = new Panel();
2129N/A mainPanel.setLayout(new BorderLayout());
2129N/A
2129N/A mainPanel.setBackground(Color.blue);
2129N/A
2129N/A dropTarget = new DnDTarget(Color.red, Color.yellow);
2129N/A dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
2129N/A
2129N/A mainPanel.add(dragSource, "North");
2129N/A mainPanel.add(dropTarget, "Center");
2129N/A f.add(mainPanel, BorderLayout.CENTER);
2129N/A
2129N/A f.setVisible(true);
2129N/A
2129N/A Point sourcePoint = dragSource.getLocationOnScreen();
2129N/A Dimension d = dragSource.getSize();
2129N/A sourcePoint.translate(d.width / 2, d.height / 2);
2129N/A
2129N/A try {
2129N/A Robot robot = new Robot();
2129N/A robot.mouseMove(sourcePoint.x, sourcePoint.y);
2129N/A Point start = new Point(
2129N/A sourcePoint.x,
2129N/A sourcePoint.y);
2129N/A Point out = new Point(
2129N/A sourcePoint.x + d.width / 2 + 10,
2129N/A sourcePoint.y + d.height);
2129N/A
2129N/A Point cur = start;
2129N/A for(int i = 2; i < 5; ++i){
2129N/A moveTo(robot, cur, start);
2129N/A robot.delay(500);
2129N/A robot.mousePress(InputEvent.BUTTON1_MASK);
2129N/A robot.delay(500);
2129N/A moveTo(robot, start, out);
2129N/A robot.keyPress(KeyEvent.VK_CONTROL);
2129N/A Point drop = new Point(
2129N/A (int)start.getX(),
2129N/A (int)start.getY() + (d.height + 5) * i );
2129N/A moveTo(robot, out, drop);
2129N/A
2129N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
2129N/A robot.delay(10);
2129N/A robot.keyRelease(KeyEvent.VK_CONTROL);
2129N/A robot.delay(1000);
2129N/A
2129N/A cur = drop;
2129N/A }
2129N/A } catch( Exception e){
2129N/A e.printStackTrace();
2129N/A throw new RuntimeException("test failed: drop was not successful with exception " + e);
2129N/A }
2129N/A }// start()
2129N/A}// class DnDAcceptanceTest
2129N/A
2129N/A
2129N/A/**
2129N/A * *************************************************
2129N/A * Standard Test Machinery
2129N/A * DO NOT modify anything below -- it's a standard
2129N/A * chunk of code whose purpose is to make user
2129N/A * interaction uniform, and thereby make it simpler
2129N/A * to read and understand someone else's test.
2129N/A * **************************************************
2129N/A */
2129N/Aclass Sysout {
2129N/A private static TestDialog dialog;
2129N/A
2129N/A public static void createDialogWithInstructions(String[] instructions) {
2129N/A dialog = new TestDialog(new Frame(), "Instructions");
2129N/A dialog.printInstructions(instructions);
2129N/A dialog.show();
2129N/A println("Any messages for the tester will display here.");
2129N/A }
2129N/A
2129N/A public static void createDialog() {
2129N/A dialog = new TestDialog(new Frame(), "Instructions");
2129N/A String[] defInstr = {"Instructions will appear here. ", ""};
2129N/A dialog.printInstructions(defInstr);
2129N/A dialog.show();
2129N/A println("Any messages for the tester will display here.");
2129N/A }
2129N/A
2129N/A
2129N/A public static void printInstructions(String[] instructions) {
2129N/A dialog.printInstructions(instructions);
2129N/A }
2129N/A
2129N/A
2129N/A public static void println(String messageIn) {
2129N/A dialog.displayMessage(messageIn);
2129N/A }
2129N/A
2129N/A}// Sysout class
2129N/A
2129N/A
2129N/Aclass TestDialog extends Dialog {
2129N/A
2129N/A TextArea instructionsText;
2129N/A TextArea messageText;
2129N/A int maxStringLength = 80;
2129N/A
2129N/A //DO NOT call this directly, go through Sysout
2129N/A public TestDialog(Frame frame, String name) {
2129N/A super(frame, name);
2129N/A int scrollBoth = TextArea.SCROLLBARS_BOTH;
2129N/A instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
2129N/A add("North", instructionsText);
2129N/A
2129N/A messageText = new TextArea("", 5, maxStringLength, scrollBoth);
2129N/A add("South", messageText);
2129N/A
2129N/A pack();
2129N/A
2129N/A show();
2129N/A }// TestDialog()
2129N/A
2129N/A //DO NOT call this directly, go through Sysout
2129N/A public void printInstructions(String[] instructions) {
2129N/A //Clear out any current instructions
2129N/A instructionsText.setText("");
2129N/A
2129N/A //Go down array of instruction strings
2129N/A
2129N/A String printStr, remainingStr;
2129N/A for (int i = 0; i < instructions.length; i++) {
2129N/A //chop up each into pieces maxSringLength long
2129N/A remainingStr = instructions[i];
2129N/A while (remainingStr.length() > 0) {
2129N/A //if longer than max then chop off first max chars to print
2129N/A if (remainingStr.length() >= maxStringLength) {
2129N/A //Try to chop on a word boundary
2129N/A int posOfSpace = remainingStr.
2129N/A lastIndexOf(' ', maxStringLength - 1);
2129N/A
2129N/A if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
2129N/A
2129N/A printStr = remainingStr.substring(0, posOfSpace + 1);
2129N/A remainingStr = remainingStr.substring(posOfSpace + 1);
2129N/A }
2129N/A //else just print
2129N/A else {
2129N/A printStr = remainingStr;
2129N/A remainingStr = "";
2129N/A }
2129N/A
2129N/A instructionsText.append(printStr + "\n");
2129N/A
2129N/A }// while
2129N/A
2129N/A }// for
2129N/A
2129N/A }//printInstructions()
2129N/A
2129N/A //DO NOT call this directly, go through Sysout
2129N/A public void displayMessage(String messageIn) {
2129N/A messageText.append(messageIn + "\n");
2129N/A }
2129N/A
2129N/A}// TestDialog class
2129N/A