4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.laf;
4632N/A
4632N/Aimport java.awt.*;
4632N/Aimport java.awt.image.BufferedImage;
4632N/Aimport java.io.File;
4632N/A
4632N/Aimport javax.swing.*;
4632N/Aimport javax.swing.plaf.*;
4632N/A
4632N/Aimport apple.laf.JRSUIConstants.Size;
4632N/Aimport apple.laf.*;
4632N/A
4632N/Aimport com.apple.laf.AquaUtilControlSize.*;
4632N/Aimport com.apple.laf.AquaUtils.RecyclableSingleton;
4632N/A
4632N/Apublic class AquaIcon {
4632N/A interface InvertableIcon extends Icon {
4632N/A public Icon getInvertedIcon();
4632N/A }
4632N/A
4632N/A static UIResource getIconFor(final JRSUIControlSpec spec, final int width, final int height) {
4632N/A return new CachableJRSUIIcon(width, height) {
4632N/A public void initIconPainter(final AquaPainter<JRSUIState> painter) {
4632N/A spec.initIconPainter(painter);
4632N/A }
4632N/A };
4632N/A }
4632N/A
4632N/A // converts an object that is an icon into an image so we can hand it off to AppKit
4632N/A public static Image getImageForIcon(final Icon i) {
4632N/A if (i instanceof ImageIcon) return ((ImageIcon)i).getImage();
4632N/A
4632N/A final int w = i.getIconWidth();
4632N/A final int h = i.getIconHeight();
4632N/A
4632N/A if (w <= 0 || h <= 0) return null;
4632N/A
4632N/A // This could be any kind of icon, so we need to make a buffer for it, draw it and then pass the new image off to appkit.
4632N/A final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
4632N/A final Graphics g = image.getGraphics();
4632N/A i.paintIcon(null, g, 0, 0);
4632N/A g.dispose();
4632N/A return image;
4632N/A }
4632N/A
4632N/A public interface JRSUIControlSpec {
4632N/A public void initIconPainter(final AquaPainter<? extends JRSUIState> painter);
4632N/A }
4632N/A
4632N/A static abstract class JRSUIIcon implements Icon, UIResource {
4632N/A protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
4632N/A
4632N/A public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
4632N/A painter.paint(g, c, x, y, getIconWidth(), getIconHeight());
4632N/A }
4632N/A }
4632N/A
4632N/A static abstract class DynamicallySizingJRSUIIcon extends JRSUIIcon {
4632N/A protected final SizeDescriptor sizeDescriptor;
4632N/A protected SizeVariant sizeVariant;
4632N/A
4632N/A public DynamicallySizingJRSUIIcon(final SizeDescriptor sizeDescriptor) {
4632N/A this.sizeDescriptor = sizeDescriptor;
4632N/A this.sizeVariant = sizeDescriptor.regular;
4632N/A initJRSUIState();
4632N/A }
4632N/A
4632N/A public abstract void initJRSUIState();
4632N/A
4632N/A public int getIconHeight() {
4632N/A return sizeVariant == null ? 0 : sizeVariant.h;
4632N/A }
4632N/A
4632N/A public int getIconWidth() {
4632N/A return sizeVariant == null ? 0 : sizeVariant.w;
4632N/A }
4632N/A
4632N/A public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
4632N/A final Size size = c instanceof JComponent ? AquaUtilControlSize.getUserSizeFrom((JComponent)c) : Size.REGULAR;
4632N/A sizeVariant = sizeDescriptor.get(size);
4632N/A painter.state.set(size);
4632N/A super.paintIcon(c, g, x, y);
4632N/A }
4632N/A }
4632N/A
4632N/A static abstract class CachingScalingIcon implements Icon, UIResource {
4632N/A int width;
4632N/A int height;
4632N/A Image image;
4632N/A
4632N/A public CachingScalingIcon(final int width, final int height) {
4632N/A this.width = width;
4632N/A this.height = height;
4632N/A }
4632N/A
4632N/A void setSize(final int width, final int height) {
4632N/A this.width = width;
4632N/A this.height = height;
4632N/A this.image = null;
4632N/A }
4632N/A
4632N/A Image getImage() {
4632N/A if (image != null) return image;
4632N/A
4632N/A if (!GraphicsEnvironment.isHeadless()) {
4632N/A image = getOptimizedImage();
4632N/A }
4632N/A
4632N/A return image;
4632N/A }
4632N/A
4632N/A private Image getOptimizedImage() {
4632N/A final Image img = createImage();
4632N/A // TODO: no RuntimeOptions for now
4632N/A //if (RuntimeOptions.getRenderer(null) != RuntimeOptions.Sun) return img;
4632N/A return getProgressiveOptimizedImage(img, getIconWidth(), getIconHeight());
4632N/A }
4632N/A
4632N/A static Image getProgressiveOptimizedImage(final Image img, final int w, final int h) {
4632N/A if (img == null) return null;
4632N/A
4632N/A final int halfImgW = img.getWidth(null) / 2;
4632N/A final int halfImgH = img.getHeight(null) / 2;
4632N/A if (w * 2 > halfImgW && h * 2 > halfImgH) return img;
4632N/A
4632N/A final BufferedImage halfImage = new BufferedImage(halfImgW, halfImgH, BufferedImage.TYPE_INT_ARGB);
4632N/A final Graphics g = halfImage.getGraphics();
4632N/A ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
4632N/A g.drawImage(img, 0, 0, halfImgW, halfImgH, null);
4632N/A g.dispose();
4632N/A
4632N/A return getProgressiveOptimizedImage(halfImage, w, h);
4632N/A }
4632N/A
4632N/A abstract Image createImage();
4632N/A
4632N/A public boolean hasIconRef() {
4632N/A return getImage() != null;
4632N/A }
4632N/A
4632N/A public void paintIcon(final Component c, Graphics g, final int x, final int y) {
4632N/A g = g.create();
4632N/A
4632N/A if (g instanceof Graphics2D) {
4632N/A // improves icon rendering quality in Quartz
4632N/A ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
4632N/A }
4632N/A
4632N/A final Image myImage = getImage();
4632N/A if (myImage != null) {
4632N/A g.drawImage(myImage, x, y, getIconWidth(), getIconHeight(), null);
4632N/A }
4632N/A
4632N/A g.dispose();
4632N/A }
4632N/A
4632N/A public int getIconWidth() {
4632N/A return width;
4632N/A }
4632N/A
4632N/A public int getIconHeight() {
4632N/A return height;
4632N/A }
4632N/A
4632N/A }
4632N/A
4632N/A static abstract class CachableJRSUIIcon extends CachingScalingIcon implements UIResource {
4632N/A public CachableJRSUIIcon(final int width, final int height) {
4632N/A super(width, height);
4632N/A }
4632N/A
4632N/A Image createImage() {
4632N/A final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
4632N/A initIconPainter(painter);
4632N/A
4632N/A final BufferedImage img = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
4632N/A final Graphics g = img.getGraphics();
4632N/A g.setClip(new Rectangle(0, 0, getIconWidth(), getIconHeight()));
4632N/A painter.paint(g, null, 0, 0, getIconWidth(), getIconHeight());
4632N/A g.dispose();
4632N/A return img;
4632N/A }
4632N/A
4632N/A public abstract void initIconPainter(final AquaPainter<JRSUIState> painter);
4632N/A }
4632N/A
4632N/A static class FileIcon extends CachingScalingIcon {
4632N/A final File file;
4632N/A
4632N/A public FileIcon(final File file, final int width, final int height) {
4632N/A super(width, height);
4632N/A this.file = file;
4632N/A }
4632N/A
4632N/A public FileIcon(final File file) {
4632N/A this(file, 16, 16);
4632N/A }
4632N/A
4632N/A Image createImage() {
4632N/A return AquaUtils.getCImageCreator().createImageOfFile(file.getAbsolutePath(), getIconWidth(), getIconHeight());
4632N/A }
4632N/A }
4632N/A
4632N/A static class SystemIconSingleton extends RecyclableSingleton<SystemIcon> {
4632N/A final String selector;
4632N/A
4632N/A public SystemIconSingleton(String selector) {
4632N/A this.selector = selector;
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected SystemIcon getInstance() {
4632N/A return new SystemIcon(selector);
4632N/A }
4632N/A }
4632N/A
4632N/A static class SystemIconUIResourceSingleton extends RecyclableSingleton<IconUIResource> {
4632N/A final String selector;
4632N/A
4632N/A public SystemIconUIResourceSingleton(String selector) {
4632N/A this.selector = selector;
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected IconUIResource getInstance() {
4632N/A return new IconUIResource(new SystemIcon(selector));
4632N/A }
4632N/A }
4632N/A
4632N/A static class SystemIcon extends CachingScalingIcon {
4632N/A private static final SystemIconUIResourceSingleton folderIcon = new SystemIconUIResourceSingleton("fldr");
4632N/A static IconUIResource getFolderIconUIResource() { return folderIcon.get(); }
4632N/A
4632N/A private static final SystemIconUIResourceSingleton openFolderIcon = new SystemIconUIResourceSingleton("ofld");
4632N/A static IconUIResource getOpenFolderIconUIResource() { return openFolderIcon.get(); }
4632N/A
4632N/A private static final SystemIconUIResourceSingleton desktopIcon = new SystemIconUIResourceSingleton("desk");
4632N/A static IconUIResource getDesktopIconUIResource() { return desktopIcon.get(); }
4632N/A
4632N/A private static final SystemIconUIResourceSingleton computerIcon = new SystemIconUIResourceSingleton("FNDR");
4632N/A static IconUIResource getComputerIconUIResource() { return computerIcon.get(); }
4632N/A
4632N/A private static final SystemIconUIResourceSingleton documentIcon = new SystemIconUIResourceSingleton("docu");
4632N/A static IconUIResource getDocumentIconUIResource() { return documentIcon.get(); }
4632N/A
4632N/A private static final SystemIconUIResourceSingleton hardDriveIcon = new SystemIconUIResourceSingleton("hdsk");
4632N/A static IconUIResource getHardDriveIconUIResource() { return hardDriveIcon.get(); }
4632N/A
4632N/A private static final SystemIconUIResourceSingleton floppyIcon = new SystemIconUIResourceSingleton("flpy");
4632N/A static IconUIResource getFloppyIconUIResource() { return floppyIcon.get(); }
4632N/A
4632N/A //private static final SystemIconUIResourceSingleton noteIcon = new SystemIconUIResourceSingleton("note");
4632N/A //static IconUIResource getNoteIconUIResource() { return noteIcon.get(); }
4632N/A
4632N/A private static final SystemIconSingleton caut = new SystemIconSingleton("caut");
4632N/A static SystemIcon getCautionIcon() { return caut.get(); }
4632N/A
4632N/A private static final SystemIconSingleton stop = new SystemIconSingleton("stop");
4632N/A static SystemIcon getStopIcon() { return stop.get(); }
4632N/A
4632N/A final String selector;
4632N/A
4632N/A public SystemIcon(final String iconSelector, final int width, final int height) {
4632N/A super(width, height);
4632N/A selector = iconSelector;
4632N/A }
4632N/A
4632N/A public SystemIcon(final String iconSelector) {
4632N/A this(iconSelector, 16, 16);
4632N/A }
4632N/A
4632N/A Image createImage() {
4632N/A return AquaUtils.getCImageCreator().createSystemImageFromSelector(selector, getIconWidth(), getIconHeight());
4632N/A }
4632N/A }
4632N/A}