/* * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.filechooser; import java.awt.Image; import java.awt.image.AbstractMultiResolutionImage; import java.io.File; import java.io.FileNotFoundException; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.UIManager; import sun.awt.shell.ShellFolder; /** * SystemIcon is JFileChooser's gateway to the file system icons. */ public class SystemIcon { /** * Icon for a file, directory, or folder as it would be displayed in * a system file browser for the requested size. * *
*
* The default implementation gets information from the ShellFolder class.
* Whenever possible the icon returned will be a multi-resolution icon image
* with maximum quality icon provided by the system and the size stored
* reflecting the size requested by the user. That will allow nice scaling
* with differen magnification factors.
*
* @param f a File
object
* @param size width and height of the icon in pixels to be scaled
* (valid range: 1 to 256)
* @return An icon as it would be displayed by a native file chooser
* or null if invalid parameters are passed such as pointer to a
* non-existing file or unsupported icon size.
* @see JFileChooser#getIcon
* @see AbstractMultiResolutionImage
* @since 12
*/
static public Icon getSystemIcon(File f, int size) {
if (f == null) {
return null;
}
if (size > 256 || size < 1) {
return null;
}
ShellFolder sf;
try {
sf = ShellFolder.getShellFolder(f);
} catch (FileNotFoundException e) {
return null;
} catch (InternalError e) {
System.err.println("FileSystemView.getShellFolder: " +
e.getLocalizedMessage());
e.printStackTrace();
return null;
}
Image img = sf.getIcon(size);
if (img != null) {
return new ImageIcon(img, sf.getFolderType());
} else {
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
}
}
}