Skip to content

Commit 8338a9a

Browse files
author
duke
committed
Backport 917c1546f353c2814de8465d1dfad66b01561f12
1 parent b683857 commit 8338a9a

File tree

8 files changed

+189
-0
lines changed

8 files changed

+189
-0
lines changed

src/java.desktop/macosx/classes/com/apple/laf/AquaFileChooserUI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,9 @@ public Component getTableCellRendererComponent(final JTable list,
11881188
setText(fc.getName(file));
11891189
setIcon(fc.getIcon(file));
11901190
setEnabled(isSelectableInList(file));
1191+
1192+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
1193+
11911194
return this;
11921195
}
11931196
}
@@ -1253,6 +1256,9 @@ public Component getListCellRendererComponent(final JList<? extends File> list,
12531256
final JFileChooser chooser = getFileChooser();
12541257
setText(chooser.getName(directory));
12551258
setIcon(chooser.getIcon(directory));
1259+
1260+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
1261+
12561262
return this;
12571263
}
12581264
};

src/java.desktop/share/classes/com/sun/java/swing/plaf/motif/MotifFileChooserUI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
664664
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
665665
setText(getFileChooser().getName((File) value));
666666
setInheritsPopupMenu(true);
667+
668+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
669+
667670
return this;
668671
}
669672
}
@@ -676,6 +679,9 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
676679
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
677680
setText(getFileChooser().getName((File) value));
678681
setInheritsPopupMenu(true);
682+
683+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
684+
679685
return this;
680686
}
681687
}

src/java.desktop/share/classes/javax/swing/JFileChooser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ protected void setup(FileSystemView view) {
399399
setFileFilter(getAcceptAllFileFilter());
400400
}
401401
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
402+
putClientProperty("html.disable", true);
402403
}
403404

404405
private void installHierarchyListener() {

src/java.desktop/share/classes/javax/swing/plaf/metal/MetalFileChooserUI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,8 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
951951
ii.depth = directoryComboBoxModel.getDepth(index);
952952
setIcon(ii);
953953

954+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
955+
954956
return this;
955957
}
956958
}

src/java.desktop/share/classes/sun/swing/FilePane.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,8 @@ public Component getTableCellRendererComponent(JTable table, Object value,
12361236

12371237
setText(text);
12381238

1239+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
1240+
12391241
return this;
12401242
}
12411243

@@ -1634,6 +1636,8 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
16341636
}
16351637
}
16361638

1639+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
1640+
16371641
return this;
16381642
}
16391643
}

src/java.desktop/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ public Component getListCellRendererComponent(JList<? extends File> list, File v
690690
ii.depth = directoryComboBoxModel.getDepth(index);
691691
label.setIcon(ii);
692692

693+
label.putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
694+
693695
return label;
694696
}
695697
}

src/java.desktop/windows/classes/com/sun/java/swing/plaf/windows/WindowsFileChooserUI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,8 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
10491049
ii.depth = directoryComboBoxModel.getDepth(index);
10501050
setIcon(ii);
10511051

1052+
putClientProperty("html.disable", getFileChooser().getClientProperty("html.disable"));
1053+
10521054
return this;
10531055
}
10541056
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2025, 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.io.File;
25+
import java.util.List;
26+
import javax.swing.Icon;
27+
import javax.swing.JFileChooser;
28+
import javax.swing.JFrame;
29+
import javax.swing.SwingUtilities;
30+
import javax.swing.UIManager;
31+
import javax.swing.filechooser.FileSystemView;
32+
33+
/*
34+
* @test id=metal
35+
* @bug 8139228
36+
* @summary JFileChooser should not render Directory names in HTML format
37+
* @library /java/awt/regtesthelpers
38+
* @build PassFailJFrame
39+
* @run main/manual HTMLFileName metal
40+
*/
41+
42+
/*
43+
* @test id=system
44+
* @bug 8139228
45+
* @summary JFileChooser should not render Directory names in HTML format
46+
* @library /java/awt/regtesthelpers
47+
* @build PassFailJFrame
48+
* @run main/manual HTMLFileName system
49+
*/
50+
51+
public class HTMLFileName {
52+
private static final String INSTRUCTIONS = """
53+
<html>
54+
<ol>
55+
<li>FileChooser shows up a virtual directory and file with name
56+
<html><h1 color=#ff00ff><font face="Comic Sans MS">Swing Rocks!.
57+
<li>On "HTML disabled" frame :
58+
<ol>
59+
<li>Verify that the folder and file name must be plain text.
60+
<li>If the name in file pane window and also in directory
61+
ComboBox remains in plain text, then press <b>Pass</b>.
62+
If it appears to be in HTML format with Pink color as
63+
shown, then press <b>Fail</b>.
64+
</ol>
65+
66+
<li>On "HTML enabled" frame :
67+
<ol>
68+
<li>Verify that the folder and file name remains in HTML
69+
format with name "Swing Rocks!" pink in color as shown.
70+
<li>If the name in file pane window and also in directory
71+
ComboBox remains in HTML format string, then press <b>Pass</b>.
72+
If it appears to be in plain text, then press <b>Fail</b>.
73+
</ol>
74+
</ol>
75+
</html>
76+
""";
77+
78+
public static void main(String[] args) throws Exception {
79+
if (args.length < 1) {
80+
throw new IllegalArgumentException("Look-and-Feel keyword is required");
81+
}
82+
83+
final String lafClassName;
84+
switch (args[0]) {
85+
case "metal" -> lafClassName = UIManager.getCrossPlatformLookAndFeelClassName();
86+
case "system" -> lafClassName = UIManager.getSystemLookAndFeelClassName();
87+
default -> throw new IllegalArgumentException("Unsupported Look-and-Feel keyword: " + args[0]);
88+
}
89+
90+
SwingUtilities.invokeAndWait(() -> {
91+
try {
92+
UIManager.setLookAndFeel(lafClassName);
93+
} catch (Exception e) {
94+
throw new RuntimeException(e);
95+
}
96+
});
97+
98+
System.out.println("Test for LookAndFeel " + lafClassName);
99+
PassFailJFrame.builder()
100+
.instructions(INSTRUCTIONS)
101+
.columns(45)
102+
.testUI(HTMLFileName::initialize)
103+
.positionTestUIBottomRowCentered()
104+
.build()
105+
.awaitAndCheck();
106+
System.out.println("Test passed for LookAndFeel " + lafClassName);
107+
}
108+
109+
private static List<JFrame> initialize() {
110+
return List.of(createFileChooser(true), createFileChooser(false));
111+
}
112+
113+
private static JFrame createFileChooser(boolean htmlEnabled) {
114+
JFileChooser jfc = new JFileChooser(new VirtualFileSystemView());
115+
jfc.putClientProperty("html.disable", htmlEnabled);
116+
jfc.setControlButtonsAreShown(false);
117+
118+
JFrame frame = new JFrame((htmlEnabled) ? "HTML enabled" : "HTML disabled");
119+
frame.add(jfc);
120+
frame.pack();
121+
return frame;
122+
}
123+
124+
private static class VirtualFileSystemView extends FileSystemView {
125+
@Override
126+
public File createNewFolder(File containingDir) {
127+
return null;
128+
}
129+
130+
@Override
131+
public File[] getRoots() {
132+
return new File[]{
133+
new File("/", "<html><h1 color=#ff00ff><font " +
134+
"face=\"Comic Sans MS\">Swing Rocks!"),
135+
new File("/", "virtualFile2.txt"),
136+
new File("/", "virtualFolder")
137+
};
138+
}
139+
140+
@Override
141+
public File getHomeDirectory() {
142+
return new File("/");
143+
}
144+
145+
@Override
146+
public File getDefaultDirectory() {
147+
return new File("/");
148+
}
149+
150+
@Override
151+
public File[] getFiles(File dir, boolean useFileHiding) {
152+
// Simulate a virtual folder structure
153+
return new File[]{
154+
new File("/", "<html><h1 color=#ff00ff><font " +
155+
"face=\"Comic Sans MS\">Swing Rocks!"),
156+
new File(dir, "virtualFile2.txt"),
157+
new File(dir, "virtualFolder")
158+
};
159+
}
160+
161+
@Override
162+
public Icon getSystemIcon(File f) {
163+
return null;
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)