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