Skip to content

Commit 9d0009e

Browse files
kumarabhi006jayathirthrao
authored andcommitted
6777156: GTK L&F: JFileChooser can jump beyond root directory in combobox and selection textarea.
Reviewed-by: jdv, tr, psadhukhan
1 parent 3ebe5ad commit 9d0009e

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, 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
@@ -980,6 +980,26 @@ protected void sort(Vector<? extends File> v) {
980980
v.sort(Comparator.comparing(fsv::getSystemDisplayName));
981981
}
982982
}
983+
984+
@Override
985+
public Vector<File> getDirectories() {
986+
Vector<File> files = super.getDirectories();
987+
988+
/*
989+
* Delete the "/.." file entry from file chooser directory list in
990+
* GTK LAF if current directory is root and files vector contains
991+
* "/.." entry.
992+
*
993+
* It is not possible to go beyond root directory.
994+
*/
995+
File crntDir = getFileChooser().getCurrentDirectory();
996+
FileSystemView fsv = getFileChooser().getFileSystemView();
997+
if (crntDir != null && fsv.isFileSystemRoot(crntDir) &&
998+
files.contains(new File("/.."))) {
999+
files.removeElementAt(0);
1000+
}
1001+
return files;
1002+
}
9831003
}
9841004

9851005
@SuppressWarnings("serial") // Superclass is not serializable across versions
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 6777156
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @requires (os.family == "linux")
30+
* @summary Verifies if user is not able to select "../" beyond
31+
* root file system.
32+
* @run main/manual TestFileChooserDirectorySelection
33+
*/
34+
35+
import javax.swing.JFileChooser;
36+
import javax.swing.JFrame;
37+
import javax.swing.SwingUtilities;
38+
import javax.swing.UIManager;
39+
40+
public class TestFileChooserDirectorySelection {
41+
private static JFrame frame;
42+
private static final String INSTRUCTIONS =
43+
"Double click on the \"../\" entry from directory list.\n\n" +
44+
"Repeat the same process till the current directory is root " +
45+
"i.e \" / \" .\n\n" +
46+
"If \" ../ \" option is not available at root directory" +
47+
", press Pass else Fail.";
48+
49+
public static void main(String[] args) throws Exception {
50+
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
51+
PassFailJFrame passFailJFrame = new PassFailJFrame(
52+
"JFileChooser Test Instructions", INSTRUCTIONS, 5, 8, 35);
53+
try {
54+
SwingUtilities.invokeAndWait(
55+
TestFileChooserDirectorySelection::createAndShowUI);
56+
passFailJFrame.awaitAndCheck();
57+
} finally {
58+
SwingUtilities.invokeAndWait(() -> {
59+
if (frame != null) {
60+
frame.dispose();
61+
}
62+
});
63+
}
64+
}
65+
private static void createAndShowUI() {
66+
frame = new JFrame("Test File Chooser Directory Selection");
67+
JFileChooser fileChooser = new JFileChooser();
68+
fileChooser.setControlButtonsAreShown(false);
69+
PassFailJFrame.addTestWindow(frame);
70+
PassFailJFrame.positionTestWindow(
71+
frame, PassFailJFrame.Position.HORIZONTAL);
72+
frame.add(fileChooser);
73+
frame.setSize(500, 500);
74+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75+
frame.setVisible(true);
76+
}
77+
}

0 commit comments

Comments
 (0)