001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.swing.tabcontrol.plaf;
042:
043: import javax.swing.*;
044: import java.awt.*;
045: import java.awt.geom.AffineTransform;
046: import java.awt.image.BufferedImage;
047: import java.util.HashMap;
048: import java.util.Arrays;
049: import java.util.Date;
050:
051: /**
052: * GenericGlowingChiclet works nicely to draw Aqua-style decorations, but is a bit
053: * slow. In particular, Area.pruneEdges() is not cheap, but is heavily used to divide
054: * the geometry.
055: *
056: * This wrapper class provides a cache of bitmaps painted by GenericGlowingChiclet,
057: * timestamped against the last
058: * time they were used, and occasionally prunes not recently used bitmaps.
059: *
060: * @author Tim Boudreau
061: */
062: public class ChicletWrapper implements Runnable {
063: private boolean allowVertical = true;
064: private boolean leftNotch = false;
065: private boolean rightNotch = false;
066: private int state = 0;
067: private Rectangle bounds = new Rectangle();
068: private float[] arcs = new float[4];
069: GenericGlowingChiclet chiclet = GenericGlowingChiclet.INSTANCE; //XXX kill static instance
070:
071: public void setState(int state) {
072: this .state = state;
073: }
074:
075: public void setBounds(int x, int y, int w, int h) {
076: bounds.setBounds(x, y, w, h);
077: }
078:
079: static int drawCount = 0;
080:
081: public void draw(Graphics g) {
082: if (bounds.width == 0 || bounds.height == 0) {
083: return;
084: }
085: BufferedImage img = findBufferedImage();
086: ((Graphics2D) g).drawRenderedImage(img, AffineTransform
087: .getTranslateInstance(0, 0));
088: drawCount++;
089: if (drawCount % 100 == 0) {
090: //Occasionally prune old bitmaps
091: SwingUtilities.invokeLater(this );
092: }
093: }
094:
095: public void setArcs(float a, float b, float c, float d) {
096: arcs[0] = a;
097: arcs[1] = b;
098: arcs[2] = c;
099: arcs[3] = d;
100: }
101:
102: public void setAllowVertical(boolean b) {
103: allowVertical = b;
104: }
105:
106: public void setNotch(boolean right, boolean left) {
107: leftNotch = left;
108: rightNotch = right;
109: }
110:
111: public Long hash() {
112: long result = state * 701 + Double.doubleToLongBits(arcs[0])
113: * 31 + Double.doubleToLongBits(arcs[1]) * 37
114: + Double.doubleToLongBits(arcs[2]) * 43
115: + Double.doubleToLongBits(arcs[3]) * 47 + bounds.width
116: * 6703 + bounds.height * 1783;
117:
118: if (leftNotch) {
119: result *= 3121;
120: }
121: if (rightNotch) {
122: result *= 4817;
123: }
124: if (allowVertical) {
125: result *= 1951;
126: }
127:
128: return new Long(result);
129: }
130:
131: private static HashMap<CacheEntry, BufferedImage> cache = new HashMap<CacheEntry, BufferedImage>();
132:
133: private BufferedImage findBufferedImage() {
134: Long hash = hash();
135: CacheEntry entry = new CacheEntry(hash);
136:
137: BufferedImage result = cache.get(entry);
138: if (result == null) {
139: result = createImage();
140: }
141: //Store our new entry with new timestamp, even if we found an old one
142: cache.put(entry, result);
143: return result;
144: }
145:
146: private BufferedImage createImage() {
147: BufferedImage img = new BufferedImage(bounds.width,
148: bounds.height, BufferedImage.TYPE_INT_ARGB_PRE);
149: chiclet.setNotch(rightNotch, leftNotch);
150: chiclet.setArcs(arcs[0], arcs[1], arcs[2], arcs[3]);
151: chiclet.setBounds(bounds.x, bounds.y, bounds.width,
152: bounds.height);
153: chiclet.setAllowVertical(allowVertical);
154: chiclet.setState(state);
155: Graphics g = img.getGraphics();
156: g.translate(-bounds.x, -bounds.y);
157: ColorUtil.setupAntialiasing(g);
158: chiclet.draw((Graphics2D) g);
159: g.translate(bounds.x, bounds.y);
160: return img;
161: }
162:
163: public void run() {
164: if (cache.size() < 5) {
165: return;
166: }
167: HashMap<CacheEntry, BufferedImage> newCache = new HashMap<CacheEntry, BufferedImage>(
168: cache);
169: long startTime = System.currentTimeMillis();
170: CacheEntry[] entries = (CacheEntry[]) newCache.keySet()
171: .toArray(new CacheEntry[0]);
172: Arrays.sort(entries);
173: for (int i = entries.length - 1; i >= entries.length / 3; i--) {
174: if (startTime - entries[i].timestamp > 240000) {
175: newCache.remove(entries[i]);
176: }
177: }
178: cache = newCache;
179: }
180:
181: private static final class CacheEntry implements Comparable {
182: private final Long hash;
183: long timestamp = System.currentTimeMillis();
184:
185: public CacheEntry(Long hash) {
186: this .hash = hash;
187: }
188:
189: public boolean equals(Object o) {
190: if (o instanceof CacheEntry) {
191: CacheEntry other = (CacheEntry) o;
192: return other.hash() == hash();
193: } else if (o instanceof Long) {
194: return ((Long) o).longValue() == hash();
195: } else {
196: return false;
197: }
198: }
199:
200: long hash() {
201: return hash.longValue();
202: }
203:
204: public int hashCode() {
205: return hash.intValue();
206: }
207:
208: public int compareTo(Object o) {
209: CacheEntry other = (CacheEntry) o;
210: //Okay, every 4 days we might let an unused bitmap get old
211: return (int) (timestamp - other.timestamp);
212: }
213:
214: public String toString() {
215: return "CacheEntry: " + new Date(timestamp) + " hash "
216: + hash();
217: }
218:
219: }
220:
221: }
|