Skip to content

Commit cafb3dc

Browse files
committedAug 21, 2024
6318027: BasicScrollBarUI does not disable timer when enclosing frame is disabled.
Reviewed-by: abhiscxk, tr
1 parent 88ccbb6 commit cafb3dc

File tree

3 files changed

+260
-13
lines changed

3 files changed

+260
-13
lines changed
 

‎src/java.desktop/macosx/classes/com/apple/laf/AquaScrollBarUI.java

+54-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,19 +25,46 @@
2525

2626
package com.apple.laf;
2727

28-
import java.awt.*;
29-
import java.awt.event.*;
30-
import java.beans.*;
31-
import java.util.*;
32-
33-
import javax.swing.*;
28+
import java.awt.Adjustable;
29+
import java.awt.Component;
30+
import java.awt.Container;
31+
import java.awt.Dimension;
32+
import java.awt.Graphics;
33+
import java.awt.Insets;
34+
import java.awt.Point;
35+
import java.awt.Rectangle;
36+
import java.awt.event.ActionEvent;
37+
import java.awt.event.ActionListener;
38+
import java.awt.event.MouseAdapter;
39+
import java.awt.event.MouseEvent;
40+
import java.awt.event.MouseMotionListener;
41+
42+
import java.beans.PropertyChangeEvent;
43+
import java.beans.PropertyChangeListener;
44+
import java.util.HashMap;
45+
import java.util.Map;
46+
47+
import javax.swing.BoundedRangeModel;
48+
import javax.swing.JComponent;
49+
import javax.swing.JFrame;
50+
import javax.swing.JScrollBar;
51+
import javax.swing.LookAndFeel;
3452
import javax.swing.Timer;
35-
import javax.swing.event.*;
36-
import javax.swing.plaf.*;
37-
38-
import apple.laf.*;
39-
import apple.laf.JRSUIConstants.*;
53+
import javax.swing.event.ChangeEvent;
54+
import javax.swing.event.ChangeListener;
55+
import javax.swing.plaf.ComponentUI;
56+
import javax.swing.plaf.ScrollBarUI;
57+
58+
import apple.laf.JRSUIStateFactory;
59+
import apple.laf.JRSUIConstants.Hit;
60+
import apple.laf.JRSUIConstants.NothingToScroll;
61+
import apple.laf.JRSUIConstants.Orientation;
62+
import apple.laf.JRSUIConstants.ScrollBarHit;
63+
import apple.laf.JRSUIConstants.ScrollBarPart;
64+
import apple.laf.JRSUIConstants.ShowArrows;
65+
import apple.laf.JRSUIConstants.State;
4066
import apple.laf.JRSUIState.ScrollBarState;
67+
import apple.laf.JRSUIUtils;
4168

4269
import com.apple.laf.AquaUtils.RecyclableSingleton;
4370

@@ -527,6 +554,21 @@ void setScrollByBlock(final boolean block) {
527554
}
528555

529556
public void actionPerformed(final ActionEvent e) {
557+
Component parent = fScrollBar.getParent();
558+
do {
559+
if (parent instanceof JFrame par) {
560+
if (!par.isEnabled()) {
561+
((Timer)e.getSource()).stop();
562+
fScrollBar.setValueIsAdjusting(false);
563+
return;
564+
}
565+
break;
566+
} else {
567+
if (parent != null) {
568+
parent = parent.getParent();
569+
}
570+
}
571+
} while (parent != null);
530572
if (fUseBlockIncrement) {
531573
Hit newPart = getPartHit(fTrackListener.fCurrentMouseX, fTrackListener.fCurrentMouseY);
532574

‎src/java.desktop/share/classes/javax/swing/plaf/basic/BasicScrollBarUI.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,7 @@
4848
import javax.swing.InputMap;
4949
import javax.swing.JButton;
5050
import javax.swing.JComponent;
51+
import javax.swing.JFrame;
5152
import javax.swing.JList;
5253
import javax.swing.JScrollBar;
5354
import javax.swing.JScrollPane;
@@ -1608,6 +1609,25 @@ public ScrollListener(int dir, boolean block) {
16081609

16091610
/** {@inheritDoc} */
16101611
public void actionPerformed(ActionEvent e) {
1612+
// If frame is disabled and timer is started in mousePressed
1613+
// and mouseReleased is not called, then timer will not be stopped
1614+
// Stop the timer if frame is disabled
1615+
Component parent = scrollbar.getParent();
1616+
do {
1617+
if (parent instanceof JFrame par) {
1618+
if (!par.isEnabled()) {
1619+
((Timer)e.getSource()).stop();
1620+
buttonListener.handledEvent = false;
1621+
scrollbar.setValueIsAdjusting(false);
1622+
return;
1623+
}
1624+
break;
1625+
} else {
1626+
if (parent != null) {
1627+
parent = parent.getParent();
1628+
}
1629+
}
1630+
} while (parent != null);
16111631
if(useBlockIncrement) {
16121632
scrollByBlock(direction);
16131633
// Stop scrolling if the thumb catches up with the mouse
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright (c) 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 6318027
27+
* @key headful
28+
* @summary Verifies BasicScrollBarUI disables timer when enclosing frame is disabled
29+
* @run main DisableFrameFromScrollBar
30+
*/
31+
32+
import java.awt.FlowLayout;
33+
import java.awt.Point;
34+
import java.awt.Rectangle;
35+
import java.awt.Robot;
36+
import java.awt.event.AdjustmentEvent;
37+
import java.awt.event.AdjustmentListener;
38+
import java.awt.event.InputEvent;
39+
40+
import javax.swing.JFrame;
41+
import javax.swing.JScrollBar;
42+
import javax.swing.SwingUtilities;
43+
import javax.swing.UIManager;
44+
import javax.swing.UnsupportedLookAndFeelException;
45+
import javax.swing.event.ChangeEvent;
46+
import javax.swing.event.ChangeListener;
47+
48+
public class DisableFrameFromScrollBar {
49+
50+
private static JFrame frame;
51+
private static JScrollBar bar;
52+
private static int oldValue;
53+
private static volatile boolean doCheck;
54+
private static volatile boolean isAdjusting;
55+
56+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
57+
try {
58+
UIManager.setLookAndFeel(laf.getClassName());
59+
} catch (UnsupportedLookAndFeelException ignored) {
60+
System.out.println("Unsupported LAF: " + laf.getClassName());
61+
} catch (ClassNotFoundException | InstantiationException
62+
| IllegalAccessException e) {
63+
throw new RuntimeException(e);
64+
}
65+
}
66+
67+
private static void createUI() {
68+
frame = new JFrame(DisableFrameFromScrollBar.class.getName());
69+
bar = new JScrollBar();
70+
bar.getModel().addChangeListener(new DisableChangeListener(frame));
71+
frame.getContentPane().setLayout(new FlowLayout());
72+
frame.add(bar);
73+
74+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75+
frame.setSize(150, 150);
76+
frame.setLocationRelativeTo(null);
77+
frame.setVisible(true);
78+
}
79+
80+
public static void main(String[] args) throws Exception {
81+
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
82+
System.out.println("Testing LAF : " + laf.getClassName());
83+
Robot robot = new Robot();
84+
robot.setAutoDelay(100);
85+
try {
86+
SwingUtilities.invokeAndWait(() -> {
87+
setLookAndFeel(laf);
88+
createUI();
89+
});
90+
91+
robot.waitForIdle();
92+
robot.delay(1000);
93+
Point point = getClickPoint();
94+
robot.mouseMove(point.x, point.y);
95+
robot.waitForIdle();
96+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
97+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
98+
99+
SwingUtilities.invokeAndWait(() -> {
100+
oldValue = bar.getValue();
101+
bar.addAdjustmentListener(new AdjustmentListener() {
102+
public void adjustmentValueChanged(AdjustmentEvent e) {
103+
int curValue = e.getValue();
104+
int extent = bar.getMaximum() - bar.getVisibleAmount();
105+
if (curValue < extent && curValue != oldValue) {
106+
oldValue = curValue;
107+
isAdjusting = true;
108+
} else {
109+
doCheck = true;
110+
isAdjusting = false;
111+
}
112+
}
113+
});
114+
});
115+
do {
116+
Thread.sleep(200);
117+
} while (isAdjusting && !doCheck);
118+
if (bar.getValue() == (bar.getMaximum() - bar.getVisibleAmount())) {
119+
throw new RuntimeException("ScrollBar didn't disable timer");
120+
}
121+
} finally {
122+
SwingUtilities.invokeAndWait(() -> {
123+
if (frame != null) {
124+
frame.dispose();
125+
}
126+
});
127+
}
128+
}
129+
}
130+
131+
private static Point getClickPoint() throws Exception {
132+
final Point[] result = new Point[1];
133+
134+
SwingUtilities.invokeAndWait(new Runnable() {
135+
136+
@Override
137+
public void run() {
138+
Point p = bar.getLocationOnScreen();
139+
Rectangle rect = bar.getBounds();
140+
result[0] = new Point((int) (p.x + rect.width / 2),
141+
(int) (p.y + rect.height - 10));
142+
}
143+
});
144+
145+
return result[0];
146+
147+
}
148+
149+
public static class DisableChangeListener implements ChangeListener {
150+
private final JFrame m_frame;
151+
private boolean m_done;
152+
153+
public DisableChangeListener(JFrame p_frame) {
154+
m_frame = p_frame;
155+
}
156+
157+
public void stateChanged(ChangeEvent p_e) {
158+
if (!m_done) {
159+
m_frame.setEnabled(false);
160+
Thread t = new Thread(new Enabler(m_frame));
161+
t.start();
162+
m_done = true;
163+
}
164+
}
165+
}
166+
167+
public static class Enabler implements Runnable {
168+
private JFrame m_frame;
169+
170+
Enabler(JFrame p_frame) {
171+
m_frame = p_frame;
172+
}
173+
174+
public void run() {
175+
try {
176+
Thread.sleep(1000);
177+
}
178+
catch (InterruptedException e) {
179+
e.printStackTrace();
180+
}
181+
m_frame.setEnabled(true);
182+
}
183+
}
184+
}
185+

0 commit comments

Comments
 (0)
Please sign in to comment.