1 19 20 package org.netbeans.modules.java.tools; 21 22 import java.lang.ref.Reference ; 23 import java.lang.ref.SoftReference ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import java.awt.Image ; 28 29 import org.openide.nodes.AbstractNode; 30 import org.openide.nodes.Children; 31 import org.openide.util.Utilities; 32 33 43 public class BadgeCache extends AbstractNode { 44 47 private Map badges; 48 49 52 private static Map cacheRegister; 53 54 57 public BadgeCache(String underlyingIconBase) { 58 super(Children.LEAF); 59 setIconBase(underlyingIconBase); 60 } 61 62 public static BadgeCache createCache(String baseIconID) { 63 if (cacheRegister == null) { 64 synchronized (BadgeCache.class) { 65 if (cacheRegister == null) 66 cacheRegister = new HashMap (7); 67 } 68 } 69 BadgeCache c = new BadgeCache(baseIconID); 70 cacheRegister.put(baseIconID, new SoftReference (c)); 71 return c; 72 } 73 74 public static BadgeCache getCache(String baseIconID) { 75 if (cacheRegister == null) { 76 return createCache(baseIconID); 77 } 78 synchronized (BadgeCache.class) { 79 Reference r = (Reference )cacheRegister.get(baseIconID); 80 BadgeCache bc; 81 82 if (r == null) 83 bc = null; 84 else 85 bc = (BadgeCache)r.get(); 86 if (bc == null) { 87 return createCache(baseIconID); 88 } else 89 return bc; 90 } 91 } 92 93 104 public synchronized void registerBadge(String id, String iconBase, int x, int y) { 105 if (badges == null) 106 badges = new HashMap (7); 107 Object b = badges.put(id, new BadgeNode(iconBase, x, y)); 108 } 109 110 119 public Image getIcon(int type, String [] badgeIds) { 120 return createCompositeImage(getIcon(type), badgeIds); 121 } 122 123 130 public Image getIcon(Image base, String [] badgeIds) { 131 if (badgeIds == null || 132 badgeIds.length == 0) 133 return base; 134 return createCompositeImage(base, badgeIds); 135 } 136 137 protected final Image createCompositeImage(Image baseImage, String [] badgeIds) { 138 if (badgeIds == null || badgeIds.length == 0) 139 return baseImage; 140 for (int i = 0; i < badgeIds.length; i++) { 141 BadgeNode bn = (BadgeNode)badges.get(badgeIds[i]); 142 if (bn == null) 143 continue; 144 Image image = Utilities.loadImage(bn.getIconBase() + ".gif"); if (image == null) { 146 System.err.println("[Badging] Badge " + bn.getIconBase() + " cannot be loaded"); } else { 148 baseImage = Utilities.mergeImages(baseImage, image, 149 bn.getX(), bn.getY()); 150 } 151 } 152 return baseImage; 153 } 154 155 158 protected final Image createCompositeImage(int type, String [] badgeIds) { 159 Image baseImage = getIcon(type); 160 return createCompositeImage(baseImage, badgeIds); 161 } 162 163 private static class BadgeNode { 164 int x; 165 int y; 166 String iconBase; 167 168 public BadgeNode(String iconBase, int x, int y) { 169 this.x = x; 170 this.y = y; 171 this.iconBase = iconBase; 172 } 173 174 public String getIconBase() { 175 return iconBase; 176 } 177 178 public int getX() { 179 return x; 180 } 181 182 public int getY() { 183 return y; 184 } 185 } 186 } 187 | Popular Tags |