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.io.File;
  31 
  32 import javax.swing.*;
  33 import javax.swing.plaf.*;
  34 
  35 import apple.laf.JRSUIConstants.Size;
  36 import apple.laf.*;
  37 
  38 import com.apple.laf.AquaUtilControlSize.*;
  39 import com.apple.laf.AquaUtils.RecyclableSingleton;
  40 import sun.awt.image.MultiResolutionBufferedImage;
  41 
  42 public class AquaIcon {
  43     interface InvertableIcon extends Icon {
  44         public Icon getInvertedIcon();
  45     }
  46 
  47     static UIResource getIconFor(final JRSUIControlSpec spec, final int width, final int height) {
  48         return new CachableJRSUIIcon(width, height) {
  49             public void initIconPainter(final AquaPainter<JRSUIState> painter) {
  50                 spec.initIconPainter(painter);
  51             }
  52         };
  53     }
  54 
  55     // converts an object that is an icon into an image so we can hand it off to AppKit
  56     public static Image getImageForIcon(final Icon i) {
  57         if (i instanceof ImageIcon) return ((ImageIcon)i).getImage();
  58 
  59         final int w = i.getIconWidth();
  60         final int h = i.getIconHeight();
  61 
  62         if (w <= 0 || h <= 0) return null;
  63 
  64         // 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.
  65         final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  66         final Graphics g = image.getGraphics();
  67         i.paintIcon(null, g, 0, 0);
  68         g.dispose();
  69         return image;
  70     }
  71 
  72     public interface JRSUIControlSpec {
  73         public void initIconPainter(final AquaPainter<? extends JRSUIState> painter);
  74     }
  75 
  76     static abstract class JRSUIIcon implements Icon, UIResource {
  77         protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
  78 
  79         public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
  80             painter.paint(g, c, x, y, getIconWidth(), getIconHeight());
  81         }
  82     }
  83 
  84     static abstract class DynamicallySizingJRSUIIcon extends JRSUIIcon {
  85         protected final SizeDescriptor sizeDescriptor;
  86         protected SizeVariant sizeVariant;
  87 
  88         public DynamicallySizingJRSUIIcon(final SizeDescriptor sizeDescriptor) {
  89             this.sizeDescriptor = sizeDescriptor;
  90             this.sizeVariant = sizeDescriptor.regular;
  91             initJRSUIState();
  92         }
  93 
  94         public abstract void initJRSUIState();
  95 
  96         public int getIconHeight() {
  97             return sizeVariant == null ? 0 : sizeVariant.h;
  98         }
  99 
 100         public int getIconWidth() {
 101             return sizeVariant == null ? 0 : sizeVariant.w;
 102         }
 103 
 104         public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
 105             final Size size = c instanceof JComponent ? AquaUtilControlSize.getUserSizeFrom((JComponent)c) : Size.REGULAR;
 106             sizeVariant = sizeDescriptor.get(size);
 107             painter.state.set(size);
 108             super.paintIcon(c, g, x, y);
 109         }
 110     }
 111 
 112     static abstract class CachingScalingIcon implements Icon, UIResource {
 113         int width;
 114         int height;
 115         Image image;
 116 
 117         public CachingScalingIcon(final int width, final int height) {
 118             this.width = width;
 119             this.height = height;
 120         }
 121 
 122         void setSize(final int width, final int height) {
 123             this.width = width;
 124             this.height = height;
 125             this.image = null;
 126         }
 127 
 128         Image getImage() {
 129             if (image != null) return image;
 130 
 131             if (!GraphicsEnvironment.isHeadless()) {
 132                 image = getOptimizedImage();
 133             }
 134 
 135             return image;
 136         }
 137 
 138         private Image getOptimizedImage() {
 139             final Image img = createImage();
 140             // TODO: no RuntimeOptions for now
 141             //if (RuntimeOptions.getRenderer(null) != RuntimeOptions.Sun) return img;
 142             return getProgressiveOptimizedImage(img, getIconWidth(), getIconHeight());
 143         }
 144 
 145         static Image getProgressiveOptimizedImage(final Image img, final int w, final int h) {
 146             if (img == null) return null;
 147 
 148             final int halfImgW = img.getWidth(null) / 2;
 149             final int halfImgH = img.getHeight(null) / 2;
 150             if (w * 2 > halfImgW && h * 2 > halfImgH) return img;
 151 
 152             final BufferedImage halfImage = new BufferedImage(halfImgW, halfImgH, BufferedImage.TYPE_INT_ARGB);
 153             final Graphics g = halfImage.getGraphics();
 154             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 155             g.drawImage(img, 0, 0, halfImgW, halfImgH, null);
 156             g.dispose();
 157 
 158             return getProgressiveOptimizedImage(halfImage, w, h);
 159         }
 160 
 161         abstract Image createImage();
 162 
 163         public boolean hasIconRef() {
 164             return getImage() != null;
 165         }
 166 
 167         public void paintIcon(final Component c, Graphics g, final int x, final int y) {
 168             g = g.create();
 169 
 170             if (g instanceof Graphics2D) {
 171                 // improves icon rendering quality in Quartz
 172                 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 173             }
 174 
 175             final Image myImage = getImage();
 176             if (myImage != null) {
 177                 g.drawImage(myImage, x, y, getIconWidth(), getIconHeight(), null);
 178             }
 179 
 180             g.dispose();
 181         }
 182 
 183         public int getIconWidth() {
 184             return width;
 185         }
 186 
 187         public int getIconHeight() {
 188             return height;
 189         }
 190 
 191     }
 192 
 193     static abstract class CachableJRSUIIcon extends CachingScalingIcon implements UIResource {
 194         public CachableJRSUIIcon(final int width, final int height) {
 195             super(width, height);
 196         }
 197 
 198         Image createImage() {
 199             final AquaPainter<JRSUIState> painter =
 200                     AquaPainter.create(JRSUIState.getInstance());
 201             initIconPainter(painter);
 202 
 203             final int w = getIconWidth();
 204             final int h = getIconHeight();
 205             Image baseImage = createImage(painter, w, h, w, h);
 206 
 207             return new MultiResolutionBufferedImage(
 208                     baseImage, (rvWidth, rvHeight) ->
 209                             createImage(painter, w, h, rvWidth, rvHeight));
 210         }
 211 
 212         private static Image createImage(AquaPainter<JRSUIState> painter,
 213                 int width, int height, int rvWidth, int rvHeight) {
 214             final BufferedImage img = new BufferedImage(rvWidth, rvHeight,
 215                     BufferedImage.TYPE_INT_ARGB_PRE);
 216             final Graphics g = img.getGraphics();
 217             g.setClip(new Rectangle(0, 0, rvWidth, rvHeight));
 218 
 219             ((Graphics2D) g).scale(rvWidth / width, rvHeight / height);
 220             painter.paint(g, null, 0, 0, width, height);
 221 
 222             g.dispose();
 223             return img;
 224         }
 225 
 226         public abstract void initIconPainter(final AquaPainter<JRSUIState> painter);
 227     }
 228 
 229     static class FileIcon extends CachingScalingIcon {
 230         final File file;
 231 
 232         public FileIcon(final File file, final int width, final int height) {
 233             super(width, height);
 234             this.file = file;
 235         }
 236 
 237         public FileIcon(final File file) {
 238             this(file, 16, 16);
 239         }
 240 
 241         Image createImage() {
 242             return AquaUtils.getCImageCreator().createImageOfFile(file.getAbsolutePath(), getIconWidth(), getIconHeight());
 243         }
 244     }
 245 
 246     static class SystemIconSingleton extends RecyclableSingleton<SystemIcon> {
 247         final String selector;
 248 
 249         public SystemIconSingleton(String selector) {
 250             this.selector = selector;
 251         }
 252 
 253         @Override
 254         protected SystemIcon getInstance() {
 255             return new SystemIcon(selector);
 256         }
 257     }
 258 
 259     static class SystemIconUIResourceSingleton extends RecyclableSingleton<IconUIResource> {
 260         final String selector;
 261 
 262         public SystemIconUIResourceSingleton(String selector) {
 263             this.selector = selector;
 264         }
 265 
 266         @Override
 267         protected IconUIResource getInstance() {
 268             return new IconUIResource(new SystemIcon(selector));
 269         }
 270     }
 271 
 272     static class SystemIcon extends CachingScalingIcon {
 273         private static final SystemIconUIResourceSingleton folderIcon = new SystemIconUIResourceSingleton("fldr");
 274         static IconUIResource getFolderIconUIResource() { return folderIcon.get(); }
 275 
 276         private static final SystemIconUIResourceSingleton openFolderIcon = new SystemIconUIResourceSingleton("ofld");
 277         static IconUIResource getOpenFolderIconUIResource() { return openFolderIcon.get(); }
 278 
 279         private static final SystemIconUIResourceSingleton desktopIcon = new SystemIconUIResourceSingleton("desk");
 280         static IconUIResource getDesktopIconUIResource() { return desktopIcon.get(); }
 281 
 282         private static final SystemIconUIResourceSingleton computerIcon = new SystemIconUIResourceSingleton("FNDR");
 283         static IconUIResource getComputerIconUIResource() { return computerIcon.get(); }
 284 
 285         private static final SystemIconUIResourceSingleton documentIcon = new SystemIconUIResourceSingleton("docu");
 286         static IconUIResource getDocumentIconUIResource() { return documentIcon.get(); }
 287 
 288         private static final SystemIconUIResourceSingleton hardDriveIcon = new SystemIconUIResourceSingleton("hdsk");
 289         static IconUIResource getHardDriveIconUIResource() { return hardDriveIcon.get(); }
 290 
 291         private static final SystemIconUIResourceSingleton floppyIcon = new SystemIconUIResourceSingleton("flpy");
 292         static IconUIResource getFloppyIconUIResource() { return floppyIcon.get(); }
 293 
 294         //private static final SystemIconUIResourceSingleton noteIcon = new SystemIconUIResourceSingleton("note");
 295         //static IconUIResource getNoteIconUIResource() { return noteIcon.get(); }
 296 
 297         private static final SystemIconSingleton caut = new SystemIconSingleton("caut");
 298         static SystemIcon getCautionIcon() { return caut.get(); }
 299 
 300         private static final SystemIconSingleton stop = new SystemIconSingleton("stop");
 301         static SystemIcon getStopIcon() { return stop.get(); }
 302 
 303         final String selector;
 304 
 305         public SystemIcon(final String iconSelector, final int width, final int height) {
 306             super(width, height);
 307             selector = iconSelector;
 308         }
 309 
 310         public SystemIcon(final String iconSelector) {
 311             this(iconSelector, 16, 16);
 312         }
 313 
 314         Image createImage() {
 315             return AquaUtils.getCImageCreator().createSystemImageFromSelector(
 316                     selector, getIconWidth(), getIconHeight());
 317         }
 318     }
 319 }