|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2024, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 4111379 |
| 27 | + * @summary Test for setting cursor to null for lightweight components |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual NullCursorTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.Button; |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Component; |
| 36 | +import java.awt.Container; |
| 37 | +import java.awt.Cursor; |
| 38 | +import java.awt.Frame; |
| 39 | +import java.awt.Graphics; |
| 40 | +import java.awt.Rectangle; |
| 41 | +import java.awt.event.ActionEvent; |
| 42 | +import java.awt.event.ActionListener; |
| 43 | +import java.awt.event.MouseAdapter; |
| 44 | +import java.awt.event.MouseEvent; |
| 45 | + |
| 46 | +public class NullCursorTest { |
| 47 | + public static void main(String[] args) throws Exception { |
| 48 | + String INSTRUCTIONS = """ |
| 49 | + 1. Hover over each colored area as described: |
| 50 | + Green area shows a CrossCursor. |
| 51 | + Red area shows a TextCursor. |
| 52 | + Yellow area shows a HandCursor. |
| 53 | + 2. Click once in red area, then: |
| 54 | + Green area shows a HandCursor. |
| 55 | + Red area shows a BusyCursor. |
| 56 | + Yellow area shows a HandCursor. |
| 57 | + 3. Click again in red area, then: |
| 58 | + Green area shows a CrossCursor. |
| 59 | + Red area shows a HandCursor. |
| 60 | + Yellow area shows a HandCursor. |
| 61 | + """; |
| 62 | + |
| 63 | + PassFailJFrame.builder() |
| 64 | + .title("Test Instructions") |
| 65 | + .instructions(INSTRUCTIONS) |
| 66 | + .rows((int) INSTRUCTIONS.lines().count() + 2) |
| 67 | + .columns(35) |
| 68 | + .testUI(NullCursorTest::createUI) |
| 69 | + .build() |
| 70 | + .awaitAndCheck(); |
| 71 | + } |
| 72 | + |
| 73 | + public static Frame createUI() { |
| 74 | + Frame f = new Frame("Null Cursor Test Frame"); |
| 75 | + f.setSize(200, 200); |
| 76 | + final Container p = f; |
| 77 | + p.setName("parent"); |
| 78 | + p.setLayout(null); |
| 79 | + |
| 80 | + final Component green = p.add(new Component() { |
| 81 | + public void paint(Graphics g) { |
| 82 | + Rectangle r = getBounds(); |
| 83 | + g.setColor(Color.green); |
| 84 | + g.fillRect(0, 0, r.width, r.height); |
| 85 | + } |
| 86 | + }); |
| 87 | + green.setName("green"); |
| 88 | + green.setBackground(Color.red); |
| 89 | + green.setBounds(50, 50, 75, 75); |
| 90 | + green.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); |
| 91 | + |
| 92 | + Container h = new Container() { |
| 93 | + public void paint(Graphics g) { |
| 94 | + Rectangle r = getBounds(); |
| 95 | + g.setColor(Color.yellow); |
| 96 | + g.fillRect(0, 0, r.width, r.height); |
| 97 | + super.paint(g); |
| 98 | + } |
| 99 | + }; |
| 100 | + h.setBounds(15, 25, 150, 150); |
| 101 | + h.setName("container"); |
| 102 | + h.setBackground(Color.yellow); |
| 103 | + h.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 104 | + final Component red = new Component() { |
| 105 | + public void paint(Graphics g) { |
| 106 | + Rectangle r = getBounds(); |
| 107 | + Color c = getBackground(); |
| 108 | + g.setColor(c); |
| 109 | + g.fillRect(0, 0, r.width, r.height); |
| 110 | + super.paint(g); |
| 111 | + } |
| 112 | + }; |
| 113 | + red.setName("red"); |
| 114 | + red.setBackground(Color.red); |
| 115 | + red.setBounds(10, 10, 120, 120); |
| 116 | + red.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); |
| 117 | + |
| 118 | + final Button b = (Button)h.add(new Button("Test")); |
| 119 | + b.setBounds(10, 10, 40, 20); |
| 120 | + h.add(red); |
| 121 | + p.add(h); |
| 122 | + |
| 123 | + b.addActionListener(new ActionListener() { |
| 124 | + boolean f = false; |
| 125 | + public void actionPerformed(ActionEvent e) { |
| 126 | + if (f) { |
| 127 | + b.setCursor(null); |
| 128 | + } else { |
| 129 | + b.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 130 | + } |
| 131 | + f = !f; |
| 132 | + } |
| 133 | + }); |
| 134 | + red.addMouseListener(new MouseAdapter() { |
| 135 | + boolean f = true; |
| 136 | + |
| 137 | + public void mouseClicked(MouseEvent e) { |
| 138 | + Component c = (Component)e.getSource(); |
| 139 | + if (f) { |
| 140 | + c.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
| 141 | + p.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); |
| 142 | + green.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 143 | + f = false; |
| 144 | + } else { |
| 145 | + c.setCursor(null); |
| 146 | + p.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
| 147 | + green.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); |
| 148 | + f = true; |
| 149 | + } |
| 150 | + } |
| 151 | + }); |
| 152 | + return f; |
| 153 | + } |
| 154 | +} |
0 commit comments