misc parser changes
[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.editors.text.EncodingActionGroup;
41 import org.eclipse.ui.texteditor.AbstractTextEditor;
42 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
43 import org.eclipse.ui.texteditor.ITextEditor;
44 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
45 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
46 /**
47  * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
48  */
49 public class PHPActionContributor extends BasicTextEditorActionContributor {
50
51   protected RetargetTextEditorAction fContentAssistTip;
52   // protected TextEditorAction fTogglePresentation;
53   protected RetargetAction fRetargetContentAssist;
54
55   protected RetargetTextEditorAction fContentAssist;
56   private RetargetTextEditorAction fGotoMatchingBracket;
57   private List fRetargetToolbarActions = new ArrayList();
58   private List fPartListeners = new ArrayList();
59
60   protected PHPParserAction fParserAction;
61   protected ShowExternalPreviewAction fShowExternalPreviewAction;
62
63   private EncodingActionGroup fEncodingActionGroup;
64   /**
65    * Default constructor.
66    */
67   public PHPActionContributor() {
68     super();
69
70     ResourceBundle b = PHPEditorMessages.getResourceBundle();
71
72     fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
73     fRetargetContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
74     markAsPartListener(fRetargetContentAssist);
75
76     fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
77     fContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
78
79     fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
80     fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
81
82     //  fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
83     //  fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
84
85     // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
86     fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
87     //  fTogglePresentation = new PresentationAction();
88
89     //  character encoding
90     fEncodingActionGroup = new EncodingActionGroup();
91
92     fParserAction = PHPParserAction.getInstance();
93
94     if (SWT.getPlatform().equals("win32")) {
95       // ExternalPreview only available as ActiveX on win32 (Eclipse2.1)
96       fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
97     }
98   }
99
100   protected final void markAsPartListener(RetargetAction action) {
101     fPartListeners.add(action);
102   }
103
104   /*
105    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
106    */
107   //  public void contributeToMenu(IMenuManager menu) {         
108   //    super.contributeToMenu(menu);
109   //    IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
110   //    if (editMenu != null) {
111   //      MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
112   //      structureSelection.add(fStructureSelectEnclosingAction);
113   //      structureSelection.add(fStructureSelectNextAction);
114   //      structureSelection.add(fStructureSelectPreviousAction);
115   //      structureSelection.add(fStructureSelectHistoryAction);
116   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
117   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
118   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
119   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
120
121   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
122   //    }
123   //  }
124   /*
125    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
126    */
127   //  public void contributeToMenu(IMenuManager menu) {
128   //            
129   //      super.contributeToMenu(menu);
130   //            
131   //      IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
132   //      if (editMenu != null) {
133   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
134   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
135   //                    
136   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
137   //      }             
138   //  }
139
140   /*
141          * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
142          */
143   public void contributeToMenu(IMenuManager menu) {
144
145     super.contributeToMenu(menu);
146
147     IMenuManager editMenu = menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
148     if (editMenu != null) {
149       editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
150       editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
151       editMenu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
152
153       //      editMenu.appendToGroup(
154       //        IContextMenuConstants.GROUP_GENERATE,
155       //        fRetargetContentAssist);
156
157       //                                MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label"), "expandSelection"); //$NON-NLS-1$ //$NON-NLS-2$ 
158       //                                structureSelection.add(fStructureSelectEnclosingAction);
159       //                                structureSelection.add(fStructureSelectNextAction);
160       //                                structureSelection.add(fStructureSelectPreviousAction);
161       //                                structureSelection.add(fStructureSelectHistoryAction);
162       //                                editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
163
164       //                                editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetShowJavaDoc);
165     }
166
167     //                  IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
168     //                  if (navigateMenu != null) {
169     //                          navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fShowOutline);
170     //                  }
171
172     IMenuManager gotoMenu = menu.findMenuUsingPath("navigate/goTo"); //$NON-NLS-1$
173     if (gotoMenu != null) {
174       gotoMenu.add(new Separator("additions2")); //$NON-NLS-1$
175       //                                gotoMenu.appendToGroup("additions2", fGotoPreviousMemberAction); //$NON-NLS-1$
176       //                                gotoMenu.appendToGroup("additions2", fGotoNextMemberAction); //$NON-NLS-1$
177       gotoMenu.appendToGroup("additions2", fGotoMatchingBracket); //$NON-NLS-1$
178     }
179   }
180   /*
181    * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
182    */
183   public void contributeToToolBar(IToolBarManager tbm) {
184     tbm.add(new Separator());
185     Iterator e = fRetargetToolbarActions.iterator();
186     while (e.hasNext())
187       tbm.add((IAction) e.next());
188   }
189
190   /*
191    * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
192    */
193   public void init(IActionBars bars, IWorkbenchPage page) {
194     Iterator e = fPartListeners.iterator();
195     while (e.hasNext())
196       page.addPartListener((RetargetAction) e.next());
197     // character encoding
198     fEncodingActionGroup.fillActionBars(bars);
199     super.init(bars, page);
200   }
201   /*
202    * @see IEditorActionBarContributor#init(IActionBars)
203    */
204   public void init(IActionBars bars) {
205     super.init(bars);
206
207     IMenuManager menuManager = bars.getMenuManager();
208     IMenuManager editMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
209     if (editMenu != null) {
210       editMenu.add(new Separator());
211       editMenu.add(fContentAssist);
212       // editMenu.add(fGotoMatchingBracket);
213       //   editMenu.add(fContentAssistTip);
214     }
215
216     bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
217     //    IToolBarManager toolBarManager = bars.getToolBarManager();
218     //    if (toolBarManager != null) {
219     //      toolBarManager.add(new Separator());
220     //      toolBarManager.add(fTogglePresentation);
221     //    }
222   }
223
224   /*
225    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
226    */
227   public void setActiveEditor(IEditorPart part) {
228     super.setActiveEditor(part);
229
230     IActionBars bars = getActionBars();
231     IStatusLineManager manager = bars.getStatusLineManager();
232     manager.setMessage(null);
233     manager.setErrorMessage(null);
234
235     ITextEditor textEditor = null;
236     if (part instanceof ITextEditor)
237       textEditor = (ITextEditor) part;
238
239     fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
240     fContentAssistTip.setAction(getAction(textEditor, "ContentAssistTip")); //$NON-NLS-1$
241     fGotoMatchingBracket.setAction(getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
242
243     bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); //$NON-NLS-1$
244     bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); //$NON-NLS-1$
245     // character encoding
246     fEncodingActionGroup.retarget(textEditor);
247
248     bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
249     bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
250     bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
251
252     if (part instanceof PHPEditor) {
253       PHPEditor phpEditor = (PHPEditor) part;
254       phpEditor.getActionGroup().fillActionBars(getActionBars());
255     }
256     //    if (part instanceof PHPEditor) {
257     //      PHPEditor cuEditor = (PHPEditor) part;
258     //      ActionGroup group = cuEditor.getActionGroup();
259     //      if (group != null)
260     //        group.fillActionBars(bars);
261     //    }
262     //    fTogglePresentation.setEditor(editor);
263     //    fTogglePresentation.update();
264
265     if (textEditor != null) {
266
267       IFile file = null;
268       IEditorInput editorInput = textEditor.getEditorInput();
269
270       if (editorInput instanceof IFileEditorInput) {
271         file = ((IFileEditorInput) editorInput).getFile();
272       }
273
274       PHPeclipsePlugin.getDefault().setLastEditorFile(file);
275       fParserAction.setEditor(textEditor);
276       fParserAction.update();
277       if (SWT.getPlatform().equals("win32") && textEditor instanceof AbstractTextEditor) {
278         fShowExternalPreviewAction.setEditor(textEditor);
279         fShowExternalPreviewAction.update();
280         IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
281         if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
282           IAction a = ShowExternalPreviewAction.getInstance();
283           if (a != null)
284             a.run();
285         }
286       }
287     }
288   }
289
290   /*
291    * @see IEditorActionBarContributor#dispose()
292    */
293   public void dispose() {
294     Iterator e = fPartListeners.iterator();
295     while (e.hasNext())
296       getPage().removePartListener((RetargetAction) e.next());
297     fPartListeners.clear();
298
299     setActiveEditor(null);
300     super.dispose();
301   }
302 }