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.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;
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;
62 private EncodingActionGroup fEncodingActionGroup;
64 * Default constructor.
66 public PHPActionContributor() {
69 ResourceBundle b = PHPEditorMessages.getResourceBundle();
71 fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
72 fRetargetContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
73 markAsPartListener(fRetargetContentAssist);
75 fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
76 fContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
78 fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
79 fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
81 // fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
82 // fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
84 // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
85 fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
86 // fTogglePresentation = new PresentationAction();
89 fEncodingActionGroup = new EncodingActionGroup();
91 fParserAction = PHPParserAction.getInstance();
93 if (SWT.getPlatform().equals("win32")) {
94 // ExternalPreview only available as ActiveX on win32 (Eclipse2.1)
95 fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
99 protected final void markAsPartListener(RetargetAction action) {
100 fPartListeners.add(action);
104 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
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);
120 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
124 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
126 // public void contributeToMenu(IMenuManager menu) {
128 // super.contributeToMenu(menu);
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));
135 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
140 * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
142 public void contributeToMenu(IMenuManager menu) {
144 super.contributeToMenu(menu);
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));
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());
196 // character encoding
197 fEncodingActionGroup.fillActionBars(bars);
198 super.init(bars, page);
201 * @see IEditorActionBarContributor#init(IActionBars)
203 public void init(IActionBars bars) {
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);
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);
224 * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
226 public void setActiveEditor(IEditorPart part) {
227 super.setActiveEditor(part);
229 IActionBars bars = getActionBars();
230 IStatusLineManager manager = bars.getStatusLineManager();
231 manager.setMessage(null);
232 manager.setErrorMessage(null);
234 ITextEditor textEditor = null;
235 if (part instanceof ITextEditor)
236 textEditor = (ITextEditor) part;
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));
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);
247 bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
248 bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
249 bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
251 if (part instanceof PHPEditor) {
252 PHPEditor phpEditor = (PHPEditor) part;
253 phpEditor.getActionGroup().fillActionBars(getActionBars());
255 // if (part instanceof PHPEditor) {
256 // PHPEditor cuEditor = (PHPEditor) part;
257 // ActionGroup group = cuEditor.getActionGroup();
258 // if (group != null)
259 // group.fillActionBars(bars);
261 // fTogglePresentation.setEditor(editor);
262 // fTogglePresentation.update();
264 if (textEditor != null) {
267 IEditorInput editorInput = textEditor.getEditorInput();
269 if (editorInput instanceof IFileEditorInput) {
270 file = ((IFileEditorInput) editorInput).getFile();
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();
290 * @see IEditorActionBarContributor#dispose()
292 public void dispose() {
293 Iterator e = fPartListeners.iterator();
295 getPage().removePartListener((RetargetAction) e.next());
296 fPartListeners.clear();
298 setActiveEditor(null);