|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 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 javax.swing.JButton; |
| 25 | +import javax.swing.JFrame; |
| 26 | +import javax.swing.JSplitPane; |
| 27 | +import javax.swing.SwingUtilities; |
| 28 | +import javax.swing.plaf.basic.BasicSplitPaneDivider; |
| 29 | +import javax.swing.plaf.basic.BasicSplitPaneUI; |
| 30 | +import java.awt.GridLayout; |
| 31 | +import java.awt.Point; |
| 32 | +import java.awt.Robot; |
| 33 | +import java.awt.event.InputEvent; |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @bug 4870674 |
| 38 | + * @summary JSplitPane's one-touch buttons should deal with resized split panes better |
| 39 | + * @key headful |
| 40 | + * @run main bug4870674 |
| 41 | + */ |
| 42 | + |
| 43 | +public class bug4870674 { |
| 44 | + private static JSplitPane jsp0, jsp1; |
| 45 | + private static JButton[] leftOneTouchButton = new JButton[2]; |
| 46 | + private static JButton[] rightOneTouchButton = new JButton[2]; |
| 47 | + private static JFrame frame; |
| 48 | + private static Robot robot; |
| 49 | + private static volatile boolean passed = true; |
| 50 | + private static volatile Point rightBtnPos0; |
| 51 | + private static volatile Point leftBtnPos0; |
| 52 | + private static volatile Point rightBtnPos1; |
| 53 | + private static volatile Point leftBtnPos1; |
| 54 | + |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + try { |
| 57 | + robot = new Robot(); |
| 58 | + SwingUtilities.invokeAndWait(() -> { |
| 59 | + frame = new JFrame("Test"); |
| 60 | + frame.getContentPane().setLayout(new GridLayout(2, 1)); |
| 61 | + |
| 62 | + jsp0 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, |
| 63 | + new JButton("Left"), |
| 64 | + new JButton("Right")); |
| 65 | + frame.getContentPane().add(jsp0); |
| 66 | + |
| 67 | + jsp0.setUI(new TestSplitPaneUI(0)); |
| 68 | + jsp0.setOneTouchExpandable(true); |
| 69 | + |
| 70 | + jsp1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, |
| 71 | + new JButton("Left"), |
| 72 | + new JButton("Right")); |
| 73 | + frame.getContentPane().add(jsp1); |
| 74 | + |
| 75 | + jsp1.setUI(new TestSplitPaneUI(1)); |
| 76 | + jsp1.setOneTouchExpandable(true); |
| 77 | + |
| 78 | + frame.setSize(300, 100); |
| 79 | + frame.setVisible(true); |
| 80 | + }); |
| 81 | + robot.waitForIdle(); |
| 82 | + robot.delay(1000); |
| 83 | + SwingUtilities.invokeAndWait(() -> { |
| 84 | + rightBtnPos0 = rightOneTouchButton[0].getLocationOnScreen(); |
| 85 | + rightBtnPos0.x += rightOneTouchButton[0].getWidth() / 2; |
| 86 | + rightBtnPos0.y += rightOneTouchButton[0].getHeight() / 2; |
| 87 | + |
| 88 | + leftBtnPos1 = leftOneTouchButton[1].getLocationOnScreen(); |
| 89 | + leftBtnPos1.x += leftOneTouchButton[0].getWidth() / 2; |
| 90 | + leftBtnPos1.y += leftOneTouchButton[0].getHeight() / 2; |
| 91 | + |
| 92 | + leftBtnPos0 = leftOneTouchButton[0].getLocationOnScreen(); |
| 93 | + leftBtnPos0.x += leftOneTouchButton[0].getWidth() / 2; |
| 94 | + leftBtnPos0.y += leftOneTouchButton[0].getHeight() / 2; |
| 95 | + |
| 96 | + rightBtnPos1 = rightOneTouchButton[1].getLocationOnScreen(); |
| 97 | + rightBtnPos1.x += rightOneTouchButton[0].getWidth() / 2; |
| 98 | + rightBtnPos1.y += rightOneTouchButton[0].getHeight() / 2; |
| 99 | + |
| 100 | + jsp0.setDividerLocation(250); |
| 101 | + }); |
| 102 | + robot.mouseMove(rightBtnPos0.x, rightBtnPos0.y); |
| 103 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 104 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 105 | + SwingUtilities.invokeAndWait(() -> { |
| 106 | + jsp1.setDividerLocation(250); |
| 107 | + }); |
| 108 | + robot.waitForIdle(); |
| 109 | + robot.delay(100); |
| 110 | + robot.mouseMove(leftBtnPos1.x, leftBtnPos1.y); |
| 111 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 112 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 113 | + SwingUtilities.invokeAndWait(() -> { |
| 114 | + frame.setSize(200, 100); |
| 115 | + }); |
| 116 | + robot.waitForIdle(); |
| 117 | + robot.delay(100); |
| 118 | + robot.mouseMove(leftBtnPos0.x, leftBtnPos0.y); |
| 119 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 120 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 121 | + robot.waitForIdle(); |
| 122 | + robot.delay(100); |
| 123 | + robot.mouseMove(rightBtnPos1.x, rightBtnPos1.y); |
| 124 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 125 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 126 | + robot.waitForIdle(); |
| 127 | + robot.delay(100); |
| 128 | + |
| 129 | + SwingUtilities.invokeAndWait(() -> { |
| 130 | + if (jsp0.getDividerLocation() > jsp0.getMaximumDividerLocation() || |
| 131 | + jsp1.getDividerLocation() > jsp1.getMaximumDividerLocation()) { |
| 132 | + passed = false; |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + if (!passed) { |
| 137 | + throw new RuntimeException("The divider location couldn't " + |
| 138 | + "be greater then its maximum location"); |
| 139 | + } |
| 140 | + System.out.println("Test Passed!"); |
| 141 | + } finally { |
| 142 | + SwingUtilities.invokeAndWait(() -> { |
| 143 | + if (frame != null) { |
| 144 | + frame.dispose(); |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + static class TestSplitPaneUI extends BasicSplitPaneUI { |
| 151 | + int i; |
| 152 | + |
| 153 | + public TestSplitPaneUI(int i) { |
| 154 | + super(); |
| 155 | + this.i = i; |
| 156 | + } |
| 157 | + |
| 158 | + public BasicSplitPaneDivider createDefaultDivider() { |
| 159 | + return new TestSplitPaneDivider(this, i); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + static class TestSplitPaneDivider extends BasicSplitPaneDivider { |
| 164 | + int i = 0; |
| 165 | + |
| 166 | + public TestSplitPaneDivider(BasicSplitPaneUI ui, int i) { |
| 167 | + super(ui); |
| 168 | + this.i = i; |
| 169 | + } |
| 170 | + |
| 171 | + protected JButton createLeftOneTouchButton() { |
| 172 | + leftOneTouchButton[i] = super.createLeftOneTouchButton(); |
| 173 | + return leftOneTouchButton[i]; |
| 174 | + } |
| 175 | + |
| 176 | + protected JButton createRightOneTouchButton() { |
| 177 | + rightOneTouchButton[i] = super.createRightOneTouchButton(); |
| 178 | + return rightOneTouchButton[i]; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Jun 11, 2024
Review
Issues