Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/laf/AquaImageFactory.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.security.PrivilegedAction;3031import javax.swing.*;32import javax.swing.plaf.*;3334import sun.lwawt.macosx.LWCToolkit;35import apple.laf.JRSUIConstants.AlignmentHorizontal;36import apple.laf.JRSUIConstants.AlignmentVertical;37import apple.laf.JRSUIConstants.Direction;38import apple.laf.JRSUIConstants.State;39import apple.laf.JRSUIConstants.Widget;40import apple.laf.*;4142import com.apple.eio.FileManager;43import com.apple.laf.AquaIcon.InvertableIcon;44import com.apple.laf.AquaIcon.JRSUIControlSpec;45import com.apple.laf.AquaIcon.SystemIcon;46import com.apple.laf.AquaUtils.RecyclableObject;47import com.apple.laf.AquaUtils.RecyclableSingleton;48import sun.awt.image.MultiResolutionImage;49import sun.awt.image.MultiResolutionCachedImage;5051public class AquaImageFactory {52public static IconUIResource getConfirmImageIcon() {53// public, because UIDefaults.ProxyLazyValue uses reflection to get this value5455return new IconUIResource(new AquaIcon.CachingScalingIcon(kAlertIconSize, kAlertIconSize) {56Image createImage() {57return getGenericJavaIcon();58}59});60}6162public static IconUIResource getCautionImageIcon() {63// public, because UIDefaults.ProxyLazyValue uses reflection to get this value64return getAppIconCompositedOn(AquaIcon.SystemIcon.getCautionIcon());65}6667public static IconUIResource getStopImageIcon() {68// public, because UIDefaults.ProxyLazyValue uses reflection to get this value69return getAppIconCompositedOn(AquaIcon.SystemIcon.getStopIcon());70}7172public static IconUIResource getLockImageIcon() {73// public, because UIDefaults.ProxyLazyValue uses reflection to get this value74if (JRSUIUtils.Images.shouldUseLegacySecurityUIPath()) {75final Image lockIcon = AquaUtils.getCImageCreator().createImageFromFile("/System/Library/CoreServices/SecurityAgent.app/Contents/Resources/Security.icns", kAlertIconSize, kAlertIconSize);76return getAppIconCompositedOn(lockIcon);77}7879final Image lockIcon = Toolkit.getDefaultToolkit().getImage("NSImage://NSSecurity");80return getAppIconCompositedOn(lockIcon);81}8283static Image getGenericJavaIcon() {84return java.security.AccessController.doPrivileged(new PrivilegedAction<Image>() {85public Image run() {86return com.apple.eawt.Application.getApplication().getDockIconImage();87}88});89}9091static String getPathToThisApplication() {92return java.security.AccessController.doPrivileged(new PrivilegedAction<String>() {93public String run() {94return FileManager.getPathToApplicationBundle();95}96});97}9899static IconUIResource getAppIconCompositedOn(final SystemIcon systemIcon) {100systemIcon.setSize(kAlertIconSize, kAlertIconSize);101return getAppIconCompositedOn(systemIcon.createImage());102}103104private static final int kAlertIconSize = 64;105static IconUIResource getAppIconCompositedOn(final Image background) {106107if (background instanceof MultiResolutionCachedImage) {108int width = background.getWidth(null);109Image mrIconImage = ((MultiResolutionCachedImage) background).map(110rv -> getAppIconImageCompositedOn(rv, rv.getWidth(null) / width));111return new IconUIResource(new ImageIcon(mrIconImage));112}113114BufferedImage iconImage = getAppIconImageCompositedOn(background, 1);115return new IconUIResource(new ImageIcon(iconImage));116}117118static BufferedImage getAppIconImageCompositedOn(final Image background, int scaleFactor) {119120final int scaledAlertIconSize = kAlertIconSize * scaleFactor;121final int kAlertSubIconSize = (int) (scaledAlertIconSize * 0.5);122final int kAlertSubIconInset = scaledAlertIconSize - kAlertSubIconSize;123final Icon smallAppIconScaled = new AquaIcon.CachingScalingIcon(124kAlertSubIconSize, kAlertSubIconSize) {125Image createImage() {126return getGenericJavaIcon();127}128};129130final BufferedImage image = new BufferedImage(scaledAlertIconSize,131scaledAlertIconSize, BufferedImage.TYPE_INT_ARGB_PRE);132final Graphics g = image.getGraphics();133g.drawImage(background, 0, 0,134scaledAlertIconSize, scaledAlertIconSize, null);135if (g instanceof Graphics2D) {136// improves icon rendering quality in Quartz137((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,138RenderingHints.VALUE_RENDER_QUALITY);139}140141smallAppIconScaled.paintIcon(null, g,142kAlertSubIconInset, kAlertSubIconInset);143g.dispose();144145return image;146}147148public static IconUIResource getTreeFolderIcon() {149// public, because UIDefaults.ProxyLazyValue uses reflection to get this value150return AquaIcon.SystemIcon.getFolderIconUIResource();151}152153public static IconUIResource getTreeOpenFolderIcon() {154// public, because UIDefaults.ProxyLazyValue uses reflection to get this value155return AquaIcon.SystemIcon.getOpenFolderIconUIResource();156}157158public static IconUIResource getTreeDocumentIcon() {159// public, because UIDefaults.ProxyLazyValue uses reflection to get this value160return AquaIcon.SystemIcon.getDocumentIconUIResource();161}162163public static UIResource getTreeExpandedIcon() {164// public, because UIDefaults.ProxyLazyValue uses reflection to get this value165return AquaIcon.getIconFor(new JRSUIControlSpec() {166public void initIconPainter(final AquaPainter<? extends JRSUIState> painter) {167painter.state.set(Widget.DISCLOSURE_TRIANGLE);168painter.state.set(State.ACTIVE);169painter.state.set(Direction.DOWN);170painter.state.set(AlignmentHorizontal.CENTER);171painter.state.set(AlignmentVertical.CENTER);172}173}, 20, 20);174}175176public static UIResource getTreeCollapsedIcon() {177// public, because UIDefaults.ProxyLazyValue uses reflection to get this value178return AquaIcon.getIconFor(new JRSUIControlSpec() {179public void initIconPainter(final AquaPainter<? extends JRSUIState> painter) {180painter.state.set(Widget.DISCLOSURE_TRIANGLE);181painter.state.set(State.ACTIVE);182painter.state.set(Direction.RIGHT);183painter.state.set(AlignmentHorizontal.CENTER);184painter.state.set(AlignmentVertical.CENTER);185}186}, 20, 20);187}188189public static UIResource getTreeRightToLeftCollapsedIcon() {190// public, because UIDefaults.ProxyLazyValue uses reflection to get this value191return AquaIcon.getIconFor(new JRSUIControlSpec() {192public void initIconPainter(final AquaPainter<? extends JRSUIState> painter) {193painter.state.set(Widget.DISCLOSURE_TRIANGLE);194painter.state.set(State.ACTIVE);195painter.state.set(Direction.LEFT);196painter.state.set(AlignmentHorizontal.CENTER);197painter.state.set(AlignmentVertical.CENTER);198}199}, 20, 20);200}201202static class NamedImageSingleton extends RecyclableSingleton<Image> {203final String namedImage;204205NamedImageSingleton(final String namedImage) {206this.namedImage = namedImage;207}208209@Override210protected Image getInstance() {211return getNSIcon(namedImage);212}213}214215static class IconUIResourceSingleton extends RecyclableSingleton<IconUIResource> {216final NamedImageSingleton holder;217218public IconUIResourceSingleton(final NamedImageSingleton holder) {219this.holder = holder;220}221222@Override223protected IconUIResource getInstance() {224return new IconUIResource(new ImageIcon(holder.get()));225}226}227228static class InvertableImageIcon extends ImageIcon implements InvertableIcon, UIResource {229Icon invertedImage;230public InvertableImageIcon(final Image image) {231super(image);232}233234@Override235public Icon getInvertedIcon() {236if (invertedImage != null) return invertedImage;237return invertedImage = new IconUIResource(new ImageIcon(AquaUtils.generateLightenedImage(getImage(), 100)));238}239}240241protected static final NamedImageSingleton northArrow = new NamedImageSingleton("NSMenuScrollUp");242protected static final IconUIResourceSingleton northArrowIcon = new IconUIResourceSingleton(northArrow);243protected static final NamedImageSingleton southArrow = new NamedImageSingleton("NSMenuScrollDown");244protected static final IconUIResourceSingleton southArrowIcon = new IconUIResourceSingleton(southArrow);245protected static final NamedImageSingleton westArrow = new NamedImageSingleton("NSMenuSubmenuLeft");246protected static final IconUIResourceSingleton westArrowIcon = new IconUIResourceSingleton(westArrow);247protected static final NamedImageSingleton eastArrow = new NamedImageSingleton("NSMenuSubmenu");248protected static final IconUIResourceSingleton eastArrowIcon = new IconUIResourceSingleton(eastArrow);249250static Image getArrowImageForDirection(final int direction) {251switch(direction) {252case SwingConstants.NORTH: return northArrow.get();253case SwingConstants.SOUTH: return southArrow.get();254case SwingConstants.EAST: return eastArrow.get();255case SwingConstants.WEST: return westArrow.get();256}257return null;258}259260static Icon getArrowIconForDirection(int direction) {261switch(direction) {262case SwingConstants.NORTH: return northArrowIcon.get();263case SwingConstants.SOUTH: return southArrowIcon.get();264case SwingConstants.EAST: return eastArrowIcon.get();265case SwingConstants.WEST: return westArrowIcon.get();266}267return null;268}269270public static Icon getMenuArrowIcon() {271return new InvertableImageIcon(AquaUtils.generateLightenedImage(eastArrow.get(), 25));272}273274public static Icon getMenuItemCheckIcon() {275return new InvertableImageIcon(AquaUtils.generateLightenedImage(276getNSIcon("NSMenuItemSelection"), 25));277}278279public static Icon getMenuItemDashIcon() {280return new InvertableImageIcon(AquaUtils.generateLightenedImage(281getNSIcon("NSMenuMixedState"), 25));282}283284private static Image getNSIcon(String imageName) {285Image icon = Toolkit.getDefaultToolkit()286.getImage("NSImage://" + imageName);287return icon;288}289290public static class NineSliceMetrics {291public final int wCut, eCut, nCut, sCut;292public final int minW, minH;293public final boolean showMiddle, stretchH, stretchV;294295public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {296this(minWidth, minHeight, westCut, eastCut, northCut, southCut, true);297}298299public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean showMiddle) {300this(minWidth, minHeight, westCut, eastCut, northCut, southCut, showMiddle, true, true);301}302303public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean showMiddle, final boolean stretchHorizontally, final boolean stretchVertically) {304this.wCut = westCut; this.eCut = eastCut; this.nCut = northCut; this.sCut = southCut;305this.minW = minWidth; this.minH = minHeight;306this.showMiddle = showMiddle; this.stretchH = stretchHorizontally; this.stretchV = stretchVertically;307}308}309310/*311* A "paintable" which holds nine images, which represent a sliced up initial312* image that can be streched from its middles.313*/314public static class SlicedImageControl {315final BufferedImage NW, N, NE;316final BufferedImage W, C, E;317final BufferedImage SW, S, SE;318319final NineSliceMetrics metrics;320321final int totalWidth, totalHeight;322final int centerColWidth, centerRowHeight;323324public SlicedImageControl(final Image img, final int westCut, final int eastCut, final int northCut, final int southCut) {325this(img, westCut, eastCut, northCut, southCut, true);326}327328public SlicedImageControl(final Image img, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle) {329this(img, westCut, eastCut, northCut, southCut, useMiddle, true, true);330}331332public SlicedImageControl(final Image img, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle, final boolean stretchHorizontally, final boolean stretchVertically) {333this(img, new NineSliceMetrics(img.getWidth(null), img.getHeight(null), westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically));334}335336public SlicedImageControl(final Image img, final NineSliceMetrics metrics) {337this.metrics = metrics;338339if (img.getWidth(null) != metrics.minW || img.getHeight(null) != metrics.minH) {340throw new IllegalArgumentException("SlicedImageControl: template image and NineSliceMetrics don't agree on minimum dimensions");341}342343totalWidth = metrics.minW;344totalHeight = metrics.minH;345centerColWidth = totalWidth - metrics.wCut - metrics.eCut;346centerRowHeight = totalHeight - metrics.nCut - metrics.sCut;347348NW = createSlice(img, 0, 0, metrics.wCut, metrics.nCut);349N = createSlice(img, metrics.wCut, 0, centerColWidth, metrics.nCut);350NE = createSlice(img, totalWidth - metrics.eCut, 0, metrics.eCut, metrics.nCut);351W = createSlice(img, 0, metrics.nCut, metrics.wCut, centerRowHeight);352C = metrics.showMiddle ? createSlice(img, metrics.wCut, metrics.nCut, centerColWidth, centerRowHeight) : null;353E = createSlice(img, totalWidth - metrics.eCut, metrics.nCut, metrics.eCut, centerRowHeight);354SW = createSlice(img, 0, totalHeight - metrics.sCut, metrics.wCut, metrics.sCut);355S = createSlice(img, metrics.wCut, totalHeight - metrics.sCut, centerColWidth, metrics.sCut);356SE = createSlice(img, totalWidth - metrics.eCut, totalHeight - metrics.sCut, metrics.eCut, metrics.sCut);357}358359static BufferedImage createSlice(final Image img, final int x, final int y, final int w, final int h) {360if (w == 0 || h == 0) return null;361362final BufferedImage slice = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);363final Graphics2D g2d = slice.createGraphics();364g2d.drawImage(img, 0, 0, w, h, x, y, x + w, y + h, null);365g2d.dispose();366367return slice;368}369370public void paint(final Graphics g, final int x, final int y, final int w, final int h) {371g.translate(x, y);372373if (w < totalWidth || h < totalHeight) {374paintCompressed(g, w, h);375} else {376paintStretchedMiddles(g, w, h);377}378379g.translate(-x, -y);380}381382void paintStretchedMiddles(final Graphics g, final int w, final int h) {383int baseX = metrics.stretchH ? 0 : ((w / 2) - (totalWidth / 2));384int baseY = metrics.stretchV ? 0 : ((h / 2) - (totalHeight / 2));385int adjustedWidth = metrics.stretchH ? w : totalWidth;386int adjustedHeight = metrics.stretchV ? h : totalHeight;387388if (NW != null) g.drawImage(NW, baseX, baseY, null);389if (N != null) g.drawImage(N, baseX + metrics.wCut, baseY, adjustedWidth - metrics.eCut - metrics.wCut, metrics.nCut, null);390if (NE != null) g.drawImage(NE, baseX + adjustedWidth - metrics.eCut, baseY, null);391if (W != null) g.drawImage(W, baseX, baseY + metrics.nCut, metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut, null);392if (C != null) g.drawImage(C, baseX + metrics.wCut, baseY + metrics.nCut, adjustedWidth - metrics.eCut - metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut, null);393if (E != null) g.drawImage(E, baseX + adjustedWidth - metrics.eCut, baseY + metrics.nCut, metrics.eCut, adjustedHeight - metrics.nCut - metrics.sCut, null);394if (SW != null) g.drawImage(SW, baseX, baseY + adjustedHeight - metrics.sCut, null);395if (S != null) g.drawImage(S, baseX + metrics.wCut, baseY + adjustedHeight - metrics.sCut, adjustedWidth - metrics.eCut - metrics.wCut, metrics.sCut, null);396if (SE != null) g.drawImage(SE, baseX + adjustedWidth - metrics.eCut, baseY + adjustedHeight - metrics.sCut, null);397398/*399if (NW != null) {g.setColor(Color.GREEN); g.fillRect(baseX, baseY, NW.getWidth(), NW.getHeight());}400if (N != null) {g.setColor(Color.RED); g.fillRect(baseX + metrics.wCut, baseY, adjustedWidth - metrics.eCut - metrics.wCut, metrics.nCut);}401if (NE != null) {g.setColor(Color.BLUE); g.fillRect(baseX + adjustedWidth - metrics.eCut, baseY, NE.getWidth(), NE.getHeight());}402if (W != null) {g.setColor(Color.PINK); g.fillRect(baseX, baseY + metrics.nCut, metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut);}403if (C != null) {g.setColor(Color.ORANGE); g.fillRect(baseX + metrics.wCut, baseY + metrics.nCut, adjustedWidth - metrics.eCut - metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut);}404if (E != null) {g.setColor(Color.CYAN); g.fillRect(baseX + adjustedWidth - metrics.eCut, baseY + metrics.nCut, metrics.eCut, adjustedHeight - metrics.nCut - metrics.sCut);}405if (SW != null) {g.setColor(Color.MAGENTA); g.fillRect(baseX, baseY + adjustedHeight - metrics.sCut, SW.getWidth(), SW.getHeight());}406if (S != null) {g.setColor(Color.DARK_GRAY); g.fillRect(baseX + metrics.wCut, baseY + adjustedHeight - metrics.sCut, adjustedWidth - metrics.eCut - metrics.wCut, metrics.sCut);}407if (SE != null) {g.setColor(Color.YELLOW); g.fillRect(baseX + adjustedWidth - metrics.eCut, baseY + adjustedHeight - metrics.sCut, SE.getWidth(), SE.getHeight());}408*/409}410411void paintCompressed(final Graphics g, final int w, final int h) {412final double heightRatio = h > totalHeight ? 1.0 : (double)h / (double)totalHeight;413final double widthRatio = w > totalWidth ? 1.0 : (double)w / (double)totalWidth;414415final int northHeight = (int)(metrics.nCut * heightRatio);416final int southHeight = (int)(metrics.sCut * heightRatio);417final int centerHeight = h - northHeight - southHeight;418419final int westWidth = (int)(metrics.wCut * widthRatio);420final int eastWidth = (int)(metrics.eCut * widthRatio);421final int centerWidth = w - westWidth - eastWidth;422423if (NW != null) g.drawImage(NW, 0, 0, westWidth, northHeight, null);424if (N != null) g.drawImage(N, westWidth, 0, centerWidth, northHeight, null);425if (NE != null) g.drawImage(NE, w - eastWidth, 0, eastWidth, northHeight, null);426if (W != null) g.drawImage(W, 0, northHeight, westWidth, centerHeight, null);427if (C != null) g.drawImage(C, westWidth, northHeight, centerWidth, centerHeight, null);428if (E != null) g.drawImage(E, w - eastWidth, northHeight, eastWidth, centerHeight, null);429if (SW != null) g.drawImage(SW, 0, h - southHeight, westWidth, southHeight, null);430if (S != null) g.drawImage(S, westWidth, h - southHeight, centerWidth, southHeight, null);431if (SE != null) g.drawImage(SE, w - eastWidth, h - southHeight, eastWidth, southHeight, null);432}433}434435public abstract static class RecyclableSlicedImageControl extends RecyclableObject<SlicedImageControl> {436final NineSliceMetrics metrics;437438public RecyclableSlicedImageControl(final NineSliceMetrics metrics) {439this.metrics = metrics;440}441442@Override443protected SlicedImageControl create() {444return new SlicedImageControl(createTemplateImage(metrics.minW, metrics.minH), metrics);445}446447protected abstract Image createTemplateImage(final int width, final int height);448}449450// when we use SystemColors, we need to proxy the color with something that implements UIResource,451// so that it will be uninstalled when the look and feel is changed.452private static class SystemColorProxy extends Color implements UIResource {453final Color color;454public SystemColorProxy(final Color color) {455super(color.getRGB());456this.color = color;457}458459public int getRGB() {460return color.getRGB();461}462}463464public static Color getWindowBackgroundColorUIResource() {465//return AquaNativeResources.getWindowBackgroundColorUIResource();466return new SystemColorProxy(SystemColor.window);467}468469public static Color getTextSelectionBackgroundColorUIResource() {470return new SystemColorProxy(SystemColor.textHighlight);471}472473public static Color getTextSelectionForegroundColorUIResource() {474return new SystemColorProxy(SystemColor.textHighlightText);475}476477public static Color getSelectionBackgroundColorUIResource() {478return new SystemColorProxy(SystemColor.controlHighlight);479}480481public static Color getSelectionForegroundColorUIResource() {482return new SystemColorProxy(SystemColor.controlLtHighlight);483}484485public static Color getFocusRingColorUIResource() {486return new SystemColorProxy(LWCToolkit.getAppleColor(LWCToolkit.KEYBOARD_FOCUS_COLOR));487}488489public static Color getSelectionInactiveBackgroundColorUIResource() {490return new SystemColorProxy(LWCToolkit.getAppleColor(LWCToolkit.INACTIVE_SELECTION_BACKGROUND_COLOR));491}492493public static Color getSelectionInactiveForegroundColorUIResource() {494return new SystemColorProxy(LWCToolkit.getAppleColor(LWCToolkit.INACTIVE_SELECTION_FOREGROUND_COLOR));495}496}497498499