Skip to content

Commit d5cf18e

Browse files
author
Tejesh R
committedDec 8, 2022
8296198: JFileChooser throws InternalError java.lang.InternalError with Windows shortcuts
Reviewed-by: serb, abhiscxk
1 parent 74f346b commit d5cf18e

File tree

2 files changed

+234
-13
lines changed

2 files changed

+234
-13
lines changed
 

‎src/java.desktop/share/classes/javax/swing/plaf/basic/BasicFileChooserUI.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -1408,22 +1408,25 @@ public void actionPerformed(ActionEvent e) {
14081408
private void changeDirectory(File dir) {
14091409
JFileChooser fc = getFileChooser();
14101410
// Traverse shortcuts on Windows
1411-
if (dir != null && FilePane.usesShellFolder(fc)) {
1411+
if (dir != null) {
14121412
try {
1413-
ShellFolder shellFolder = ShellFolder.getShellFolder(dir);
1414-
1415-
if (shellFolder.isLink()) {
1416-
File linkedTo = shellFolder.getLinkLocation();
1417-
1418-
// If linkedTo is null we try to use dir
1419-
if (linkedTo != null) {
1420-
if (fc.isTraversable(linkedTo)) {
1421-
dir = linkedTo;
1422-
} else {
1423-
return;
1413+
File linkedTo = null;
1414+
if (FilePane.usesShellFolder(fc)) {
1415+
ShellFolder shellFolder = ShellFolder.getShellFolder(dir);
1416+
if (shellFolder.isLink()) {
1417+
linkedTo = shellFolder.getLinkLocation();
1418+
if (linkedTo == null) {
1419+
dir = shellFolder;
14241420
}
1421+
}
1422+
} else if ( fc.getFileSystemView().isLink(dir)){
1423+
linkedTo = fc.getFileSystemView().getLinkLocation(dir);
1424+
}
1425+
if (linkedTo != null) {
1426+
if (fc.isTraversable(linkedTo)) {
1427+
dir = linkedTo;
14251428
} else {
1426-
dir = shellFolder;
1429+
return;
14271430
}
14281431
}
14291432
} catch (FileNotFoundException ex) {
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+
import java.awt.BorderLayout;
25+
import java.io.File;
26+
import java.io.FileNotFoundException;
27+
import java.io.IOException;
28+
import java.lang.reflect.InvocationTargetException;
29+
30+
import javax.swing.JFrame;
31+
import javax.swing.JFileChooser;
32+
import javax.swing.SwingUtilities;
33+
import javax.swing.Icon;
34+
import javax.swing.WindowConstants;
35+
import javax.swing.filechooser.FileSystemView;
36+
37+
/*
38+
* @test
39+
* @bug 8296198
40+
* @key headful
41+
* @requires (os.family == "windows")
42+
* @library /java/awt/regtesthelpers
43+
* @build PassFailJFrame
44+
* @summary Test to check if the Link to a folder is traversable with custom
45+
* FileSystemView is valid on ValueChanged property listener.
46+
* @run main/manual CustomFSVLinkTest
47+
*/
48+
public class CustomFSVLinkTest {
49+
static JFrame frame;
50+
static JFileChooser jfc;
51+
52+
static PassFailJFrame passFailJFrame;
53+
54+
public static void main(String[] args) throws Exception {
55+
SwingUtilities.invokeAndWait(new Runnable() {
56+
public void run() {
57+
try {
58+
initialize();
59+
} catch (InterruptedException | InvocationTargetException e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
});
64+
passFailJFrame.awaitAndCheck();
65+
}
66+
67+
static void initialize() throws InterruptedException, InvocationTargetException {
68+
//Initialize the components
69+
final String INSTRUCTIONS = """
70+
Instructions to Test:
71+
1. Create a link to a any valid folder.
72+
2. Navigate to the linked folder through the link created
73+
(From FileChooser).
74+
3. If "link" can't be traversed or if its absolute path is null
75+
click FAIL. If "link" can be traversed then click PASS.
76+
""";
77+
frame = new JFrame("JFileChooser Link test");
78+
jfc = new JFileChooser(new MyFileSystemView());
79+
passFailJFrame = new PassFailJFrame("Test Instructions", INSTRUCTIONS, 5L, 8, 40);
80+
81+
PassFailJFrame.addTestWindow(frame);
82+
PassFailJFrame.positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL);
83+
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
84+
jfc.setDialogType(JFileChooser.CUSTOM_DIALOG);
85+
86+
frame.add(jfc, BorderLayout.CENTER);
87+
frame.pack();
88+
frame.setVisible(true);
89+
}
90+
91+
private static class MyFileSystemView extends FileSystemView {
92+
FileSystemView delegate;
93+
94+
MyFileSystemView() {
95+
delegate = FileSystemView.getFileSystemView();
96+
}
97+
98+
@Override
99+
public File createNewFolder(File containingDir) throws IOException {
100+
return delegate.createNewFolder(containingDir);
101+
}
102+
103+
@Override
104+
public boolean isRoot(File f) {
105+
return delegate.isRoot(f);
106+
}
107+
108+
@Override
109+
public Boolean isTraversable(File f) {
110+
return delegate.isTraversable(f);
111+
}
112+
113+
@Override
114+
public String getSystemDisplayName(File f) {
115+
return delegate.getSystemDisplayName(f);
116+
}
117+
118+
@Override
119+
public String getSystemTypeDescription(File f) {
120+
return delegate.getSystemTypeDescription(f);
121+
}
122+
123+
@Override
124+
public Icon getSystemIcon(File f) {
125+
return delegate.getSystemIcon(f);
126+
}
127+
128+
@Override
129+
public boolean isParent(File folder, File file) {
130+
return delegate.isParent(folder, file);
131+
}
132+
133+
@Override
134+
public File getChild(File parent, String fileName) {
135+
return delegate.getChild(parent, fileName);
136+
}
137+
138+
@Override
139+
public boolean isFileSystem(File f) {
140+
return delegate.isFileSystem(f);
141+
}
142+
143+
@Override
144+
public boolean isHiddenFile(File f) {
145+
return delegate.isHiddenFile(f);
146+
}
147+
148+
@Override
149+
public boolean isFileSystemRoot(File dir) {
150+
return delegate.isFileSystemRoot(dir);
151+
}
152+
153+
@Override
154+
public boolean isDrive(File dir) {
155+
return delegate.isDrive(dir);
156+
}
157+
158+
@Override
159+
public boolean isFloppyDrive(File dir) {
160+
return delegate.isFloppyDrive(dir);
161+
}
162+
163+
@Override
164+
public boolean isComputerNode(File dir) {
165+
return delegate.isComputerNode(dir);
166+
}
167+
168+
@Override
169+
public File[] getRoots() {
170+
return delegate.getRoots();
171+
}
172+
173+
@Override
174+
public File getHomeDirectory() {
175+
return delegate.getHomeDirectory();
176+
}
177+
178+
@Override
179+
public File getDefaultDirectory() {
180+
return delegate.getDefaultDirectory();
181+
}
182+
183+
@Override
184+
public File createFileObject(File dir, String filename) {
185+
return delegate.createFileObject(dir, filename);
186+
}
187+
188+
@Override
189+
public File createFileObject(String path) {
190+
return delegate.createFileObject(path);
191+
}
192+
193+
@Override
194+
public File[] getFiles(File dir, boolean useFileHiding) {
195+
return delegate.getFiles(dir, useFileHiding);
196+
}
197+
198+
@Override
199+
public File getParentDirectory(File dir) {
200+
return delegate.getParentDirectory(dir);
201+
}
202+
203+
@Override
204+
public File[] getChooserComboBoxFiles() {
205+
return delegate.getChooserComboBoxFiles();
206+
}
207+
208+
@Override
209+
public boolean isLink(File file) {
210+
return delegate.isLink(file);
211+
}
212+
213+
@Override
214+
public File getLinkLocation(File file) throws FileNotFoundException {
215+
return delegate.getLinkLocation(file);
216+
}
217+
}
218+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 8, 2022

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