added a first php $f command to the PHP console
[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.actions.GotoMatchingBracketAction;
21 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.ui.IActionBars;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IWorkbenchActionConstants;
33 import org.eclipse.ui.actions.ActionGroup;
34 import org.eclipse.ui.actions.RetargetAction;
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.RetargetTextEditorAction;
39
40 /**
41  * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
42  */
43 public class PHPActionContributor extends BasicTextEditorActionContributor {
44
45   // protected RetargetTextEditorAction fContentAssistTip;
46   // protected TextEditorAction fTogglePresentation;
47   protected RetargetAction fRetargetContentAssist;
48
49   protected RetargetTextEditorAction fContentAssist;
50   private RetargetTextEditorAction fGotoMatchingBracket;
51   private List fRetargetToolbarActions = new ArrayList();
52
53   protected PHPParserAction fParserAction;
54   protected ShowExternalPreviewAction fShowExternalPreviewAction;
55
56   /**
57    * Default constructor.
58    */
59   public PHPActionContributor() {
60     super();
61
62     ResourceBundle b = PHPEditorMessages.getResourceBundle();
63
64     fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
65     fRetargetContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
66
67     fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
68     fContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
69
70     fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
71     fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
72
73     //  fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
74     //  fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
75
76     // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
77     //  fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
78     //  fTogglePresentation = new PresentationAction();
79
80     fParserAction = PHPParserAction.getInstance();
81    
82     if (SWT.getPlatform().equals("win32")) {
83       fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
84     }
85   }
86
87   /*
88    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
89    */
90   //  public void contributeToMenu(IMenuManager menu) {         
91   //    super.contributeToMenu(menu);
92   //    IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
93   //    if (editMenu != null) {
94   //      MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
95   //      structureSelection.add(fStructureSelectEnclosingAction);
96   //      structureSelection.add(fStructureSelectNextAction);
97   //      structureSelection.add(fStructureSelectPreviousAction);
98   //      structureSelection.add(fStructureSelectHistoryAction);
99   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
100   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
101   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
102   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
103
104   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
105   //    }
106   //  }
107   /*
108    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
109    */
110   //  public void contributeToMenu(IMenuManager menu) {
111   //            
112   //      super.contributeToMenu(menu);
113   //            
114   //      IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
115   //      if (editMenu != null) {
116   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
117   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
118   //                    
119   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
120   //      }             
121   //  }
122
123   /*
124    * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
125    */
126   public void contributeToToolBar(IToolBarManager tbm) {
127     tbm.add(new Separator());
128     Iterator e = fRetargetToolbarActions.iterator();
129     while (e.hasNext())
130       tbm.add((IAction) e.next());
131   }
132
133   /*
134    * @see IEditorActionBarContributor#init(IActionBars)
135    */
136   public void init(IActionBars bars) {
137     super.init(bars);
138
139     IMenuManager menuManager = bars.getMenuManager();
140     IMenuManager editMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
141     if (editMenu != null) {
142       editMenu.add(new Separator());
143       editMenu.add(fContentAssist);
144       editMenu.add(fGotoMatchingBracket);
145
146       //   editMenu.add(fContentAssistTip);
147     }
148
149     bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
150     //    IToolBarManager toolBarManager = bars.getToolBarManager();
151     //    if (toolBarManager != null) {
152     //      toolBarManager.add(new Separator());
153     //      toolBarManager.add(fTogglePresentation);
154     //    }
155   }
156
157   private void doSetActiveEditor(IEditorPart part) {
158     super.setActiveEditor(part);
159
160     ITextEditor textEditor = null;
161     if (part instanceof ITextEditor)
162       textEditor = (ITextEditor) part;
163
164     fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
165     //  fContentAssistTip.setAction(getAction(editor, "ContentAssistTip")); //$NON-NLS-1$
166     fGotoMatchingBracket.setAction(getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
167
168     IActionBars bars = getActionBars();
169     bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
170     bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
171     bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
172
173     if (part instanceof PHPEditor) {
174       PHPEditor cuEditor = (PHPEditor) part;
175       ActionGroup group = cuEditor.getActionGroup();
176       if (group != null)
177         group.fillActionBars(bars);
178     }
179     //    fTogglePresentation.setEditor(editor);
180     //    fTogglePresentation.update();
181
182     PHPeclipsePlugin.getDefault().setTextEditor(textEditor);
183     fParserAction.setEditor(textEditor);
184     fParserAction.update();
185     if (SWT.getPlatform().equals("win32") &&
186         textEditor instanceof AbstractTextEditor) {
187       fShowExternalPreviewAction.setEditor(textEditor);
188       fShowExternalPreviewAction.update();
189       IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
190       if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
191         IAction a = ShowExternalPreviewAction.getInstance();
192         if (a != null)
193           a.run();
194       }
195     }
196   }
197
198   /*
199    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
200    */
201   public void setActiveEditor(IEditorPart part) {
202     doSetActiveEditor(part);
203   }
204
205   /*
206    * @see IEditorActionBarContributor#dispose()
207    */
208   public void dispose() {
209     doSetActiveEditor(null);
210     super.dispose();
211   }
212 }