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.IAction;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.text.source.ISourceViewer;
28 import org.eclipse.jface.util.IPropertyChangeListener;
29 import org.eclipse.jface.util.PropertyChangeEvent;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.editors.text.TextEditor;
33 import org.eclipse.ui.help.WorkbenchHelp;
34 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
35 import org.eclipse.ui.texteditor.TextOperationAction;
36 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
38 * Java specific text editor.
40 public class PHPEditor extends TextEditor {
42 /** The outline page */
43 private PHPContentOutlinePage fOutlinePage;
46 * Default constructor.
52 /** The <code>JavaEditor</code> implementation of this
53 * <code>AbstractTextEditor</code> method extend the
54 * actions to add those specific to the receiver
56 protected void createActions() {
57 super.createActions();
59 "ContentAssistProposal",
60 new TextOperationAction(
61 PHPEditorMessages.getResourceBundle(),
62 "ContentAssistProposal.",
64 ISourceViewer.CONTENTASSIST_PROPOSALS));
67 new TextOperationAction(
68 PHPEditorMessages.getResourceBundle(),
71 ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
74 /** The <code>JavaEditor</code> implementation of this
75 * <code>AbstractTextEditor</code> method performs any extra
76 * disposal actions required by the java editor.
78 public void dispose() {
79 PHPEditorEnvironment.disconnect(this);
80 if (fOutlinePage != null)
81 fOutlinePage.setInput(null);
85 /** The <code>PHPEditor</code> implementation of this
86 * <code>AbstractTextEditor</code> method performs any extra
87 * revert behavior required by the php editor.
89 public void doRevertToSaved() {
90 super.doRevertToSaved();
91 if (fOutlinePage != null)
92 fOutlinePage.update();
95 /** The <code>PHPEditor</code> implementation of this
96 * <code>AbstractTextEditor</code> method performs any extra
97 * save behavior required by the php editor.
99 public void doSave(IProgressMonitor monitor) {
100 super.doSave(monitor);
101 // compile or not, according to the user preferences
102 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
103 if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
104 IAction a = PHPParserAction.getInstance();
108 if (fOutlinePage != null)
109 fOutlinePage.update();
112 /** The <code>PHPEditor</code> implementation of this
113 * <code>AbstractTextEditor</code> method performs any extra
114 * save as behavior required by the php editor.
116 public void doSaveAs() {
118 if (fOutlinePage != null)
119 fOutlinePage.update();
122 /** The <code>PHPEditor</code> implementation of this
123 * <code>AbstractTextEditor</code> method performs sets the
124 * input of the outline page after AbstractTextEditor has set input.
126 public void doSetInput(IEditorInput input) throws CoreException {
127 super.doSetInput(input);
128 if (fOutlinePage != null)
129 fOutlinePage.setInput(input);
132 /** The <code>JavaEditor</code> implementation of this
133 * <code>AbstractTextEditor</code> method adds any
134 * JavaEditor specific entries.
136 public void editorContextMenuAboutToShow(MenuManager menu) {
137 super.editorContextMenuAboutToShow(menu);
138 addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
139 addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
142 /** The <code>JavaEditor</code> implementation of this
143 * <code>AbstractTextEditor</code> method performs gets
144 * the java content outline page if request is for a an
147 public Object getAdapter(Class required) {
148 if (IContentOutlinePage.class.equals(required)) {
149 if (fOutlinePage == null) {
150 fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
151 if (getEditorInput() != null)
152 fOutlinePage.setInput(getEditorInput());
156 return super.getAdapter(required);
159 public void openContextHelp() {
160 IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
161 ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
162 int pos = selection.getOffset();
163 String word = getFunctionName(doc, pos);
164 openContextHelp(word);
167 private void openContextHelp(String word) {
171 public static void open(String word) {
172 IHelp help = WorkbenchHelp.getHelpSupport();
174 IHelpResource helpResource = new PHPFunctionHelpResource(word);
175 WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
177 // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
181 private String getFunctionName(IDocument doc, int pos) {
182 Point word = PHPWordExtractor.findWord(doc, pos);
185 return doc.get(word.x, word.y).replace('_', '-');
186 } catch (BadLocationException e) {
193 * Method declared on AbstractTextEditor
195 protected void initializeEditor() {
197 PHPEditorEnvironment.connect(this);
199 setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
200 setRangeIndicator(new DefaultRangeIndicator());
201 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
202 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
203 // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
205 PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
206 public void propertyChange(PropertyChangeEvent event) {
207 PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
208 if (scanner != null) {
209 scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
211 if (getSourceViewer() != null) {
212 getSourceViewer().invalidateTextPresentation();