1 7 package javax.swing.plaf.metal; 8 9 import java.awt.*; 10 import java.awt.image.*; 11 import javax.swing.Icon ; 12 import java.lang.ref.SoftReference ; 13 import java.util.*; 14 15 30 abstract class CachedPainter { 31 private static final Map<Object ,Cache> cacheMap = 33 new HashMap<Object ,Cache>(); 34 35 36 private static Cache getCache(Object key) { 37 synchronized(cacheMap) { 38 Cache cache = cacheMap.get(key); 39 if (cache == null) { 40 cache = new Cache(1); 41 cacheMap.put(key, cache); 42 } 43 return cache; 44 } 45 } 46 47 53 public CachedPainter(int cacheCount) { 54 getCache(getClass()).setMaxCount(cacheCount); 55 } 56 57 62 protected void paint(Component c, Graphics g, int x, 63 int y, int w, int h, Object ... args) { 64 if (w <= 0 || h <= 0) { 65 return; 66 } 67 Object key = getClass(); 68 GraphicsConfiguration config = c.getGraphicsConfiguration(); 69 Cache cache = getCache(key); 70 Image image = cache.getImage(key, config, w, h, args); 71 int attempts = 0; 72 do { 73 boolean draw = false; 74 if (image instanceof VolatileImage) { 75 switch (((VolatileImage)image).validate(config)) { 77 case VolatileImage.IMAGE_INCOMPATIBLE: 78 ((VolatileImage)image).flush(); 79 image = null; 80 break; 81 case VolatileImage.IMAGE_RESTORED: 82 draw = true; 83 break; 84 } 85 } 86 if (image == null) { 87 image = createImage(c, w, h, config); 89 cache.setImage(key, config, w, h, args, image); 90 draw = true; 91 } 92 if (draw) { 93 Graphics g2 = image.getGraphics(); 95 paintToImage(c, g2, w, h, args); 96 g2.dispose(); 97 } 98 99 paintImage(c, g, x, y, w, h, image, args); 101 102 } while ((image instanceof VolatileImage) && 106 ((VolatileImage)image).contentsLost() && ++attempts < 3); 107 } 108 109 118 protected abstract void paintToImage(Component c, Graphics g, 119 int w, int h, Object [] args); 120 121 122 134 protected void paintImage(Component c, Graphics g, 135 int x, int y, int w, int h, Image image, 136 Object [] args) { 137 g.drawImage(image, x, y, null); 138 } 139 140 151 protected Image createImage(Component c, int w, int h, 152 GraphicsConfiguration config) { 153 if (config == null) { 154 return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 155 } 156 return config.createCompatibleVolatileImage(w, h); 157 } 158 159 160 163 private static class Cache { 164 private int maxCount; 166 private java.util.List <SoftReference <Entry>> entries; 168 169 Cache(int maxCount) { 170 this.maxCount = maxCount; 171 entries = new ArrayList<SoftReference <Entry>>(maxCount); 172 } 173 174 void setMaxCount(int maxCount) { 175 this.maxCount = maxCount; 176 } 177 178 private Entry getEntry(Object key, GraphicsConfiguration config, 179 int w, int h, Object [] args) { 180 synchronized(this) { 181 Entry entry; 182 for (int counter = entries.size() - 1; counter >= 0;counter--){ 183 entry = entries.get(counter).get(); 184 if (entry == null) { 185 entries.remove(counter); 187 } 188 else if (entry.equals(config, w, h, args)) { 189 return entry; 191 } 192 } 193 entry = new Entry(config, w, h, args); 195 if (entries.size() == maxCount) { 196 entries.remove(0); 197 } 198 entries.add(new SoftReference <Entry>(entry)); 199 return entry; 200 } 201 } 202 203 206 public Image getImage(Object key, GraphicsConfiguration config, 207 int w, int h, Object [] args) { 208 Entry entry = getEntry(key, config, w, h, args); 209 return entry.getImage(); 210 } 211 212 215 public void setImage(Object key, GraphicsConfiguration config, 216 int w, int h, Object [] args, Image image) { 217 Entry entry = getEntry(key, config, w, h, args); 218 entry.setImage(image); 219 } 220 221 222 225 private static class Entry { 226 private GraphicsConfiguration config; 227 private Object [] args; 228 private Image image; 229 private int w; 230 private int h; 231 232 Entry(GraphicsConfiguration config, int w, int h, Object [] args) { 233 this.config = config; 234 this.args = args; 235 this.w = w; 236 this.h = h; 237 } 238 239 public void setImage(Image image) { 240 this.image = image; 241 } 242 243 public Image getImage() { 244 return image; 245 } 246 247 public String toString() { 248 String value = super.toString() + 249 "[ graphicsConfig=" + config + 250 ", image=" + image + 251 ", w=" + w + ", h=" + h; 252 if (args != null) { 253 for (int counter = 0; counter < args.length; counter++) { 254 value += ", " + args[counter]; 255 } 256 } 257 value += "]"; 258 return value; 259 } 260 261 public boolean equals(GraphicsConfiguration config, int w, int h, 262 Object [] args){ 263 if (this.w == w && this.h == h && 264 ((this.config != null && this.config.equals(config)) || 265 (this.config == null && config == null))) { 266 if (this.args == null && args == null) { 267 return true; 268 } 269 if (this.args != null && args != null && 270 this.args.length == args.length) { 271 for (int counter = args.length - 1; counter >= 0; 272 counter--) { 273 if (!this.args[counter].equals(args[counter])) { 274 return false; 275 } 276 } 277 return true; 278 } 279 } 280 return false; 281 } 282 } 283 } 284 } 285 | Popular Tags |