KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > EditorActionBars


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13 import org.eclipse.core.expressions.Expression;
14 import org.eclipse.jface.action.ActionContributionItem;
15 import org.eclipse.jface.action.ContributionManager;
16 import org.eclipse.jface.action.GroupMarker;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.action.IContributionManager;
19 import org.eclipse.jface.action.IContributionManagerOverrides;
20 import org.eclipse.jface.action.ICoolBarManager;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.IToolBarManager;
23 import org.eclipse.jface.action.SubContributionManager;
24 import org.eclipse.jface.action.SubMenuManager;
25 import org.eclipse.jface.action.SubStatusLineManager;
26 import org.eclipse.jface.action.SubToolBarManager;
27 import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem;
28 import org.eclipse.ui.IActionBars2;
29 import org.eclipse.ui.IEditorActionBarContributor;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IWorkbenchActionConstants;
32 import org.eclipse.ui.IWorkbenchPart;
33 import org.eclipse.ui.SubActionBars2;
34 import org.eclipse.ui.actions.RetargetAction;
35 import org.eclipse.ui.internal.expressions.LegacyEditorActionBarExpression;
36 import org.eclipse.ui.internal.misc.Policy;
37 import org.eclipse.ui.internal.provisional.presentations.IActionBarPresentationFactory;
38 import org.eclipse.ui.services.IServiceLocator;
39
40 /**
41  * The action bars for an editor.
42  */

43 public class EditorActionBars extends SubActionBars2 {
44
45     private class Overrides implements IContributionManagerOverrides {
46
47         public Integer JavaDoc getAccelerator(IContributionItem item) {
48             return null;
49         }
50
51         public String JavaDoc getAcceleratorText(IContributionItem item) {
52             return null;
53         }
54
55         public Boolean JavaDoc getEnabled(IContributionItem item) {
56             if (((item instanceof ActionContributionItem) && (((ActionContributionItem) item)
57                     .getAction() instanceof RetargetAction))
58                     || enabledAllowed) {
59                 return null;
60             } else {
61                 return Boolean.FALSE;
62             }
63         }
64
65         public String JavaDoc getText(IContributionItem item) {
66             return null;
67         }
68     }
69
70     /**
71      * A switch controlling verbose debugging options surrounding the disposal
72      * of tool bar contribution items. There have been problems in the past with
73      * reusing disposed items, and leaking memory by failing to drop references
74      * to disposed items.
75      */

76     private static final boolean DEBUG_TOOLBAR_DISPOSAL = Policy.DEBUG_TOOLBAR_DISPOSAL;
77
78     private IToolBarManager coolItemToolBarMgr = null;
79
80     private IEditorActionBarContributor editorContributor;
81
82     private boolean enabledAllowed = false;
83
84     private IEditorActionBarContributor extensionContributor;
85
86     private int refCount;
87
88     private IToolBarContributionItem toolBarContributionItem = null;
89
90     private String JavaDoc type;
91     
92     private IActionBarPresentationFactory actionBarPresentation;
93
94
95     /**
96      * Constructs the EditorActionBars for an editor.
97      */

98     public EditorActionBars(WorkbenchPage page,
99             final IServiceLocator serviceLocator, String JavaDoc type) {
100         super((IActionBars2)page.getActionBars(), serviceLocator);
101         this.type = type;
102         this.actionBarPresentation = ((WorkbenchWindow) page
103                 .getWorkbenchWindow()).getActionBarPresentationFactory();
104     }
105
106     /**
107      * Activate the contributions.
108      */

109     public void activate(boolean forceVisibility) {
110         setActive(true, forceVisibility);
111     }
112
113     /**
114      * Add one ref to the bars.
115      */

116     public void addRef() {
117         ++refCount;
118     }
119
120     /*
121      * (non-Javadoc) Method declared on SubActionBars.
122      */

123     protected SubMenuManager createSubMenuManager(IMenuManager parent) {
124         return new EditorMenuManager(parent);
125     }
126
127     /*
128      * (non-Javadoc) Method declared on SubActionBars.
129      */

130     protected SubToolBarManager createSubToolBarManager(IToolBarManager parent) {
131         // return null, editor actions are managed by CoolItemToolBarManagers
132
return null;
133     }
134
135     /**
136      * Deactivate the contributions.
137      */

138     public void deactivate(boolean forceVisibility) {
139         setActive(false, forceVisibility);
140     }
141
142     /**
143      * Dispose the contributions.
144      */

145     public void dispose() {
146         super.dispose();
147         if (editorContributor != null) {
148             editorContributor.dispose();
149         }
150         if (extensionContributor != null) {
151             extensionContributor.dispose();
152         }
153
154         /*
155          * Dispose of the contribution item, but also make sure that no one else
156          * is holding on to it. In this case, go through the SubCoolBarManager
157          * to its parent (the real CoolBarManager), and replace the reference
158          * with a placeholder.
159          */

160         if (toolBarContributionItem != null) {
161             // Create a placeholder and place it in the cool bar manager.
162
ICoolBarManager coolBarManager = getCoolBarManager();
163             if (coolBarManager instanceof SubContributionManager) {
164                 SubContributionManager subManager = (SubContributionManager) coolBarManager;
165                 IContributionManager manager = subManager.getParent();
166                 if (manager instanceof ContributionManager) {
167                     final IContributionItem replacementItem = new PlaceholderContributionItem(
168                             toolBarContributionItem);
169                     boolean succeeded = ((ContributionManager) manager)
170                             .replaceItem(replacementItem.getId(),
171                                     replacementItem);
172                     if (!succeeded && DEBUG_TOOLBAR_DISPOSAL) {
173                         System.out
174                                 .println("FAILURE WHILE DISPOSING EditorActionBars"); //$NON-NLS-1$
175
System.out
176                                 .println("Could not replace " + replacementItem.getId() + " in the contribution manager."); //$NON-NLS-1$ //$NON-NLS-2$
177
}
178                 } else if (DEBUG_TOOLBAR_DISPOSAL) {
179                     System.out
180                             .println("FAILURE WHILE DISPOSING EditorActionBars"); //$NON-NLS-1$
181
System.out
182                             .println("The manager was not a ContributionManager."); //$NON-NLS-1$
183
System.out
184                             .println("It was a " + manager.getClass().getName()); //$NON-NLS-1$
185
}
186             } else if (DEBUG_TOOLBAR_DISPOSAL) {
187                 System.out.println("FAILURE WHILE DISPOSING EditorActionBars"); //$NON-NLS-1$
188
System.out
189                         .println("The coolBarManager was not a SubContributionManager"); //$NON-NLS-1$
190
System.out
191                         .println("It was a " + coolBarManager.getClass().getName()); //$NON-NLS-1$
192
}
193
194             // Dispose of the replaced item.
195
toolBarContributionItem.dispose();
196         }
197         toolBarContributionItem = null;
198         // Remove actions
199
if (coolItemToolBarMgr != null) {
200             coolItemToolBarMgr.removeAll();
201         }
202         coolItemToolBarMgr = null;
203         editorHandlerExpression = null;
204     }
205
206     /**
207      * Gets the editor contributor
208      */

209     public IEditorActionBarContributor getEditorContributor() {
210         return editorContributor;
211     }
212
213     /**
214      * Returns the editor type.
215      */

216     public String JavaDoc getEditorType() {
217         return type;
218     }
219
220     /**
221      * Gets the extension contributor
222      */

223     public IEditorActionBarContributor getExtensionContributor() {
224         return extensionContributor;
225     }
226
227     /**
228      * Returns the reference count.
229      */

230     public int getRef() {
231         return refCount;
232     }
233
234     /**
235      * Returns the tool bar manager. If items are added or removed from the
236      * manager be sure to call <code>updateActionBars</code>. Overridden to
237      * support CoolBars.
238      *
239      * @return the tool bar manager
240      */

241     public IToolBarManager getToolBarManager() {
242
243         // by pass the sub coolBar and use the real cool bar.
244
ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager();
245         if (coolBarManager == null) {
246             return null;
247         }
248
249         // add the editor group if the app did not add it already,
250
// otherwise the references to it below will fail
251
if (coolBarManager.find(IWorkbenchActionConstants.GROUP_EDITOR) == null) {
252             coolBarManager.add(new GroupMarker(
253                     IWorkbenchActionConstants.GROUP_EDITOR));
254         }
255         if (toolBarContributionItem == null) {
256             IContributionItem foundItem = coolBarManager.find(type);
257             if ((foundItem instanceof IToolBarContributionItem)) {
258                 toolBarContributionItem = (IToolBarContributionItem) foundItem;
259                 coolItemToolBarMgr = toolBarContributionItem.getToolBarManager();
260                 if (coolItemToolBarMgr == null) {
261                     coolItemToolBarMgr = actionBarPresentation.createToolBarManager();
262                     toolBarContributionItem = actionBarPresentation.createToolBarContributionItem(
263                             coolItemToolBarMgr, type);
264                     // Add editor item to group
265
coolBarManager.prependToGroup(
266                             IWorkbenchActionConstants.GROUP_EDITOR,
267                             toolBarContributionItem);
268                 }
269             } else {
270                 coolItemToolBarMgr = actionBarPresentation.createToolBarManager();
271                 if ((coolBarManager instanceof ContributionManager)
272                         && (foundItem instanceof PlaceholderContributionItem)) {
273                     PlaceholderContributionItem placeholder = (PlaceholderContributionItem) foundItem;
274                     toolBarContributionItem = createToolBarContributionItem(coolItemToolBarMgr, placeholder);
275                     // Restore from a placeholder
276
((ContributionManager) coolBarManager).replaceItem(type,
277                             toolBarContributionItem);
278                 } else {
279                     toolBarContributionItem = actionBarPresentation.createToolBarContributionItem(
280                             coolItemToolBarMgr, type);
281                     // Add editor item to group
282
coolBarManager.prependToGroup(
283                             IWorkbenchActionConstants.GROUP_EDITOR,
284                             toolBarContributionItem);
285                 }
286             }
287             ((ContributionManager)coolItemToolBarMgr).setOverrides(new Overrides());
288             toolBarContributionItem.setVisible(getActive());
289             coolItemToolBarMgr.markDirty();
290         }
291
292         return coolItemToolBarMgr;
293     }
294     
295     /*
296      * Creates a new tool bar contribution item on the given manager -- using
297      * the stored data to initialize some of its properties.
298      */

299     IToolBarContributionItem createToolBarContributionItem(
300             final IToolBarManager manager, PlaceholderContributionItem item) {
301         IToolBarContributionItem toolBarContributionItem = actionBarPresentation
302                 .createToolBarContributionItem(manager, item.getId());
303         toolBarContributionItem.setCurrentHeight(item.getHeight());
304         toolBarContributionItem.setCurrentWidth(item.getWidth());
305         toolBarContributionItem.setMinimumItemsToShow(item.getMinimumItemsToShow());
306         toolBarContributionItem.setUseChevron(item.getUseChevron());
307         return toolBarContributionItem;
308     }
309
310     /**
311      * Returns whether the contribution list is visible. If the visibility is
312      * <code>true</code> then each item within the manager appears within the
313      * parent manager. Otherwise, the items are not visible.
314      *
315      * @return <code>true</code> if the manager is visible
316      */

317     private boolean isVisible() {
318         if (toolBarContributionItem != null) {
319             return toolBarContributionItem.isVisible();
320         }
321         return false;
322     }
323
324     /**
325      * Sets the target part for the action bars. For views this is ignored
326      * because each view has its own action vector. For editors this is
327      * important because the action vector is shared by editors of the same
328      * type.
329      */

330     public void partChanged(IWorkbenchPart part) {
331         super.partChanged(part);
332         if (part instanceof IEditorPart) {
333             IEditorPart editor = (IEditorPart) part;
334             if (editorContributor != null) {
335                 editorContributor.setActiveEditor(editor);
336             }
337             if (extensionContributor != null) {
338                 extensionContributor.setActiveEditor(editor);
339             }
340         }
341     }
342
343     /**
344      * Remove one ref to the bars.
345      */

346     public void removeRef() {
347         --refCount;
348     }
349
350     /**
351      * Activate / Deactivate the contributions.
352      *
353      * Workaround for flashing when editor contributes many menu/tool
354      * contributions. In this case, the force visibility flag determines if the
355      * contributions should be actually made visible/hidden or just change the
356      * enablement state.
357      */

358     private void setActive(boolean set, boolean forceVisibility) {
359         basicSetActive(set);
360         if (isSubMenuManagerCreated()) {
361             ((EditorMenuManager) getMenuManager()).setVisible(set,
362                     forceVisibility);
363         }
364
365         if (isSubStatusLineManagerCreated()) {
366             ((SubStatusLineManager) getStatusLineManager()).setVisible(set);
367         }
368
369         setVisible(set, forceVisibility);
370     }
371
372     /**
373      * Sets the editor contributor
374      */

375     public void setEditorContributor(IEditorActionBarContributor c) {
376         editorContributor = c;
377     }
378
379     /**
380      * Sets the enablement ability of all the items contributed by the editor.
381      *
382      * @param enabledAllowed
383      * <code>true</code> if the items may enable
384      * @since 2.0
385      */

386     private void setEnabledAllowed(boolean enabledAllowed) {
387         if (this.enabledAllowed == enabledAllowed) {
388             return;
389         }
390         this.enabledAllowed = enabledAllowed;
391         if (coolItemToolBarMgr != null) {
392             IContributionItem[] items = coolItemToolBarMgr.getItems();
393             for (int i = 0; i < items.length; i++) {
394                 IContributionItem item = items[i];
395                 item.update(IContributionManagerOverrides.P_ENABLED);
396             }
397         }
398     }
399
400     /**
401      * Sets the extension contributor
402      */

403     public void setExtensionContributor(IEditorActionBarContributor c) {
404         extensionContributor = c;
405     }
406
407     /**
408      * Sets the visibility of the manager. If the visibility is
409      * <code>true</code> then each item within the manager appears within the
410      * parent manager. Otherwise, the items are not visible.
411      *
412      * @param visible
413      * the new visibility
414      */

415     private void setVisible(boolean visible) {
416         if (toolBarContributionItem != null) {
417             toolBarContributionItem.setVisible(visible);
418             if (toolBarContributionItem.getParent() != null) {
419                 toolBarContributionItem.getParent().markDirty();
420             }
421         }
422     }
423
424     /**
425      * Sets the visibility of the manager. If the visibility is
426      * <code>true</code> then each item within the manager appears within the
427      * parent manager. Otherwise, the items are not visible if force visibility
428      * is <code>true</code>, or grayed out if force visibility is
429      * <code>false</code>
430      * <p>
431      * This is a workaround for the layout flashing when editors contribute
432      * large amounts of items.
433      * </p>
434      *
435      * @param visible
436      * the new visibility
437      * @param forceVisibility
438      * <code>true</code> to change the visibility or
439      * <code>false</code> to change just the enablement state. This
440      * parameter is ignored if visible is <code>true</code>.
441      */

442     private void setVisible(boolean visible, boolean forceVisibility) {
443         if (visible) {
444             setEnabledAllowed(true);
445             if (!isVisible()) {
446                 setVisible(true);
447             }
448         } else {
449             if (forceVisibility) {
450                 // Remove the editor tool bar items
451
setVisible(false);
452             } else {
453                 // Disabled the tool bar items.
454
setEnabledAllowed(false);
455             }
456         }
457
458         ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager();
459         if ((coolItemToolBarMgr != null) && (coolBarManager != null)) {
460             IContributionItem[] items = coolItemToolBarMgr.getItems();
461             for (int i = 0; i < items.length; i++) {
462                 IContributionItem item = items[i];
463                 item.setVisible(visible || !forceVisibility);
464                 coolItemToolBarMgr.markDirty();
465                 if (!coolBarManager.isDirty()) {
466                     coolBarManager.markDirty();
467                 }
468             }
469             // Update the manager
470
coolItemToolBarMgr.update(false);
471             if (toolBarContributionItem != null) {
472                 toolBarContributionItem.setVisible(visible || !forceVisibility);
473             }
474         }
475     }
476
477     private LegacyEditorActionBarExpression editorHandlerExpression = null;
478     
479     /**
480      * Returns the expression used for action handler activation.
481      * @return the expression used for action handler activation.
482      */

483     public Expression getHandlerExpression() {
484         if (editorHandlerExpression == null) {
485             editorHandlerExpression = new LegacyEditorActionBarExpression(type);
486         }
487         return editorHandlerExpression;
488     }
489 }
490
Popular Tags