KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > CookieSetLkp


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 package org.openide.nodes;
20
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.concurrent.ConcurrentHashMap JavaDoc;
26 import org.openide.util.lookup.AbstractLookup;
27 import org.openide.util.lookup.AbstractLookup.Pair;
28
29 import java.lang.ref.WeakReference JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import org.openide.util.Lookup;
35 import org.openide.util.lookup.InstanceContent;
36
37
38 /** Content for a cookie set.
39  */

40 final class CookieSetLkp extends AbstractLookup {
41     private final CookieSet.Before before;
42     
43     public CookieSetLkp(CookieSet.Before b) {
44         this.before = b;
45     }
46     
47     public void add(Object JavaDoc obj) {
48         addPair(new SimpleItem<Object JavaDoc>(obj));
49     }
50     public final <T,R> void add(T inst, InstanceContent.Convertor<T,R> conv) {
51         addPair(new ConvertingItem<T,R>(inst, conv));
52     }
53     
54     public void remove(Object JavaDoc obj) {
55         removePair(new SimpleItem<Object JavaDoc>(obj));
56     }
57     public final <T,R> void remove(T inst, InstanceContent.Convertor<T,R> conv) {
58         removePair(new ConvertingItem<T,R>(inst, conv));
59     }
60
61     void superRemovePair(Pair pair) {
62         removePair(pair);
63     }
64
65     private ThreadLocal JavaDoc<Object JavaDoc> isInReplaceInst = new ThreadLocal JavaDoc<Object JavaDoc>();
66     <T> void replaceInstances(Class JavaDoc<? extends T> clazz, T[] instances, CookieSet set) {
67         Iterator JavaDoc<? extends Lookup.Item> it;
68         Set JavaDoc<Lookup.Item> toRemove;
69         List JavaDoc<AbstractLookup.Pair> pairs;
70         
71         Object JavaDoc prev = isInReplaceInst.get();
72         try {
73             isInReplaceInst.set(this);
74             
75                 it = lookupResult(Object JavaDoc.class).allItems().iterator();
76                 toRemove = new HashSet JavaDoc<Lookup.Item>(lookupResult(clazz).allItems());
77                 pairs = new ArrayList JavaDoc<AbstractLookup.Pair>();
78         
79             boolean change = false;
80             int index = 0;
81             while (it.hasNext()) {
82                 Lookup.Item item = it.next();
83                 assert item instanceof AbstractLookup.Pair;
84
85                 if (toRemove.remove(item)) {
86                     if (index < instances.length) {
87                         if (item instanceof SimpleItem) {
88                             SimpleItem<?> simple = (SimpleItem<?>)item;
89                             if (simple.obj == instances[index]) {
90                                 index++;
91                                 pairs.add(simple);
92                                 continue;
93                             }
94                         }
95
96                         change = true;
97                         pairs.add(new SimpleItem<T>(instances[index++]));
98                     } else {
99                         change = true;
100                     }
101                 } else {
102                     pairs.add((AbstractLookup.Pair)item);
103                 }
104             }
105             assert toRemove.isEmpty();
106
107             while (index < instances.length) {
108                 change = true;
109                 pairs.add(new SimpleItem<T>(instances[index++]));
110             }
111
112             if (change) {
113                 setPairs(pairs);
114                 set.fireChangeEvent();
115             }
116         } finally {
117             isInReplaceInst.set(prev);
118         }
119     }
120
121     protected void beforeLookup(Lookup.Template<?> template) {
122         beforeLookupImpl(template.getType());
123     }
124     
125     final void beforeLookupImpl(Class JavaDoc<?> clazz) {
126         if (before != null && isInReplaceInst.get() == null) {
127             before.beforeLookup(clazz);
128         }
129     }
130
131     /** Instance of one item representing an object.
132      */

133     final static class SimpleItem<T> extends Pair<T> {
134         private T obj;
135
136         /** Create an item.
137          * @obj object to register
138          */

139         public SimpleItem(T obj) {
140             if (obj == null) {
141                 throw new NullPointerException JavaDoc();
142             }
143             this.obj = obj;
144         }
145
146         /** Tests whether this item can produce object
147          * of class c.
148          */

149         public boolean instanceOf(Class JavaDoc<?> c) {
150             return c.isInstance(obj);
151         }
152
153         /** Get instance of registered object. If convertor is specified then
154          * method InstanceLookup.Convertor.convertor is used and weak reference
155          * to converted object is saved.
156          * @return the instance of the object.
157          */

158         public T getInstance() {
159             return obj;
160         }
161
162         public boolean equals(Object JavaDoc o) {
163             if (o instanceof SimpleItem) {
164                 return obj.equals(((SimpleItem) o).obj);
165             } else {
166                 return false;
167             }
168         }
169
170         public int hashCode() {
171             return obj.hashCode();
172         }
173
174         /** An identity of the item.
175          * @return string representing the item, that can be used for
176          * persistance purposes to locate the same item next time
177          */

178         public String JavaDoc getId() {
179             return "IL[" + obj.toString(); // NOI18N
180
}
181
182         /** Getter for display name of the item.
183          */

184         public String JavaDoc getDisplayName() {
185             return obj.toString();
186         }
187
188         /** Method that can test whether an instance of a class has been created
189          * by this item.
190          *
191          * @param obj the instance
192          * @return if the item has already create an instance and it is the same
193          * as obj.
194          */

195         protected boolean creatorOf(Object JavaDoc obj) {
196             return obj == this.obj;
197         }
198
199         /** The class of this item.
200          * @return the correct class
201          */

202         @SuppressWarnings JavaDoc("unchecked")
203         public Class JavaDoc<? extends T> getType() {
204             return (Class JavaDoc<? extends T>)obj.getClass();
205         }
206     } // end of SimpleItem
207

208     /** Instance of one item registered in the map.
209      */

210     final static class ConvertingItem<T,R> extends Pair<R> {
211         /** registered object */
212         private T obj;
213
214         /** Reference to converted object. */
215         private WeakReference JavaDoc<R> ref;
216
217         /** convertor to use */
218         private InstanceContent.Convertor<? super T,R> conv;
219
220         /** Create an item.
221          * @obj object to register
222          * @conv a convertor, can be <code>null</code>.
223          */

224         public ConvertingItem(T obj, InstanceContent.Convertor<? super T,R> conv) {
225             this.obj = obj;
226             this.conv = conv;
227         }
228
229         /** Tests whether this item can produce object
230          * of class c.
231          */

232         public boolean instanceOf(Class JavaDoc<?> c) {
233             return c.isAssignableFrom(getType());
234         }
235
236         /** Returns converted object or null if obj has not been converted yet
237          * or reference was cleared by garbage collector.
238          */

239         private R getConverted() {
240             if (ref == null) {
241                 return null;
242             }
243
244             return ref.get();
245         }
246
247         /** Get instance of registered object. If convertor is specified then
248          * method InstanceLookup.Convertor.convertor is used and weak reference
249          * to converted object is saved.
250          * @return the instance of the object.
251          */

252         public synchronized R getInstance() {
253             R converted = getConverted();
254
255             if (converted == null) {
256                 converted = conv.convert(obj);
257                 ref = new WeakReference JavaDoc<R>(converted);
258             }
259
260             return converted;
261         }
262
263         public boolean equals(Object JavaDoc o) {
264             if (o instanceof ConvertingItem) {
265                 return obj.equals(((ConvertingItem) o).obj);
266             } else {
267                 return false;
268             }
269         }
270
271         public int hashCode() {
272             return obj.hashCode();
273         }
274
275         /** An identity of the item.
276          * @return string representing the item, that can be used for
277          * persistance purposes to locate the same item next time
278          */

279         public String JavaDoc getId() {
280             return conv.id(obj);
281         }
282
283         /** Getter for display name of the item.
284          */

285         public String JavaDoc getDisplayName() {
286             return conv.displayName(obj);
287         }
288
289         /** Method that can test whether an instance of a class has been created
290          * by this item.
291          *
292          * @param obj the instance
293          * @return if the item has already create an instance and it is the same
294          * as obj.
295          */

296         protected boolean creatorOf(Object JavaDoc obj) {
297             if (conv == null) {
298                 return obj == this.obj;
299             } else {
300                 return obj == getConverted();
301             }
302         }
303
304         /** The class of this item.
305          * @return the correct class
306          */

307         @SuppressWarnings JavaDoc("unchecked")
308         public Class JavaDoc<? extends R> getType() {
309             R converted = getConverted();
310
311             if (converted == null) {
312                 return conv.type(obj);
313             }
314
315             return (Class JavaDoc<? extends R>)converted.getClass();
316         }
317     } // end of ConvertingItem
318
}
319
Popular Tags