1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 //import java.applet.Applet;
  26 import javax.swing.BorderFactory;
  27 import javax.swing.JApplet;
  28 import javax.swing.JFrame;
  29 import javax.swing.JLayeredPane;
  30 import javax.swing.JPanel;
  31 
  32 /**
  33  * @test
  34  * @bug 8007155
  35  * @summary [macosx] Disabled panel takes mouse input in JLayeredPane
  36  * @author Alexander Scherbatiy: area=java.awt.Cursor
  37  * @run applet/manual=yesno CursorOverlappedPanelsTest.html
  38  */
  39 public class CursorOverlappedPanelsTest extends JApplet {
  40     //Declare things used in the test, like buttons and labels here
  41 
  42     public void init() {
  43         //Create instructions for the user here, as well as set up
  44         // the environment -- set the layout manager, add buttons,
  45         // etc.
  46         this.setLayout(new BorderLayout());
  47 
  48         String[] instructions = {
  49             "Verify that the Crosshair cursor from enabled panel"
  50             + " is displayed on the panels intersection",
  51             "1) Move the mosue cursor on the Enabled and Disabled panels"
  52             + " intersection",
  53             "2) Check that the Crosshair cursor is displayed ",
  54             "If so, press PASS, else press FAIL."
  55         };
  56         Sysout.createDialogWithInstructions(instructions);
  57 
  58     }//End  init()
  59 
  60     public void start() {
  61         //Get things going.  Request focus, set size, et cetera
  62         setSize(200, 200);
  63         setVisible(true);
  64         validate();
  65         try {
  66             EventQueue.invokeAndWait(new Runnable() {
  67                 public void run() {
  68                     createAndShowGUI();
  69                 }
  70             });
  71         } catch (Exception e) {
  72             throw new RuntimeException(e);
  73         }
  74     }// start()
  75 
  76     //The rest of this class is the actions which perform the test...
  77     //Use Sysout.println to communicate with the user NOT System.out!!
  78     //Sysout.println ("Something Happened!");
  79     private static JPanel createPanel(Point location, boolean enabled) {
  80         final JPanel panel = new JPanel();
  81         panel.setOpaque(false);
  82         panel.setEnabled(enabled);
  83         panel.setSize(new Dimension(200, 200));
  84         panel.setLocation(location);
  85         panel.setBorder(BorderFactory.createTitledBorder(
  86                 enabled ? "Enabled" : "Disabled"));
  87         panel.setCursor(Cursor.getPredefinedCursor(
  88                 enabled ? Cursor.CROSSHAIR_CURSOR : Cursor.DEFAULT_CURSOR));
  89         System.out.println("cursor: " + Cursor.getPredefinedCursor(enabled ? Cursor.CROSSHAIR_CURSOR : Cursor.DEFAULT_CURSOR));
  90         return panel;
  91     }
  92 
  93     private static void createAndShowGUI() {
  94         final JFrame frame = new JFrame("Test");
  95         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96 
  97         JLayeredPane layeredPane = new JLayeredPane();
  98         layeredPane.setPreferredSize(new Dimension(400, 400));
  99         JPanel enabledPanel = createPanel(new Point(10, 10), true);
 100         JPanel disabledPanel = createPanel(new Point(100, 100), false);
 101         layeredPane.add(disabledPanel, JLayeredPane.PALETTE_LAYER);
 102         layeredPane.add(enabledPanel, JLayeredPane.DEFAULT_LAYER);
 103 
 104         frame.getContentPane().add(layeredPane);
 105         frame.pack();
 106         frame.setVisible(true);
 107     }
 108 }// class BlockedWindowTest
 109 
 110 /* Place other classes related to the test after this line */
 111 /**
 112  * **************************************************
 113  * Standard Test Machinery DO NOT modify anything below -- it's a standard chunk
 114  * of code whose purpose is to make user interaction uniform, and thereby make
 115  * it simpler to read and understand someone else's test.
 116  * **************************************************
 117  */
 118 /**
 119  * This is part of the standard test machinery. It creates a dialog (with the
 120  * instructions), and is the interface for sending text messages to the user. To
 121  * print the instructions, send an array of strings to Sysout.createDialog
 122  * WithInstructions method. Put one line of instructions per array entry. To
 123  * display a message for the tester to see, simply call Sysout.println with the
 124  * string to be displayed. This mimics System.out.println but works within the
 125  * test harness as well as standalone.
 126  */
 127 class Sysout {
 128 
 129     private static TestDialog dialog;
 130 
 131     public static void createDialogWithInstructions(String[] instructions) {
 132         dialog = new TestDialog(new Frame(), "Instructions");
 133         dialog.printInstructions(instructions);
 134         dialog.setVisible(true);
 135         println("Any messages for the tester will display here.");
 136     }
 137 
 138     public static void createDialog() {
 139         dialog = new TestDialog(new Frame(), "Instructions");
 140         String[] defInstr = {"Instructions will appear here. ", ""};
 141         dialog.printInstructions(defInstr);
 142         dialog.setVisible(true);
 143         println("Any messages for the tester will display here.");
 144     }
 145 
 146     public static void printInstructions(String[] instructions) {
 147         dialog.printInstructions(instructions);
 148     }
 149 
 150     public static void println(String messageIn) {
 151         dialog.displayMessage(messageIn);
 152     }
 153 }// Sysout  class
 154 
 155 /**
 156  * This is part of the standard test machinery. It provides a place for the test
 157  * instructions to be displayed, and a place for interactive messages to the
 158  * user to be displayed. To have the test instructions displayed, see Sysout. To
 159  * have a message to the user be displayed, see Sysout. Do not call anything in
 160  * this dialog directly.
 161  */
 162 class TestDialog extends Dialog {
 163 
 164     TextArea instructionsText;
 165     TextArea messageText;
 166     int maxStringLength = 80;
 167 
 168     //DO NOT call this directly, go through Sysout
 169     public TestDialog(Frame frame, String name) {
 170         super(frame, name);
 171         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 172         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
 173         add("North", instructionsText);
 174 
 175         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
 176         add("Center", messageText);
 177 
 178         pack();
 179 
 180         setVisible(true);
 181     }// TestDialog()
 182 
 183     //DO NOT call this directly, go through Sysout
 184     public void printInstructions(String[] instructions) {
 185         //Clear out any current instructions
 186         instructionsText.setText("");
 187 
 188         //Go down array of instruction strings
 189 
 190         String printStr, remainingStr;
 191         for (int i = 0; i < instructions.length; i++) {
 192             //chop up each into pieces maxSringLength long
 193             remainingStr = instructions[ i];
 194             while (remainingStr.length() > 0) {
 195                 //if longer than max then chop off first max chars to print
 196                 if (remainingStr.length() >= maxStringLength) {
 197                     //Try to chop on a word boundary
 198                     int posOfSpace = remainingStr.
 199                             lastIndexOf(' ', maxStringLength - 1);
 200 
 201                     if (posOfSpace <= 0) {
 202                         posOfSpace = maxStringLength - 1;
 203                     }
 204 
 205                     printStr = remainingStr.substring(0, posOfSpace + 1);
 206                     remainingStr = remainingStr.substring(posOfSpace + 1);
 207                 } //else just print
 208                 else {
 209                     printStr = remainingStr;
 210                     remainingStr = "";
 211                 }
 212 
 213                 instructionsText.append(printStr + "\n");
 214 
 215             }// while
 216 
 217         }// for
 218 
 219     }//printInstructions()
 220 
 221     //DO NOT call this directly, go through Sysout
 222     public void displayMessage(String messageIn) {
 223         messageText.append(messageIn + "\n");
 224         System.out.println(messageIn);
 225     }
 226 }// TestDialog  class