KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > components > registry > ComponentScope


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.components.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.ui.internal.WorkbenchPlugin;
20 import org.eclipse.ui.internal.components.ComponentUtil;
21 import org.eclipse.ui.internal.components.framework.ClassIdentifier;
22 import org.eclipse.ui.internal.components.framework.ComponentException;
23 import org.eclipse.ui.internal.components.framework.ComponentFactory;
24 import org.eclipse.ui.internal.components.framework.ComponentHandle;
25 import org.eclipse.ui.internal.components.framework.IServiceProvider;
26 import org.eclipse.ui.internal.components.framework.NonDisposingHandle;
27 import org.eclipse.ui.internal.components.framework.ServiceFactory;
28
29 /**
30  * @since 3.1
31  */

32 public class ComponentScope extends ServiceFactory implements IComponentScope {
33     
34     private String JavaDoc scopeId;
35  
36     private ComponentTypeMap types = new ComponentTypeMap();
37     private ComponentTypeMap modifiers = new ComponentTypeMap();
38     private IScopeReference[] parentScopes = new IScopeReference[0];
39     private ClassIdentifier[] dependencies = new ClassIdentifier[0];
40     private static final String JavaDoc impossibleToSatisfyDependency = "Dependency that is impossible to satisfy"; //$NON-NLS-1$
41

42     private boolean loaded = false;
43
44     private ArrayList JavaDoc children = new ArrayList JavaDoc();
45     
46     private ServiceFactory[] parentContexts = new ServiceFactory[0];
47     
48     private ArrayList JavaDoc scopeDependencies = new ArrayList JavaDoc();
49     
50     public ComponentScope(String JavaDoc scopeId) {
51         this.scopeId = scopeId;
52     }
53     
54     public IServiceProvider getContainer(ServiceFactory context) {
55         return null;
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.core.component.IContainerContext#getFactory(java.lang.Object)
60      */

61     public ComponentHandle createHandle(Object JavaDoc key, IServiceProvider container) throws ComponentException {
62         
63         if (key == this) {
64             return new NonDisposingHandle(this);
65         }
66         
67         if (key instanceof Class JavaDoc) {
68             ComponentFactory factory = (ComponentFactory)types.get((Class JavaDoc)key);
69             
70             if (factory != null) {
71                 return factory.createHandle(container);
72             }
73         }
74         
75         for (int i = 0; i < parentContexts.length; i++) {
76             ServiceFactory context = parentContexts[i];
77             
78             ComponentHandle handle = context.createHandle(key, container);
79             if (handle != null) {
80                 return handle;
81             }
82         }
83
84         return null;
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.core.components.IComponentContext#hasKey(java.lang.Object)
89      */

90     public boolean hasService(Object JavaDoc componentKey) {
91         return hasKey(componentKey, null);
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.core.components.IContainerContext#hasComponent(java.lang.Object, org.eclipse.core.components.IContainer)
96      */

97     private boolean hasKey(Object JavaDoc key, ServiceFactory parentScopeToSkip) {
98         if (key == this) {
99             return true;
100         }
101         
102         if (key instanceof Class JavaDoc) {
103             if (types.containsKey((Class JavaDoc)key)) {
104                 return true;
105             }
106         }
107         
108         for (int i = 0; i < parentContexts.length; i++) {
109             ServiceFactory context = parentContexts[i];
110             
111             if (context == parentScopeToSkip) {
112                 continue;
113             }
114             
115             if (context.hasService(key)) {
116                 return true;
117             }
118         }
119         
120         return false;
121     }
122     
123 // private IComponentFactory getComponentFactory(Object key, IContainerContext toSkip) {
124
// if (key instanceof Class) {
125
// IComponentFactory result = (IComponentFactory)types.get((Class)key);
126
//
127
// if (result != null) {
128
// return result;
129
// }
130
// }
131
//
132
//// for (int i = 0; i < parentContexts.length; i++) {
133
//// IContainerContext parent = parentContexts[i];
134
////
135
//// if (parent == toSkip) {
136
//// continue;
137
//// }
138
////
139
//// IComponentFactory parentFactory = parent.getComponentHandle(key);
140
////
141
//// if (parentFactory != null) {
142
//// return parentFactory;
143
//// }
144
//// }
145
//
146
// return null;
147
// }
148

149     public ComponentFactory lookup(ClassIdentifier type) {
150         return (ComponentFactory)types.get(type);
151     }
152     
153     public void put(ClassIdentifier type, ComponentFactory factory) {
154         types.put(type, factory);
155     }
156     
157     public void remove(ClassIdentifier type) {
158         types.remove(type);
159     }
160     
161     /* (non-Javadoc)
162      * @see org.eclipse.core.component.registry.IComponentScope#getTypes()
163      */

164     public ClassIdentifier[] getTypes() {
165         return types.getTypes();
166     }
167
168     /* (non-Javadoc)
169      * @see org.eclipse.core.component.registry.IComponentScope#getScopeId()
170      */

171     public String JavaDoc getScopeId() {
172         return scopeId;
173     }
174
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.core.component.registry.IComponentScope#getParentScopes()
178      */

179     public IScopeReference[] getParentScopes() {
180         return parentScopes;
181     }
182
183     /* (non-Javadoc)
184      * @see org.eclipse.core.component.registry.IComponentScope#getDependencies()
185      */

186     public ClassIdentifier[] getDependencies() {
187         return dependencies;
188     }
189
190     /* (non-Javadoc)
191      * @see org.eclipse.core.component.registry.IComponentScope#getContext()
192      */

193     public ServiceFactory getContext() {
194         return this;
195     }
196
197     /* (non-Javadoc)
198      * @see org.eclipse.core.component.IContainerContext#getMissingDependencies()
199      */

200     public Collection JavaDoc getMissingDependencies() {
201         if (!loaded) {
202             List JavaDoc result = new ArrayList JavaDoc();
203             result.add (impossibleToSatisfyDependency);
204             return result;
205         }
206         
207         HashSet JavaDoc result = new HashSet JavaDoc();
208         for (int i = 0; i < dependencies.length; i++) {
209             ClassIdentifier type = dependencies[i];
210             
211             try {
212                 Class JavaDoc dep = ComponentUtil.loadClass(type);
213                 // Ensure that we haven't satisfied this dependency
214
if (types.get(dep) == null) {
215                     result.add(dep);
216                 }
217             } catch (ComponentException e) {
218                 WorkbenchPlugin.log(e);
219                 result.add(impossibleToSatisfyDependency);
220             }
221         }
222         
223         // If we're referring to any scopes by delegation, then the scopes themselves
224
// become dependencies
225
for (int i = 0; i < parentScopes.length; i++) {
226             IScopeReference ref = parentScopes[i];
227             
228             if (ref.getRelationship() == IScopeReference.REL_REQUIRES) {
229                 ServiceFactory context = ((ComponentScope)ref.getTarget()).getContext();
230                 
231                 if (!hasService(context)) {
232                     result.add(context);
233                 }
234             }
235         }
236         
237         // Check for dependencies inherited from extended scopes
238

239         for (int i = 0; i < parentContexts.length; i++) {
240             ServiceFactory next = parentContexts[i];
241
242             // If we extend by inheritance, then we inherit all of the parent's dependencies
243
Collection JavaDoc parentMissing = next.getMissingDependencies();
244             
245             for (Iterator JavaDoc iter = parentMissing.iterator(); iter.hasNext();) {
246                 Object JavaDoc object = (Object JavaDoc) iter.next();
247             
248                 // Don't propogate the dependency if we've satisfied it
249
if (!hasKey(object, next)) {
250                     result.add(object);
251                 }
252             }
253         }
254
255         return result;
256     }
257     
258     public void load(ScopeDefinition def, ComponentRegistry reg) {
259         if (loaded) {
260             unload(reg);
261         }
262         
263         dependencies = def.getDependencies();
264         //description = def.getDescription();
265

266         SymbolicScopeReference[] refs = def.getExtends();
267         IScopeReference[] parents = new IScopeReference[refs.length];
268         List JavaDoc parentContextList = new ArrayList JavaDoc();
269         
270         for (int i = 0; i < refs.length; i++) {
271             SymbolicScopeReference reference = refs[i];
272             
273             IScopeReference ref = new ScopeReference(reference.relationship,
274                     reg.linkSubScope(reference.scopeId, this, reference.relationship));
275             parents[i] = ref;
276             
277             if (reference.relationship == IScopeReference.REL_REQUIRES) {
278                 scopeDependencies.add(((ComponentScope) ref.getTarget()).getContext());
279             } else {
280                 parentContextList.add(((ComponentScope) ref.getTarget()).getContext());
281             }
282         }
283         
284         parentContexts = (ServiceFactory[]) parentContextList.toArray(
285                 new ServiceFactory[parentContextList.size()]);
286         parentScopes = parents;
287         loaded = true;
288     }
289     
290     public void unload(ComponentRegistry reg) {
291         for (int i = 0; i < parentScopes.length; i++) {
292             IScopeReference ref = parentScopes[i];
293             
294             reg.unlinkSubScope(ref.getTarget().getScopeId(), this);
295         }
296         
297         scopeDependencies = new ArrayList JavaDoc();
298         parentScopes = new IScopeReference[0];
299         dependencies = new ClassIdentifier[0];
300         parentContexts = new ServiceFactory[0];
301         //description = UNRESOLVED_SCOPE;
302
loaded = false;
303     }
304     
305     public boolean isRedundant() {
306         return (!loaded) && getChildScopes().length == 0 && types.isEmpty() && modifiers.isEmpty();
307     }
308
309     /**
310      * @since 3.1
311      *
312      * @param child
313      */

314     public void addChild(ComponentScope child, int relationship) {
315         children.add(new ScopeReference(relationship, child));
316     }
317     
318     public void removeChild(ComponentScope child) {
319         IScopeReference[] refs = (IScopeReference[]) children.toArray(new IScopeReference[children.size()]);
320         for (int i = 0; i < refs.length; i++) {
321             IScopeReference reference = refs[i];
322             if (reference.getTarget() == child) {
323                 children.remove(reference);
324             }
325         }
326     }
327
328     /* (non-Javadoc)
329      * @see java.lang.Object#toString()
330      */

331     public String JavaDoc toString() {
332         return "scope " + scopeId; //$NON-NLS-1$
333
}
334     
335     /* (non-Javadoc)
336      * @see org.eclipse.core.component.registry.IComponentScope#getChildScopes()
337      */

338     public IScopeReference[] getChildScopes() {
339         return (IScopeReference[]) children.toArray(new IScopeReference[children.size()]);
340     }
341
342
343
344 }
345
Popular Tags