Skip to content

Commit 4a68210

Browse files
Abhishek KumarTejesh R
Abhishek Kumar
authored and
Tejesh R
committedNov 10, 2022
6972078: Can not select single directory with GTKLookAndFeel
Reviewed-by: psadhukhan, tr
1 parent 4465361 commit 4a68210

File tree

2 files changed

+219
-1
lines changed

2 files changed

+219
-1
lines changed
 

‎src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public void valueChanged(ListSelectionEvent e) {
488488
if (objects.length == 1
489489
&& ((File)objects[0]).isDirectory()
490490
&& chooser.isTraversable(((File)objects[0]))
491-
&& (chooser.getFileSelectionMode() != JFileChooser.DIRECTORIES_ONLY
491+
&& (chooser.getFileSelectionMode() == JFileChooser.FILES_ONLY
492492
|| !chooser.getFileSystemView().isFileSystem(((File)objects[0])))) {
493493
setDirectorySelected(true);
494494
setDirectory(((File)objects[0]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Copyright (c) 2022, 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 6972078
27+
* @key headful
28+
* @requires (os.family == "linux")
29+
* @summary Verifies if user is able to select single directory if multi selection
30+
* is enabled for JFileChooser.
31+
* @run main TestFileChooserSingleDirectorySelection
32+
*/
33+
34+
import java.io.File;
35+
import java.awt.BorderLayout;
36+
import java.awt.event.ActionEvent;
37+
import java.awt.event.ActionListener;
38+
import java.awt.event.InputEvent;
39+
import java.awt.Point;
40+
import java.awt.Robot;
41+
import javax.swing.JButton;
42+
import javax.swing.JFileChooser;
43+
import javax.swing.JFrame;
44+
import javax.swing.SwingUtilities;
45+
import javax.swing.UIManager;
46+
import javax.swing.UnsupportedLookAndFeelException;
47+
48+
public class TestFileChooserSingleDirectorySelection {
49+
private static JFrame frame;
50+
private static JFileChooser fileChooser;
51+
private static JButton getSelectedFilesButton;
52+
private static Robot robot;
53+
private static boolean passed;
54+
private static File[] testDir;
55+
private static File[] tempFile;
56+
57+
public static void main(String[] args) throws Exception {
58+
System.setProperty("sun.java2d.uiScale", "1.0");
59+
robot = new Robot();
60+
robot.setAutoDelay(100);
61+
62+
try {
63+
// create test directory
64+
String tmpDir = System.getProperty("user.home");
65+
testDir = new File[1];
66+
testDir[0] = new File(tmpDir, "testDir");
67+
if (!testDir[0].exists())
68+
testDir[0].mkdir();
69+
testDir[0].deleteOnExit();
70+
71+
// create temporary files inside testDir
72+
tempFile = new File[5];
73+
for (int i = 0; i < 5; ++i) {
74+
tempFile[i] = File.createTempFile("temp", ".txt",
75+
new File(testDir[0].getAbsolutePath()));
76+
tempFile[i].deleteOnExit();
77+
}
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
for (UIManager.LookAndFeelInfo laf :
82+
UIManager.getInstalledLookAndFeels()) {
83+
System.out.println("Testing LAF: " + laf.getClassName());
84+
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
85+
checkFileOnlyTest(laf);
86+
checkDirectoriesOnlyTest(laf);
87+
checkFilesAndDirectoriesTest(laf);
88+
System.out.println("Passed");
89+
}
90+
}
91+
92+
private static void checkFileOnlyTest(UIManager.LookAndFeelInfo laf)
93+
throws Exception {
94+
System.out.println("Testing File Only mode");
95+
try {
96+
SwingUtilities.invokeAndWait(() -> {
97+
createAndShowUI();
98+
fileChooser.setCurrentDirectory(testDir[0]);
99+
});
100+
101+
robot.waitForIdle();
102+
robot.delay(1000);
103+
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
104+
doTesting(laf, 230);
105+
} finally {
106+
SwingUtilities.invokeAndWait(() -> {
107+
if (frame != null) {
108+
frame.dispose();
109+
}
110+
});
111+
}
112+
}
113+
114+
private static void checkDirectoriesOnlyTest(UIManager.LookAndFeelInfo laf)
115+
throws Exception {
116+
System.out.println("Testing Directories Only mode");
117+
try {
118+
SwingUtilities.invokeAndWait(() -> {
119+
createAndShowUI();
120+
});
121+
robot.waitForIdle();
122+
robot.delay(1000);
123+
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
124+
doTesting(laf, 50);
125+
} finally {
126+
SwingUtilities.invokeAndWait(() -> {
127+
if (frame != null) {
128+
frame.dispose();
129+
}
130+
});
131+
}
132+
}
133+
134+
private static void checkFilesAndDirectoriesTest(UIManager.LookAndFeelInfo laf)
135+
throws Exception {
136+
System.out.println("Testing Files and Directories mode");
137+
try {
138+
SwingUtilities.invokeAndWait(() -> {
139+
createAndShowUI();
140+
});
141+
robot.waitForIdle();
142+
robot.delay(1000);
143+
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
144+
doTesting(laf, 50);
145+
} finally {
146+
SwingUtilities.invokeAndWait(() -> {
147+
if (frame != null) {
148+
frame.dispose();
149+
}
150+
});
151+
}
152+
}
153+
154+
private static void createAndShowUI() {
155+
frame = new JFrame("Test File Chooser Single Directory Selection");
156+
frame.getContentPane().setLayout(new BorderLayout());
157+
fileChooser = new JFileChooser("user.home");
158+
fileChooser.setMultiSelectionEnabled(true);
159+
fileChooser.setControlButtonsAreShown(false);
160+
161+
getSelectedFilesButton = new JButton();
162+
getSelectedFilesButton.setText("Print selected Files");
163+
getSelectedFilesButton.addActionListener(new ActionListener() {
164+
public void actionPerformed(ActionEvent evt) {
165+
passed = false;
166+
File files[] = fileChooser.getSelectedFiles();
167+
if (files.length != 0) {
168+
passed = true;
169+
}
170+
}
171+
});
172+
173+
frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
174+
frame.getContentPane().add(getSelectedFilesButton, BorderLayout.SOUTH);
175+
frame.pack();
176+
frame.setLocationRelativeTo(null);
177+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
178+
frame.setVisible(true);
179+
}
180+
181+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
182+
try {
183+
UIManager.setLookAndFeel(laf.getClassName());
184+
} catch (UnsupportedLookAndFeelException ignored) {
185+
System.out.println("Unsupported LAF: " + laf.getClassName());
186+
} catch (ClassNotFoundException | InstantiationException
187+
| IllegalAccessException e) {
188+
throw new RuntimeException(e);
189+
}
190+
}
191+
192+
private static void doTesting(UIManager.LookAndFeelInfo laf, int xOffset) {
193+
Point frameLocation = fileChooser.getLocationOnScreen();
194+
int frameWidth = frame.getWidth();
195+
int frameHeight = frame.getHeight();
196+
197+
Point btnLocation = getSelectedFilesButton.getLocationOnScreen();
198+
int btnWidth = getSelectedFilesButton.getWidth();
199+
int btnHeight = getSelectedFilesButton.getHeight();
200+
clickMouse(frameLocation, 0, frameHeight, xOffset);
201+
clickMouse(btnLocation, btnWidth, btnHeight, 0);
202+
checkResult(laf);
203+
}
204+
205+
private static void clickMouse(Point point, int width, int height,
206+
int xOffset) {
207+
robot.mouseMove(point.x + width/2 + xOffset , point.y + height/3);
208+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
209+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
210+
robot.delay(100);
211+
}
212+
213+
private static void checkResult(UIManager.LookAndFeelInfo laf) {
214+
if (!passed)
215+
throw new RuntimeException("getSelectedFiles returned " +
216+
"empty array for LAF: "+laf.getClassName());
217+
}
218+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Nov 10, 2022

@openjdk-notifier[bot]
Please sign in to comment.