Skip to content

Commit

Permalink
6318027: BasicScrollBarUI does not disable timer when enclosing frame…
Browse files Browse the repository at this point in the history
… is disabled.

Reviewed-by: abhiscxk, tr
  • Loading branch information
prsadhuk committed Aug 21, 2024
1 parent 88ccbb6 commit cafb3dc
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 13 deletions.
66 changes: 54 additions & 12 deletions src/java.desktop/macosx/classes/com/apple/laf/AquaScrollBarUI.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,19 +25,46 @@

package com.apple.laf;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;

import javax.swing.*;
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 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 apple.laf.*;
import apple.laf.JRSUIConstants.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.ScrollBarUI;

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;

Expand Down Expand Up @@ -527,6 +554,21 @@ void setScrollByBlock(final boolean block) {
}

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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -48,6 +48,7 @@
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;
Expand Down Expand Up @@ -1608,6 +1609,25 @@ public ScrollListener(int dir, boolean block) {

/** {@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
Expand Down
185 changes: 185 additions & 0 deletions test/jdk/javax/swing/JScrollBar/DisableFrameFromScrollBar.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit cafb3dc

Please sign in to comment.