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.RetargetTextEditorAction;
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   private EncodingActionGroup fEncodingActionGroup;
63   /**
64    * Default constructor.
65    */
66   public PHPActionContributor() {
67     super();
68
69     ResourceBundle b = PHPEditorMessages.getResourceBundle();
70
71     fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
72     fRetargetContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
73     markAsPartListener(fRetargetContentAssist);
74
75     fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
76     fContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
77
78     fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
79     fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
80
81     //  fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
82     //  fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
83
84     // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
85     fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
86     //  fTogglePresentation = new PresentationAction();
87
88     //  character encoding
89     fEncodingActionGroup = new EncodingActionGroup();
90
91     fParserAction = PHPParserAction.getInstance();
92
93     if (SWT.getPlatform().equals("win32")) {
94       // ExternalPreview only available as ActiveX on win32 (Eclipse2.1)
95       fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
96     }
97   }
98
99   protected final void markAsPartListener(RetargetAction action) {
100     fPartListeners.add(action);
101   }
102
103   /*
104    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
105    */
106   //  public void contributeToMenu(IMenuManager menu) {         
107   //    super.contributeToMenu(menu);
108   //    IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
109   //    if (editMenu != null) {
110   //      MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
111   //      structureSelection.add(fStructureSelectEnclosingAction);
112   //      structureSelection.add(fStructureSelectNextAction);
113   //      structureSelection.add(fStructureSelectPreviousAction);
114   //      structureSelection.add(fStructureSelectHistoryAction);
115   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
116   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
117   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
118   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
119
120   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
121   //    }
122   //  }
123   /*
124    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
125    */
126   //  public void contributeToMenu(IMenuManager menu) {
127   //            
128   //      super.contributeToMenu(menu);
129   //            
130   //      IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
131   //      if (editMenu != null) {
132   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
133   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
134   //                    
135   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
136   //      }             
137   //  }
138
139   /*
140          * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
141          */
142   public void contributeToMenu(IMenuManager menu) {
143
144     super.contributeToMenu(menu);
145
146     IMenuManager editMenu = 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     // character encoding
197     fEncodingActionGroup.fillActionBars(bars);
198     super.init(bars, page);
199   }
200   /*
201    * @see IEditorActionBarContributor#init(IActionBars)
202    */
203   public void init(IActionBars bars) {
204     super.init(bars);
205
206     IMenuManager menuManager = bars.getMenuManager();
207     IMenuManager editMenu = 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(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
216     //    IToolBarManager toolBarManager = bars.getToolBarManager();
217     //    if (toolBarManager != null) {
218     //      toolBarManager.add(new Separator());
219     //      toolBarManager.add(fTogglePresentation);
220     //    }
221   }
222
223   /*
224    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
225    */
226   public void setActiveEditor(IEditorPart part) {
227     super.setActiveEditor(part);
228
229     IActionBars bars = getActionBars();
230     IStatusLineManager manager = bars.getStatusLineManager();
231     manager.setMessage(null);
232     manager.setErrorMessage(null);
233
234     ITextEditor textEditor = null;
235     if (part instanceof ITextEditor)
236       textEditor = (ITextEditor) part;
237
238     fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
239     fContentAssistTip.setAction(getAction(textEditor, "ContentAssistTip")); //$NON-NLS-1$
240     fGotoMatchingBracket.setAction(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     // character encoding
245     fEncodingActionGroup.retarget(textEditor);
246
247     bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
248     bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
249     bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
250
251     if (part instanceof PHPEditor) {
252       PHPEditor phpEditor = (PHPEditor) part;
253       phpEditor.getActionGroup().fillActionBars(getActionBars());
254     }
255     //    if (part instanceof PHPEditor) {
256     //      PHPEditor cuEditor = (PHPEditor) part;
257     //      ActionGroup group = cuEditor.getActionGroup();
258     //      if (group != null)
259     //        group.fillActionBars(bars);
260     //    }
261     //    fTogglePresentation.setEditor(editor);
262     //    fTogglePresentation.update();
263
264     if (textEditor != null) {
265
266       IFile file = null;
267       IEditorInput editorInput = textEditor.getEditorInput();
268
269       if (editorInput instanceof IFileEditorInput) {
270         file = ((IFileEditorInput) editorInput).getFile();
271       }
272
273       PHPeclipsePlugin.getDefault().setLastEditorFile(file);
274       fParserAction.setEditor(textEditor);
275       fParserAction.update();
276       if (SWT.getPlatform().equals("win32") && textEditor instanceof AbstractTextEditor) {
277         fShowExternalPreviewAction.setEditor(textEditor);
278         fShowExternalPreviewAction.update();
279         IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
280         if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
281           IAction a = ShowExternalPreviewAction.getInstance();
282           if (a != null)
283             a.run();
284         }
285       }
286     }
287   }
288
289   /*
290    * @see IEditorActionBarContributor#dispose()
291    */
292   public void dispose() {
293     Iterator e = fPartListeners.iterator();
294     while (e.hasNext())
295       getPage().removePartListener((RetargetAction) e.next());
296     fPartListeners.clear();
297
298     setActiveEditor(null);
299     super.dispose();
300   }
301 }