Skip to content

Commit 7f52c50

Browse files
author
Alexander Zuev
committed
8182043: Access to Windows Large Icons
Reviewed-by: aivanov, azvegint, prr
1 parent 8a31c07 commit 7f52c50

File tree

6 files changed

+369
-80
lines changed

6 files changed

+369
-80
lines changed

src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2021, 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
@@ -26,6 +26,7 @@
2626
package javax.swing.filechooser;
2727

2828
import java.awt.Image;
29+
import java.awt.image.AbstractMultiResolutionImage;
2930
import java.beans.PropertyChangeListener;
3031
import java.io.File;
3132
import java.io.FileNotFoundException;
@@ -225,7 +226,7 @@ public String getSystemTypeDescription(File f) {
225226
* Icon for a file, directory, or folder as it would be displayed in
226227
* a system file browser. Example from Windows: the "M:\" directory
227228
* displays a CD-ROM icon.
228-
*
229+
* <p>
229230
* The default implementation gets information from the ShellFolder class.
230231
*
231232
* @param f a <code>File</code> object
@@ -255,6 +256,67 @@ public Icon getSystemIcon(File f) {
255256
}
256257
}
257258

259+
/**
260+
* Returns an icon for a file, directory, or folder as it would be displayed
261+
* in a system file browser for the requested size.
262+
* <p>
263+
* Example: <pre>
264+
* FileSystemView fsv = FileSystemView.getFileSystemView();
265+
* Icon icon = fsv.getSystemIcon(new File("application.exe"), 64, 64);
266+
* JLabel label = new JLabel(icon);
267+
* </pre>
268+
*
269+
* @implSpec The available icons may be platform specific and so the
270+
* available sizes determined by the platform. Therefore an exact match
271+
* for the requested size may not be possible.
272+
*
273+
* The icon returned may be a multi-resolution icon image,
274+
* which allows better support for High DPI environments
275+
* with different scaling factors.
276+
*
277+
* @param f a {@code File} object for which the icon will be retrieved
278+
* @param width width of the icon in user coordinate system.
279+
* @param height height of the icon in user coordinate system.
280+
* @return an icon as it would be displayed by a native file chooser
281+
* or null for a non-existent or inaccessible file.
282+
* @throws IllegalArgumentException if an invalid parameter such
283+
* as a negative size or a null file reference is passed.
284+
* @see JFileChooser#getIcon
285+
* @see AbstractMultiResolutionImage
286+
* @see FileSystemView#getSystemIcon(File)
287+
* @since 17
288+
*/
289+
public Icon getSystemIcon(File f, int width, int height) {
290+
if (height < 1 || width < 1) {
291+
throw new IllegalArgumentException("Icon size can not be below 1");
292+
}
293+
294+
if (f == null) {
295+
throw new IllegalArgumentException("The file reference should not be null");
296+
}
297+
298+
if(!f.exists()) {
299+
return null;
300+
}
301+
302+
ShellFolder sf;
303+
304+
try {
305+
sf = ShellFolder.getShellFolder(f);
306+
} catch (FileNotFoundException e) {
307+
return null;
308+
}
309+
310+
Image img = sf.getIcon(width, height);
311+
312+
if (img != null) {
313+
return new ImageIcon(img, sf.getFolderType());
314+
} else {
315+
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon"
316+
: "FileView.fileIcon");
317+
}
318+
}
319+
258320
/**
259321
* On Windows, a file can appear in multiple folders, other than its
260322
* parent directory in the filesystem. Folder could for example be the

src/java.desktop/share/classes/sun/awt/shell/ShellFolder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ public Image getIcon(boolean getLargeIcon) {
207207
return null;
208208
}
209209

210+
/**
211+
* Returns the icon of the specified size used to display this shell folder.
212+
*
213+
* @param width width of the icon > 0
214+
* @param height height of the icon > 0
215+
* @return The icon of the specified size used to display this shell folder
216+
*/
217+
public Image getIcon(int width, int height) {
218+
return null;
219+
}
210220

211221
// Static
212222

0 commit comments

Comments
 (0)