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

Print this page


   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


  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 }
   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


  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 }