1 11 package org.eclipse.ui.internal.part.multiplexer; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 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 39 public class NestedContext extends ServiceFactory { 40 41 private IServiceProvider sharedComponents; 43 private ServiceFactory nestedFactories; 44 private Map componentMap = new HashMap (); 45 46 private ISharedContext sharedContext = new ISharedContext() { 47 50 public IServiceProvider getSharedComponents() { 51 return sharedComponents; 52 } 53 }; 54 55 61 public NestedContext(IServiceProvider sharedComponents, ServiceFactory nestedFactories) { 62 this.sharedComponents = sharedComponents; 63 this.nestedFactories = nestedFactories; 64 } 65 66 public ComponentHandle createHandle(Object 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 component = handle.getInstance(); 77 78 if (!(component instanceof INestedComponent)) { 79 throw new ComponentException(NLS.bind(WorkbenchMessages.NestedContext_0, new String [] { INestedComponent.class.getName(), component.getClass().getName()} 81 ), null); 82 } 83 84 componentMap.put(componentKey, component); 85 86 return handle; 87 } 88 89 92 public boolean hasService(Object componentKey) { 93 return nestedFactories.hasService(componentKey); 94 } 95 96 101 public void activate(Part partBeingActivated) { 102 Collection componentList = componentMap.values(); 103 104 for (Iterator iter = componentList.iterator(); iter.hasNext();) { 105 INestedComponent next = (INestedComponent) iter.next(); 106 107 next.activate(partBeingActivated); 108 } 109 } 110 111 117 public void deactivate(NestedContext newActive) { 118 Set entries = componentMap.entrySet(); 119 for (Iterator iter = entries.iterator(); iter.hasNext();) { 120 Map.Entry next = (Map.Entry ) iter.next(); 121 INestedComponent component = (INestedComponent)next.getValue(); 122 123 component.deactivate(newActive.componentMap.get(next.getKey())); 124 } 125 } 126 127 130 public Collection getMissingDependencies() { 131 Collection result = nestedFactories.getMissingDependencies(); 132 result.remove(ISharedContext.class); 133 return result; 134 } 135 } 136 | Popular Tags |