ebbc55303410035dbafb7337e6a27f46d3bb5a8e
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpeclipse.phpeditor;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.ResourceBundle;
17
18 import net.sourceforge.phpdt.internal.ui.actions.FoldingActionGroup;
19 import net.sourceforge.phpdt.ui.IContextMenuConstants;
20 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
21
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.IStatusLineManager;
24 import org.eclipse.jface.action.Separator;
25 import org.eclipse.ui.IActionBars;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IWorkbenchActionConstants;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.actions.RetargetAction;
30 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
31 import org.eclipse.ui.texteditor.GotoAnnotationAction;
32 import org.eclipse.ui.texteditor.ITextEditor;
33 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
34 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
35 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
36
37 /**
38  * Common base class for action contributors for Java editors.
39  */
40 public class BasicJavaEditorActionContributor extends
41                 BasicTextEditorActionContributor {
42
43         private List fPartListeners = new ArrayList();
44
45         private TogglePresentationAction fTogglePresentation;
46
47         private GotoAnnotationAction fPreviousAnnotation;
48         private GotoAnnotationAction fNextAnnotation;
49
50         private RetargetTextEditorAction fGotoMatchingBracket;
51
52         // private RetargetTextEditorAction fShowOutline;
53         // private RetargetTextEditorAction fOpenStructure;
54         // private RetargetTextEditorAction fOpenHierarchy;
55
56         // private RetargetAction fRetargetShowJavaDoc;
57         // private RetargetTextEditorAction fShowJavaDoc;
58
59         // private RetargetTextEditorAction fStructureSelectEnclosingAction;
60         // private RetargetTextEditorAction fStructureSelectNextAction;
61         // private RetargetTextEditorAction fStructureSelectPreviousAction;
62         // private RetargetTextEditorAction fStructureSelectHistoryAction;
63
64         private RetargetTextEditorAction fGotoNextMemberAction;
65
66         private RetargetTextEditorAction fGotoPreviousMemberAction;
67
68         //
69         // private RetargetTextEditorAction fRemoveOccurrenceAnnotationsAction;
70
71         public BasicJavaEditorActionContributor() {
72                 super();
73
74                 ResourceBundle b = PHPEditorMessages.getResourceBundle();
75
76                 // fRetargetShowJavaDoc= new
77                 // RetargetAction(PHPdtActionConstants.SHOW_JAVA_DOC,
78                 // PHPEditorMessages.getString("ShowJavaDoc.label")); //$NON-NLS-1$
79                 // fRetargetShowJavaDoc.setActionDefinitionId(net.sourceforge.phpdt.ui.actions.PHPEditorActionDefinitionIds.SHOW_JAVADOC);
80                 // markAsPartListener(fRetargetShowJavaDoc);
81
82                 // actions that are "contributed" to editors, they are considered
83                 // belonging to the active editor
84                 fTogglePresentation = new TogglePresentationAction();
85
86                 fPreviousAnnotation = new GotoAnnotationAction(b,
87                                 "PreviousAnnotation.", null, false); //$NON-NLS-1$
88                 fNextAnnotation = new GotoAnnotationAction(b,
89                                 "NextAnnotation.", null, true); //$NON-NLS-1$
90
91                 fGotoMatchingBracket = new RetargetTextEditorAction(b,
92                                 "GotoMatchingBracket."); //$NON-NLS-1$
93                 fGotoMatchingBracket
94                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
95
96                 // fShowJavaDoc= new RetargetTextEditorAction(b, "ShowJavaDoc.");
97                 // //$NON-NLS-1$
98                 // fShowJavaDoc.setActionDefinitionId(net.sourceforge.phpdt.ui.actions.PHPEditorActionDefinitionIds.SHOW_JAVADOC);
99
100                 // fShowOutline= new
101                 // RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(),
102                 // "ShowOutline."); //$NON-NLS-1$
103                 // fShowOutline.setActionDefinitionId(PHPEditorActionDefinitionIds.SHOW_OUTLINE);
104                 //
105                 // fOpenHierarchy= new
106                 // RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(),
107                 // "OpenHierarchy."); //$NON-NLS-1$
108                 // fOpenHierarchy.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_HIERARCHY);
109                 //      
110                 // fOpenStructure= new
111                 // RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(),
112                 // "OpenStructure."); //$NON-NLS-1$
113                 // fOpenStructure.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_STRUCTURE);
114
115                 // fStructureSelectEnclosingAction= new RetargetTextEditorAction(b,
116                 // "StructureSelectEnclosing."); //$NON-NLS-1$
117                 // fStructureSelectEnclosingAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_ENCLOSING);
118                 // fStructureSelectNextAction= new RetargetTextEditorAction(b,
119                 // "StructureSelectNext."); //$NON-NLS-1$
120                 // fStructureSelectNextAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_NEXT);
121                 // fStructureSelectPreviousAction= new RetargetTextEditorAction(b,
122                 // "StructureSelectPrevious."); //$NON-NLS-1$
123                 // fStructureSelectPreviousAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_PREVIOUS);
124                 // fStructureSelectHistoryAction= new RetargetTextEditorAction(b,
125                 // "StructureSelectHistory."); //$NON-NLS-1$
126                 // fStructureSelectHistoryAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_LAST);
127                 //              
128                 fGotoNextMemberAction = new RetargetTextEditorAction(b,
129                                 "GotoNextMember."); //$NON-NLS-1$
130                 fGotoNextMemberAction
131                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_NEXT_MEMBER);
132                 fGotoPreviousMemberAction = new RetargetTextEditorAction(b,
133                                 "GotoPreviousMember."); //$NON-NLS-1$
134                 fGotoPreviousMemberAction
135                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_PREVIOUS_MEMBER);
136                 //
137                 // fRemoveOccurrenceAnnotationsAction= new RetargetTextEditorAction(b,
138                 // "RemoveOccurrenceAnnotations."); //$NON-NLS-1$
139                 // fRemoveOccurrenceAnnotationsAction.setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_OCCURRENCE_ANNOTATIONS);
140         }
141
142         protected final void markAsPartListener(RetargetAction action) {
143                 fPartListeners.add(action);
144         }
145
146         /*
147          * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
148          */
149         public void init(IActionBars bars, IWorkbenchPage page) {
150                 Iterator e = fPartListeners.iterator();
151                 while (e.hasNext())
152                         page.addPartListener((RetargetAction) e.next());
153
154                 super.init(bars, page);
155
156                 // register actions that have a dynamic editor.
157                 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, fNextAnnotation);
158                 bars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, fPreviousAnnotation);
159                 bars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, fNextAnnotation);
160                 bars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, fPreviousAnnotation);
161                 bars
162                                 .setGlobalActionHandler(
163                                                 ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY,
164                                                 fTogglePresentation);
165
166                 // bars.setGlobalActionHandler(PHPdtActionConstants.SHOW_JAVA_DOC,
167                 // fShowJavaDoc);
168         }
169
170         /*
171          * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
172          */
173         public void contributeToMenu(IMenuManager menu) {
174
175                 super.contributeToMenu(menu);
176
177                 IMenuManager editMenu = menu
178                                 .findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
179                 if (editMenu != null) {
180
181                         editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
182                         editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
183                         editMenu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
184
185                         // MenuManager structureSelection= new
186                         // MenuManager(PHPEditorMessages.getString("ExpandSelectionMenu.label"),
187                         // "expandSelection"); //$NON-NLS-1$ //$NON-NLS-2$
188                         // structureSelection.add(fStructureSelectEnclosingAction);
189                         // structureSelection.add(fStructureSelectNextAction);
190                         // structureSelection.add(fStructureSelectPreviousAction);
191                         // structureSelection.add(fStructureSelectHistoryAction);
192                         // editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
193                         // structureSelection);
194
195                         // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE,
196                         // fRetargetShowJavaDoc);
197                 }
198
199                 // IMenuManager navigateMenu=
200                 // menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
201                 // if (navigateMenu != null) {
202                 // navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT,
203                 // fShowOutline);
204                 // navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT,
205                 // fOpenHierarchy);
206                 // }
207
208                 IMenuManager gotoMenu = menu.findMenuUsingPath("navigate/goTo"); //$NON-NLS-1$
209                 if (gotoMenu != null) {
210                         gotoMenu.add(new Separator("additions2")); //$NON-NLS-1$
211                         gotoMenu.appendToGroup("additions2", fGotoPreviousMemberAction); //$NON-NLS-1$
212                         gotoMenu.appendToGroup("additions2", fGotoNextMemberAction); //$NON-NLS-1$
213                         gotoMenu.appendToGroup("additions2", fGotoMatchingBracket); //$NON-NLS-1$
214                 }
215         }
216
217         /*
218          * @see EditorActionBarContributor#setActiveEditor(IEditorPart)
219          */
220         public void setActiveEditor(IEditorPart part) {
221
222                 super.setActiveEditor(part);
223
224                 IActionBars actionBars = getActionBars();
225                 IStatusLineManager manager = actionBars.getStatusLineManager();
226                 manager.setMessage(null);
227                 manager.setErrorMessage(null);
228
229                 ITextEditor textEditor = null;
230                 if (part instanceof ITextEditor)
231                         textEditor = (ITextEditor) part;
232
233                 fTogglePresentation.setEditor(textEditor);
234                 fPreviousAnnotation.setEditor(textEditor);
235                 fNextAnnotation.setEditor(textEditor);
236
237                 fGotoMatchingBracket.setAction(getAction(textEditor,
238                                 GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
239                 // fShowJavaDoc.setAction(getAction(textEditor, "ShowJavaDoc"));
240                 // //$NON-NLS-1$
241                 // fShowOutline.setAction(getAction(textEditor,
242                 // IJavaEditorActionDefinitionIds.SHOW_OUTLINE));
243                 // fOpenHierarchy.setAction(getAction(textEditor,
244                 // IJavaEditorActionDefinitionIds.OPEN_HIERARCHY));
245                 // fOpenStructure.setAction(getAction(textEditor,
246                 // IJavaEditorActionDefinitionIds.OPEN_STRUCTURE));
247
248                 // fStructureSelectEnclosingAction.setAction(getAction(textEditor,
249                 // StructureSelectionAction.ENCLOSING));
250                 // fStructureSelectNextAction.setAction(getAction(textEditor,
251                 // StructureSelectionAction.NEXT));
252                 // fStructureSelectPreviousAction.setAction(getAction(textEditor,
253                 // StructureSelectionAction.PREVIOUS));
254                 // fStructureSelectHistoryAction.setAction(getAction(textEditor,
255                 // StructureSelectionAction.HISTORY));
256
257                 // fGotoNextMemberAction.setAction(getAction(textEditor,
258                 // GoToNextPreviousMemberAction.NEXT_MEMBER));
259                 // fGotoPreviousMemberAction.setAction(getAction(textEditor,
260                 // GoToNextPreviousMemberAction.PREVIOUS_MEMBER));
261
262                 // fRemoveOccurrenceAnnotationsAction.setAction(getAction(textEditor,
263                 // "RemoveOccurrenceAnnotations")); //$NON-NLS-1$
264                 if (part instanceof PHPEditor) {
265                         PHPEditor javaEditor = (PHPEditor) part;
266                         javaEditor.getActionGroup().fillActionBars(getActionBars());
267                         FoldingActionGroup foldingActions = javaEditor
268                                         .getFoldingActionGroup();
269                         if (foldingActions != null)
270                                 foldingActions.updateActionBars();
271                 }
272         }
273
274         /*
275          * @see IEditorActionBarContributor#dispose()
276          */
277         public void dispose() {
278
279                 Iterator e = fPartListeners.iterator();
280                 while (e.hasNext())
281                         getPage().removePartListener((RetargetAction) e.next());
282                 fPartListeners.clear();
283
284                 setActiveEditor(null);
285                 super.dispose();
286         }
287 }