1 19 package org.netbeans.swing.tabcontrol.plaf; 20 21 import javax.swing.*; 22 import java.awt.*; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.image.BufferedImage ; 25 import java.util.HashMap ; 26 import java.util.Arrays ; 27 import java.util.Date ; 28 29 40 public class ChicletWrapper implements Runnable { 41 private boolean allowVertical = true; 42 private boolean leftNotch = false; 43 private boolean rightNotch = false; 44 private int state = 0; 45 private Rectangle bounds = new Rectangle(); 46 private float[] arcs = new float[4]; 47 GenericGlowingChiclet chiclet = GenericGlowingChiclet.INSTANCE; 49 public void setState (int state) { 50 this.state = state; 51 } 52 53 public void setBounds (int x, int y, int w, int h) { 54 bounds.setBounds (x, y, w, h); 55 } 56 57 static int drawCount = 0; 58 public void draw (Graphics g) { 59 if (bounds.width == 0 || bounds.height == 0) { 60 return; 61 } 62 BufferedImage img = findBufferedImage(); 63 ((Graphics2D) g).drawRenderedImage(img, AffineTransform.getTranslateInstance(0, 0)); 64 drawCount ++; 65 if (drawCount % 100 == 0) { 66 SwingUtilities.invokeLater(this); 68 } 69 } 70 71 public void setArcs (float a, float b, float c, float d) { 72 arcs[0] = a; 73 arcs[1] = b; 74 arcs[2] = c; 75 arcs[3] = d; 76 } 77 78 79 public void setAllowVertical (boolean b) { 80 allowVertical = b; 81 } 82 83 public void setNotch (boolean right, boolean left) { 84 leftNotch = left; 85 rightNotch = right; 86 } 87 88 public Long hash() { 89 long result = 90 state * 701 91 + Double.doubleToLongBits(arcs[0]) * 31 92 + Double.doubleToLongBits(arcs[1]) * 37 93 + Double.doubleToLongBits(arcs[2]) * 43 94 + Double.doubleToLongBits(arcs[3]) * 47 95 + bounds.width * 6703 96 + bounds.height * 1783; 97 98 if (leftNotch) { 99 result *= 3121; 100 } 101 if (rightNotch) { 102 result *= 4817; 103 } 104 if (allowVertical) { 105 result *= 1951; 106 } 107 108 return new Long (result); 109 } 110 111 private static HashMap <CacheEntry,BufferedImage > cache = new HashMap <CacheEntry,BufferedImage >(); 112 113 private BufferedImage findBufferedImage() { 114 Long hash = hash(); 115 CacheEntry entry = new CacheEntry (hash); 116 117 BufferedImage result = cache.get(entry); 118 if (result == null) { 119 result = createImage(); 120 } 121 cache.put (entry, result); 123 return result; 124 } 125 126 private BufferedImage createImage() { 127 BufferedImage img = new BufferedImage (bounds.width, bounds.height, 128 BufferedImage.TYPE_INT_ARGB_PRE); 129 chiclet.setNotch(rightNotch, leftNotch); 130 chiclet.setArcs (arcs[0], arcs[1], arcs[2], arcs[3]); 131 chiclet.setBounds (bounds.x, bounds.y, bounds.width, bounds.height); 132 chiclet.setAllowVertical(allowVertical); 133 chiclet.setState (state); 134 Graphics g = img.getGraphics(); 135 g.translate (-bounds.x, -bounds.y); 136 ColorUtil.setupAntialiasing(g); 137 chiclet.draw((Graphics2D)g); 138 g.translate (bounds.x, bounds.y); 139 return img; 140 } 141 142 public void run() { 143 if (cache.size() < 5) { 144 return; 145 } 146 HashMap <CacheEntry,BufferedImage > newCache = new HashMap <CacheEntry,BufferedImage >( cache ); 147 long startTime = System.currentTimeMillis(); 148 CacheEntry[] entries = (CacheEntry[]) newCache.keySet().toArray(new CacheEntry[0]); 149 Arrays.sort (entries); 150 for (int i=entries.length-1; i >= entries.length / 3; i--) { 151 if (startTime - entries[i].timestamp > 240000) { 152 newCache.remove (entries[i]); 153 } 154 } 155 cache = newCache; 156 } 157 158 private static final class CacheEntry implements Comparable { 159 private final Long hash; 160 long timestamp = System.currentTimeMillis(); 161 public CacheEntry (Long hash) { 162 this.hash = hash; 163 } 164 165 public boolean equals (Object o) { 166 if (o instanceof CacheEntry) { 167 CacheEntry other = (CacheEntry) o; 168 return other.hash() == hash(); 169 } else if (o instanceof Long ) { 170 return ((Long ) o).longValue() == hash(); 171 } else { 172 return false; 173 } 174 } 175 176 long hash() { 177 return hash.longValue(); 178 } 179 180 public int hashCode() { 181 return hash.intValue(); 182 } 183 184 public int compareTo(Object o) { 185 CacheEntry other = (CacheEntry) o; 186 return (int) (timestamp - other.timestamp); 188 } 189 190 public String toString() { 191 return "CacheEntry: " + new Date (timestamp) + " hash " + hash(); 192 } 193 194 } 195 196 } 197 | Popular Tags |