KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MultiViewTopComponentLookup


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 OriginalM
16  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.multiview;
21
22 import javax.swing.Action JavaDoc;
23 import org.openide.util.Lookup;
24 import org.openide.util.WeakListeners;
25 import java.beans.*;
26 import java.util.*;
27 import javax.swing.ActionMap JavaDoc;
28
29 import org.openide.nodes.*;
30 import org.openide.util.LookupEvent;
31 import org.openide.util.LookupListener;
32 import org.openide.util.lookup.AbstractLookup;
33 import org.openide.util.lookup.Lookups;
34 import org.openide.util.lookup.ProxyLookup;
35
36 class MultiViewTopComponentLookup extends Lookup {
37
38     private MyProxyLookup proxy;
39     private InitialProxyLookup initial;
40     
41     public MultiViewTopComponentLookup(ActionMap JavaDoc initialObject) {
42         super();
43         // need to delegate in order to get the correct Lookup.Templates that refresh..
44
initial = new InitialProxyLookup(initialObject);
45         proxy = new MyProxyLookup(initial);
46     }
47     
48     
49     public void setElementLookup(Lookup look) {
50         proxy.setElementLookup(look);
51         initial.refreshLookup();
52     }
53     
54     public Lookup.Item lookupItem(Lookup.Template template) {
55         Lookup.Item retValue;
56         if (template.getType() == ActionMap JavaDoc.class || (template.getId() != null && template.getId().equals("javax.swing.ActionMap"))) {
57             return initial.lookupItem(template);
58         }
59         // do something here??
60
retValue = super.lookupItem(template);
61         return retValue;
62     }
63     
64      
65     public Object JavaDoc lookup(Class JavaDoc clazz) {
66         if (clazz == ActionMap JavaDoc.class) {
67             return initial.lookup(clazz);
68         }
69         Object JavaDoc retValue;
70         
71         retValue = proxy.lookup(clazz);
72         return retValue;
73     }
74     
75     public Lookup.Result lookup(Lookup.Template template) {
76         
77         if (template.getType() == ActionMap JavaDoc.class || (template.getId() != null && template.getId().equals("javax.swing.ActionMap"))) {
78             return initial.lookup(template);
79         }
80         Lookup.Result retValue;
81         retValue = proxy.lookup(template);
82         retValue = new ExclusionResult(retValue);
83         return retValue;
84     }
85     
86     /**
87      * A lookup result excluding some instances.
88      */

89     private static final class ExclusionResult extends Lookup.Result implements LookupListener {
90         
91         private final Lookup.Result delegate;
92         private final List listeners = new ArrayList(); // List<LookupListener>
93
private Collection lastResults;
94         
95         public ExclusionResult(Lookup.Result delegate) {
96             this.delegate = delegate;
97         }
98         
99         public Collection allInstances() {
100             // this shall remove duplicates??
101
Set s = new HashSet(delegate.allInstances());
102             return s;
103         }
104         
105         public Set allClasses() {
106             return delegate.allClasses(); // close enough
107
}
108         
109         public Collection allItems() {
110             // remove duplicates..
111
Set s = new HashSet(delegate.allItems());
112             Iterator it = s.iterator();
113             Set instances = new HashSet();
114             while (it.hasNext()) {
115                 Lookup.Item i = (Lookup.Item)it.next();
116                 if (instances.contains(i.getInstance())) {
117                     it.remove();
118                 } else {
119                     instances.add(i.getInstance());
120                 }
121             }
122             return s;
123         }
124         
125         public void addLookupListener(LookupListener l) {
126             synchronized (listeners) {
127                 if (listeners.isEmpty()) {
128                     if (lastResults == null) {
129                         lastResults = allInstances();
130                     }
131                     delegate.addLookupListener(this);
132                 }
133                 listeners.add(l);
134             }
135         }
136         
137         public void removeLookupListener(LookupListener l) {
138             synchronized (listeners) {
139                 listeners.remove(l);
140                 if (listeners.isEmpty()) {
141                     delegate.removeLookupListener(this);
142                     lastResults = null;
143                 }
144             }
145         }
146         
147         public void resultChanged(LookupEvent ev) {
148             synchronized (listeners) {
149                 Collection current = allInstances();
150                 boolean equal = lastResults != null && current != null && current.containsAll(lastResults) && lastResults.containsAll(current);
151                 if (equal) {
152                     // the merged list is the same, ignore...
153
return ;
154                 }
155                 lastResults = current;
156             }
157                 
158             LookupEvent ev2 = new LookupEvent(this);
159             LookupListener[] ls;
160             synchronized (listeners) {
161                 ls = (LookupListener[])listeners.toArray(new LookupListener[listeners.size()]);
162             }
163             for (int i = 0; i < ls.length; i++) {
164                 ls[i].resultChanged(ev2);
165             }
166         }
167         
168     }
169     
170     private static class MyProxyLookup extends ProxyLookup {
171         private Lookup initialLookup;
172         public MyProxyLookup(Lookup initial) {
173             super(new Lookup[] {initial});
174             initialLookup = initial;
175         }
176
177         public void setElementLookup(Lookup look) {
178             setLookups(new Lookup[] {initialLookup, look});
179         }
180     }
181     
182     static class InitialProxyLookup extends ProxyLookup {
183         private ActionMap JavaDoc initObject;
184         public InitialProxyLookup(ActionMap JavaDoc obj) {
185             super(new Lookup[] {Lookups.fixed(new Object JavaDoc[] {new LookupProxyActionMap(obj)})});
186             initObject = obj;
187         }
188
189         public void refreshLookup() {
190             setLookups(new Lookup[] {Lookups.fixed(new Object JavaDoc[] {new LookupProxyActionMap(initObject)})});
191         }
192         
193     }
194     
195     /**
196      * A proxy ActionMap that delegates to the original one, used because of #47991
197      * non private because of tests..
198      */

199     static class LookupProxyActionMap extends ActionMap JavaDoc {
200         private ActionMap JavaDoc map;
201         public LookupProxyActionMap(ActionMap JavaDoc original) {
202             map = original;
203         }
204         
205         public void setParent(ActionMap JavaDoc map) {
206             this.map.setParent(map);
207         }
208         
209         
210         public ActionMap JavaDoc getParent() {
211             return map.getParent();
212         }
213         
214         public void put(Object JavaDoc key, Action JavaDoc action) {
215             map.put(key, action);
216         }
217         
218         public Action JavaDoc get(Object JavaDoc key) {
219             return map.get(key);
220         }
221         
222         public void remove(Object JavaDoc key) {
223             map.remove(key);
224         }
225         
226         public void clear() {
227             map.clear();
228         }
229         
230         public Object JavaDoc[] keys() {
231             return map.keys();
232         }
233         
234         public int size() {
235             return map.size();
236         }
237         
238         public Object JavaDoc[] allKeys() {
239             return map.allKeys();
240         }
241         
242     }
243 }
244
Popular Tags