|
| 1 | +/* |
| 2 | + * Copyright (c) 1998, 2023, 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.BorderLayout; |
| 25 | +import java.awt.Button; |
| 26 | +import java.awt.Component; |
| 27 | +import java.awt.Dimension; |
| 28 | +import java.awt.EventQueue; |
| 29 | +import java.awt.Frame; |
| 30 | +import java.awt.GridLayout; |
| 31 | +import java.awt.Panel; |
| 32 | +import java.awt.Point; |
| 33 | +import java.awt.Robot; |
| 34 | +import java.awt.ScrollPane; |
| 35 | +import java.awt.TextField; |
| 36 | +import java.awt.event.ActionEvent; |
| 37 | +import java.awt.event.ActionListener; |
| 38 | +import java.awt.event.InputEvent; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @bug 4124460 |
| 43 | + * @key headful |
| 44 | + * @summary Test for initializing a Motif peer component causes a crash. |
| 45 | +*/ |
| 46 | + |
| 47 | +public class ScrollPaneTest { |
| 48 | + private static volatile Point p1 = null; |
| 49 | + private static volatile Point p2 = null; |
| 50 | + private static Robot robot = null; |
| 51 | + |
| 52 | + private static Point getClickPoint(Component component) { |
| 53 | + Point locationOnScreen = component.getLocationOnScreen(); |
| 54 | + Dimension size = component.getSize(); |
| 55 | + locationOnScreen.x += size.width / 2; |
| 56 | + locationOnScreen.y += size.height / 2; |
| 57 | + return locationOnScreen; |
| 58 | + } |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + robot = new Robot(); |
| 61 | + robot.setAutoWaitForIdle(true); |
| 62 | + robot.setAutoDelay(100); |
| 63 | + |
| 64 | + try { |
| 65 | + doTest(); |
| 66 | + } finally { |
| 67 | + ScrollPaneTester.disposeAll(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static void doTest() throws Exception { |
| 72 | + EventQueue.invokeAndWait(ScrollPaneTester::initAndShowGui); |
| 73 | + |
| 74 | + robot.waitForIdle(); |
| 75 | + robot.delay(1000); |
| 76 | + |
| 77 | + EventQueue.invokeAndWait(() -> { |
| 78 | + p1 = getClickPoint(ScrollPaneTester.st1.buttonRight); |
| 79 | + p2 = getClickPoint(ScrollPaneTester.st1.buttonSwap); |
| 80 | + }); |
| 81 | + |
| 82 | + robot.mouseMove(p1.x, p1.y); |
| 83 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 84 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 85 | + |
| 86 | + robot.mouseMove(p2.x, p2.y); |
| 87 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 88 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 89 | + |
| 90 | + robot.delay(1000); |
| 91 | + |
| 92 | + EventQueue.invokeAndWait(() -> { |
| 93 | + p1 = getClickPoint(ScrollPaneTester.st2.buttonRight); |
| 94 | + p2 = getClickPoint(ScrollPaneTester.st2.buttonSwap); |
| 95 | + }); |
| 96 | + |
| 97 | + robot.mouseMove(p1.x, p1.y); |
| 98 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 99 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 100 | + |
| 101 | + robot.mouseMove(p2.x, p2.y); |
| 102 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 103 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class ScrollPaneTester implements ActionListener { |
| 108 | + static ScrollPaneTester st1, st2; |
| 109 | + final Button buttonLeft, buttonRight, buttonQuit, buttonSwap; |
| 110 | + protected ScrollPane sp; |
| 111 | + protected Frame f; |
| 112 | + |
| 113 | + public static void initAndShowGui() { |
| 114 | + ScrollPaneTester.st1 = new ScrollPaneTester(true); |
| 115 | + ScrollPaneTester.st2 = new ScrollPaneTester(false); |
| 116 | + } |
| 117 | + |
| 118 | + public ScrollPaneTester(boolean large) { |
| 119 | + sp = new ScrollPane(ScrollPane.SCROLLBARS_NEVER); |
| 120 | + |
| 121 | + Panel p = new Panel(); |
| 122 | + |
| 123 | + if (large) { |
| 124 | + p.setLayout(new GridLayout(10, 10)); |
| 125 | + for (int i = 0; i < 10; i++) |
| 126 | + for (int j = 0; j < 10; j++) { |
| 127 | + TextField tf = new TextField("I am " + i + j); |
| 128 | + tf.setSize(100, 20); |
| 129 | + p.add(tf); |
| 130 | + } |
| 131 | + } else { |
| 132 | + TextField tf = new TextField("Smallness:"); |
| 133 | + tf.setSize(150, 50); |
| 134 | + p.add(tf); |
| 135 | + } |
| 136 | + |
| 137 | + sp.add(p); |
| 138 | + |
| 139 | + // Button panel |
| 140 | + buttonLeft = new Button("Left"); |
| 141 | + buttonLeft.addActionListener(this); |
| 142 | + buttonQuit = new Button("Quit"); |
| 143 | + buttonQuit.addActionListener(this); |
| 144 | + buttonSwap = new Button("Swap"); |
| 145 | + buttonSwap.addActionListener(this); |
| 146 | + buttonRight = new Button("Right"); |
| 147 | + buttonRight.addActionListener(this); |
| 148 | + |
| 149 | + Panel bp = new Panel(); |
| 150 | + bp.add(buttonLeft); |
| 151 | + bp.add(buttonSwap); |
| 152 | + bp.add(buttonQuit); |
| 153 | + bp.add(buttonRight); |
| 154 | + |
| 155 | + // Window w/ button panel and scrollpane |
| 156 | + f = new Frame("ScrollPane Test " + (large ? "large" : "small")); |
| 157 | + f.setLayout(new BorderLayout()); |
| 158 | + f.add("South", bp); |
| 159 | + f.add("Center", sp); |
| 160 | + |
| 161 | + if (large) { |
| 162 | + f.setSize(300, 200); |
| 163 | + f.setLocation(100, 100); |
| 164 | + } else { |
| 165 | + f.setSize(200, 100); |
| 166 | + f.setLocation(500, 100); |
| 167 | + } |
| 168 | + |
| 169 | + f.setVisible(true); |
| 170 | + } |
| 171 | + |
| 172 | + public static void disposeAll() { |
| 173 | + ScrollPaneTester.st1.f.dispose(); |
| 174 | + ScrollPaneTester.st2.f.dispose(); |
| 175 | + } |
| 176 | + |
| 177 | + public static void |
| 178 | + swapPanels() { |
| 179 | + ScrollPane sss = st1.sp; |
| 180 | + |
| 181 | + st1.f.add("Center", st2.sp); |
| 182 | + st1.sp = st2.sp; |
| 183 | + |
| 184 | + st2.f.add("Center", sss); |
| 185 | + st2.sp = sss; |
| 186 | + } |
| 187 | + |
| 188 | + public void |
| 189 | + actionPerformed(ActionEvent ev) { |
| 190 | + Object s = ev.getSource(); |
| 191 | + |
| 192 | + if (s == buttonLeft) { |
| 193 | + scroll(true); |
| 194 | + } else if (s == buttonRight) { |
| 195 | + scroll(false); |
| 196 | + } else if (s == buttonSwap) { |
| 197 | + swapPanels(); |
| 198 | + } else if (s == buttonQuit) { |
| 199 | + disposeAll(); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private void |
| 204 | + scroll(boolean scroll_left) { |
| 205 | + Point p = sp.getScrollPosition(); |
| 206 | + |
| 207 | + if (scroll_left) |
| 208 | + p.x = Math.max(0, p.x - 20); |
| 209 | + else { |
| 210 | + int cwidth = sp.getComponent(0).getSize().width; |
| 211 | + p.x = Math.min(p.x + 20, cwidth); |
| 212 | + } |
| 213 | + |
| 214 | + sp.setScrollPosition(p); |
| 215 | + } |
| 216 | +} |
0 commit comments