1 /*
   2  * Copyright (c) 2011, 2013, 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.*;
  30 import java.util.HashMap;
  31 
  32 import com.apple.laf.AquaImageFactory.RecyclableSlicedImageControl;
  33 import com.apple.laf.AquaImageFactory.NineSliceMetrics;
  34 import com.apple.laf.AquaImageFactory.SlicedImageControl;
  35 
  36 import sun.awt.image.*;
  37 import sun.java2d.*;
  38 import sun.print.*;
  39 import apple.laf.*;
  40 import apple.laf.JRSUIUtils.NineSliceMetricsProvider;
  41 
  42 abstract class AquaPainter <T extends JRSUIState> {
  43     static <T extends JRSUIState> AquaPainter<T> create(final T state) {
  44         return new AquaSingleImagePainter<>(state);
  45     }
  46 
  47     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
  48         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
  49     }
  50 
  51     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle) {
  52         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, true, true);
  53     }
  54 
  55     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle, final boolean stretchHorizontally, final boolean stretchVertically) {
  56         return create(state, new NineSliceMetricsProvider() {
  57             @Override
  58                public NineSliceMetrics getNineSliceMetricsForState(JRSUIState state) {
  59                 return new NineSliceMetrics(minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically);
  60             }
  61         });
  62     }
  63 
  64     static <T extends JRSUIState> AquaPainter<T> create(final T state, final NineSliceMetricsProvider metricsProvider) {
  65         return new AquaNineSlicingImagePainter<>(state, metricsProvider);
  66     }
  67 
  68     abstract void paint(Graphics2D g, T stateToPaint);
  69 
  70     final Rectangle boundsRect = new Rectangle();
  71     final JRSUIControl control;
  72     T state;
  73     AquaPainter(final JRSUIControl control, final T state) {
  74         this.control = control;
  75         this.state = state;
  76     }
  77 
  78     final JRSUIControl getControl() {
  79         control.set(state = state.derive());
  80         return control;
  81     }
  82 
  83     final void paint(final Graphics g, final Component c, final int x,
  84                      final int y, final int w, final int h) {
  85         boundsRect.setBounds(x, y, w, h);
  86 
  87         final T nextState = state.derive();
  88         final Graphics2D g2d = getGraphics2D(g);
  89         if (g2d != null) paint(g2d, nextState);
  90         state = nextState;
  91     }
  92 
  93     private static class AquaNineSlicingImagePainter<T extends JRSUIState>
  94             extends AquaPainter<T> {
  95 
  96         private final HashMap<T, RecyclableJRSUISlicedImageControl> slicedControlImages;
  97         private final NineSliceMetricsProvider metricsProvider;
  98 
  99         AquaNineSlicingImagePainter(final T state) {
 100             this(state, null);
 101         }
 102 
 103         AquaNineSlicingImagePainter(final T state, final NineSliceMetricsProvider metricsProvider) {
 104             super(new JRSUIControl(false), state);
 105             this.metricsProvider = metricsProvider;
 106             slicedControlImages = new HashMap<>();
 107         }
 108 
 109         @Override
 110         void paint(final Graphics2D g, final T stateToPaint) {
 111             if (metricsProvider == null) {
 112                 AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 113                 return;
 114             }
 115 
 116             RecyclableJRSUISlicedImageControl slicesRef = slicedControlImages.get(stateToPaint);
 117             if (slicesRef == null) {
 118                 final NineSliceMetrics metrics = metricsProvider.getNineSliceMetricsForState(stateToPaint);
 119                 if (metrics == null) {
 120                     AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 121                     return;
 122                 }
 123                 slicesRef = new RecyclableJRSUISlicedImageControl(control, stateToPaint, metrics);
 124                 slicedControlImages.put(stateToPaint, slicesRef);
 125             }
 126             final SlicedImageControl slices = slicesRef.get();
 127             slices.paint(g, boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
 128         }
 129     }
 130 
 131     private static final class AquaSingleImagePainter<T extends JRSUIState>
 132             extends AquaPainter<T> {
 133 
 134         AquaSingleImagePainter(final T state) {
 135             super(new JRSUIControl(false), state);
 136         }
 137 
 138         @Override
 139         void paint(final Graphics2D g, final T stateToPaint) {
 140             paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 141         }
 142 
 143         static void paintFromSingleCachedImage(final Graphics2D g,
 144                 final JRSUIControl control, final JRSUIState controlState,
 145                 final Rectangle bounds) {
 146             if (bounds.width <= 0 || bounds.height <= 0) {
 147                 return;
 148             }
 149 
 150             int scale = 1;
 151             if (g instanceof SunGraphics2D) {
 152                 scale = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 153             }
 154             final GraphicsConfiguration config = g.getDeviceConfiguration();
 155             final ImageCache cache = ImageCache.getInstance();
 156             final int imgW = bounds.width * scale;
 157             final int imgH = bounds.height * scale;
 158             BufferedImage img = (BufferedImage) cache.getImage(config, imgW, imgH, scale, controlState);
 159             if (img == null) {
 160                 img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB_PRE);
 161                 cache.setImage(img, config, imgW, imgH, scale, controlState);
 162                 final WritableRaster raster = img.getRaster();
 163                 final DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
 164 
 165                 control.set(controlState);
 166                 control.paint(SunWritableRaster.stealData(buffer, 0),
 167                         imgW, imgH, 0, 0, bounds.width, bounds.height);
 168                 SunWritableRaster.markDirty(buffer);
 169             }
 170 
 171             g.drawImage(img, bounds.x, bounds.y, bounds.width, bounds.height, null);
 172         }
 173     }
 174 
 175     private static class RecyclableJRSUISlicedImageControl
 176             extends RecyclableSlicedImageControl {
 177 
 178         private final JRSUIControl control;
 179         private final JRSUIState state;
 180 
 181         RecyclableJRSUISlicedImageControl(final JRSUIControl control, final JRSUIState state, final NineSliceMetrics metrics) {
 182             super(metrics);
 183             this.control = control;
 184             this.state = state;
 185         }
 186 
 187         @Override
 188         protected Image createTemplateImage(int width, int height) {
 189             BufferedImage image = new BufferedImage(metrics.minW, metrics.minH, BufferedImage.TYPE_INT_ARGB_PRE);
 190 
 191             final WritableRaster raster = image.getRaster();
 192             final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
 193 
 194             control.set(state);
 195             control.paint(SunWritableRaster.stealData(buffer, 0), metrics.minW, metrics.minH, 0, 0, metrics.minW, metrics.minH);
 196 
 197             SunWritableRaster.markDirty(buffer);
 198 
 199             return image;
 200         }
 201     }
 202 
 203     private Graphics2D getGraphics2D(final Graphics g) {
 204         try {
 205             return (SunGraphics2D)g; // doing a blind try is faster than checking instanceof
 206         } catch (Exception ignored) {
 207             if (g instanceof PeekGraphics) {
 208                 // if it is a peek just dirty the region
 209                 g.fillRect(boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
 210             } else if (g instanceof ProxyGraphics2D) {
 211                 final ProxyGraphics2D pg = (ProxyGraphics2D)g;
 212                 final Graphics2D g2d = pg.getDelegate();
 213                 if (g2d instanceof SunGraphics2D) {
 214                     return g2d;
 215                 }
 216             } else if (g instanceof Graphics2D) {
 217                 return (Graphics2D) g;
 218             }
 219         }
 220 
 221         return null;
 222     }
 223 }