4365952: Cannot disable JFileChooser

Reviewed-by: prr, abhiscxk
This commit is contained in:
Tejesh R 2023-11-06 13:29:48 +00:00
parent 2d4bbf4787
commit 96e6e670b5
9 changed files with 216 additions and 2 deletions
src/java.desktop
macosx/classes/com/apple/laf
share/classes
windows/classes/com/sun/java/swing/plaf/windows
test/jdk/javax/swing/JFileChooser

@ -744,6 +744,10 @@ public class AquaFileChooserUI extends FileChooserUI {
public void mouseClicked(final MouseEvent e) {
if (e.getClickCount() != 2) return;
if (!getFileChooser().isEnabled()) {
return;
}
final int index = list.locationToIndex(e.getPoint());
if (index < 0) return;
@ -1500,6 +1504,9 @@ public class AquaFileChooserUI extends FileChooserUI {
// Instead of dragging, it selects which one to sort by
public void setDraggedColumn(final TableColumn aColumn) {
if (!getFileChooser().isEnabled()) {
return;
}
if (aColumn != null) {
final int colIndex = aColumn.getModelIndex();
if (colIndex != fSortColumn) {
@ -1840,6 +1847,10 @@ public class AquaFileChooserUI extends FileChooserUI {
// The autoscroller can generate drag events outside the Table's range.
if ((column == -1) || (row == -1)) { return; }
if (!getFileChooser().isEnabled()) {
return;
}
final File clickedFile = (File)(fFileList.getValueAt(row, 0));
// rdar://problem/3734130 -- don't populate the text field if this file isn't selectable in this mode.

@ -435,6 +435,11 @@ class GTKFileChooserUI extends SynthFileChooserUI {
}
public void mouseClicked(MouseEvent e) {
if (!getFileChooser().isEnabled()) {
return;
}
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
if (index >= 0) {

@ -2078,4 +2078,24 @@ public class JFileChooser extends JComponent implements Accessible {
}
}
}
/**
* {@inheritDoc}
* @param enabled {@inheritDoc}
*/
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
setEnabled(this, enabled);
}
private static void setEnabled(Container container, boolean enabled) {
for (Component component : container.getComponents()) {
component.setEnabled(enabled);
if (component instanceof Container) {
setEnabled((Container) component, enabled);
}
}
}
}

@ -643,6 +643,9 @@ public class BasicFileChooserUI extends FileChooserUI {
public void mouseClicked(MouseEvent evt) {
// Note: we can't depend on evt.getSource() because of backward
// compatibility
if (!getFileChooser().isEnabled()) {
return;
}
if (list != null &&
SwingUtilities.isLeftMouseButton(evt) &&
(evt.getClickCount()%2 == 0)) {

@ -2228,7 +2228,7 @@ public abstract class BasicLookAndFeel extends LookAndFeel implements Serializab
src = (JComponent)
((BasicSplitPaneDivider)c).getParent();
}
if(src != null) {
if(src != null && src.isEnabled()) {
JPopupMenu componentPopupMenu = src.getComponentPopupMenu();
if(componentPopupMenu != null) {
Point pt = src.getPopupLocation(me);

@ -998,7 +998,8 @@ public class BasicScrollPaneUI
//
public void mouseWheelMoved(MouseWheelEvent e) {
if (scrollpane.isWheelScrollingEnabled() &&
e.getWheelRotation() != 0) {
scrollpane.isEnabled() &&
e.getWheelRotation() != 0) {
JScrollBar toScroll = scrollpane.getVerticalScrollBar();
int direction = e.getWheelRotation() < 0 ? -1 : 1;
int orientation = SwingConstants.VERTICAL;

@ -1954,6 +1954,10 @@ public class FilePane extends JPanel implements PropertyChangeListener {
public void mouseClicked(MouseEvent evt) {
JComponent source = (JComponent)evt.getSource();
if (!source.isEnabled()) {
return;
}
int index;
if (source instanceof JList) {
index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint());

@ -371,6 +371,10 @@ public class WindowsFileChooserUI extends BasicFileChooserUI {
viewMenuButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (!getFileChooser().isEnabled()) {
return;
}
if (SwingUtilities.isLeftMouseButton(e) && !viewMenuButton.isSelected()) {
viewMenuButton.setSelected(true);
@ -380,6 +384,10 @@ public class WindowsFileChooserUI extends BasicFileChooserUI {
});
viewMenuButton.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (!getFileChooser().isEnabled()) {
return;
}
// Forbid keyboard actions if the button is not in rollover state
if (e.getKeyCode() == KeyEvent.VK_SPACE && viewMenuButton.getModel().isRollover()) {
viewMenuButton.setSelected(true);

@ -0,0 +1,162 @@
/*
* Copyright (c) 2023, 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.
*/
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.UnsupportedLookAndFeelException;
import java.util.function.Predicate;
/*
* @test
* @bug 4365952
* @key headful
* @summary Test to check if JFileChooser can be disabled
* @run main FileChooserDisableTest
*/
public class FileChooserDisableTest {
static JFrame frame;
static JFileChooser jfc;
static volatile Point movePoint;
static String buttonToolTip;
static volatile AbstractButton openBtn;
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if (laf.getClassName().toLowerCase().contains("motif")) {
continue;
}
if (laf.getClassName().toLowerCase().contains("gtk")) {
buttonToolTip = "Open selected file.";
} else {
buttonToolTip = "Open selected file";
}
System.out.println("Testing LAF : " + laf.getClassName());
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
try {
SwingUtilities.invokeAndWait(() -> {
initialize();
});
String defaultDirectory = jfc.getCurrentDirectory().toString();
robot.delay(1000);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
openBtn = clickOpenButton();
movePoint = openBtn.getLocationOnScreen();
});
Dimension btnSize = openBtn.getSize();
robot.mouseMove(movePoint.x + btnSize.width / 2, movePoint.y + btnSize.height / 2);
robot.delay(100);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
robot.waitForIdle();
String currentDirectory = jfc.getCurrentDirectory().toString();
if (!currentDirectory.equals(defaultDirectory)) {
throw new RuntimeException("File chooser disable failed");
}
System.out.println("Test Pass");
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
robot.delay(200);
robot.waitForIdle();
}
}
private static AbstractButton clickOpenButton() {
AbstractButton details = findOpenButton(jfc);
if (details == null) {
throw new Error("Didn't find 'Open Selected File' button in JFileChooser");
}
return details;
}
private static Component findComponent(final Container container,
final Predicate<Component> predicate) {
for (Component child : container.getComponents()) {
if (predicate.test(child)) {
return child;
}
if (child instanceof Container cont && cont.getComponentCount() > 0) {
Component result = findComponent(cont, predicate);
if (result != null) {
return result;
}
}
}
return null;
}
private static AbstractButton findOpenButton(final Container container) {
Component result = findComponent(container,
c -> c instanceof JButton button
&& buttonToolTip.equals(button.getToolTipText()));
return (AbstractButton) result;
}
static void initialize() {
frame = new JFrame("JFileChooser Disable test");
jfc = new JFileChooser();
jfc.setEnabled(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(jfc, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
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);
}
}
}