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