KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > lookup > DelegatingStorage


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.util.lookup;
20
21 import org.openide.util.Lookup;
22
23 import java.io.*;
24
25 import java.lang.ref.WeakReference JavaDoc;
26
27 import java.util.*;
28 import org.openide.util.lookup.AbstractLookup.Pair;
29
30
31 /** Storages that can switch between another storages.
32  * @author Jaroslav Tulach
33  */

34 final class DelegatingStorage<Transaction> extends Object JavaDoc
35 implements Serializable, AbstractLookup.Storage<Transaction> {
36     /** object to delegate to */
37     private AbstractLookup.Storage<Transaction> delegate;
38
39     /** thread just accessing the storage */
40     private Thread JavaDoc owner;
41
42     public DelegatingStorage(AbstractLookup.Storage<Transaction> d) {
43         this.delegate = d;
44         this.owner = Thread.currentThread();
45     }
46
47     /** Never serialize yourself, always put there the delegate */
48     public Object JavaDoc writeReplace() {
49         return this.delegate;
50     }
51
52     /** Method to check whether there is not multiple access from the same thread.
53      */

54     public void checkForTreeModification() {
55         if (Thread.currentThread() == owner) {
56             throw new AbstractLookup.ISE("You are trying to modify lookup from lookup query!"); // NOI18N
57
}
58     }
59
60     /** Checks whether we have simple behaviour or complex.
61      */

62     public static boolean isSimple(AbstractLookup.Storage s) {
63         if (s instanceof DelegatingStorage) {
64             return ((DelegatingStorage) s).delegate instanceof ArrayStorage;
65         } else {
66             return s instanceof ArrayStorage;
67         }
68     }
69
70     /** Exits from the owners ship of the storage.
71      */

72     public AbstractLookup.Storage<Transaction> exitDelegate() {
73         if (Thread.currentThread() != owner) {
74             throw new IllegalStateException JavaDoc("Onwer: " + owner + " caller: " + Thread.currentThread()); // NOI18N
75
}
76
77         AbstractLookup.Storage<Transaction> d = delegate;
78         delegate = null;
79
80         return d;
81     }
82
83     public boolean add(AbstractLookup.Pair<?> item, Transaction transaction) {
84         return delegate.add(item, transaction);
85     }
86
87     public void remove(org.openide.util.lookup.AbstractLookup.Pair item, Transaction transaction) {
88         delegate.remove(item, transaction);
89     }
90
91     public void retainAll(Map retain, Transaction transaction) {
92         delegate.retainAll(retain, transaction);
93     }
94
95     /** A special method to change the backing storage.
96      * In fact it is not much typesafe as it changes the
97      * type of Transaction but we know that nobody is currently
98      * holding a transaction object, so there cannot be inconsitencies.
99      */

100     @SuppressWarnings JavaDoc("unchecked")
101     private void changeDelegate(InheritanceTree st) {
102         delegate = (AbstractLookup.Storage<Transaction>)st;
103     }
104
105     public Transaction beginTransaction(int ensure) {
106         try {
107             return delegate.beginTransaction(ensure);
108         } catch (UnsupportedOperationException JavaDoc ex) {
109             // let's convert to InheritanceTree
110
ArrayStorage arr = (ArrayStorage) delegate;
111             InheritanceTree inh = new InheritanceTree();
112             changeDelegate(inh);
113
114             //
115
// Copy content
116
//
117
Enumeration<Pair<Object JavaDoc>> en = arr.lookup(Object JavaDoc.class);
118
119             while (en.hasMoreElements()) {
120                 if (!inh.add(en.nextElement(), new ArrayList<Class JavaDoc>())) {
121                     throw new IllegalStateException JavaDoc("All objects have to be accepted"); // NOI18N
122
}
123             }
124
125             //
126
// Copy listeners
127
//
128
AbstractLookup.ReferenceToResult<?> ref = arr.cleanUpResult(null);
129
130             if (ref != null) {
131                 ref.cloneList(inh);
132             }
133
134             // we have added the current content and now we can start transaction
135
return delegate.beginTransaction(ensure);
136         }
137     }
138
139     public org.openide.util.lookup.AbstractLookup.ReferenceToResult cleanUpResult(
140         org.openide.util.Lookup.Template templ
141     ) {
142         return delegate.cleanUpResult(templ);
143     }
144
145     public void endTransaction(Transaction transaction, Set<AbstractLookup.R> modified) {
146         delegate.endTransaction(transaction, modified);
147     }
148
149     public <T> Enumeration<Pair<T>> lookup(Class JavaDoc<T> clazz) {
150         return delegate.lookup(clazz);
151     }
152
153     public org.openide.util.lookup.AbstractLookup.ReferenceToResult registerReferenceToResult(
154         org.openide.util.lookup.AbstractLookup.ReferenceToResult newRef
155     ) {
156         return delegate.registerReferenceToResult(newRef);
157     }
158 }
159
Popular Tags