/* * Copyright (c) 2019, 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 javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.UIManager; import sun.awt.shell.ShellFolder; import java.io.File; import java.io.FileNotFoundException; /** * SystemIcon is JFileChooser's gateway to the * file system icons. * *
*
* @author shashidhara
*/
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.
*
* @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
* @see JFileChooser#getIcon
* @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: f="+f);
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");
}
}
}