6318027: BasicScrollBarUI does not disable timer when enclosing frame is disabled.

Reviewed-by: abhiscxk, tr
This commit is contained in:
Prasanta Sadhukhan 2024-08-21 07:04:05 +00:00
parent 88ccbb6091
commit cafb3dc491
3 changed files with 258 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,19 +25,46 @@
package com.apple.laf;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.awt.Adjustable;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BoundedRangeModel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.LookAndFeel;
import javax.swing.Timer;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.ScrollBarUI;
import apple.laf.*;
import apple.laf.JRSUIConstants.*;
import apple.laf.JRSUIStateFactory;
import apple.laf.JRSUIConstants.Hit;
import apple.laf.JRSUIConstants.NothingToScroll;
import apple.laf.JRSUIConstants.Orientation;
import apple.laf.JRSUIConstants.ScrollBarHit;
import apple.laf.JRSUIConstants.ScrollBarPart;
import apple.laf.JRSUIConstants.ShowArrows;
import apple.laf.JRSUIConstants.State;
import apple.laf.JRSUIState.ScrollBarState;
import apple.laf.JRSUIUtils;
import com.apple.laf.AquaUtils.RecyclableSingleton;
@ -527,6 +554,21 @@ public class AquaScrollBarUI extends ScrollBarUI {
}
public void actionPerformed(final ActionEvent e) {
Component parent = fScrollBar.getParent();
do {
if (parent instanceof JFrame par) {
if (!par.isEnabled()) {
((Timer)e.getSource()).stop();
fScrollBar.setValueIsAdjusting(false);
return;
}
break;
} else {
if (parent != null) {
parent = parent.getParent();
}
}
} while (parent != null);
if (fUseBlockIncrement) {
Hit newPart = getPartHit(fTrackListener.fCurrentMouseX, fTrackListener.fCurrentMouseY);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -48,6 +48,7 @@ import javax.swing.BoundedRangeModel;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
@ -1608,6 +1609,25 @@ public class BasicScrollBarUI
/** {@inheritDoc} */
public void actionPerformed(ActionEvent e) {
// If frame is disabled and timer is started in mousePressed
// and mouseReleased is not called, then timer will not be stopped
// Stop the timer if frame is disabled
Component parent = scrollbar.getParent();
do {
if (parent instanceof JFrame par) {
if (!par.isEnabled()) {
((Timer)e.getSource()).stop();
buttonListener.handledEvent = false;
scrollbar.setValueIsAdjusting(false);
return;
}
break;
} else {
if (parent != null) {
parent = parent.getParent();
}
}
} while (parent != null);
if(useBlockIncrement) {
scrollByBlock(direction);
// Stop scrolling if the thumb catches up with the mouse

View File

@ -0,0 +1,185 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6318027
* @key headful
* @summary Verifies BasicScrollBarUI disables timer when enclosing frame is disabled
* @run main DisableFrameFromScrollBar
*/
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.InputEvent;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DisableFrameFromScrollBar {
private static JFrame frame;
private static JScrollBar bar;
private static int oldValue;
private static volatile boolean doCheck;
private static volatile boolean isAdjusting;
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
UIManager.setLookAndFeel(laf.getClassName());
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Unsupported LAF: " + laf.getClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static void createUI() {
frame = new JFrame(DisableFrameFromScrollBar.class.getName());
bar = new JScrollBar();
bar.getModel().addChangeListener(new DisableChangeListener(frame));
frame.getContentPane().setLayout(new FlowLayout());
frame.add(bar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(150, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
System.out.println("Testing LAF : " + laf.getClassName());
Robot robot = new Robot();
robot.setAutoDelay(100);
try {
SwingUtilities.invokeAndWait(() -> {
setLookAndFeel(laf);
createUI();
});
robot.waitForIdle();
robot.delay(1000);
Point point = getClickPoint();
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
SwingUtilities.invokeAndWait(() -> {
oldValue = bar.getValue();
bar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
int curValue = e.getValue();
int extent = bar.getMaximum() - bar.getVisibleAmount();
if (curValue < extent && curValue != oldValue) {
oldValue = curValue;
isAdjusting = true;
} else {
doCheck = true;
isAdjusting = false;
}
}
});
});
do {
Thread.sleep(200);
} while (isAdjusting && !doCheck);
if (bar.getValue() == (bar.getMaximum() - bar.getVisibleAmount())) {
throw new RuntimeException("ScrollBar didn't disable timer");
}
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
}
private static Point getClickPoint() throws Exception {
final Point[] result = new Point[1];
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
Point p = bar.getLocationOnScreen();
Rectangle rect = bar.getBounds();
result[0] = new Point((int) (p.x + rect.width / 2),
(int) (p.y + rect.height - 10));
}
});
return result[0];
}
public static class DisableChangeListener implements ChangeListener {
private final JFrame m_frame;
private boolean m_done;
public DisableChangeListener(JFrame p_frame) {
m_frame = p_frame;
}
public void stateChanged(ChangeEvent p_e) {
if (!m_done) {
m_frame.setEnabled(false);
Thread t = new Thread(new Enabler(m_frame));
t.start();
m_done = true;
}
}
}
public static class Enabler implements Runnable {
private JFrame m_frame;
Enabler(JFrame p_frame) {
m_frame = p_frame;
}
public void run() {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
m_frame.setEnabled(true);
}
}
}