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.ITextEditorActionDefinitionIds;
45 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
47 * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
49 public class PHPActionContributor extends BasicTextEditorActionContributor {
51 protected RetargetTextEditorAction fContentAssistTip;
52 // protected TextEditorAction fTogglePresentation;
53 protected RetargetAction fRetargetContentAssist;
55 protected RetargetTextEditorAction fContentAssist;
56 private RetargetTextEditorAction fGotoMatchingBracket;
57 private List fRetargetToolbarActions = new ArrayList();
58 private List fPartListeners = new ArrayList();
60 protected PHPParserAction fParserAction;
61 protected ShowExternalPreviewAction fShowExternalPreviewAction;
63 private EncodingActionGroup fEncodingActionGroup;
65 * Default constructor.
67 public PHPActionContributor() {
70 ResourceBundle b = PHPEditorMessages.getResourceBundle();
72 fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
73 fRetargetContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
74 markAsPartListener(fRetargetContentAssist);
76 fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
77 fContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
79 fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
80 fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
82 // fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
83 // fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
85 // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
86 fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
87 // fTogglePresentation = new PresentationAction();
90 fEncodingActionGroup = new EncodingActionGroup();
92 fParserAction = PHPParserAction.getInstance();
94 if (SWT.getPlatform().equals("win32")) {
95 // ExternalPreview only available as ActiveX on win32 (Eclipse2.1)
96 fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
100 protected final void markAsPartListener(RetargetAction action) {
101 fPartListeners.add(action);
105 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
107 // public void contributeToMenu(IMenuManager menu) {
108 // super.contributeToMenu(menu);
109 // IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
110 // if (editMenu != null) {
111 // MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
112 // structureSelection.add(fStructureSelectEnclosingAction);
113 // structureSelection.add(fStructureSelectNextAction);
114 // structureSelection.add(fStructureSelectPreviousAction);
115 // structureSelection.add(fStructureSelectHistoryAction);
116 // editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
117 // editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
118 // editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
119 // editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
121 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
125 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
127 // public void contributeToMenu(IMenuManager menu) {
129 // super.contributeToMenu(menu);
131 // IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
132 // if (editMenu != null) {
133 // editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
134 // editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
136 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
141 * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
143 public void contributeToMenu(IMenuManager menu) {
145 super.contributeToMenu(menu);
147 IMenuManager editMenu = menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
148 if (editMenu != null) {
149 editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
150 editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
151 editMenu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
153 // editMenu.appendToGroup(
154 // IContextMenuConstants.GROUP_GENERATE,
155 // fRetargetContentAssist);
157 // MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label"), "expandSelection"); //$NON-NLS-1$ //$NON-NLS-2$
158 // structureSelection.add(fStructureSelectEnclosingAction);
159 // structureSelection.add(fStructureSelectNextAction);
160 // structureSelection.add(fStructureSelectPreviousAction);
161 // structureSelection.add(fStructureSelectHistoryAction);
162 // editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
164 // editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetShowJavaDoc);
167 // IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
168 // if (navigateMenu != null) {
169 // navigateMenu.appendToGroup(IWorkbenchActionConstants.SHOW_EXT, fShowOutline);
172 IMenuManager gotoMenu = menu.findMenuUsingPath("navigate/goTo"); //$NON-NLS-1$
173 if (gotoMenu != null) {
174 gotoMenu.add(new Separator("additions2")); //$NON-NLS-1$
175 // gotoMenu.appendToGroup("additions2", fGotoPreviousMemberAction); //$NON-NLS-1$
176 // gotoMenu.appendToGroup("additions2", fGotoNextMemberAction); //$NON-NLS-1$
177 gotoMenu.appendToGroup("additions2", fGotoMatchingBracket); //$NON-NLS-1$
181 * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
183 public void contributeToToolBar(IToolBarManager tbm) {
184 tbm.add(new Separator());
185 Iterator e = fRetargetToolbarActions.iterator();
187 tbm.add((IAction) e.next());
191 * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
193 public void init(IActionBars bars, IWorkbenchPage page) {
194 Iterator e = fPartListeners.iterator();
196 page.addPartListener((RetargetAction) e.next());
197 // character encoding
198 fEncodingActionGroup.fillActionBars(bars);
199 super.init(bars, page);
202 * @see IEditorActionBarContributor#init(IActionBars)
204 public void init(IActionBars bars) {
207 IMenuManager menuManager = bars.getMenuManager();
208 IMenuManager editMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
209 if (editMenu != null) {
210 editMenu.add(new Separator());
211 editMenu.add(fContentAssist);
212 // editMenu.add(fGotoMatchingBracket);
213 // editMenu.add(fContentAssistTip);
216 bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
217 // IToolBarManager toolBarManager = bars.getToolBarManager();
218 // if (toolBarManager != null) {
219 // toolBarManager.add(new Separator());
220 // toolBarManager.add(fTogglePresentation);
225 * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
227 public void setActiveEditor(IEditorPart part) {
228 super.setActiveEditor(part);
230 IActionBars bars = getActionBars();
231 IStatusLineManager manager = bars.getStatusLineManager();
232 manager.setMessage(null);
233 manager.setErrorMessage(null);
235 ITextEditor textEditor = null;
236 if (part instanceof ITextEditor)
237 textEditor = (ITextEditor) part;
239 fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
240 fContentAssistTip.setAction(getAction(textEditor, "ContentAssistTip")); //$NON-NLS-1$
241 fGotoMatchingBracket.setAction(getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
243 bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); //$NON-NLS-1$
244 bars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); //$NON-NLS-1$
245 // character encoding
246 fEncodingActionGroup.retarget(textEditor);
248 bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
249 bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
250 bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
252 if (part instanceof PHPEditor) {
253 PHPEditor phpEditor = (PHPEditor) part;
254 phpEditor.getActionGroup().fillActionBars(getActionBars());
256 // if (part instanceof PHPEditor) {
257 // PHPEditor cuEditor = (PHPEditor) part;
258 // ActionGroup group = cuEditor.getActionGroup();
259 // if (group != null)
260 // group.fillActionBars(bars);
262 // fTogglePresentation.setEditor(editor);
263 // fTogglePresentation.update();
265 if (textEditor != null) {
268 IEditorInput editorInput = textEditor.getEditorInput();
270 if (editorInput instanceof IFileEditorInput) {
271 file = ((IFileEditorInput) editorInput).getFile();
274 PHPeclipsePlugin.getDefault().setLastEditorFile(file);
275 fParserAction.setEditor(textEditor);
276 fParserAction.update();
277 if (SWT.getPlatform().equals("win32") && textEditor instanceof AbstractTextEditor) {
278 fShowExternalPreviewAction.setEditor(textEditor);
279 fShowExternalPreviewAction.update();
280 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
281 if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
282 IAction a = ShowExternalPreviewAction.getInstance();
291 * @see IEditorActionBarContributor#dispose()
293 public void dispose() {
294 Iterator e = fPartListeners.iterator();
296 getPage().removePartListener((RetargetAction) e.next());
297 fPartListeners.clear();
299 setActiveEditor(null);