KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.ui.IActionBars;
19 import org.eclipse.ui.IActionBars2;
20 import org.eclipse.ui.IEditorActionBarContributor;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IEditorRegistry;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.internal.EditorActionBars;
25 import org.eclipse.ui.internal.EditorActionBuilder;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27 import org.eclipse.ui.internal.components.framework.Components;
28 import org.eclipse.ui.internal.components.framework.IDisposable;
29 import org.eclipse.ui.internal.part.Part;
30 import org.eclipse.ui.internal.part.components.services.IActionBarContributor;
31 import org.eclipse.ui.internal.part.components.services.IActionBarContributorFactory;
32 import org.eclipse.ui.internal.part.components.services.IPartActionBars;
33 import org.eclipse.ui.internal.part.components.services.IPartDescriptor;
34 import org.eclipse.ui.internal.part.components.services.IStatusFactory;
35 import org.eclipse.ui.internal.part.components.services.IStatusHandler;
36 import org.eclipse.ui.internal.registry.EditorDescriptor;
37
38 public class ActionBarContributorFactory implements IActionBarContributorFactory, IDisposable {
39
40 // private IPartActionBars partActionBars;
41
private IActionBars2 partToEditorActionBars;
42     private IWorkbenchPage page;
43     
44     private Map JavaDoc actionCache = new HashMap JavaDoc();
45     private class NullActivator implements IActionBarContributor {
46         IPartDescriptor descriptor = new NullPartDescriptor();
47         
48         public void dispose() {
49             
50         }
51         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
52             return null;
53         }
54         
55         public IPartDescriptor getDescriptor() {
56             return descriptor;
57         }
58     };
59     
60     private NullActivator nullActivator = new NullActivator();
61     
62     private class EditorActionBarActivator implements IActionBarContributor {
63         EditorActionBars actionBars;
64         IPartDescriptor descriptor;
65         IEditorPart activePart = null;
66         boolean isActive = false;
67         
68         EditorActionBarActivator(EditorActionBars actionBars, IPartDescriptor descriptor) {
69             this.actionBars = actionBars;
70             this.descriptor = descriptor;
71         }
72         
73         public void dispose() {
74             if (actionBars == null) {
75                 return;
76             }
77             
78             actionBars.removeRef();
79             if (actionBars.getRef() <= 0) {
80                 String JavaDoc type = actionBars.getEditorType();
81                 actionCache.remove(type);
82                 actionBars.dispose();
83                 actionBars = null;
84             }
85         }
86         
87         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
88             if (actionBars == null) {
89                 return null;
90             }
91             if (adapter == IEditorActionBarContributor.class) {
92                 return actionBars.getEditorContributor();
93             }
94             if (adapter == IActionBars.class) {
95                 return actionBars;
96             }
97             return null;
98         }
99
100         public IPartDescriptor getDescriptor() {
101             return descriptor;
102         }
103         
104         public void activate(Part activePart) {
105             if (actionBars == null) {
106                 return;
107             }
108             
109             IEditorPart editor = (IEditorPart)Components.getAdapter(activePart, IEditorPart.class);
110             
111             if (!isActive) {
112                 isActive = true;
113                 actionBars.activate(true);
114             }
115             
116             if (editor != activePart) {
117                 actionBars.partChanged(editor);
118                 this.activePart = editor;
119             }
120             
121             actionBars.updateActionBars();
122         }
123         
124         public void deactivate() {
125             if (actionBars == null) {
126                 return;
127             }
128             if (!isActive) {
129                 return;
130             }
131             
132             actionBars.deactivate(true);
133             isActive = false;
134         }
135     };
136     
137     public ActionBarContributorFactory(IPartActionBars parent, IWorkbenchPage page, IStatusHandler handler, IStatusFactory factory) {
138         this.partToEditorActionBars = new PartToEditorActionBarsAdapter(parent,
139                 handler, factory);
140         this.page = page;
141     }
142     
143     public void activateBars(IActionBarContributor toActivate, Part actualPart) {
144         if (toActivate instanceof EditorActionBarActivator) {
145             EditorActionBarActivator eaba = (EditorActionBarActivator) toActivate;
146             
147             eaba.activate(actualPart);
148         }
149     }
150     
151     public void deactivateBars(IActionBarContributor toDeactivate) {
152         if (toDeactivate instanceof EditorActionBarActivator) {
153             EditorActionBarActivator eaba = (EditorActionBarActivator) toDeactivate;
154             
155             eaba.deactivate();
156         }
157     }
158     
159     public IActionBarContributor getContributor(IPartDescriptor descriptor) {
160         String JavaDoc id = descriptor.getId();
161         
162         IEditorRegistry reg = WorkbenchPlugin.getDefault().getEditorRegistry();
163         EditorDescriptor desc = (EditorDescriptor) reg.findEditor(id);
164         if (desc == null) {
165             return nullActivator;
166         }
167         
168         EditorActionBarActivator activator = (EditorActionBarActivator) actionCache.get(id);
169         if (activator == null) {
170             // Create a new action bar set.
171
EditorActionBars actionBars = new EditorActionBars(
172                     partToEditorActionBars, id);
173
174             // Read base contributor.
175
IEditorActionBarContributor contr = desc.createActionBarContributor();
176             if (contr != null) {
177                 actionBars.setEditorContributor(contr);
178                 contr.init(actionBars, page);
179             }
180
181             // Read action extensions.
182
EditorActionBuilder builder = new EditorActionBuilder();
183             contr = builder.readActionExtensions(desc);
184             if (contr != null) {
185                 actionBars.setExtensionContributor(contr);
186                 contr.init(actionBars, page);
187             }
188             
189             activator = new EditorActionBarActivator(actionBars, descriptor);
190             
191             actionCache.put(id, activator);
192         }
193         
194         activator.actionBars.addRef();
195         return activator;
196     }
197
198     public void dispose() {
199         Collection JavaDoc activators = actionCache.values();
200         
201         for (Iterator JavaDoc iter = activators.iterator(); iter.hasNext();) {
202             EditorActionBarActivator next = (EditorActionBarActivator) iter.next();
203             
204             next.actionBars.dispose();
205         }
206         
207         actionCache.clear();
208     }
209 }
210
Popular Tags