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