001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.nodes;
042:
043: import java.util.Collections;
044: import java.util.HashMap;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.concurrent.ConcurrentHashMap;
048: import org.openide.util.lookup.AbstractLookup;
049: import org.openide.util.lookup.AbstractLookup.Pair;
050:
051: import java.lang.ref.WeakReference;
052: import java.util.ArrayList;
053: import java.util.List;
054: import java.util.Map;
055: import java.util.Set;
056: import org.openide.util.Lookup;
057: import org.openide.util.lookup.InstanceContent;
058:
059: /** Content for a cookie set.
060: */
061: final class CookieSetLkp extends AbstractLookup {
062: private final CookieSet.Before before;
063:
064: public CookieSetLkp(CookieSet.Before b) {
065: this .before = b;
066: }
067:
068: public void add(Object obj) {
069: addPair(new SimpleItem<Object>(obj));
070: }
071:
072: public final <T, R> void add(T inst,
073: InstanceContent.Convertor<T, R> conv) {
074: addPair(new ConvertingItem<T, R>(inst, conv));
075: }
076:
077: public void remove(Object obj) {
078: removePair(new SimpleItem<Object>(obj));
079: }
080:
081: public final <T, R> void remove(T inst,
082: InstanceContent.Convertor<T, R> conv) {
083: removePair(new ConvertingItem<T, R>(inst, conv));
084: }
085:
086: void super RemovePair(Pair pair) {
087: removePair(pair);
088: }
089:
090: private ThreadLocal<Object> isInReplaceInst = new ThreadLocal<Object>();
091:
092: <T> void replaceInstances(Class<? extends T> clazz, T[] instances,
093: CookieSet set) {
094: Iterator<? extends Lookup.Item> it;
095: Set<Lookup.Item> toRemove;
096: List<AbstractLookup.Pair> pairs;
097:
098: Object prev = isInReplaceInst.get();
099: try {
100: isInReplaceInst.set(this );
101:
102: it = lookupResult(Object.class).allItems().iterator();
103: toRemove = new HashSet<Lookup.Item>(lookupResult(clazz)
104: .allItems());
105: pairs = new ArrayList<AbstractLookup.Pair>();
106:
107: boolean change = false;
108: int index = 0;
109: while (it.hasNext()) {
110: Lookup.Item item = it.next();
111: assert item instanceof AbstractLookup.Pair;
112:
113: if (toRemove.remove(item)) {
114: if (index < instances.length) {
115: if (item instanceof SimpleItem) {
116: SimpleItem<?> simple = (SimpleItem<?>) item;
117: if (simple.obj == instances[index]) {
118: index++;
119: pairs.add(simple);
120: continue;
121: }
122: }
123:
124: change = true;
125: pairs
126: .add(new SimpleItem<T>(
127: instances[index++]));
128: } else {
129: change = true;
130: }
131: } else {
132: pairs.add((AbstractLookup.Pair) item);
133: }
134: }
135: assert toRemove.isEmpty();
136:
137: while (index < instances.length) {
138: change = true;
139: pairs.add(new SimpleItem<T>(instances[index++]));
140: }
141:
142: if (change) {
143: setPairs(pairs);
144: set.fireChangeEvent();
145: }
146: } finally {
147: isInReplaceInst.set(prev);
148: }
149: }
150:
151: protected void beforeLookup(Lookup.Template<?> template) {
152: beforeLookupImpl(template.getType());
153: }
154:
155: final void beforeLookupImpl(Class<?> clazz) {
156: if (before != null && isInReplaceInst.get() == null) {
157: before.beforeLookup(clazz);
158: }
159: }
160:
161: /** Instance of one item representing an object.
162: */
163: final static class SimpleItem<T> extends Pair<T> {
164: private T obj;
165:
166: /** Create an item.
167: * @obj object to register
168: */
169: public SimpleItem(T obj) {
170: if (obj == null) {
171: throw new NullPointerException();
172: }
173: this .obj = obj;
174: }
175:
176: /** Tests whether this item can produce object
177: * of class c.
178: */
179: public boolean instanceOf(Class<?> c) {
180: return c.isInstance(obj);
181: }
182:
183: /** Get instance of registered object. If convertor is specified then
184: * method InstanceLookup.Convertor.convertor is used and weak reference
185: * to converted object is saved.
186: * @return the instance of the object.
187: */
188: public T getInstance() {
189: return obj;
190: }
191:
192: public boolean equals(Object o) {
193: if (o instanceof SimpleItem) {
194: return obj.equals(((SimpleItem) o).obj);
195: } else {
196: return false;
197: }
198: }
199:
200: public int hashCode() {
201: return obj.hashCode();
202: }
203:
204: /** An identity of the item.
205: * @return string representing the item, that can be used for
206: * persistance purposes to locate the same item next time
207: */
208: public String getId() {
209: return "IL[" + obj.toString(); // NOI18N
210: }
211:
212: /** Getter for display name of the item.
213: */
214: public String getDisplayName() {
215: return obj.toString();
216: }
217:
218: /** Method that can test whether an instance of a class has been created
219: * by this item.
220: *
221: * @param obj the instance
222: * @return if the item has already create an instance and it is the same
223: * as obj.
224: */
225: protected boolean creatorOf(Object obj) {
226: return obj == this .obj;
227: }
228:
229: /** The class of this item.
230: * @return the correct class
231: */
232: @SuppressWarnings("unchecked")
233: public Class<? extends T> getType() {
234: return (Class<? extends T>) obj.getClass();
235: }
236: } // end of SimpleItem
237:
238: /** Instance of one item registered in the map.
239: */
240: final static class ConvertingItem<T, R> extends Pair<R> {
241: /** registered object */
242: private T obj;
243:
244: /** Reference to converted object. */
245: private WeakReference<R> ref;
246:
247: /** convertor to use */
248: private InstanceContent.Convertor<? super T, R> conv;
249:
250: /** Create an item.
251: * @obj object to register
252: * @conv a convertor, can be <code>null</code>.
253: */
254: public ConvertingItem(T obj,
255: InstanceContent.Convertor<? super T, R> conv) {
256: this .obj = obj;
257: this .conv = conv;
258: }
259:
260: /** Tests whether this item can produce object
261: * of class c.
262: */
263: public boolean instanceOf(Class<?> c) {
264: return c.isAssignableFrom(getType());
265: }
266:
267: /** Returns converted object or null if obj has not been converted yet
268: * or reference was cleared by garbage collector.
269: */
270: private R getConverted() {
271: if (ref == null) {
272: return null;
273: }
274:
275: return ref.get();
276: }
277:
278: /** Get instance of registered object. If convertor is specified then
279: * method InstanceLookup.Convertor.convertor is used and weak reference
280: * to converted object is saved.
281: * @return the instance of the object.
282: */
283: public synchronized R getInstance() {
284: R converted = getConverted();
285:
286: if (converted == null) {
287: converted = conv.convert(obj);
288: ref = new WeakReference<R>(converted);
289: }
290:
291: return converted;
292: }
293:
294: public boolean equals(Object o) {
295: if (o instanceof ConvertingItem) {
296: return obj.equals(((ConvertingItem) o).obj);
297: } else {
298: return false;
299: }
300: }
301:
302: public int hashCode() {
303: return obj.hashCode();
304: }
305:
306: /** An identity of the item.
307: * @return string representing the item, that can be used for
308: * persistance purposes to locate the same item next time
309: */
310: public String getId() {
311: return conv.id(obj);
312: }
313:
314: /** Getter for display name of the item.
315: */
316: public String getDisplayName() {
317: return conv.displayName(obj);
318: }
319:
320: /** Method that can test whether an instance of a class has been created
321: * by this item.
322: *
323: * @param obj the instance
324: * @return if the item has already create an instance and it is the same
325: * as obj.
326: */
327: protected boolean creatorOf(Object obj) {
328: if (conv == null) {
329: return obj == this .obj;
330: } else {
331: return obj == getConverted();
332: }
333: }
334:
335: /** The class of this item.
336: * @return the correct class
337: */
338: @SuppressWarnings("unchecked")
339: public Class<? extends R> getType() {
340: R converted = getConverted();
341:
342: if (converted == null) {
343: return conv.type(obj);
344: }
345:
346: return (Class<? extends R>) converted.getClass();
347: }
348: } // end of ConvertingItem
349: }
|