Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/laf/AquaIcon.java
38831 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;3940public class AquaIcon {41interface InvertableIcon extends Icon {42public Icon getInvertedIcon();43}4445static UIResource getIconFor(final JRSUIControlSpec spec, final int width, final int height) {46return new ScalingJRSUIIcon(width, height) {47@Override48public void initIconPainter(final AquaPainter<JRSUIState> painter) {49spec.initIconPainter(painter);50}51};52}5354// converts an object that is an icon into an image so we can hand it off to AppKit55public static Image getImageForIcon(final Icon i) {56if (i instanceof ImageIcon) return ((ImageIcon)i).getImage();5758final int w = i.getIconWidth();59final int h = i.getIconHeight();6061if (w <= 0 || h <= 0) return null;6263// 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.64final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);65final Graphics g = image.getGraphics();66i.paintIcon(null, g, 0, 0);67g.dispose();68return image;69}7071public interface JRSUIControlSpec {72public void initIconPainter(final AquaPainter<? extends JRSUIState> painter);73}7475static abstract class JRSUIIcon implements Icon, UIResource {76protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());7778public void paintIcon(final Component c, final Graphics g, final int x, final int y) {79painter.paint(g, c, x, y, getIconWidth(), getIconHeight());80}81}8283static abstract class DynamicallySizingJRSUIIcon extends JRSUIIcon {84protected final SizeDescriptor sizeDescriptor;85protected SizeVariant sizeVariant;8687public DynamicallySizingJRSUIIcon(final SizeDescriptor sizeDescriptor) {88this.sizeDescriptor = sizeDescriptor;89this.sizeVariant = sizeDescriptor.regular;90initJRSUIState();91}9293public abstract void initJRSUIState();9495public int getIconHeight() {96return sizeVariant == null ? 0 : sizeVariant.h;97}9899public int getIconWidth() {100return sizeVariant == null ? 0 : sizeVariant.w;101}102103public void paintIcon(final Component c, final Graphics g, final int x, final int y) {104final Size size = c instanceof JComponent ? AquaUtilControlSize.getUserSizeFrom((JComponent)c) : Size.REGULAR;105sizeVariant = sizeDescriptor.get(size);106painter.state.set(size);107super.paintIcon(c, g, x, y);108}109}110111static abstract class CachingScalingIcon implements Icon, UIResource {112int width;113int height;114Image image;115116public CachingScalingIcon(final int width, final int height) {117this.width = width;118this.height = height;119}120121void setSize(final int width, final int height) {122this.width = width;123this.height = height;124this.image = null;125}126127Image getImage() {128if (image != null) return image;129130if (!GraphicsEnvironment.isHeadless()) {131image = createImage();132}133134return image;135}136137abstract Image createImage();138139public boolean hasIconRef() {140return getImage() != null;141}142143public void paintIcon(final Component c, Graphics g, final int x, final int y) {144g = g.create();145146if (g instanceof Graphics2D) {147// improves icon rendering quality in Quartz148((Graphics2D)g).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);149}150151final Image myImage = getImage();152if (myImage != null) {153g.drawImage(myImage, x, y, getIconWidth(), getIconHeight(), null);154}155156g.dispose();157}158159public int getIconWidth() {160return width;161}162163public int getIconHeight() {164return height;165}166167}168169static abstract class ScalingJRSUIIcon implements Icon, UIResource {170final int width;171final int height;172173public ScalingJRSUIIcon(final int width, final int height) {174this.width = width;175this.height = height;176}177178@Override179public void paintIcon(final Component c, Graphics g,180final int x, final int y) {181if (GraphicsEnvironment.isHeadless()) {182return;183}184185g = g.create();186187if (g instanceof Graphics2D) {188// improves icon rendering quality in Quartz189((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,190RenderingHints.VALUE_RENDER_QUALITY);191}192193final AquaPainter<JRSUIState> painter =194AquaPainter.create(JRSUIState.getInstance());195initIconPainter(painter);196197g.clipRect(x, y, width, height);198painter.paint(g, c, x, y, width, height);199g.dispose();200}201202public abstract void initIconPainter(final AquaPainter<JRSUIState> painter);203204@Override205public int getIconWidth() {206return width;207}208209@Override210public int getIconHeight() {211return height;212}213}214215static class FileIcon extends CachingScalingIcon {216final File file;217218public FileIcon(final File file, final int width, final int height) {219super(width, height);220this.file = file;221}222223public FileIcon(final File file) {224this(file, 16, 16);225}226227Image createImage() {228return AquaUtils.getCImageCreator().createImageOfFile(file.getAbsolutePath(), getIconWidth(), getIconHeight());229}230}231232static class SystemIconSingleton extends RecyclableSingleton<SystemIcon> {233final String selector;234235public SystemIconSingleton(String selector) {236this.selector = selector;237}238239@Override240protected SystemIcon getInstance() {241return new SystemIcon(selector);242}243}244245static class SystemIconUIResourceSingleton extends RecyclableSingleton<IconUIResource> {246final String selector;247248public SystemIconUIResourceSingleton(String selector) {249this.selector = selector;250}251252@Override253protected IconUIResource getInstance() {254return new IconUIResource(new SystemIcon(selector));255}256}257258static class SystemIcon extends CachingScalingIcon {259private static final SystemIconUIResourceSingleton folderIcon = new SystemIconUIResourceSingleton("fldr");260static IconUIResource getFolderIconUIResource() { return folderIcon.get(); }261262private static final SystemIconUIResourceSingleton openFolderIcon = new SystemIconUIResourceSingleton("ofld");263static IconUIResource getOpenFolderIconUIResource() { return openFolderIcon.get(); }264265private static final SystemIconUIResourceSingleton desktopIcon = new SystemIconUIResourceSingleton("desk");266static IconUIResource getDesktopIconUIResource() { return desktopIcon.get(); }267268private static final SystemIconUIResourceSingleton computerIcon = new SystemIconUIResourceSingleton("FNDR");269static IconUIResource getComputerIconUIResource() { return computerIcon.get(); }270271private static final SystemIconUIResourceSingleton documentIcon = new SystemIconUIResourceSingleton("docu");272static IconUIResource getDocumentIconUIResource() { return documentIcon.get(); }273274private static final SystemIconUIResourceSingleton hardDriveIcon = new SystemIconUIResourceSingleton("hdsk");275static IconUIResource getHardDriveIconUIResource() { return hardDriveIcon.get(); }276277private static final SystemIconUIResourceSingleton floppyIcon = new SystemIconUIResourceSingleton("flpy");278static IconUIResource getFloppyIconUIResource() { return floppyIcon.get(); }279280//private static final SystemIconUIResourceSingleton noteIcon = new SystemIconUIResourceSingleton("note");281//static IconUIResource getNoteIconUIResource() { return noteIcon.get(); }282283private static final SystemIconSingleton caut = new SystemIconSingleton("caut");284static SystemIcon getCautionIcon() { return caut.get(); }285286private static final SystemIconSingleton stop = new SystemIconSingleton("stop");287static SystemIcon getStopIcon() { return stop.get(); }288289final String selector;290291public SystemIcon(final String iconSelector, final int width, final int height) {292super(width, height);293selector = iconSelector;294}295296public SystemIcon(final String iconSelector) {297this(iconSelector, 16, 16);298}299300Image createImage() {301return AquaUtils.getCImageCreator().createSystemImageFromSelector(302selector, getIconWidth(), getIconHeight());303}304}305}306307308