|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
26 | 26 | package javax.swing.filechooser; |
27 | 27 |
|
28 | 28 | import java.awt.Image; |
| 29 | +import java.awt.image.AbstractMultiResolutionImage; |
29 | 30 | import java.beans.PropertyChangeListener; |
30 | 31 | import java.io.File; |
31 | 32 | import java.io.FileNotFoundException; |
@@ -225,7 +226,7 @@ public String getSystemTypeDescription(File f) { |
225 | 226 | * Icon for a file, directory, or folder as it would be displayed in |
226 | 227 | * a system file browser. Example from Windows: the "M:\" directory |
227 | 228 | * displays a CD-ROM icon. |
228 | | - * |
| 229 | + * <p> |
229 | 230 | * The default implementation gets information from the ShellFolder class. |
230 | 231 | * |
231 | 232 | * @param f a <code>File</code> object |
@@ -255,6 +256,67 @@ public Icon getSystemIcon(File f) { |
255 | 256 | } |
256 | 257 | } |
257 | 258 |
|
| 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 | + |
258 | 320 | /** |
259 | 321 | * On Windows, a file can appear in multiple folders, other than its |
260 | 322 | * parent directory in the filesystem. Folder could for example be the |
|
0 commit comments