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