KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > tools > BadgeCache


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.tools;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.SoftReference JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import java.awt.Image JavaDoc;
28
29 import org.openide.nodes.AbstractNode;
30 import org.openide.nodes.Children;
31 import org.openide.util.Utilities;
32
33 /**
34  * This class used to cache composite images of the node + badges attached to
35  * it. Now, image cache moved to the openide, so this object only registers
36  * different <resourceID, x, y> under some unique identifiers and composes
37  * them - it's just an utility class that allows you to register first, then
38  * use only the identifier instead of passing all parameters each time.
39  *
40  * @author sdedic
41  * @version
42  */

43 public class BadgeCache extends AbstractNode {
44     /**
45      * Holds a map of all available badges.
46      */

47     private Map JavaDoc badges;
48     
49     /**
50      * Static register of all caches - keyed by icon bases.
51      */

52     private static Map JavaDoc cacheRegister;
53     
54     /**
55      * Creates a badge cache for a base icon identified by the parameter.
56      */

57     public BadgeCache(String JavaDoc underlyingIconBase) {
58         super(Children.LEAF);
59         setIconBase(underlyingIconBase);
60     }
61
62     public static BadgeCache createCache(String JavaDoc baseIconID) {
63         if (cacheRegister == null) {
64             synchronized (BadgeCache.class) {
65                 if (cacheRegister == null)
66                     cacheRegister = new HashMap JavaDoc(7);
67             }
68         }
69         BadgeCache c = new BadgeCache(baseIconID);
70         cacheRegister.put(baseIconID, new SoftReference JavaDoc(c));
71         return c;
72     }
73     
74     public static BadgeCache getCache(String JavaDoc baseIconID) {
75         if (cacheRegister == null) {
76             return createCache(baseIconID);
77         }
78         synchronized (BadgeCache.class) {
79             Reference JavaDoc r = (Reference JavaDoc)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     /**
94      * Registers a badge for this cache. The iconBase will uniquely identify the badge.
95      * The ID string should be unique; it may be the resource name itself, as long as
96      * the badge is always placed at the specified location. It is advised, that the same
97      * of String as passed to the `id' parameter is used in icon badge lists to speed
98      * up composite icon lookups.
99      *
100      * @param iconBase classpath resource ID for the badge icon
101      * @param x
102      * @param y
103      */

104     public synchronized void registerBadge(String JavaDoc id, String JavaDoc iconBase, int x, int y) {
105         if (badges == null)
106             badges = new HashMap JavaDoc(7);
107         Object JavaDoc b = badges.put(id, new BadgeNode(iconBase, x, y));
108     }
109     
110     /**
111      * Creates a icon with the specified badges. If the requested badge is not found
112      * in the cache, it is ignored.
113      * @param type type of the result icon, see {@link java.beans.BeanInfo} for symbolic
114      * constatnts.
115      * @param badgeIds ordered collection (array) of badges that should be applied to the
116      * base icon.
117      * @deprecated use {@link #createCompositeImage(Image, String[]) instead.
118      */

119     public Image JavaDoc getIcon(int type, String JavaDoc[] badgeIds) {
120         return createCompositeImage(getIcon(type), badgeIds);
121     }
122
123     /*
124      * Creates a icon with the specified badges. If the requested badge is not found
125      * in the cache, it is ignored.
126      * @param base base image that should be badged
127      * @param badgeIds ordered collection (array) of badges that should be applied to the
128      * base icon.
129      */

130     public Image JavaDoc getIcon(Image JavaDoc base, String JavaDoc[] badgeIds) {
131         if (badgeIds == null ||
132             badgeIds.length == 0)
133             return base;
134         return createCompositeImage(base, badgeIds);
135     }
136
137     protected final Image JavaDoc createCompositeImage(Image JavaDoc baseImage, String JavaDoc[] 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 JavaDoc image = Utilities.loadImage(bn.getIconBase() + ".gif"); // NOI18N
145
if (image == null) {
146                 System.err.println("[Badging] Badge " + bn.getIconBase() + " cannot be loaded"); // NOI18N
147
} else {
148                 baseImage = Utilities.mergeImages(baseImage, image,
149                     bn.getX(), bn.getY());
150             }
151         }
152         return baseImage;
153     }
154     
155     /**
156      * @deprecated use {@link #createCompositeImage(Image, String[]) instead.
157      */

158     protected final Image JavaDoc createCompositeImage(int type, String JavaDoc[] badgeIds) {
159         Image JavaDoc baseImage = getIcon(type);
160         return createCompositeImage(baseImage, badgeIds);
161     }
162     
163     private static class BadgeNode {
164         int x;
165         int y;
166         String JavaDoc iconBase;
167         
168         public BadgeNode(String JavaDoc iconBase, int x, int y) {
169             this.x = x;
170             this.y = y;
171             this.iconBase = iconBase;
172         }
173         
174         public String JavaDoc 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