KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.ui.IActionBars;
17 import org.eclipse.ui.IEditorActionBarContributor;
18 import org.eclipse.ui.IEditorDescriptor;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
22
23 /**
24  * This class reads the registry for extensions that plug into
25  * 'editorActions' extension point.
26  */

27 public class EditorActionBuilder extends PluginActionBuilder {
28     private static final String JavaDoc TAG_CONTRIBUTION_TYPE = "editorContribution"; //$NON-NLS-1$
29

30     /**
31      * The constructor.
32      */

33     public EditorActionBuilder() {
34     }
35
36     /* (non-Javadoc)
37      * Method declared on PluginActionBuilder.
38      */

39     protected ActionDescriptor createActionDescriptor(
40             IConfigurationElement element) {
41         return new ActionDescriptor(element, ActionDescriptor.T_EDITOR);
42     }
43
44     /* (non-Javadoc)
45      * Method declared on PluginActionBuilder.
46      */

47     protected BasicContribution createContribution() {
48         return new EditorContribution();
49     }
50
51     /**
52      * Reads and apply all external contributions for this editor's ID
53      * registered in 'editorActions' extension point.
54      */

55     public IEditorActionBarContributor readActionExtensions(
56             IEditorDescriptor desc) {
57         ExternalContributor ext = null;
58         readContributions(desc.getId(), TAG_CONTRIBUTION_TYPE,
59                 IWorkbenchRegistryConstants.PL_EDITOR_ACTIONS);
60         if (cache != null) {
61             ext = new ExternalContributor(cache);
62             cache = null;
63         }
64         return ext;
65     }
66
67     /**
68      * Helper class to collect the menus and actions defined within a
69      * contribution element.
70      */

71     private static class EditorContribution extends BasicContribution {
72         public void dispose() {
73             disposeActions();
74             super.dispose();
75         }
76
77         public void editorChanged(IEditorPart editor) {
78             if (actions != null) {
79                 for (int i = 0; i < actions.size(); i++) {
80                     ActionDescriptor ad = (ActionDescriptor) actions.get(i);
81                     EditorPluginAction action = (EditorPluginAction) ad
82                             .getAction();
83                     action.editorChanged(editor);
84                 }
85             }
86         }
87     }
88
89     /**
90      * Helper class that will populate the menu and toobar with the external
91      * editor contributions.
92      */

93     public static class ExternalContributor implements
94             IEditorActionBarContributor {
95         private ArrayList JavaDoc cache;
96
97         public ExternalContributor(ArrayList JavaDoc cache) {
98             this.cache = cache;
99         }
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 JavaDoc results = new ArrayList JavaDoc();
109             for (int i = 0; i < cache.size(); i++) {
110                 EditorContribution ec = (EditorContribution) cache.get(i);
111                 if (ec.actions != null) {
112                     results.addAll(ec.actions);
113                 }
114             }
115             return (ActionDescriptor[]) results
116                     .toArray(new ActionDescriptor[results.size()]);
117         }
118
119         public void init(IActionBars bars, IWorkbenchPage page) {
120             for (int i = 0; i < cache.size(); i++) {
121                 ((EditorContribution) cache.get(i)).contribute(bars
122                         .getMenuManager(), false, bars.getToolBarManager(),
123                         true);
124             }
125         }
126
127         public void setActiveEditor(IEditorPart editor) {
128             for (int i = 0; i < cache.size(); i++) {
129                 ((EditorContribution) cache.get(i)).editorChanged(editor);
130             }
131         }
132     }
133 }
134
Popular Tags