src/macosx/classes/com/apple/laf/AquaImageFactory.java

Print this page




  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());


 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         if (background instanceof MultiResolutionBufferedImage) {
 129             int width = background.getWidth(null);
 130             Image mrIconImage = ((MultiResolutionBufferedImage) background).map(
 131                     rv -> getAppIconImageCompositedOn(rv, rv.getWidth(null) / width));
 132             return new IconUIResource(new ImageIcon(mrIconImage));
 133         }
 134 
 135         BufferedImage iconImage = getAppIconImageCompositedOn(background, 1);
 136         return new IconUIResource(new ImageIcon(iconImage));
 137     }
 138 
 139     static BufferedImage getAppIconImageCompositedOn(final Image background, int scaleFactor) {
 140 
 141         final int scaledAlertIconSize = kAlertIconSize * scaleFactor;
 142         final int kAlertSubIconSize = (int) (scaledAlertIconSize * 0.5);
 143         final int kAlertSubIconInset = scaledAlertIconSize - kAlertSubIconSize;
 144         final Icon smallAppIconScaled = new AquaIcon.CachingScalingIcon(
 145                 kAlertSubIconSize, kAlertSubIconSize) {
 146                     Image createImage() {
 147                         return getThisApplicationsIcon(kAlertSubIconSize, kAlertSubIconSize);
 148                     }
 149                 };
 150 


 289         return null;
 290     }
 291 
 292     public static Icon getMenuArrowIcon() {
 293         return new InvertableImageIcon(AquaUtils.generateLightenedImage(eastArrow.get(), 25));
 294     }
 295 
 296     public static Icon getMenuItemCheckIcon() {
 297         return new InvertableImageIcon(AquaUtils.generateLightenedImage(
 298                 getNSIcon("NSMenuItemSelection"), 25));
 299     }
 300 
 301     public static Icon getMenuItemDashIcon() {
 302         return new InvertableImageIcon(AquaUtils.generateLightenedImage(
 303                 getNSIcon("NSMenuMixedState"), 25));
 304     }
 305 
 306     private static Image getNSIcon(String imageName) {
 307         Image icon = Toolkit.getDefaultToolkit()
 308                 .getImage("NSImage://" + imageName);
 309 
 310         if (icon instanceof MultiResolutionImage) {
 311             return icon;
 312         }
 313 
 314         int w = icon.getWidth(null);
 315         int h = icon.getHeight(null);
 316 
 317         Dimension[] sizes = new Dimension[]{
 318             new Dimension(w, h), new Dimension(2 * w, 2 * h)
 319         };
 320 
 321         return new MultiResolutionBufferedImage(icon, sizes, (width, height) ->
 322                 AquaUtils.getCImageCreator().createImageFromName(
 323                         imageName, width, height));
 324     }
 325 
 326     public static class NineSliceMetrics {
 327         public final int wCut, eCut, nCut, sCut;
 328         public final int minW, minH;
 329         public final boolean showMiddle, stretchH, stretchV;
 330 
 331         public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
 332             this(minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
 333         }
 334 
 335         public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean showMiddle) {
 336             this(minWidth, minHeight, westCut, eastCut, northCut, southCut, showMiddle, true, true);
 337         }
 338 
 339         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) {
 340             this.wCut = westCut; this.eCut = eastCut; this.nCut = northCut; this.sCut = southCut;
 341             this.minW = minWidth; this.minH = minHeight;
 342             this.showMiddle = showMiddle; this.stretchH = stretchHorizontally; this.stretchV = stretchVertically;
 343         }
 344     }
 345 




  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 sun.awt.image.MultiResolutionImage;
  50 import sun.awt.image.MultiResolutionCachedImage;
  51 
  52 public class AquaImageFactory {
  53     public static IconUIResource getConfirmImageIcon() {
  54         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  55 
  56         return new IconUIResource(new AquaIcon.CachingScalingIcon(kAlertIconSize, kAlertIconSize) {
  57             Image createImage() {
  58                 return getThisApplicationsIcon(kAlertIconSize, kAlertIconSize);
  59             }
  60         });
  61     }
  62 
  63     public static IconUIResource getCautionImageIcon() {
  64         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  65         return getAppIconCompositedOn(AquaIcon.SystemIcon.getCautionIcon());
  66     }
  67 
  68     public static IconUIResource getStopImageIcon() {
  69         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  70         return getAppIconCompositedOn(AquaIcon.SystemIcon.getStopIcon());


 106             }
 107         });
 108     }
 109 
 110     static String getPathToThisApplication() {
 111         return java.security.AccessController.doPrivileged(new PrivilegedAction<String>() {
 112             public String run() {
 113                 return FileManager.getPathToApplicationBundle();
 114             }
 115         });
 116     }
 117 
 118     static IconUIResource getAppIconCompositedOn(final SystemIcon systemIcon) {
 119         systemIcon.setSize(kAlertIconSize, kAlertIconSize);
 120         return getAppIconCompositedOn(systemIcon.createImage());
 121     }
 122 
 123     private static final int kAlertIconSize = 64;
 124     static IconUIResource getAppIconCompositedOn(final Image background) {
 125 
 126         if (background instanceof MultiResolutionCachedImage) {
 127             int width = background.getWidth(null);
 128             Image mrIconImage = ((MultiResolutionCachedImage) background).map(
 129                     rv -> getAppIconImageCompositedOn(rv, rv.getWidth(null) / width));
 130             return new IconUIResource(new ImageIcon(mrIconImage));
 131         }
 132 
 133         BufferedImage iconImage = getAppIconImageCompositedOn(background, 1);
 134         return new IconUIResource(new ImageIcon(iconImage));
 135     }
 136 
 137     static BufferedImage getAppIconImageCompositedOn(final Image background, int scaleFactor) {
 138 
 139         final int scaledAlertIconSize = kAlertIconSize * scaleFactor;
 140         final int kAlertSubIconSize = (int) (scaledAlertIconSize * 0.5);
 141         final int kAlertSubIconInset = scaledAlertIconSize - kAlertSubIconSize;
 142         final Icon smallAppIconScaled = new AquaIcon.CachingScalingIcon(
 143                 kAlertSubIconSize, kAlertSubIconSize) {
 144                     Image createImage() {
 145                         return getThisApplicationsIcon(kAlertSubIconSize, kAlertSubIconSize);
 146                     }
 147                 };
 148 


 287         return null;
 288     }
 289 
 290     public static Icon getMenuArrowIcon() {
 291         return new InvertableImageIcon(AquaUtils.generateLightenedImage(eastArrow.get(), 25));
 292     }
 293 
 294     public static Icon getMenuItemCheckIcon() {
 295         return new InvertableImageIcon(AquaUtils.generateLightenedImage(
 296                 getNSIcon("NSMenuItemSelection"), 25));
 297     }
 298 
 299     public static Icon getMenuItemDashIcon() {
 300         return new InvertableImageIcon(AquaUtils.generateLightenedImage(
 301                 getNSIcon("NSMenuMixedState"), 25));
 302     }
 303 
 304     private static Image getNSIcon(String imageName) {
 305         Image icon = Toolkit.getDefaultToolkit()
 306                 .getImage("NSImage://" + imageName);


 307         return icon;
 308     }
 309 












 310     public static class NineSliceMetrics {
 311         public final int wCut, eCut, nCut, sCut;
 312         public final int minW, minH;
 313         public final boolean showMiddle, stretchH, stretchV;
 314 
 315         public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
 316             this(minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
 317         }
 318 
 319         public NineSliceMetrics(final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean showMiddle) {
 320             this(minWidth, minHeight, westCut, eastCut, northCut, southCut, showMiddle, true, true);
 321         }
 322 
 323         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) {
 324             this.wCut = westCut; this.eCut = eastCut; this.nCut = northCut; this.sCut = southCut;
 325             this.minW = minWidth; this.minH = minHeight;
 326             this.showMiddle = showMiddle; this.stretchH = stretchHorizontally; this.stretchV = stretchVertically;
 327         }
 328     }
 329