jdk/src/share/classes/sun/swing/ImageCache.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 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.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Cache is used to cache an image based on a set of arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ImageCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    // Maximum number of entries to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int maxCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // The entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final private LinkedList<SoftReference<Entry>> entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public ImageCache(int maxCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.maxCount = maxCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        entries = new LinkedList<SoftReference<Entry>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    void setMaxCount(int maxCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.maxCount = maxCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Entry getEntry(Object key, GraphicsConfiguration config,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                           int w, int h, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Entry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Iterator<SoftReference<Entry>> iter = entries.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            SoftReference<Entry> ref = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            entry = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                // SoftReference was invalidated, remove the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            else if (entry.equals(config, w, h, args)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                // Put most recently used entries at the head
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                entries.addFirst(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // Entry doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        entry = new Entry(config, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (entries.size() >= maxCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            entries.removeLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        entries.addFirst(new SoftReference<Entry>(entry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the cached Image, or null, for the specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public Image getImage(Object key, GraphicsConfiguration config,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            int w, int h, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Entry entry = getEntry(key, config, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return entry.getImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Sets the cached image for the specified constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void setImage(Object key, GraphicsConfiguration config,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            int w, int h, Object[] args, Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Entry entry = getEntry(key, config, w, h, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        entry.setImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Caches set of arguments and Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static class Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        final private GraphicsConfiguration config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        final private int w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        final private int h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        final private Object[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        private Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Entry(GraphicsConfiguration config, int w, int h, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.config = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            this.w = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            this.h = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public void setImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String value = super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    "[ graphicsConfig=" + config +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    ", image=" + image +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    ", w=" + w + ", h=" + h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (args != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                for (int counter = 0; counter < args.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    value += ", " + args[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            value += "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public boolean equals(GraphicsConfiguration config,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                 int w, int h, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (this.w == w && this.h == h &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    ((this.config != null && this.config.equals(config)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    (this.config == null && config == null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (this.args == null && args == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (this.args != null && args != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        this.args.length == args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    for (int counter = args.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        Object a1 = this.args[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        Object a2 = args[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        if ((a1 == null && a2 != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                (a1 != null && !a1.equals(a2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}