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.*;
  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<T>(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 AquaPainter.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 AquaPainter.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(final Graphics2D g, final T stateToPaint, final Component c);
  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     JRSUIControl getControl() {
  79         control.set(state = (T)state.derive());
  80         return control;
  81     }
  82 
  83     void paint(final Graphics g, final Component c, final int x, final int y, final int w, final int h) {
  84         boundsRect.setBounds(x, y, w, h);
  85 
  86         final T nextState = (T)state.derive();
  87         final Graphics2D g2d = getGraphics2D(g);
  88         if (g2d != null) paint(g2d, nextState, c);
  89         state = nextState;
  90     }
  91 
  92     static class AquaNineSlicingImagePainter<T extends JRSUIState> extends AquaPainter<T> {
  93         protected final HashMap<T, RecyclableJRSUISlicedImageControl> slicedControlImages;
  94         protected final NineSliceMetricsProvider metricsProvider;
  95 
  96         AquaNineSlicingImagePainter(final T state) {
  97             this(state, null);
  98         }
  99 
 100         AquaNineSlicingImagePainter(final T state, final NineSliceMetricsProvider metricsProvider) {
 101             super(new JRSUIControl(false), state);
 102             this.metricsProvider = metricsProvider;
 103             slicedControlImages = new HashMap<>();
 104         }
 105 
 106         @Override
 107         void paint(final Graphics2D g, final T stateToPaint, final Component c) {
 108             if (metricsProvider == null) {
 109                 AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, c, boundsRect);
 110                 return;
 111             }
 112 
 113             RecyclableJRSUISlicedImageControl slicesRef = slicedControlImages.get(stateToPaint);
 114             if (slicesRef == null) {
 115                 final NineSliceMetrics metrics = metricsProvider.getNineSliceMetricsForState(stateToPaint);
 116                 if (metrics == null) {
 117                     AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, c, boundsRect);
 118                     return;
 119                 }
 120                 slicesRef = new RecyclableJRSUISlicedImageControl(control, stateToPaint, metrics);
 121                 slicedControlImages.put(stateToPaint, slicesRef);
 122             }
 123             final SlicedImageControl slices = slicesRef.get();
 124             slices.paint(g, boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
 125         }
 126     }
 127 
 128     static class AquaSingleImagePainter<T extends JRSUIState> extends AquaPainter<T> {
 129         AquaSingleImagePainter(final T state) {
 130             super(new JRSUIControl(false), state);
 131         }
 132 
 133         @Override
 134         void paint(Graphics2D g, T stateToPaint, Component c) {
 135             paintFromSingleCachedImage(g, control, stateToPaint, c, boundsRect);
 136         }
 137 
 138         static void paintFromSingleCachedImage(final Graphics2D g, final JRSUIControl control, final JRSUIState controlState, final Component c, final Rectangle boundsRect) {
 139             final Rectangle clipRect = g.getClipBounds();
 140             final Rectangle intersection = boundsRect.intersection(clipRect);
 141             if (intersection.width <= 0 || intersection.height <= 0) return;
 142 
 143             final int imgX1 = intersection.x - boundsRect.x;
 144             final int imgY1 = intersection.y - boundsRect.y;
 145 
 146             final GraphicsConfiguration config = g.getDeviceConfiguration();
 147             final ImageCache cache = ImageCache.getInstance();
 148             BufferedImage image = (BufferedImage)cache.getImage(config, boundsRect.width, boundsRect.height, controlState);
 149             if (image == null) {
 150                 image = new BufferedImage(boundsRect.width, boundsRect.height, BufferedImage.TYPE_INT_ARGB_PRE);
 151                 cache.setImage(image, config, boundsRect.width, boundsRect.height, controlState);
 152                 final WritableRaster raster = image.getRaster();
 153                 final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
 154 
 155                 control.set(controlState);
 156                 control.paint(SunWritableRaster.stealData(buffer, 0),
 157                               image.getWidth(), image.getHeight(), 0, 0, boundsRect.width, boundsRect.height);
 158                 SunWritableRaster.markDirty(buffer);
 159             }
 160 
 161             g.drawImage(image, intersection.x, intersection.y, intersection.x + intersection.width, intersection.y + intersection.height,
 162                     imgX1, imgY1, imgX1 + intersection.width, imgY1 + intersection.height, null);
 163         }
 164     }
 165 
 166     static class RecyclableJRSUISlicedImageControl extends RecyclableSlicedImageControl {
 167         final JRSUIControl control;
 168         final JRSUIState state;
 169 
 170         RecyclableJRSUISlicedImageControl(final JRSUIControl control, final JRSUIState state, final NineSliceMetrics metrics) {
 171             super(metrics);
 172             this.control = control;
 173             this.state = state;
 174         }
 175 
 176         @Override
 177         protected Image createTemplateImage(int width, int height) {
 178             BufferedImage image = new BufferedImage(metrics.minW, metrics.minH, BufferedImage.TYPE_INT_ARGB_PRE);
 179 
 180             final WritableRaster raster = image.getRaster();
 181             final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
 182 
 183             control.set(state);
 184             control.paint(SunWritableRaster.stealData(buffer, 0), metrics.minW, metrics.minH, 0, 0, metrics.minW, metrics.minH);
 185 
 186             SunWritableRaster.markDirty(buffer);
 187 
 188             return image;
 189         }
 190     }
 191 
 192     protected Graphics2D getGraphics2D(final Graphics g) {
 193         try {
 194             return (SunGraphics2D)g; // doing a blind try is faster than checking instanceof
 195         } catch (Exception e) {
 196             if (g instanceof PeekGraphics) {
 197                 // if it is a peek just dirty the region
 198                 g.fillRect(boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
 199             } else if (g instanceof ProxyGraphics2D) {
 200                 final ProxyGraphics2D pg = (ProxyGraphics2D)g;
 201                 final Graphics2D g2d = pg.getDelegate();
 202                 if (g2d instanceof SunGraphics2D) { return (SunGraphics2D)g2d; }
 203             } else if (g instanceof Graphics2D) {
 204                 return (Graphics2D) g;
 205             }
 206         }
 207 
 208         return null;
 209     }
 210 }