KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > services > ChildActionBarContributorFactory


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 Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.part.services;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.ui.internal.components.framework.ComponentException;
18 import org.eclipse.ui.internal.components.framework.IDisposable;
19 import org.eclipse.ui.internal.components.framework.IServiceProvider;
20 import org.eclipse.ui.internal.part.Part;
21 import org.eclipse.ui.internal.part.components.services.IActionBarContributor;
22 import org.eclipse.ui.internal.part.components.services.IActionBarContributorFactory;
23 import org.eclipse.ui.internal.part.components.services.IPartDescriptor;
24 import org.eclipse.ui.internal.part.multiplexer.INestedComponent;
25 import org.eclipse.ui.internal.part.multiplexer.ISharedContext;
26
27 public class ChildActionBarContributorFactory implements INestedComponent, IActionBarContributorFactory, IDisposable {
28     
29     private IActionBarContributorFactory parent;
30     private Map JavaDoc activeBars = new HashMap JavaDoc();
31     private boolean active = false;
32     private IPartDescriptor descriptor;
33     private IActionBarContributor defaultContributor;
34     
35     private final class PartMap {
36         PartMap(IActionBarContributor contributor, Part part) {
37             this.contributor = contributor;
38             this.part = part;
39         }
40         
41         IActionBarContributor contributor;
42         Part part;
43     };
44     
45     public ChildActionBarContributorFactory(IPartDescriptor descr, ISharedContext shared) throws ComponentException {
46         IServiceProvider sharedContainer = shared.getSharedComponents();
47         this.parent = (IActionBarContributorFactory)sharedContainer.getService(IActionBarContributorFactory.class);
48         this.descriptor = descr;
49         this.defaultContributor = parent.getContributor(descr);
50     }
51     
52     public void activate(Part activePart) {
53         for (Iterator JavaDoc iter = activeBars.values().iterator(); iter.hasNext();) {
54             PartMap next = (PartMap) iter.next();
55             
56             parent.activateBars(next.contributor, next.part);
57         }
58         
59         // Note: if the parent and child are of the same type, the parent gets precidence
60
parent.activateBars(defaultContributor, activePart);
61         
62         active = true;
63     }
64     
65     public void deactivate(Object JavaDoc newActive) {
66         ChildActionBarContributorFactory factory = null;
67         if (newActive instanceof ChildActionBarContributorFactory) {
68             factory = (ChildActionBarContributorFactory)newActive;
69         }
70         
71         // Deactivate all the bars that won't be re-activated when the new factory becomes active.
72
for (Iterator JavaDoc iter = activeBars.values().iterator(); iter.hasNext();) {
73             PartMap next = (PartMap) iter.next();
74             
75             String JavaDoc nextId = next.contributor.getDescriptor().getId();
76             // No need to deactivate the bars if they'll be reused by the new contributor
77
if (factory == null
78                     || (factory.activeBars.get(nextId) == null
79                     && !factory.defaultContributor.getDescriptor().getId().equals(nextId))) {
80                 
81                 parent.deactivateBars(next.contributor);
82             }
83         }
84
85         String JavaDoc nextId = defaultContributor.getDescriptor().getId();
86         // No need to deactivate the bars if they'll be reused by the new contributor
87
if (factory == null
88                 || (factory.activeBars.get(nextId) == null
89                 && !factory.defaultContributor.getDescriptor().getId().equals(nextId))) {
90             
91             parent.deactivateBars(defaultContributor);
92         }
93         
94         active = false;
95     }
96     
97     public void activateBars(IActionBarContributor toActivate, Part actualPart) {
98         String JavaDoc key = toActivate.getDescriptor().getId();
99         PartMap existing = (PartMap) activeBars.get(key);
100         
101         if (existing == null) {
102             existing = new PartMap(toActivate, actualPart);
103             activeBars.put(key, existing);
104         } else {
105             existing.contributor = toActivate;
106             existing.part = actualPart;
107         }
108         
109         if (active) {
110             parent.activateBars(toActivate, actualPart);
111         }
112     }
113     
114     public void deactivateBars(IActionBarContributor toDeactivate) {
115         String JavaDoc key = toDeactivate.getDescriptor().getId();
116         PartMap existing = (PartMap) activeBars.get(key);
117         
118         if (existing == null) {
119             return;
120         }
121         
122         activeBars.remove(key);
123                 
124         if (active) {
125             parent.deactivateBars(toDeactivate);
126         }
127     }
128     
129     public IActionBarContributor getContributor(IPartDescriptor descriptor) {
130         return parent.getContributor(descriptor);
131     }
132
133     public void dispose() {
134         defaultContributor.dispose();
135     }
136 }
137
Popular Tags