jdk/src/share/classes/sun/swing/CachedPainter.java
author alexp
Wed, 02 Sep 2009 17:47:19 +0400
changeset 3749 d0ecbd7075b1
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6797139: JButton title is truncating for some strings irrespective of preferred size. Reviewed-by: peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A base class used for icons or images that are expensive to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A subclass will do the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <li>Invoke <code>paint</code> when you want to paint the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *     if you are implementing <code>Icon</code> you'll invoke this from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *     <code>paintIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *     The args argument is useful when additional state is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <li>Override <code>paintToImage</code> to render the image.  The code that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *     lives here is equivalent to what previously would go in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *     <code>paintIcon</code>, for an <code>Icon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The two ways to use this class are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li>Invoke <code>paint</code> to draw the cached reprensentation at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li>Invoke <code>getImage</code> to get the cached reprensentation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     draw the image yourself.  This is primarly useful when you are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     using <code>VolatileImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public abstract class CachedPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // CacheMap maps from class to ImageCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final Map<Object,ImageCache> cacheMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                   new HashMap<Object,ImageCache>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static ImageCache getCache(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        synchronized(CachedPainter.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            ImageCache cache = cacheMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                cache = new ImageCache(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                cacheMap.put(key, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Creates an instance of <code>CachedPainter</code> that will cache up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * to <code>cacheCount</code> images of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param cacheCount Max number of images to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public CachedPainter(int cacheCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        getCache(getClass()).setMaxCount(cacheCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Renders the cached image to the the passed in <code>Graphic</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * If there is no cached image <code>paintToImage</code> will be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>paintImage</code> is invoked to paint the cached image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param c Component rendering to, this may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param x X-coordinate to render to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param y Y-coordinate to render to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param w Width to render in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param h Height to render in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param arg Variable arguments that will be passed to paintToImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void paint(Component c, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                         int y, int w, int h, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            synchronized(c.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                synchronized(CachedPainter.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    // If c is non-null, synchronize on the tree lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    // This is necessary because asking for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    // GraphicsConfiguration will grab a tree lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    paint0(c, g, x, y, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            synchronized(CachedPainter.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                paint0(c, g, x, y, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private void paint0(Component c, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                         int y, int w, int h, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Object key = getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        GraphicsConfiguration config = getGraphicsConfiguration(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        ImageCache cache = getCache(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Image image = cache.getImage(key, config, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int attempts = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            boolean draw = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (image instanceof VolatileImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // See if we need to recreate the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                switch (((VolatileImage)image).validate(config)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                case VolatileImage.IMAGE_INCOMPATIBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    ((VolatileImage)image).flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                case VolatileImage.IMAGE_RESTORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    draw = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                // Recreate the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                image = createImage(c, w, h, config, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                cache.setImage(key, config, w, h, args, image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                draw = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (draw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // Render to the Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                Graphics g2 = image.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                paintToImage(c, image, g2, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                g2.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // Render to the passed in Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            paintImage(c, g, x, y, w, h, image, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // If we did this 3 times and the contents are still lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // assume we're painting to a VolatileImage that is bogus and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // give up.  Presumably we'll be called again to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } while ((image instanceof VolatileImage) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                 ((VolatileImage)image).contentsLost() && ++attempts < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Paints the representation to cache to the supplied Graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param c Component painting to, may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param image Image to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param g Graphics to paint to, obtained from the passed in Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param w Width to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param h Height to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param args Arguments supplied to <code>paint</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected abstract void paintToImage(Component c, Image image, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                         int w, int h, Object[] args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Paints the image to the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param c Component painting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param x X coordinate to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param y Y coordinate to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param w Width to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param h Height to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param image Image to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param args Arguments supplied to <code>paint</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected void paintImage(Component c, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                              int x, int y, int w, int h, Image image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                              Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        g.drawImage(image, x, y, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Creates the image to cache.  This returns an opaque image, subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * that require translucency or transparency will need to override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param c Component painting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param w Width of image to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param h Height to image to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param config GraphicsConfiguration that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *        rendered to, this may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param args Arguments passed to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected Image createImage(Component c, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                GraphicsConfiguration config, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (config == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return config.createCompatibleVolatileImage(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Clear the image cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        synchronized(CachedPainter.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            getCache(getClass()).flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private GraphicsConfiguration getGraphicsConfiguration(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return c.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}