001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.ui.IActionBars;
016: import org.eclipse.ui.IEditorActionBarContributor;
017: import org.eclipse.ui.IEditorDescriptor;
018: import org.eclipse.ui.IEditorPart;
019: import org.eclipse.ui.IWorkbenchPage;
020: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
021:
022: /**
023: * This class reads the registry for extensions that plug into
024: * 'editorActions' extension point.
025: */
026: public class EditorActionBuilder extends PluginActionBuilder {
027: private static final String TAG_CONTRIBUTION_TYPE = "editorContribution"; //$NON-NLS-1$
028:
029: /**
030: * The constructor.
031: */
032: public EditorActionBuilder() {
033: }
034:
035: /* (non-Javadoc)
036: * Method declared on PluginActionBuilder.
037: */
038: protected ActionDescriptor createActionDescriptor(
039: IConfigurationElement element) {
040: return new ActionDescriptor(element, ActionDescriptor.T_EDITOR);
041: }
042:
043: /* (non-Javadoc)
044: * Method declared on PluginActionBuilder.
045: */
046: protected BasicContribution createContribution() {
047: return new EditorContribution();
048: }
049:
050: /**
051: * Reads and apply all external contributions for this editor's ID
052: * registered in 'editorActions' extension point.
053: */
054: public IEditorActionBarContributor readActionExtensions(
055: IEditorDescriptor desc) {
056: ExternalContributor ext = null;
057: readContributions(desc.getId(), TAG_CONTRIBUTION_TYPE,
058: IWorkbenchRegistryConstants.PL_EDITOR_ACTIONS);
059: if (cache != null) {
060: ext = new ExternalContributor(cache);
061: cache = null;
062: }
063: return ext;
064: }
065:
066: /**
067: * Helper class to collect the menus and actions defined within a
068: * contribution element.
069: */
070: private static class EditorContribution extends BasicContribution {
071: public void dispose() {
072: disposeActions();
073: super .dispose();
074: }
075:
076: public void editorChanged(IEditorPart editor) {
077: if (actions != null) {
078: for (int i = 0; i < actions.size(); i++) {
079: ActionDescriptor ad = (ActionDescriptor) actions
080: .get(i);
081: EditorPluginAction action = (EditorPluginAction) ad
082: .getAction();
083: action.editorChanged(editor);
084: }
085: }
086: }
087: }
088:
089: /**
090: * Helper class that will populate the menu and toobar with the external
091: * editor contributions.
092: */
093: public static class ExternalContributor implements
094: IEditorActionBarContributor {
095: private ArrayList cache;
096:
097: public ExternalContributor(ArrayList cache) {
098: this .cache = cache;
099: }
100:
101: public void dispose() {
102: for (int i = 0; i < cache.size(); i++) {
103: ((EditorContribution) cache.get(i)).dispose();
104: }
105: }
106:
107: public ActionDescriptor[] getExtendedActions() {
108: ArrayList results = new ArrayList();
109: for (int i = 0; i < cache.size(); i++) {
110: EditorContribution ec = (EditorContribution) cache
111: .get(i);
112: if (ec.actions != null) {
113: results.addAll(ec.actions);
114: }
115: }
116: return (ActionDescriptor[]) results
117: .toArray(new ActionDescriptor[results.size()]);
118: }
119:
120: public void init(IActionBars bars, IWorkbenchPage page) {
121: for (int i = 0; i < cache.size(); i++) {
122: ((EditorContribution) cache.get(i)).contribute(bars
123: .getMenuManager(), false, bars
124: .getToolBarManager(), true);
125: }
126: }
127:
128: public void setActiveEditor(IEditorPart editor) {
129: for (int i = 0; i < cache.size(); i++) {
130: ((EditorContribution) cache.get(i))
131: .editorChanged(editor);
132: }
133: }
134: }
135: }
|