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 **********************************************************************/
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.help.IHelp;
20 import org.eclipse.help.IHelpResource;
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextOperationTarget;
28 import org.eclipse.jface.text.ITextSelection;
29 import org.eclipse.jface.text.source.ISourceViewer;
30 import org.eclipse.jface.util.IPropertyChangeListener;
31 import org.eclipse.jface.util.PropertyChangeEvent;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.editors.text.TextEditor;
35 import org.eclipse.ui.help.WorkbenchHelp;
36 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
37 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
38 import org.eclipse.ui.texteditor.TextOperationAction;
39 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
41 * Java specific text editor.
43 public class PHPEditor extends TextEditor {
45 protected PHPActionGroup actionGroup;
46 /** The outline page */
47 private PHPContentOutlinePage fOutlinePage;
50 * Default constructor.
56 /** The <code>JavaEditor</code> implementation of this
57 * <code>AbstractTextEditor</code> method extend the
58 * actions to add those specific to the receiver
60 protected void createActions() {
61 super.createActions();
63 "ContentAssistProposal",
64 new TextOperationAction(
65 PHPEditorMessages.getResourceBundle(),
66 "ContentAssistProposal.",
68 ISourceViewer.CONTENTASSIST_PROPOSALS));
71 new TextOperationAction(
72 PHPEditorMessages.getResourceBundle(),
75 ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
77 Action action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX);
78 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
79 setAction("Comment", action);
81 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX);
82 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
83 setAction("Uncomment", action);
85 actionGroup = new PHPActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
89 /** The <code>JavaEditor</code> implementation of this
90 * <code>AbstractTextEditor</code> method performs any extra
91 * disposal actions required by the java editor.
93 public void dispose() {
94 PHPEditorEnvironment.disconnect(this);
95 if (fOutlinePage != null)
96 fOutlinePage.setInput(null);
100 /** The <code>PHPEditor</code> implementation of this
101 * <code>AbstractTextEditor</code> method performs any extra
102 * revert behavior required by the php editor.
104 public void doRevertToSaved() {
105 super.doRevertToSaved();
106 if (fOutlinePage != null)
107 fOutlinePage.update();
110 /** The <code>PHPEditor</code> implementation of this
111 * <code>AbstractTextEditor</code> method performs any extra
112 * save behavior required by the php editor.
114 public void doSave(IProgressMonitor monitor) {
115 super.doSave(monitor);
116 // compile or not, according to the user preferences
117 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
118 if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
119 IAction a = PHPParserAction.getInstance();
123 if (fOutlinePage != null)
124 fOutlinePage.update();
127 /** The <code>PHPEditor</code> implementation of this
128 * <code>AbstractTextEditor</code> method performs any extra
129 * save as behavior required by the php editor.
131 public void doSaveAs() {
133 if (fOutlinePage != null)
134 fOutlinePage.update();
137 /** The <code>PHPEditor</code> implementation of this
138 * <code>AbstractTextEditor</code> method performs sets the
139 * input of the outline page after AbstractTextEditor has set input.
141 public void doSetInput(IEditorInput input) throws CoreException {
142 super.doSetInput(input);
143 if (fOutlinePage != null)
144 fOutlinePage.setInput(input);
147 /** The <code>JavaEditor</code> implementation of this
148 * <code>AbstractTextEditor</code> method adds any
149 * JavaEditor specific entries.
151 public void editorContextMenuAboutToShow(MenuManager menu) {
152 super.editorContextMenuAboutToShow(menu);
153 // addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
154 // addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
155 actionGroup.fillContextMenu(menu);
158 /** The <code>JavaEditor</code> implementation of this
159 * <code>AbstractTextEditor</code> method performs gets
160 * the java content outline page if request is for a an
163 public Object getAdapter(Class required) {
164 if (IContentOutlinePage.class.equals(required)) {
165 if (fOutlinePage == null) {
166 fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
167 if (getEditorInput() != null)
168 fOutlinePage.setInput(getEditorInput());
172 return super.getAdapter(required);
175 public void openContextHelp() {
176 IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
177 ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
178 int pos = selection.getOffset();
179 String word = getFunctionName(doc, pos);
180 openContextHelp(word);
183 private void openContextHelp(String word) {
187 public static void open(String word) {
188 IHelp help = WorkbenchHelp.getHelpSupport();
190 IHelpResource helpResource = new PHPFunctionHelpResource(word);
191 WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
193 // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
197 private String getFunctionName(IDocument doc, int pos) {
198 Point word = PHPWordExtractor.findWord(doc, pos);
201 return doc.get(word.x, word.y).replace('_', '-');
202 } catch (BadLocationException e) {
209 * Method declared on AbstractTextEditor
211 protected void initializeEditor() {
213 PHPEditorEnvironment.connect(this);
215 setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
216 setRangeIndicator(new DefaultRangeIndicator());
217 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
218 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
219 // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
221 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
222 public void propertyChange(PropertyChangeEvent event) {
223 PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
224 if (scanner != null) {
225 scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
227 if (getSourceViewer() != null) {
228 getSourceViewer().invalidateTextPresentation();