KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > BasicJavaEditorActionContributor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.javaeditor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.action.Separator;
23
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IWorkbenchActionConstants;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.actions.RetargetAction;
29 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
30 import org.eclipse.ui.texteditor.ITextEditor;
31 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
32 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
33 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
34
35 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
36
37 import org.eclipse.jdt.internal.ui.actions.CopyQualifiedNameAction;
38 import org.eclipse.jdt.internal.ui.actions.FoldingActionGroup;
39 import org.eclipse.jdt.internal.ui.javaeditor.selectionactions.GoToNextPreviousMemberAction;
40 import org.eclipse.jdt.internal.ui.javaeditor.selectionactions.StructureSelectionAction;
41
42 /**
43  * Common base class for action contributors for Java editors.
44  */

45 public class BasicJavaEditorActionContributor extends BasicTextEditorActionContributor {
46
47     private List JavaDoc fPartListeners= new ArrayList JavaDoc();
48
49     private TogglePresentationAction fTogglePresentation;
50     private ToggleMarkOccurrencesAction fToggleMarkOccurrencesAction;
51
52     private RetargetTextEditorAction fGotoMatchingBracket;
53     private RetargetTextEditorAction fShowOutline;
54     private RetargetTextEditorAction fOpenStructure;
55     private RetargetTextEditorAction fOpenHierarchy;
56
57     private RetargetTextEditorAction fRetargetShowInformationAction;
58
59     private RetargetTextEditorAction fStructureSelectEnclosingAction;
60     private RetargetTextEditorAction fStructureSelectNextAction;
61     private RetargetTextEditorAction fStructureSelectPreviousAction;
62     private RetargetTextEditorAction fStructureSelectHistoryAction;
63
64     private RetargetTextEditorAction fGotoNextMemberAction;
65     private RetargetTextEditorAction fGotoPreviousMemberAction;
66
67     private RetargetTextEditorAction fRemoveOccurrenceAnnotationsAction;
68
69     public BasicJavaEditorActionContributor() {
70         super();
71
72         ResourceBundle JavaDoc b= JavaEditorMessages.getBundleForConstructedKeys();
73
74         fRetargetShowInformationAction= new RetargetTextEditorAction(b, "Editor.ShowInformation."); //$NON-NLS-1$
75
fRetargetShowInformationAction.setActionDefinitionId(ITextEditorActionDefinitionIds.SHOW_INFORMATION);
76
77         // actions that are "contributed" to editors, they are considered belonging to the active editor
78
fTogglePresentation= new TogglePresentationAction();
79
80         fToggleMarkOccurrencesAction= new ToggleMarkOccurrencesAction();
81
82         fGotoMatchingBracket= new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
83
fGotoMatchingBracket.setActionDefinitionId(IJavaEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
84
85         fShowOutline= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "ShowOutline."); //$NON-NLS-1$
86
fShowOutline.setActionDefinitionId(IJavaEditorActionDefinitionIds.SHOW_OUTLINE);
87
88         fOpenHierarchy= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "OpenHierarchy."); //$NON-NLS-1$
89
fOpenHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_HIERARCHY);
90
91         fOpenStructure= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "OpenStructure."); //$NON-NLS-1$
92
fOpenStructure.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_STRUCTURE);
93
94         fStructureSelectEnclosingAction= new RetargetTextEditorAction(b, "StructureSelectEnclosing."); //$NON-NLS-1$
95
fStructureSelectEnclosingAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SELECT_ENCLOSING);
96         fStructureSelectNextAction= new RetargetTextEditorAction(b, "StructureSelectNext."); //$NON-NLS-1$
97
fStructureSelectNextAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SELECT_NEXT);
98         fStructureSelectPreviousAction= new RetargetTextEditorAction(b, "StructureSelectPrevious."); //$NON-NLS-1$
99
fStructureSelectPreviousAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SELECT_PREVIOUS);
100         fStructureSelectHistoryAction= new RetargetTextEditorAction(b, "StructureSelectHistory."); //$NON-NLS-1$
101
fStructureSelectHistoryAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SELECT_LAST);
102
103         fGotoNextMemberAction= new RetargetTextEditorAction(b, "GotoNextMember."); //$NON-NLS-1$
104
fGotoNextMemberAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.GOTO_NEXT_MEMBER);
105         fGotoPreviousMemberAction= new RetargetTextEditorAction(b, "GotoPreviousMember."); //$NON-NLS-1$
106
fGotoPreviousMemberAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.GOTO_PREVIOUS_MEMBER);
107
108         fRemoveOccurrenceAnnotationsAction= new RetargetTextEditorAction(b, "RemoveOccurrenceAnnotations."); //$NON-NLS-1$
109
fRemoveOccurrenceAnnotationsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.REMOVE_OCCURRENCE_ANNOTATIONS);
110     }
111
112     protected final void markAsPartListener(RetargetAction action) {
113         fPartListeners.add(action);
114     }
115
116     /*
117      * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
118      */

119     public void init(IActionBars bars, IWorkbenchPage page) {
120         Iterator JavaDoc e= fPartListeners.iterator();
121         while (e.hasNext())
122             page.addPartListener((RetargetAction) e.next());
123
124         super.init(bars, page);
125
126         bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY, fTogglePresentation);
127         bars.setGlobalActionHandler(IJavaEditorActionDefinitionIds.TOGGLE_MARK_OCCURRENCES, fToggleMarkOccurrencesAction);
128
129     }
130
131     /*
132      * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
133      */

134     public void contributeToMenu(IMenuManager menu) {
135
136         super.contributeToMenu(menu);
137
138         IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
139         if (editMenu != null) {
140
141             MenuManager structureSelection= new MenuManager(JavaEditorMessages.ExpandSelectionMenu_label, "expandSelection"); //$NON-NLS-1$
142
editMenu.insertAfter(ITextEditorActionConstants.SELECT_ALL, structureSelection);
143             structureSelection.add(fStructureSelectEnclosingAction);
144             structureSelection.add(fStructureSelectNextAction);
145             structureSelection.add(fStructureSelectPreviousAction);
146             structureSelection.add(fStructureSelectHistoryAction);
147
148             editMenu.appendToGroup(ITextEditorActionConstants.GROUP_INFORMATION, fRetargetShowInformationAction);
149         }
150
151         IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
152         if (navigateMenu != null) {
153             navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fShowOutline);
154             navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fOpenHierarchy);
155         }
156
157         IMenuManager gotoMenu= menu.findMenuUsingPath("navigate/goTo"); //$NON-NLS-1$
158
if (gotoMenu != null) {
159             gotoMenu.add(new Separator("additions2")); //$NON-NLS-1$
160
gotoMenu.appendToGroup("additions2", fGotoPreviousMemberAction); //$NON-NLS-1$
161
gotoMenu.appendToGroup("additions2", fGotoNextMemberAction); //$NON-NLS-1$
162
gotoMenu.appendToGroup("additions2", fGotoMatchingBracket); //$NON-NLS-1$
163
}
164     }
165
166     /*
167      * @see EditorActionBarContributor#setActiveEditor(IEditorPart)
168      */

169     public void setActiveEditor(IEditorPart part) {
170
171         super.setActiveEditor(part);
172
173         ITextEditor textEditor= null;
174         if (part instanceof ITextEditor)
175             textEditor= (ITextEditor)part;
176
177         fTogglePresentation.setEditor(textEditor);
178         fToggleMarkOccurrencesAction.setEditor(textEditor);
179
180         fGotoMatchingBracket.setAction(getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
181         fShowOutline.setAction(getAction(textEditor, IJavaEditorActionDefinitionIds.SHOW_OUTLINE));
182         fOpenHierarchy.setAction(getAction(textEditor, IJavaEditorActionDefinitionIds.OPEN_HIERARCHY));
183         fOpenStructure.setAction(getAction(textEditor, IJavaEditorActionDefinitionIds.OPEN_STRUCTURE));
184
185         fStructureSelectEnclosingAction.setAction(getAction(textEditor, StructureSelectionAction.ENCLOSING));
186         fStructureSelectNextAction.setAction(getAction(textEditor, StructureSelectionAction.NEXT));
187         fStructureSelectPreviousAction.setAction(getAction(textEditor, StructureSelectionAction.PREVIOUS));
188         fStructureSelectHistoryAction.setAction(getAction(textEditor, StructureSelectionAction.HISTORY));
189
190         fGotoNextMemberAction.setAction(getAction(textEditor, GoToNextPreviousMemberAction.NEXT_MEMBER));
191         fGotoPreviousMemberAction.setAction(getAction(textEditor, GoToNextPreviousMemberAction.PREVIOUS_MEMBER));
192
193         fRemoveOccurrenceAnnotationsAction.setAction(getAction(textEditor, "RemoveOccurrenceAnnotations")); //$NON-NLS-1$
194
fRetargetShowInformationAction.setAction(getAction(textEditor, ITextEditorActionConstants.SHOW_INFORMATION));
195
196         if (part instanceof JavaEditor) {
197             JavaEditor javaEditor= (JavaEditor) part;
198             javaEditor.getActionGroup().fillActionBars(getActionBars());
199             FoldingActionGroup foldingActions= javaEditor.getFoldingActionGroup();
200             if (foldingActions != null)
201                 foldingActions.updateActionBars();
202         }
203
204         IActionBars actionBars= getActionBars();
205         IStatusLineManager manager= actionBars.getStatusLineManager();
206         manager.setMessage(null);
207         manager.setErrorMessage(null);
208         
209         /** The global actions to be connected with editor actions */
210         IAction action= getAction(textEditor, ITextEditorActionConstants.NEXT);
211         actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, action);
212         actionBars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, action);
213         action= getAction(textEditor, ITextEditorActionConstants.PREVIOUS);
214         actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, action);
215         actionBars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, action);
216         action= getAction(textEditor, IJavaEditorActionConstants.COPY_QUALIFIED_NAME);
217         actionBars.setGlobalActionHandler(CopyQualifiedNameAction.ACTION_HANDLER_ID, action);
218     }
219
220     /*
221      * @see IEditorActionBarContributor#dispose()
222      */

223     public void dispose() {
224
225         Iterator JavaDoc e= fPartListeners.iterator();
226         while (e.hasNext())
227             getPage().removePartListener((RetargetAction) e.next());
228         fPartListeners.clear();
229
230         setActiveEditor(null);
231         super.dispose();
232     }
233 }
234
Popular Tags