1 package net.sourceforge.phpeclipse.phpeditor;
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
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.ResourceBundle;
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;
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;
46 * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
48 public class PHPActionContributor extends BasicTextEditorActionContributor {
50 protected RetargetTextEditorAction fContentAssistTip;
51 // protected TextEditorAction fTogglePresentation;
52 protected RetargetAction fRetargetContentAssist;
54 protected RetargetTextEditorAction fContentAssist;
55 private RetargetTextEditorAction fGotoMatchingBracket;
56 private List fRetargetToolbarActions = new ArrayList();
57 private List fPartListeners = new ArrayList();
59 protected PHPParserAction fParserAction;
60 protected ShowExternalPreviewAction fShowExternalPreviewAction;
63 * Default constructor.
65 public PHPActionContributor() {
68 ResourceBundle b = PHPEditorMessages.getResourceBundle();
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);
75 fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
76 fContentAssist.setActionDefinitionId(
77 PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
79 fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
80 fGotoMatchingBracket.setActionDefinitionId(
81 PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
83 // fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
84 // fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
86 // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
87 fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
88 // fTogglePresentation = new PresentationAction();
90 fParserAction = PHPParserAction.getInstance();
92 if (SWT.getPlatform().equals("win32")) {
93 // ExternalPreview only available as ActiveX on win32 (Eclipse2.1)
94 fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
98 protected final void markAsPartListener(RetargetAction action) {
99 fPartListeners.add(action);
103 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
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);
119 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
123 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
125 // public void contributeToMenu(IMenuManager menu) {
127 // super.contributeToMenu(menu);
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));
134 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
139 * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
141 public void contributeToMenu(IMenuManager menu) {
143 super.contributeToMenu(menu);
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));
152 // editMenu.appendToGroup(
153 // IContextMenuConstants.GROUP_GENERATE,
154 // fRetargetContentAssist);
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);
163 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetShowJavaDoc);
166 // IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
167 // if (navigateMenu != null) {
168 // navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fShowOutline);
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$
180 * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
182 public void contributeToToolBar(IToolBarManager tbm) {
183 tbm.add(new Separator());
184 Iterator e = fRetargetToolbarActions.iterator();
186 tbm.add((IAction) e.next());
190 * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
192 public void init(IActionBars bars, IWorkbenchPage page) {
193 Iterator e = fPartListeners.iterator();
195 page.addPartListener((RetargetAction) e.next());
197 super.init(bars, page);
200 * @see IEditorActionBarContributor#init(IActionBars)
202 public void init(IActionBars bars) {
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);
215 bars.setGlobalActionHandler(
216 PHPdtActionConstants.CONTENT_ASSIST,
218 // IToolBarManager toolBarManager = bars.getToolBarManager();
219 // if (toolBarManager != null) {
220 // toolBarManager.add(new Separator());
221 // toolBarManager.add(fTogglePresentation);
225 private void doSetActiveEditor(IEditorPart part) {
226 super.setActiveEditor(part);
228 IActionBars bars = getActionBars();
229 IStatusLineManager manager = bars.getStatusLineManager();
230 manager.setMessage(null);
231 manager.setErrorMessage(null);
233 ITextEditor textEditor = null;
234 if (part instanceof ITextEditor)
235 textEditor = (ITextEditor) part;
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));
242 bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); //$NON-NLS-1$
243 bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); //$NON-NLS-1$
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"));
255 if (part instanceof PHPEditor) {
256 PHPEditor phpEditor = (PHPEditor) part;
257 phpEditor.getActionGroup().fillActionBars(getActionBars());
259 // if (part instanceof PHPEditor) {
260 // PHPEditor cuEditor = (PHPEditor) part;
261 // ActionGroup group = cuEditor.getActionGroup();
262 // if (group != null)
263 // group.fillActionBars(bars);
265 // fTogglePresentation.setEditor(editor);
266 // fTogglePresentation.update();
268 if (textEditor != null) {
271 IEditorInput editorInput = textEditor.getEditorInput();
273 if (editorInput instanceof IFileEditorInput) {
274 file = ((IFileEditorInput) editorInput).getFile();
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();
297 * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
299 public void setActiveEditor(IEditorPart part) {
300 doSetActiveEditor(part);
304 * @see IEditorActionBarContributor#dispose()
306 public void dispose() {
307 Iterator e = fPartListeners.iterator();
309 getPage().removePartListener((RetargetAction) e.next());
310 fPartListeners.clear();
312 doSetActiveEditor(null);