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


 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 
 151         final BufferedImage image = new BufferedImage(scaledAlertIconSize,
 152                 scaledAlertIconSize, BufferedImage.TYPE_INT_ARGB);
 153         final Graphics g = image.getGraphics();
 154         g.drawImage(background, 0, 0,
 155                 scaledAlertIconSize, scaledAlertIconSize, null);
 156         if (g instanceof Graphics2D) {
 157             // improves icon rendering quality in Quartz
 158             ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,
 159                     RenderingHints.VALUE_RENDER_QUALITY);
 160         }
 161 
 162         smallAppIconScaled.paintIcon(null, g,
 163                 kAlertSubIconInset, kAlertSubIconInset);
 164         g.dispose();
 165 
 166         return image;
 167     }




  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.MultiResolutionBufferedImage;
  50 import sun.awt.image.MultiResolutionImage;
  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 getGenericJavaIcon();
  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());
  71     }
  72 
  73     public static IconUIResource getLockImageIcon() {
  74         // public, because UIDefaults.ProxyLazyValue uses reflection to get this value
  75         if (JRSUIUtils.Images.shouldUseLegacySecurityUIPath()) {
  76             final Image lockIcon = AquaUtils.getCImageCreator().createImageFromFile("/System/Library/CoreServices/SecurityAgent.app/Contents/Resources/Security.icns", kAlertIconSize, kAlertIconSize);
  77             return getAppIconCompositedOn(lockIcon);
  78         }
  79 
  80         final Image lockIcon = Toolkit.getDefaultToolkit().getImage("NSImage://NSSecurity");
  81         return getAppIconCompositedOn(lockIcon);
  82     }
  83 


















  84     static Image getGenericJavaIcon() {
  85         return java.security.AccessController.doPrivileged(new PrivilegedAction<Image>() {
  86             public Image run() {
  87                 return com.apple.eawt.Application.getApplication().getDockIconImage();
  88             }
  89         });
  90     }
  91 
  92     static String getPathToThisApplication() {
  93         return java.security.AccessController.doPrivileged(new PrivilegedAction<String>() {
  94             public String run() {
  95                 return FileManager.getPathToApplicationBundle();
  96             }
  97         });
  98     }
  99 
 100     static IconUIResource getAppIconCompositedOn(final SystemIcon systemIcon) {
 101         systemIcon.setSize(kAlertIconSize, kAlertIconSize);
 102         return getAppIconCompositedOn(systemIcon.createImage());
 103     }


 107 
 108         if (background instanceof MultiResolutionBufferedImage) {
 109             int width = background.getWidth(null);
 110             Image mrIconImage = ((MultiResolutionBufferedImage) background).map(
 111                     rv -> getAppIconImageCompositedOn(rv, rv.getWidth(null) / width));
 112             return new IconUIResource(new ImageIcon(mrIconImage));
 113         }
 114 
 115         BufferedImage iconImage = getAppIconImageCompositedOn(background, 1);
 116         return new IconUIResource(new ImageIcon(iconImage));
 117     }
 118 
 119     static BufferedImage getAppIconImageCompositedOn(final Image background, int scaleFactor) {
 120 
 121         final int scaledAlertIconSize = kAlertIconSize * scaleFactor;
 122         final int kAlertSubIconSize = (int) (scaledAlertIconSize * 0.5);
 123         final int kAlertSubIconInset = scaledAlertIconSize - kAlertSubIconSize;
 124         final Icon smallAppIconScaled = new AquaIcon.CachingScalingIcon(
 125                 kAlertSubIconSize, kAlertSubIconSize) {
 126                     Image createImage() {
 127                         return getGenericJavaIcon();
 128                     }
 129                 };
 130 
 131         final BufferedImage image = new BufferedImage(scaledAlertIconSize,
 132                 scaledAlertIconSize, BufferedImage.TYPE_INT_ARGB);
 133         final Graphics g = image.getGraphics();
 134         g.drawImage(background, 0, 0,
 135                 scaledAlertIconSize, scaledAlertIconSize, null);
 136         if (g instanceof Graphics2D) {
 137             // improves icon rendering quality in Quartz
 138             ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,
 139                     RenderingHints.VALUE_RENDER_QUALITY);
 140         }
 141 
 142         smallAppIconScaled.paintIcon(null, g,
 143                 kAlertSubIconInset, kAlertSubIconInset);
 144         g.dispose();
 145 
 146         return image;
 147     }