Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaIcon.java
41154 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;28import java.awt.image.BufferedImage;29import java.io.File;3031import javax.swing.*;32import javax.swing.plaf.*;3334import apple.laf.JRSUIConstants.Size;35import apple.laf.*;3637import com.apple.laf.AquaUtilControlSize.*;38import com.apple.laf.AquaUtils.RecyclableSingleton;39import sun.lwawt.macosx.CImage;4041public class AquaIcon {42interface InvertableIcon extends Icon {43public Icon getInvertedIcon();44}4546static UIResource getIconFor(final JRSUIControlSpec spec, final int width, final int height) {47return new ScalingJRSUIIcon(width, height) {48@Override49public void initIconPainter(final AquaPainter<JRSUIState> painter) {50spec.initIconPainter(painter);51}52};53}5455// converts an object that is an icon into an image so we can hand it off to AppKit56public static Image getImageForIcon(final Icon i) {57if (i instanceof ImageIcon) return ((ImageIcon)i).getImage();5859final int w = i.getIconWidth();60final int h = i.getIconHeight();6162if (w <= 0 || h <= 0) return null;6364// 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.65final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);66final Graphics g = image.getGraphics();67i.paintIcon(null, g, 0, 0);68g.dispose();69return image;70}7172public interface JRSUIControlSpec {73public void initIconPainter(final AquaPainter<? extends JRSUIState> painter);74}7576abstract static class JRSUIIcon implements Icon, UIResource {77protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());7879public void paintIcon(final Component c, final Graphics g, final int x, final int y) {80painter.paint(g, c, x, y, getIconWidth(), getIconHeight());81}82}8384abstract static class DynamicallySizingJRSUIIcon extends JRSUIIcon {85protected final SizeDescriptor sizeDescriptor;86protected SizeVariant sizeVariant;8788public DynamicallySizingJRSUIIcon(final SizeDescriptor sizeDescriptor) {89this.sizeDescriptor = sizeDescriptor;90this.sizeVariant = sizeDescriptor.regular;91initJRSUIState();92}9394public abstract void initJRSUIState();9596public int getIconHeight() {97return sizeVariant == null ? 0 : sizeVariant.h;98}99100public int getIconWidth() {101return sizeVariant == null ? 0 : sizeVariant.w;102}103104public void paintIcon(final Component c, final Graphics g, final int x, final int y) {105final Size size = c instanceof JComponent ? AquaUtilControlSize.getUserSizeFrom((JComponent)c) : Size.REGULAR;106sizeVariant = sizeDescriptor.get(size);107painter.state.set(size);108super.paintIcon(c, g, x, y);109}110}111112abstract static class CachingScalingIcon implements Icon, UIResource {113int width;114int height;115Image image;116117public CachingScalingIcon(final int width, final int height) {118this.width = width;119this.height = height;120}121122void setSize(final int width, final int height) {123this.width = width;124this.height = height;125this.image = null;126}127128Image getImage() {129if (image != null) return image;130131if (!GraphicsEnvironment.isHeadless()) {132image = createImage();133}134135return image;136}137138abstract Image createImage();139140public boolean hasIconRef() {141return getImage() != null;142}143144public void paintIcon(final Component c, Graphics g, final int x, final int y) {145g = g.create();146147if (g instanceof Graphics2D) {148// improves icon rendering quality in Quartz149((Graphics2D)g).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);150}151152final Image myImage = getImage();153if (myImage != null) {154g.drawImage(myImage, x, y, getIconWidth(), getIconHeight(), null);155}156157g.dispose();158}159160public int getIconWidth() {161return width;162}163164public int getIconHeight() {165return height;166}167168}169170abstract static class ScalingJRSUIIcon implements Icon, UIResource {171final int width;172final int height;173174public ScalingJRSUIIcon(final int width, final int height) {175this.width = width;176this.height = height;177}178179@Override180public void paintIcon(final Component c, Graphics g,181final int x, final int y) {182if (GraphicsEnvironment.isHeadless()) {183return;184}185186g = g.create();187188if (g instanceof Graphics2D) {189// improves icon rendering quality in Quartz190((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,191RenderingHints.VALUE_RENDER_QUALITY);192}193194final AquaPainter<JRSUIState> painter =195AquaPainter.create(JRSUIState.getInstance());196initIconPainter(painter);197198g.clipRect(x, y, width, height);199painter.paint(g, c, x, y, width, height);200g.dispose();201}202203public abstract void initIconPainter(final AquaPainter<JRSUIState> painter);204205@Override206public int getIconWidth() {207return width;208}209210@Override211public int getIconHeight() {212return height;213}214}215216static class FileIcon extends CachingScalingIcon {217final File file;218219public FileIcon(final File file, final int width, final int height) {220super(width, height);221this.file = file;222}223224public FileIcon(final File file) {225this(file, 16, 16);226}227228Image createImage() {229return CImage.createImageOfFile(file.getAbsolutePath(), getIconWidth(), getIconHeight());230}231}232233static class SystemIconSingleton extends RecyclableSingleton<SystemIcon> {234final String selector;235236public SystemIconSingleton(String selector) {237this.selector = selector;238}239240@Override241protected SystemIcon getInstance() {242return new SystemIcon(selector);243}244}245246static class SystemIconUIResourceSingleton extends RecyclableSingleton<IconUIResource> {247final String selector;248249public SystemIconUIResourceSingleton(String selector) {250this.selector = selector;251}252253@Override254protected IconUIResource getInstance() {255return new IconUIResource(new SystemIcon(selector));256}257}258259static class SystemIcon extends CachingScalingIcon {260private static final SystemIconUIResourceSingleton folderIcon = new SystemIconUIResourceSingleton("fldr");261static IconUIResource getFolderIconUIResource() { return folderIcon.get(); }262263private static final SystemIconUIResourceSingleton openFolderIcon = new SystemIconUIResourceSingleton("ofld");264static IconUIResource getOpenFolderIconUIResource() { return openFolderIcon.get(); }265266private static final SystemIconUIResourceSingleton desktopIcon = new SystemIconUIResourceSingleton("desk");267static IconUIResource getDesktopIconUIResource() { return desktopIcon.get(); }268269private static final SystemIconUIResourceSingleton computerIcon = new SystemIconUIResourceSingleton("FNDR");270static IconUIResource getComputerIconUIResource() { return computerIcon.get(); }271272private static final SystemIconUIResourceSingleton documentIcon = new SystemIconUIResourceSingleton("docu");273static IconUIResource getDocumentIconUIResource() { return documentIcon.get(); }274275private static final SystemIconUIResourceSingleton hardDriveIcon = new SystemIconUIResourceSingleton("hdsk");276static IconUIResource getHardDriveIconUIResource() { return hardDriveIcon.get(); }277278private static final SystemIconUIResourceSingleton floppyIcon = new SystemIconUIResourceSingleton("flpy");279static IconUIResource getFloppyIconUIResource() { return floppyIcon.get(); }280281//private static final SystemIconUIResourceSingleton noteIcon = new SystemIconUIResourceSingleton("note");282//static IconUIResource getNoteIconUIResource() { return noteIcon.get(); }283284private static final SystemIconSingleton caut = new SystemIconSingleton("caut");285static SystemIcon getCautionIcon() { return caut.get(); }286287private static final SystemIconSingleton stop = new SystemIconSingleton("stop");288static SystemIcon getStopIcon() { return stop.get(); }289290final String selector;291292public SystemIcon(final String iconSelector, final int width, final int height) {293super(width, height);294selector = iconSelector;295}296297public SystemIcon(final String iconSelector) {298this(iconSelector, 16, 16);299}300301Image createImage() {302return CImage.createSystemImageFromSelector(303selector, getIconWidth(), getIconHeight());304}305}306}307308309