--- /dev/null 2018-09-28 14:23:17.000000000 +0530 +++ new/src/java.desktop/share/classes/javax/swing/filechooser/SystemIcon.java 2018-09-28 14:23:14.837001900 +0530 @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2018, 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.awt.RenderingHints; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.io.FileNotFoundException; +import java.awt.Transparency; +import java.io.File; +import java.io.IOException; +import java.awt.image.MultiResolutionImage; +import java.awt.image.BaseMultiResolutionImage; + +/** + * SystemIcon is JFileChooser's gateway to the + * file system icons. + * + *
+ *
+ * @author shashidhara
+ */
+public class SystemIcon extends FileSystemView {
+
+ /**
+ * Store the reference to the file
+ */
+ protected final File file;
+
+ /**
+ * Create a reference to the file
+ */
+ public SystemIcon() {
+ this(null);
+ }
+
+ /**
+ * Stere the reference to the file
+ *
+ * @param f a File
object
+ */
+ public SystemIcon(File f) {
+ this.file = f;
+ }
+
+ /**
+ * Icon for a file, directory, or folder as it would be displayed in
+ * a system file browser. Example from Windows: the "M:\" directory
+ * displays a CD-ROM icon.
+ *
+ * The default implementation gets information from the ShellFolder class.
+ *
+ * @param f a File
object
+ * @return an icon as it would be displayed by a native file chooser
+ * @see JFileChooser#getIcon
+ * @since 12
+ */
+ public Icon getSystemIcon() {
+ if (this.file == null) {
+ return null;
+ }
+
+ return getSystemIcon(this.file);
+ }
+
+ /**
+ * Scaled icon for a file, directory, or folder as it would be displayed in
+ * a system file browser. Example from Windows: the "M:\" directory
+ * displays a CD-ROM icon.
+ *
+ * The default implementation gets information from the ShellFolder class.
+ *
+ * @param f a File
object
+ * @param width width of the icon in pixels to be scaled(valid range: 1 to 256)
+ * @param height 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
+ */
+ public Icon getSystemIcon(int width, int height) {
+ if (this.file == null) {
+ return null;
+ }
+
+ return getSystemIcon(this.file, width, height);
+ }
+
+ /**
+ * Scaled icon for a file, directory, or folder as it would be displayed in
+ * a system file browser. Example from Windows: the "M:\" directory
+ * displays a CD-ROM icon.
+ *
+ * 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
+ */
+ public Icon getSystemIcon(int size) {
+ if(this.file == null) {
+ return null;
+ }
+
+ return getSystemIcon(this.file, size, size);
+ }
+
+ private int getSize(File f, int width, int height) {
+ if (f == null) {
+ throw new IllegalArgumentException("File reference is null");
+ }
+
+ if ((width > 256 || width < 1) || (height > 256 || height < 1)) {
+ throw new IllegalArgumentException("Icon width and height must be within 1 and 256");
+ }
+
+ int size = Math.max(width, height);
+
+ return size;
+ }
+
+ /**
+ * Returns scaled icon for a file, directory, or folder as it would be displayed in
+ * a system file browser. Example from Windows: the "M:\" directory
+ * displays a CD-ROM icon.
+ *
+ * @param f a File
object
+ * @param width width of the icon in pixels(valid range: 1 to 256)
+ * @param height height of the icon in pixels(valid range: 1 to 256)
+ * @return an icon of the requested size(possibly scaled) as it would be displayed by a native file chooser
+ * @throws IllegalArgumentException if file reference is null
+ * @throws IllegalArgumentException if icon width or height is outside of range 1 to 256
+ * @see JFileChooser#getIcon
+ * @since 12
+ */
+ public Icon getSystemIcon(File f, int width, int height) {
+ int size = getSize(f, width, height);
+ Icon icon = getSystemIcon(f, size);
+ Image scaledImg = null;
+
+ // scale the icon to the required size
+ if (icon != null) {
+ scaledImg = scaleIconImage(icon, width, height);
+ }
+
+ if (scaledImg != null) {
+ return new ImageIcon(scaledImg);
+ }
+
+ return null;
+ }
+
+ /**
+ * Returns scaled icon for a file, directory, or folder as it would be displayed in
+ * a system file browser. Example from Windows: the "M:\" directory
+ * displays a CD-ROM icon.
+ *
+ * @param f a File
object
+ * @param width width of the icon in pixels(valid range: 1 to 256)
+ * @param height height of the icon in pixels(valid range: 1 to 256)
+ * @return an multi resolution image icons containing original icon and scaled icon
+ * @see JFileChooser#getIcon
+ * @since 12
+ */
+ public MultiResolutionImage getSystemIcons(File f, int width, int height) {
+ Image[] imageArray;
+ int size = getSize(f, width, height);
+ Icon icon = getSystemIcon(f, size);
+ Image scaledImg = null;
+
+ // scale the icon to the required size
+ if (icon != null) {
+ scaledImg = scaleIconImage(icon, width, height);
+ }
+
+ if (scaledImg != null) {
+ imageArray = new Image[2];
+
+ if (icon instanceof ImageIcon) {
+ imageArray[0] = ((ImageIcon)icon).getImage(); // store the original icon returned from the system
+ } else {
+ BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
+ icon.paintIcon(null, image.getGraphics(), 0, 0);
+ imageArray[0] = (Image)image; // store the original icon returned from the system
+ }
+ imageArray[1] = scaledImg; // store the scaled up icon
+
+ return new BaseMultiResolutionImage(imageArray);
+ }
+
+ return null;
+ }
+
+ private static Image scaleIconImage(Icon srcIcon, int scaledW, int scaledH) {
+ if (srcIcon == null) {
+ return null;
+ }
+
+ int w;
+ int h;
+
+ w = srcIcon.getIconWidth();
+ h = srcIcon.getIconHeight();
+
+ GraphicsEnvironment ge =
+ GraphicsEnvironment.getLocalGraphicsEnvironment();
+ GraphicsDevice gd = ge.getDefaultScreenDevice();
+ GraphicsConfiguration gc = gd.getDefaultConfiguration();
+
+ // convert to image
+ BufferedImage iconImage = gc.createCompatibleImage(w, h,
+ Transparency.TRANSLUCENT);
+ Graphics2D g = iconImage.createGraphics();
+ srcIcon.paintIcon(null, g, 0, 0);
+ g.dispose();
+
+ // and scale it nicely
+ BufferedImage scaledImage = gc.createCompatibleImage(scaledW, scaledH,
+ Transparency.TRANSLUCENT);
+ g = scaledImage.createGraphics();
+ g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+ RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ g.drawImage(iconImage, 0, 0, scaledW, scaledH, null);
+ g.dispose();
+
+ return (Image)scaledImage;
+ }
+
+ public File createNewFolder(File containingDir) throws IOException {
+ return null;
+ }
+}