Extended the builder with an index file generator for PHP class- and function-names.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPActionContributor.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
9
10 Contributors:
11     IBM Corporation - Initial implementation
12     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.ResourceBundle;
19
20 import net.sourceforge.phpdt.ui.IContextMenuConstants;
21 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
22 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.action.IMenuManager;
28 import org.eclipse.jface.action.IStatusLineManager;
29 import org.eclipse.jface.action.IToolBarManager;
30 import org.eclipse.jface.action.Separator;
31 import org.eclipse.jface.preference.IPreferenceStore;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.ui.IActionBars;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.IFileEditorInput;
37 import org.eclipse.ui.IWorkbenchActionConstants;
38 import org.eclipse.ui.IWorkbenchPage;
39 import org.eclipse.ui.actions.RetargetAction;
40 import org.eclipse.ui.texteditor.AbstractTextEditor;
41 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
42 import org.eclipse.ui.texteditor.ITextEditor;
43 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
44
45 /**
46  * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
47  */
48 public class PHPActionContributor extends BasicTextEditorActionContributor {
49
50   protected RetargetTextEditorAction fContentAssistTip;
51   // protected TextEditorAction fTogglePresentation;
52   protected RetargetAction fRetargetContentAssist;
53
54   protected RetargetTextEditorAction fContentAssist;
55   private RetargetTextEditorAction fGotoMatchingBracket;
56   private List fRetargetToolbarActions = new ArrayList();
57   private List fPartListeners = new ArrayList();
58
59   protected PHPParserAction fParserAction;
60   protected ShowExternalPreviewAction fShowExternalPreviewAction;
61
62   /**
63    * Default constructor.
64    */
65   public PHPActionContributor() {
66     super();
67
68     ResourceBundle b = PHPEditorMessages.getResourceBundle();
69
70     fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
71     fRetargetContentAssist.setActionDefinitionId(
72       PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
73     markAsPartListener(fRetargetContentAssist);
74
75     fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
76     fContentAssist.setActionDefinitionId(
77       PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
78
79     fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
80     fGotoMatchingBracket.setActionDefinitionId(
81       PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
82
83     //  fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
84     //  fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
85
86     // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
87     fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
88     //  fTogglePresentation = new PresentationAction();
89
90     fParserAction = PHPParserAction.getInstance();
91
92     if (SWT.getPlatform().equals("win32")) {
93       // ExternalPreview only available as ActiveX on win32 (Eclipse2.1)
94       fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
95     }
96   }
97
98   protected final void markAsPartListener(RetargetAction action) {
99     fPartListeners.add(action);
100   }
101
102   /*
103    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
104    */
105   //  public void contributeToMenu(IMenuManager menu) {         
106   //    super.contributeToMenu(menu);
107   //    IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
108   //    if (editMenu != null) {
109   //      MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
110   //      structureSelection.add(fStructureSelectEnclosingAction);
111   //      structureSelection.add(fStructureSelectNextAction);
112   //      structureSelection.add(fStructureSelectPreviousAction);
113   //      structureSelection.add(fStructureSelectHistoryAction);
114   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
115   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
116   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
117   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
118
119   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
120   //    }
121   //  }
122   /*
123    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
124    */
125   //  public void contributeToMenu(IMenuManager menu) {
126   //            
127   //      super.contributeToMenu(menu);
128   //            
129   //      IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
130   //      if (editMenu != null) {
131   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
132   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
133   //                    
134   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
135   //      }             
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 =
146       menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
147     if (editMenu != null) {
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 //      editMenu.appendToGroup(
153 //        IContextMenuConstants.GROUP_GENERATE,
154 //        fRetargetContentAssist);
155
156       //                                MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label"), "expandSelection"); //$NON-NLS-1$ //$NON-NLS-2$ 
157       //                                structureSelection.add(fStructureSelectEnclosingAction);
158       //                                structureSelection.add(fStructureSelectNextAction);
159       //                                structureSelection.add(fStructureSelectPreviousAction);
160       //                                structureSelection.add(fStructureSelectHistoryAction);
161       //                                editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
162
163       //                                editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetShowJavaDoc);
164     }
165
166     //                  IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
167     //                  if (navigateMenu != null) {
168     //                          navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fShowOutline);
169     //                  }
170
171     IMenuManager gotoMenu = menu.findMenuUsingPath("navigate/goTo"); //$NON-NLS-1$
172     if (gotoMenu != null) {
173       gotoMenu.add(new Separator("additions2")); //$NON-NLS-1$
174       //                                gotoMenu.appendToGroup("additions2", fGotoPreviousMemberAction); //$NON-NLS-1$
175       //                                gotoMenu.appendToGroup("additions2", fGotoNextMemberAction); //$NON-NLS-1$
176       gotoMenu.appendToGroup("additions2", fGotoMatchingBracket); //$NON-NLS-1$
177     }
178   }
179   /*
180    * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
181    */
182   public void contributeToToolBar(IToolBarManager tbm) {
183     tbm.add(new Separator());
184     Iterator e = fRetargetToolbarActions.iterator();
185     while (e.hasNext())
186       tbm.add((IAction) e.next());
187   }
188
189   /*
190    * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
191    */
192   public void init(IActionBars bars, IWorkbenchPage page) {
193     Iterator e = fPartListeners.iterator();
194     while (e.hasNext())
195       page.addPartListener((RetargetAction) e.next());
196
197     super.init(bars, page);
198   }
199   /*
200    * @see IEditorActionBarContributor#init(IActionBars)
201    */
202   public void init(IActionBars bars) {
203     super.init(bars);
204
205     IMenuManager menuManager = bars.getMenuManager();
206     IMenuManager editMenu =
207       menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
208     if (editMenu != null) {
209       editMenu.add(new Separator());
210       editMenu.add(fContentAssist);
211       // editMenu.add(fGotoMatchingBracket);
212       //   editMenu.add(fContentAssistTip);
213     }
214
215     bars.setGlobalActionHandler(
216       PHPdtActionConstants.CONTENT_ASSIST,
217       fContentAssist);
218     //    IToolBarManager toolBarManager = bars.getToolBarManager();
219     //    if (toolBarManager != null) {
220     //      toolBarManager.add(new Separator());
221     //      toolBarManager.add(fTogglePresentation);
222     //    }
223   }
224
225   private void doSetActiveEditor(IEditorPart part) {
226     super.setActiveEditor(part);
227
228     IActionBars bars = getActionBars();
229     IStatusLineManager manager = bars.getStatusLineManager();
230     manager.setMessage(null);
231     manager.setErrorMessage(null);
232
233     ITextEditor textEditor = null;
234     if (part instanceof ITextEditor)
235       textEditor = (ITextEditor) part;
236
237     fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
238     fContentAssistTip.setAction(getAction(textEditor, "ContentAssistTip")); //$NON-NLS-1$
239     fGotoMatchingBracket.setAction(
240       getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
241
242     bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); //$NON-NLS-1$
243     bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); //$NON-NLS-1$
244
245     bars.setGlobalActionHandler(
246       PHPdtActionConstants.COMMENT,
247       getAction(textEditor, "Comment"));
248     bars.setGlobalActionHandler(
249       PHPdtActionConstants.UNCOMMENT,
250       getAction(textEditor, "Uncomment"));
251     bars.setGlobalActionHandler(
252       PHPdtActionConstants.FORMAT,
253       getAction(textEditor, "Format"));
254
255     if (part instanceof PHPEditor) {
256       PHPEditor phpEditor = (PHPEditor) part;
257       phpEditor.getActionGroup().fillActionBars(getActionBars());
258     }
259     //    if (part instanceof PHPEditor) {
260     //      PHPEditor cuEditor = (PHPEditor) part;
261     //      ActionGroup group = cuEditor.getActionGroup();
262     //      if (group != null)
263     //        group.fillActionBars(bars);
264     //    }
265     //    fTogglePresentation.setEditor(editor);
266     //    fTogglePresentation.update();
267
268     if (textEditor != null) {
269
270       IFile file = null;
271       IEditorInput editorInput = textEditor.getEditorInput();
272
273       if (editorInput instanceof IFileEditorInput) {
274         file = ((IFileEditorInput) editorInput).getFile();
275       }
276
277       PHPeclipsePlugin.getDefault().setLastEditorFile(file);
278       fParserAction.setEditor(textEditor);
279       fParserAction.update();
280       if (SWT.getPlatform().equals("win32")
281         && textEditor instanceof AbstractTextEditor) {
282         fShowExternalPreviewAction.setEditor(textEditor);
283         fShowExternalPreviewAction.update();
284         IPreferenceStore store =
285           PHPeclipsePlugin.getDefault().getPreferenceStore();
286         if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
287           IAction a = ShowExternalPreviewAction.getInstance();
288           if (a != null)
289             a.run();
290         }
291       }
292     }
293
294   }
295
296   /*
297    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
298    */
299   public void setActiveEditor(IEditorPart part) {
300     doSetActiveEditor(part);
301   }
302
303   /*
304    * @see IEditorActionBarContributor#dispose()
305    */
306   public void dispose() {
307     Iterator e = fPartListeners.iterator();
308     while (e.hasNext())
309       getPage().removePartListener((RetargetAction) e.next());
310     fPartListeners.clear();
311
312     doSetActiveEditor(null);
313     super.dispose();
314   }
315 }