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/manual=yesno ImageDecoratedDnD.html
  32 */
  33 
  34 import java.applet.Applet;
  35 import java.awt.*;
  36 import java.awt.dnd.DragSource;
  37 
  38 
  39 
  40 public class ImageDecoratedDnD extends Applet {
  41     //Declare things used in the test, like buttons and labels here
  42 
  43     public void init() {
  44         //Create instructions for the user here, as well as set up
  45         // the environment -- set the layout manager, add buttons,
  46         // etc.
  47         this.setLayout(new BorderLayout());
  48 
  49         String[] instructions =
  50                 {
  51                         "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
  52                         "a red panel, will appear below. ",
  53                         "1. Click on the button and drag 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. Release the mouse button.",
  62                         "The panel should turn red again and a yellow button labeled ",
  63                         "\"Drag ME!\" should appear inside the panel. You should be able ",
  64                         "to repeat this operation multiple times."
  65                 };
  66         Sysout.createDialogWithInstructions(instructions);
  67 
  68     }//End  init()
  69 
  70     public void start() {
  71         Frame f = new Frame("Use keyboard for DnD change");
  72         Panel mainPanel;
  73         Component dragSource, dropTarget;
  74 
  75         f.setBounds(0, 400, 200, 200);
  76         f.setLayout(new BorderLayout());
  77 
  78         mainPanel = new Panel();
  79         mainPanel.setLayout(new BorderLayout());
  80 
  81         mainPanel.setBackground(Color.blue);
  82 
  83         dropTarget = new DnDTarget(Color.red, Color.yellow);
  84         dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
  85 
  86         mainPanel.add(dragSource, "North");
  87         mainPanel.add(dropTarget, "Center");
  88         f.add(mainPanel, BorderLayout.CENTER);
  89 
  90         f.setVisible(true);
  91     }// start()
  92 }// class DnDAcceptanceTest
  93 
  94 
  95 /**
  96  * *************************************************
  97  * Standard Test Machinery
  98  * DO NOT modify anything below -- it's a standard
  99  * chunk of code whose purpose is to make user
 100  * interaction uniform, and thereby make it simpler
 101  * to read and understand someone else's test.
 102  * **************************************************
 103  */
 104 class Sysout {
 105     private static TestDialog dialog;
 106 
 107     public static void createDialogWithInstructions(String[] instructions) {
 108         dialog = new TestDialog(new Frame(), "Instructions");
 109         dialog.printInstructions(instructions);
 110         dialog.show();
 111         println("Any messages for the tester will display here.");
 112     }
 113 
 114     public static void createDialog() {
 115         dialog = new TestDialog(new Frame(), "Instructions");
 116         String[] defInstr = {"Instructions will appear here. ", ""};
 117         dialog.printInstructions(defInstr);
 118         dialog.show();
 119         println("Any messages for the tester will display here.");
 120     }
 121 
 122 
 123     public static void printInstructions(String[] instructions) {
 124         dialog.printInstructions(instructions);
 125     }
 126 
 127 
 128     public static void println(String messageIn) {
 129         dialog.displayMessage(messageIn);
 130     }
 131 
 132 }// Sysout  class
 133 
 134 
 135 class TestDialog extends Dialog {
 136 
 137     TextArea instructionsText;
 138     TextArea messageText;
 139     int maxStringLength = 80;
 140 
 141     //DO NOT call this directly, go through Sysout
 142     public TestDialog(Frame frame, String name) {
 143         super(frame, name);
 144         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 145         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
 146         add("North", instructionsText);
 147 
 148         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
 149         add("South", messageText);
 150 
 151         pack();
 152 
 153         show();
 154     }// TestDialog()
 155 
 156     //DO NOT call this directly, go through Sysout
 157     public void printInstructions(String[] instructions) {
 158         //Clear out any current instructions
 159         instructionsText.setText("");
 160 
 161         //Go down array of instruction strings
 162 
 163         String printStr, remainingStr;
 164         for (int i = 0; i < instructions.length; i++) {
 165             //chop up each into pieces maxSringLength long
 166             remainingStr = instructions[i];
 167             while (remainingStr.length() > 0) {
 168                 //if longer than max then chop off first max chars to print
 169                 if (remainingStr.length() >= maxStringLength) {
 170                     //Try to chop on a word boundary
 171                     int posOfSpace = remainingStr.
 172                             lastIndexOf(' ', maxStringLength - 1);
 173 
 174                     if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
 175 
 176                     printStr = remainingStr.substring(0, posOfSpace + 1);
 177                     remainingStr = remainingStr.substring(posOfSpace + 1);
 178                 }
 179                 //else just print
 180                 else {
 181                     printStr = remainingStr;
 182                     remainingStr = "";
 183                 }
 184 
 185                 instructionsText.append(printStr + "\n");
 186 
 187             }// while
 188 
 189         }// for
 190 
 191     }//printInstructions()
 192 
 193     //DO NOT call this directly, go through Sysout
 194     public void displayMessage(String messageIn) {
 195         messageText.append(messageIn + "\n");
 196     }
 197 
 198 }// TestDialog  class
 199