1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.image.BufferedImage;
  30 import java.security.PrivilegedAction;
  31 
  32 import javax.swing.*;
  33 import javax.swing.plaf.*;
  34 
  35 import sun.lwawt.macosx.LWCToolkit;
  36 import apple.laf.JRSUIConstants.AlignmentHorizontal;
  37 import apple.laf.JRSUIConstants.AlignmentVertical;
  38 import apple.laf.JRSUIConstants.Direction;
  39 import apple.laf.JRSUIConstants.State;
  40 import apple.laf.JRSUIConstants.Widget;
  41 import apple.laf.*;
  42 
  43 import com.apple.eio.FileManager;
  44 import com.apple.laf.AquaIcon.InvertableIcon;
  45 import com.apple.laf.AquaIcon.JRSUIControlSpec;
  46 import com.apple.laf.AquaIcon.SystemIcon;
  47 import com.apple.laf.AquaUtils.RecyclableObject;
  48 import com.apple.laf.AquaUtils.RecyclableSingleton;
  49 import java.util.Arrays;
  50 import java.util.List;
  51 import sun.awt.image.MultiResolutionBufferedImage;
  52 import sun.awt.image.MultiResolutionImage;
  53 
  54 public class AquaImageFactory {
  55     public static IconUIResource getConfirmImageIcon() {
  56         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  57 
  58         return new IconUIResource(new AquaIcon.CachingScalingIcon(kAlertIconSize, kAlertIconSize) {
  59             Image createImage() {
  60                 return getThisApplicationsIcon(kAlertIconSize, kAlertIconSize);
  61             }
  62         });
  63     }
  64 
  65     public static IconUIResource getCautionImageIcon() {
  66         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  67         return getAppIconCompositedOn(AquaIcon.SystemIcon.getCautionIcon());
  68     }
  69 
  70     public static IconUIResource getStopImageIcon() {
  71         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  72         return getAppIconCompositedOn(AquaIcon.SystemIcon.getStopIcon());
  73     }
  74 
  75     public static IconUIResource getLockImageIcon() {
  76         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  77         if (JRSUIUtils.Images.shouldUseLegacySecurityUIPath()) {
  78             final Image lockIcon = AquaUtils.getCImageCreator().createImageFromFile("/System/Library/CoreServices/SecurityAgent.app/Contents/Resources/Security.icns", kAlertIconSize, kAlertIconSize);
  79             return getAppIconCompositedOn(lockIcon);
  80         }
  81 
  82         final Image lockIcon = Toolkit.getDefaultToolkit().getImage("NSImage://NSSecurity");
  83         return getAppIconCompositedOn(lockIcon);
  84     }
  85 
  86     static Image getThisApplicationsIcon(final int width, final int height) {
  87         final String path = getPathToThisApplication();
  88 
  89         if (path == null) {
  90             return getGenericJavaIcon();
  91         }
  92 
  93         if (path.endsWith("/Home/bin")) {
  94             return getGenericJavaIcon();
  95         }
  96 
  97         if (path.startsWith("/usr/bin")) {
  98             return getGenericJavaIcon();
  99         }
 100 
 101         return AquaUtils.getCImageCreator().createImageOfFile(path, height, width);
 102     }
 103 
 104     static Image getGenericJavaIcon() {
 105         return java.security.AccessController.doPrivileged(new PrivilegedAction<Image>() {
 106             public Image run() {
 107                 return com.apple.eawt.Application.getApplication().getDockIconImage();
 108             }
 109         });
 110     }
 111 
 112     static String getPathToThisApplication() {
 113         return java.security.AccessController.doPrivileged(new PrivilegedAction<String>() {
 114             public String run() {
 115                 return FileManager.getPathToApplicationBundle();
 116             }
 117         });
 118     }
 119 
 120     static IconUIResource getAppIconCompositedOn(final SystemIcon systemIcon) {
 121         systemIcon.setSize(kAlertIconSize, kAlertIconSize);
 122         return getAppIconCompositedOn(systemIcon.createImage());
 123     }
 124 
 125     private static final int kAlertIconSize = 64;
 126     static IconUIResource getAppIconCompositedOn(final Image background) {
 127 
 128         final BufferedImage iconImage = getAppIconImageCompositedOn(background, 1);
 129 
 130         if (background instanceof MultiResolutionIconImage) {
 131             BufferedImage background2x
 132                     = ((MultiResolutionIconImage) background).resolutionVariant;
 133             BufferedImage icon2xImage = getAppIconImageCompositedOn(background2x, 2);
 134 
 135             return new IconUIResource(new ImageIcon(
 136                     new MultiResolutionIconImage(iconImage, icon2xImage)));
 137         }
 138         return new IconUIResource(new ImageIcon(iconImage));
 139     }
 140 
 141     static BufferedImage getAppIconImageCompositedOn(final Image background, int scaleFactor) {
 142 
 143         final int scaledAlertIconSize = kAlertIconSize * scaleFactor;
 144         final int kAlertSubIconSize = (int) (scaledAlertIconSize * 0.5);
 145         final int kAlertSubIconInset = scaledAlertIconSize - kAlertSubIconSize;
 146         final Icon smallAppIconScaled = new AquaIcon.CachingScalingIcon(
 147                 kAlertSubIconSize, kAlertSubIconSize) {
 148                     Image createImage() {
 149                         return getThisApplicationsIcon(kAlertSubIconSize, kAlertSubIconSize);
 150                     }
 151                 };
 152 
 153         final BufferedImage image = new BufferedImage(scaledAlertIconSize,
 154                 scaledAlertIconSize, BufferedImage.TYPE_INT_ARGB);
 155         final Graphics g = image.getGraphics();
 156         g.drawImage(background, 0, 0,
 157                 scaledAlertIconSize, scaledAlertIconSize, null);
 158         if (g instanceof Graphics2D) {
 159             // improves icon rendering quality in Quartz
 160             ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,
 161                     RenderingHints.VALUE_RENDER_QUALITY);
 162         }
 163 
 164         smallAppIconScaled.paintIcon(null, g,
 165                 kAlertSubIconInset, kAlertSubIconInset);
 166         g.dispose();
 167 
 168         return image;
 169     }
 170 
 171     public static IconUIResource getTreeFolderIcon() {
 172         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
 173         return AquaIcon.SystemIcon.getFolderIconUIResource();
 174     }
 175 
 176     public static IconUIResource getTreeOpenFolderIcon() {
 177         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
 178         return AquaIcon.SystemIcon.getOpenFolderIconUIResource();
 179     }
 180 
 181     public static IconUIResource getTreeDocumentIcon() {
 182         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
 183         return AquaIcon.SystemIcon.getDocumentIconUIResource();
 184     }
 185 
 186     public static UIResource getTreeExpandedIcon() {
 187         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
 188         return AquaIcon.getIconFor(new JRSUIControlSpec() {
 189             public void initIconPainter(final AquaPainter<? extends JRSUIState> painter) {
 190                 painter.state.set(Widget.DISCLOSURE_TRIANGLE);
 191                 painter.state.set(State.ACTIVE);
 192                 painter.state.set(Direction.DOWN);
 193                 painter.state.set(AlignmentHorizontal.CENTER);
 194                 painter.state.set(AlignmentVertical.CENTER);
 195             }
 196         }, 20, 20);
 197     }
 198 
 199     public static UIResource getTreeCollapsedIcon() {
 200         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
 201         return AquaIcon.getIconFor(new JRSUIControlSpec() {
 202             public void initIconPainter(final AquaPainter<? extends JRSUIState> painter) {
 203                 painter.state.set(Widget.DISCLOSURE_TRIANGLE);
 204                 painter.state.set(State.ACTIVE);
 205                 painter.state.set(Direction.RIGHT);
 206                 painter.state.set(AlignmentHorizontal.CENTER);
 207                 painter.state.set(AlignmentVertical.CENTER);
 208             }
 209         }, 20, 20);
 210     }
 211 
 212     public static UIResource getTreeRightToLeftCollapsedIcon() {
 213         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
 214         return AquaIcon.getIconFor(new JRSUIControlSpec() {
 215             public void initIconPainter(final AquaPainter<? extends JRSUIState> painter) {
 216                 painter.state.set(Widget.DISCLOSURE_TRIANGLE);
 217                 painter.state.set(State.ACTIVE);
 218                 painter.state.set(Direction.LEFT);
 219                 painter.state.set(AlignmentHorizontal.CENTER);
 220                 painter.state.set(AlignmentVertical.CENTER);
 221             }
 222         }, 20, 20);
 223     }
 224 
 225     static class NamedImageSingleton extends RecyclableSingleton<Image> {
 226         final String namedImage;
 227 
 228         NamedImageSingleton(final String namedImage) {
 229             this.namedImage = namedImage;
 230         }
 231 
 232         @Override
 233         protected Image getInstance() {
 234             return getNSIcon(namedImage);
 235         }
 236     }
 237 
 238     static class IconUIResourceSingleton extends RecyclableSingleton<IconUIResource> {
 239         final NamedImageSingleton holder;
 240 
 241         public IconUIResourceSingleton(final NamedImageSingleton holder) {
 242             this.holder = holder;
 243         }
 244 
 245         @Override
 246         protected IconUIResource getInstance() {
 247             return new IconUIResource(new ImageIcon(holder.get()));
 248         }
 249     }
 250 
 251     static class InvertableImageIcon extends ImageIcon implements InvertableIcon, UIResource {
 252         Icon invertedImage;
 253         public InvertableImageIcon(final Image image) {
 254             super(image);
 255         }
 256 
 257         @Override
 258         public Icon getInvertedIcon() {
 259             if (invertedImage != null) return invertedImage;
 260             return invertedImage = new IconUIResource(new ImageIcon(AquaUtils.generateLightenedImage(getImage(), 100)));
 261         }
 262     }
 263 
 264     protected static final NamedImageSingleton northArrow = new NamedImageSingleton("NSMenuScrollUp");
 265     protected static final IconUIResourceSingleton northArrowIcon = new IconUIResourceSingleton(northArrow);
 266     protected static final NamedImageSingleton southArrow = new NamedImageSingleton("NSMenuScrollDown");
 267     protected static final IconUIResourceSingleton southArrowIcon = new IconUIResourceSingleton(southArrow);
 268     protected static final NamedImageSingleton westArrow = new NamedImageSingleton("NSMenuSubmenuLeft");
 269     protected static final IconUIResourceSingleton westArrowIcon = new IconUIResourceSingleton(westArrow);
 270     protected static final NamedImageSingleton eastArrow = new NamedImageSingleton("NSMenuSubmenu");
 271     protected static final IconUIResourceSingleton eastArrowIcon = new IconUIResourceSingleton(eastArrow);
 272 
 273     static Image getArrowImageForDirection(final int direction) {
 274         switch(direction) {
 275             case SwingConstants.NORTH: return northArrow.get();
 276             case SwingConstants.SOUTH: return southArrow.get();
 277             case SwingConstants.EAST: return eastArrow.get();
 278             case SwingConstants.WEST: return westArrow.get();
 279         }
 280         return null;
 281     }
 282 
 283     static Icon getArrowIconForDirection(int direction) {
 284         switch(direction) {
 285             case SwingConstants.NORTH: return northArrowIcon.get();
 286             case SwingConstants.SOUTH: return southArrowIcon.get();
 287             case SwingConstants.EAST: return eastArrowIcon.get();
 288             case SwingConstants.WEST: return westArrowIcon.get();
 289         }
 290         return null;
 291     }
 292 
 293     public static Icon getMenuArrowIcon() {
 294         return new InvertableImageIcon(AquaUtils.generateLightenedImage(eastArrow.get(), 25));
 295     }
 296 
 297     public static Icon getMenuItemCheckIcon() {
 298         return new InvertableImageIcon(AquaUtils.generateLightenedImage(
 299                 getNSIcon("NSMenuItemSelection"), 25));
 300     }
 301 
 302     public static Icon getMenuItemDashIcon() {
 303         return new InvertableImageIcon(AquaUtils.generateLightenedImage(
 304                 getNSIcon("NSMenuMixedState"), 25));
 305     }
 306 
 307     private static Image getNSIcon(String imageName) {
 308         Image icon = Toolkit.getDefaultToolkit()
 309                 .getImage("NSImage://" + imageName);
 310 
 311         if (icon instanceof MultiResolutionImage) {
 312             return icon;
 313         }
 314 
 315         Image icon2x = AquaUtils.getCImageCreator().createImageFromName(
 316                 imageName, 2 * icon.getWidth(null), 2 * icon.getHeight(null));
 317         return new MultiResolutionBufferedImage(
 318                 BufferedImage.TYPE_INT_ARGB_PRE, 0, icon, icon2x);
 319     }
 320 
 321     public static class NineSliceMetrics {
 322         public final int wCut, eCut, nCut, sCut;
 323         public final int minW, minH;
 324         public final boolean showMiddle, stretchH, stretchV;
 325 
 326         public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
 327             this(minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
 328         }
 329 
 330         public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean showMiddle) {
 331             this(minWidth, minHeight, westCut, eastCut, northCut, southCut, showMiddle, true, true);
 332         }
 333 
 334         public 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) {
 335             this.wCut = westCut; this.eCut = eastCut; this.nCut = northCut; this.sCut = southCut;
 336             this.minW = minWidth; this.minH = minHeight;
 337             this.showMiddle = showMiddle; this.stretchH = stretchHorizontally; this.stretchV = stretchVertically;
 338         }
 339     }
 340 
 341     /*
 342      * A "paintable" which holds nine images, which represent a sliced up initial
 343      * image that can be streched from its middles.
 344      */
 345     public static class SlicedImageControl {
 346         final BufferedImage NW, N, NE;
 347         final BufferedImage W, C, E;
 348         final BufferedImage SW, S, SE;
 349 
 350         final NineSliceMetrics metrics;
 351 
 352         final int totalWidth, totalHeight;
 353         final int centerColWidth, centerRowHeight;
 354 
 355         public SlicedImageControl(final Image img, final int westCut, final int eastCut, final int northCut, final int southCut) {
 356             this(img, westCut, eastCut, northCut, southCut, true);
 357         }
 358 
 359         public SlicedImageControl(final Image img, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle) {
 360             this(img, westCut, eastCut, northCut, southCut, useMiddle, true, true);
 361         }
 362 
 363         public 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) {
 364             this(img, new NineSliceMetrics(img.getWidth(null), img.getHeight(null), westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically));
 365         }
 366 
 367         public SlicedImageControl(final Image img, final NineSliceMetrics metrics) {
 368             this.metrics = metrics;
 369 
 370             if (img.getWidth(null) != metrics.minW || img.getHeight(null) != metrics.minH) {
 371                 throw new IllegalArgumentException("SlicedImageControl: template image and NineSliceMetrics don't agree on minimum dimensions");
 372             }
 373 
 374             totalWidth = metrics.minW;
 375             totalHeight = metrics.minH;
 376             centerColWidth = totalWidth - metrics.wCut - metrics.eCut;
 377             centerRowHeight = totalHeight - metrics.nCut - metrics.sCut;
 378 
 379             NW = createSlice(img, 0, 0, metrics.wCut, metrics.nCut);
 380             N = createSlice(img, metrics.wCut, 0, centerColWidth, metrics.nCut);
 381             NE = createSlice(img, totalWidth - metrics.eCut, 0, metrics.eCut, metrics.nCut);
 382             W = createSlice(img, 0, metrics.nCut, metrics.wCut, centerRowHeight);
 383             C = metrics.showMiddle ? createSlice(img, metrics.wCut, metrics.nCut, centerColWidth, centerRowHeight) : null;
 384             E = createSlice(img, totalWidth - metrics.eCut, metrics.nCut, metrics.eCut, centerRowHeight);
 385             SW = createSlice(img, 0, totalHeight - metrics.sCut, metrics.wCut, metrics.sCut);
 386             S = createSlice(img, metrics.wCut, totalHeight - metrics.sCut, centerColWidth, metrics.sCut);
 387             SE = createSlice(img, totalWidth - metrics.eCut, totalHeight - metrics.sCut, metrics.eCut, metrics.sCut);
 388         }
 389 
 390         static BufferedImage createSlice(final Image img, final int x, final int y, final int w, final int h) {
 391             if (w == 0 || h == 0) return null;
 392 
 393             final BufferedImage slice = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
 394             final Graphics2D g2d = slice.createGraphics();
 395             g2d.drawImage(img, 0, 0, w, h, x, y, x + w, y + h, null);
 396             g2d.dispose();
 397 
 398             return slice;
 399         }
 400 
 401         public void paint(final Graphics g, final int x, final int y, final int w, final int h) {
 402             g.translate(x, y);
 403 
 404             if (w < totalWidth || h < totalHeight) {
 405                 paintCompressed(g, w, h);
 406             } else {
 407                 paintStretchedMiddles(g, w, h);
 408             }
 409 
 410             g.translate(-x, -y);
 411         }
 412 
 413         void paintStretchedMiddles(final Graphics g, final int w, final int h) {
 414             int baseX = metrics.stretchH ? 0 : ((w / 2) - (totalWidth / 2));
 415             int baseY = metrics.stretchV ? 0 : ((h / 2) - (totalHeight / 2));
 416             int adjustedWidth = metrics.stretchH ? w : totalWidth;
 417             int adjustedHeight = metrics.stretchV ? h : totalHeight;
 418 
 419             if (NW != null) g.drawImage(NW, baseX, baseY, null);
 420             if (N != null) g.drawImage(N, baseX + metrics.wCut, baseY, adjustedWidth - metrics.eCut - metrics.wCut, metrics.nCut, null);
 421             if (NE != null) g.drawImage(NE, baseX + adjustedWidth - metrics.eCut, baseY, null);
 422             if (W != null) g.drawImage(W, baseX, baseY + metrics.nCut, metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut, null);
 423             if (C != null) g.drawImage(C, baseX + metrics.wCut, baseY + metrics.nCut, adjustedWidth - metrics.eCut - metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut, null);
 424             if (E != null) g.drawImage(E, baseX + adjustedWidth - metrics.eCut, baseY + metrics.nCut, metrics.eCut, adjustedHeight - metrics.nCut - metrics.sCut, null);
 425             if (SW != null) g.drawImage(SW, baseX, baseY + adjustedHeight - metrics.sCut, null);
 426             if (S != null) g.drawImage(S, baseX + metrics.wCut, baseY + adjustedHeight - metrics.sCut, adjustedWidth - metrics.eCut - metrics.wCut, metrics.sCut, null);
 427             if (SE != null) g.drawImage(SE, baseX + adjustedWidth - metrics.eCut, baseY + adjustedHeight - metrics.sCut, null);
 428 
 429             /*
 430             if (NW != null) {g.setColor(Color.GREEN); g.fillRect(baseX, baseY, NW.getWidth(), NW.getHeight());}
 431             if (N != null) {g.setColor(Color.RED); g.fillRect(baseX + metrics.wCut, baseY, adjustedWidth - metrics.eCut - metrics.wCut, metrics.nCut);}
 432             if (NE != null) {g.setColor(Color.BLUE); g.fillRect(baseX + adjustedWidth - metrics.eCut, baseY, NE.getWidth(), NE.getHeight());}
 433             if (W != null) {g.setColor(Color.PINK); g.fillRect(baseX, baseY + metrics.nCut, metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut);}
 434             if (C != null) {g.setColor(Color.ORANGE); g.fillRect(baseX + metrics.wCut, baseY + metrics.nCut, adjustedWidth - metrics.eCut - metrics.wCut, adjustedHeight - metrics.nCut - metrics.sCut);}
 435             if (E != null) {g.setColor(Color.CYAN); g.fillRect(baseX + adjustedWidth - metrics.eCut, baseY + metrics.nCut, metrics.eCut, adjustedHeight - metrics.nCut - metrics.sCut);}
 436             if (SW != null) {g.setColor(Color.MAGENTA); g.fillRect(baseX, baseY + adjustedHeight - metrics.sCut, SW.getWidth(), SW.getHeight());}
 437             if (S != null) {g.setColor(Color.DARK_GRAY); g.fillRect(baseX + metrics.wCut, baseY + adjustedHeight - metrics.sCut, adjustedWidth - metrics.eCut - metrics.wCut, metrics.sCut);}
 438             if (SE != null) {g.setColor(Color.YELLOW); g.fillRect(baseX + adjustedWidth - metrics.eCut, baseY + adjustedHeight - metrics.sCut, SE.getWidth(), SE.getHeight());}
 439             */
 440         }
 441 
 442         void paintCompressed(final Graphics g, final int w, final int h) {
 443             final double heightRatio = h > totalHeight ? 1.0 : (double)h / (double)totalHeight;
 444             final double widthRatio = w > totalWidth ? 1.0 : (double)w / (double)totalWidth;
 445 
 446             final int northHeight = (int)(metrics.nCut * heightRatio);
 447             final int southHeight = (int)(metrics.sCut * heightRatio);
 448             final int centerHeight = h - northHeight - southHeight;
 449 
 450             final int westWidth = (int)(metrics.wCut * widthRatio);
 451             final int eastWidth = (int)(metrics.eCut * widthRatio);
 452             final int centerWidth = w - westWidth - eastWidth;
 453 
 454             if (NW != null) g.drawImage(NW, 0, 0, westWidth, northHeight, null);
 455             if (N != null) g.drawImage(N, westWidth, 0, centerWidth, northHeight, null);
 456             if (NE != null) g.drawImage(NE, w - eastWidth, 0, eastWidth, northHeight, null);
 457             if (W != null) g.drawImage(W, 0, northHeight, westWidth, centerHeight, null);
 458             if (C != null) g.drawImage(C, westWidth, northHeight, centerWidth, centerHeight, null);
 459             if (E != null) g.drawImage(E, w - eastWidth, northHeight, eastWidth, centerHeight, null);
 460             if (SW != null) g.drawImage(SW, 0, h - southHeight, westWidth, southHeight, null);
 461             if (S != null) g.drawImage(S, westWidth, h - southHeight, centerWidth, southHeight, null);
 462             if (SE != null) g.drawImage(SE, w - eastWidth, h - southHeight, eastWidth, southHeight, null);
 463         }
 464     }
 465 
 466     public abstract static class RecyclableSlicedImageControl extends RecyclableObject<SlicedImageControl> {
 467         final NineSliceMetrics metrics;
 468 
 469         public RecyclableSlicedImageControl(final NineSliceMetrics metrics) {
 470             this.metrics = metrics;
 471         }
 472 
 473         @Override
 474         protected SlicedImageControl create() {
 475             return new SlicedImageControl(createTemplateImage(metrics.minW, metrics.minH), metrics);
 476         }
 477 
 478         protected abstract Image createTemplateImage(final int width, final int height);
 479     }
 480 
 481     // when we use SystemColors, we need to proxy the color with something that implements UIResource,
 482     // so that it will be uninstalled when the look and feel is changed.
 483     private static class SystemColorProxy extends Color implements UIResource {
 484         final Color color;
 485         public SystemColorProxy(final Color color) {
 486             super(color.getRGB());
 487             this.color = color;
 488         }
 489 
 490         public int getRGB() {
 491             return color.getRGB();
 492         }
 493     }
 494 
 495     public static Color getWindowBackgroundColorUIResource() {
 496         //return AquaNativeResources.getWindowBackgroundColorUIResource();
 497         return new SystemColorProxy(SystemColor.window);
 498     }
 499 
 500     public static Color getTextSelectionBackgroundColorUIResource() {
 501         return new SystemColorProxy(SystemColor.textHighlight);
 502     }
 503 
 504     public static Color getTextSelectionForegroundColorUIResource() {
 505         return new SystemColorProxy(SystemColor.textHighlightText);
 506     }
 507 
 508     public static Color getSelectionBackgroundColorUIResource() {
 509         return new SystemColorProxy(SystemColor.controlHighlight);
 510     }
 511 
 512     public static Color getSelectionForegroundColorUIResource() {
 513         return new SystemColorProxy(SystemColor.controlLtHighlight);
 514     }
 515 
 516     public static Color getFocusRingColorUIResource() {
 517         return new SystemColorProxy(LWCToolkit.getAppleColor(LWCToolkit.KEYBOARD_FOCUS_COLOR));
 518     }
 519 
 520     public static Color getSelectionInactiveBackgroundColorUIResource() {
 521         return new SystemColorProxy(LWCToolkit.getAppleColor(LWCToolkit.INACTIVE_SELECTION_BACKGROUND_COLOR));
 522     }
 523 
 524     public static Color getSelectionInactiveForegroundColorUIResource() {
 525         return new SystemColorProxy(LWCToolkit.getAppleColor(LWCToolkit.INACTIVE_SELECTION_FOREGROUND_COLOR));
 526     }
 527 
 528     static class MultiResolutionIconImage extends BufferedImage
 529             implements MultiResolutionImage {
 530 
 531         BufferedImage resolutionVariant;
 532 
 533         public MultiResolutionIconImage(BufferedImage image, BufferedImage resolutionVariant) {
 534             super(image.getWidth(), image.getHeight(), image.getType());
 535             this.resolutionVariant = resolutionVariant;
 536             Graphics g = getGraphics();
 537             g.drawImage(image, 0, 0, null);
 538             g.dispose();
 539         }
 540 
 541         @Override
 542         public Image getResolutionVariant(int width, int height) {
 543             return ((width <= getWidth() && height <= getHeight()))
 544                     ? this : resolutionVariant;
 545         }
 546 
 547         @Override
 548         public List<Image> getResolutionVariants() {
 549             return Arrays.asList(this, resolutionVariant);
 550         }
 551     }
 552 }