KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > multiplexer > NestedContext


1 /*******************************************************************************
2  * Copyright (c) 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.part.multiplexer;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.ui.internal.WorkbenchMessages;
21 import org.eclipse.ui.internal.components.framework.ComponentException;
22 import org.eclipse.ui.internal.components.framework.ComponentHandle;
23 import org.eclipse.ui.internal.components.framework.IServiceProvider;
24 import org.eclipse.ui.internal.components.framework.ServiceFactory;
25 import org.eclipse.ui.internal.components.util.ServiceMap;
26 import org.eclipse.ui.internal.part.Part;
27
28 /**
29  * Contains a factory for services that can delegate to a shared implementation.
30  * Many <code>NestedContext</code> instances can share the same <code>ISharedComponents</code>
31  * instance, however only one of them will be active at a time. A <code>NestedContext</code>
32  * remembers everything it has created. Calling activate
33  *
34  * When a <code>NestedContext</code>
35  * is activated, it activate
36  *
37  * @since 3.1
38  */

39 public class NestedContext extends ServiceFactory {
40
41     //private List componentList = new ArrayList();
42
private IServiceProvider sharedComponents;
43     private ServiceFactory nestedFactories;
44     private Map JavaDoc componentMap = new HashMap JavaDoc();
45     
46     private ISharedContext sharedContext = new ISharedContext() {
47     /* (non-Javadoc)
48      * @see org.eclipse.core.components.nesting.ISharedContext#getSharedComponents()
49      */

50     public IServiceProvider getSharedComponents() {
51         return sharedComponents;
52     }
53     };
54     
55     /**
56      * Creates a new NestedContext
57      *
58      * @param sharedComponents
59      * @param nestedFactories
60      */

61     public NestedContext(IServiceProvider sharedComponents, ServiceFactory nestedFactories) {
62         this.sharedComponents = sharedComponents;
63         this.nestedFactories = nestedFactories;
64     }
65     
66     public ComponentHandle createHandle(Object JavaDoc componentKey, IServiceProvider container)
67             throws ComponentException {
68         
69         ComponentHandle handle = nestedFactories.createHandle(componentKey, new ServiceMap(container)
70                 .map(ISharedContext.class, sharedContext));
71         
72         if (handle == null) {
73             return null;
74         }
75         
76         Object JavaDoc component = handle.getInstance();
77         
78         if (!(component instanceof INestedComponent)) {
79             throw new ComponentException(NLS.bind(WorkbenchMessages.NestedContext_0, new String JavaDoc[] { //$NON-NLS-1$
80
INestedComponent.class.getName(), component.getClass().getName()}
81             ), null);
82         }
83         
84         componentMap.put(componentKey, component);
85         
86         return handle;
87     }
88     
89     /* (non-Javadoc)
90      * @see org.eclipse.core.components.IComponentContext#hasKey(java.lang.Object)
91      */

92     public boolean hasService(Object JavaDoc componentKey) {
93         return nestedFactories.hasService(componentKey);
94     }
95     
96     /**
97      * Activates all the components created by this context. The components
98      * will copy their current state to the shared container and start
99      * delegating to the shared implementation.
100      */

101     public void activate(Part partBeingActivated) {
102         Collection JavaDoc componentList = componentMap.values();
103         
104         for (Iterator JavaDoc iter = componentList.iterator(); iter.hasNext();) {
105             INestedComponent next = (INestedComponent) iter.next();
106             
107             next.activate(partBeingActivated);
108         }
109     }
110     
111     /**
112      * Deactivates all the components created by this context. The components
113      * will stop delegating to the shared implementation.
114      *
115      * @param newActive context that is about to be activated (or null if none)
116      */

117     public void deactivate(NestedContext newActive) {
118         Set JavaDoc entries = componentMap.entrySet();
119         for (Iterator JavaDoc iter = entries.iterator(); iter.hasNext();) {
120             Map.Entry JavaDoc next = (Map.Entry JavaDoc) iter.next();
121             INestedComponent component = (INestedComponent)next.getValue();
122             
123             component.deactivate(newActive.componentMap.get(next.getKey()));
124         }
125     }
126     
127     /* (non-Javadoc)
128      * @see org.eclipse.core.components.IComponentContext#getMissingDependencies()
129      */

130     public Collection JavaDoc getMissingDependencies() {
131         Collection JavaDoc result = nestedFactories.getMissingDependencies();
132         result.remove(ISharedContext.class);
133         return result;
134     }
135 }
136
Popular Tags