Hi Oleg,
> I'm not a swing developer, but I'd suggest to not use static field for
> cache, but have per-AppContext cache instead (due to security reasons)
Whoops. I have to get used to that .. Improved patch attached.
Cheers, Roman
--
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com * Tel: +49-721-663 968-0
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
diff -r a36c38fe9ff1 src/share/classes/javax/swing/GrayFilter.java
--- a/src/share/classes/javax/swing/GrayFilter.java Tue Apr 22 16:57:20 2008 +0200
+++ b/src/share/classes/javax/swing/GrayFilter.java Wed Apr 23 15:18:48 2008 +0200
@@ -26,6 +26,11 @@ package javax.swing;
import java.awt.*;
import java.awt.image.*;
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import sun.awt.AppContext;
/**
* An image filter that "disables" an image by turning
@@ -38,6 +43,10 @@ import java.awt.image.*;
* @author Jim Graham
*/
public class GrayFilter extends RGBImageFilter {
+
+ private final static Object GRAY_IMAGE_CACHE_KEY =
+ new StringBuilder("GRAY_IMAGE_CACHE_KEY");
+
private boolean brighter;
private int percent;
@@ -45,10 +54,41 @@ public class GrayFilter extends RGBImage
* Creates a disabled image
*/
public static Image createDisabledImage (Image i) {
- GrayFilter filter = new GrayFilter(true, 50);
- ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
- Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
+ Image grayImage;
+ Map<Image,Image> grayImageCache = getGrayImageCache();
+ Image cached = grayImageCache.get(i);
+ if (cached == null) {
+ GrayFilter filter = new GrayFilter(true, 50);
+ ImageProducer prod = new FilteredImageSource(i.getSource(),
+ filter);
+ grayImage = Toolkit.getDefaultToolkit().createImage(prod);
+ grayImageCache.put(i, grayImage);
+ } else {
+ grayImage = cached;
+ }
return grayImage;
+ }
+
+ /**
+ * Returns the gray image cache from the current app context. Creates
+ * a new one, if the current app context doesn't have a cache already.
+ *
+ * @return the gray image cache from the current app context
+ */
+ private static Map<Image,Image> getGrayImageCache() {
+
+ AppContext ctx = AppContext.getAppContext();
+ Object cacheObj;
+ synchronized (ctx) {
+ cacheObj = ctx.get(GRAY_IMAGE_CACHE_KEY);
+ if (cacheObj == null) {
+ WeakHashMap<Image,Image> map = new WeakHashMap<Image,Image>();
+ cacheObj = Collections.synchronizedMap(map);
+ ctx.put(GRAY_IMAGE_CACHE_KEY, cacheObj);
+ }
+ }
+
+ return (WeakHashMap<Image,Image>) cacheObj;
}
/**