Syntax Highlighting Prefs work now / Automatic parse on save option available
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPEditor.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
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
9
10 Contributors:
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;
37 /**
38  * Java specific text editor.
39  */
40 public class PHPEditor extends TextEditor {
41
42   /** The outline page */
43   private PHPContentOutlinePage fOutlinePage;
44
45   /**
46    * Default constructor.
47    */
48   public PHPEditor() {
49     super();
50   }
51
52   /** The <code>JavaEditor</code> implementation of this 
53    * <code>AbstractTextEditor</code> method extend the 
54    * actions to add those specific to the receiver
55    */
56   protected void createActions() {
57     super.createActions();
58     setAction(
59       "ContentAssistProposal",
60       new TextOperationAction(
61         PHPEditorMessages.getResourceBundle(),
62         "ContentAssistProposal.",
63         this,
64         ISourceViewer.CONTENTASSIST_PROPOSALS));
65     setAction(
66       "ContentAssistTip",
67       new TextOperationAction(
68         PHPEditorMessages.getResourceBundle(),
69         "ContentAssistTip.",
70         this,
71         ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
72   }
73
74   /** The <code>JavaEditor</code> implementation of this 
75    * <code>AbstractTextEditor</code> method performs any extra 
76    * disposal actions required by the java editor.
77    */
78   public void dispose() {
79     PHPEditorEnvironment.disconnect(this);
80     if (fOutlinePage != null)
81       fOutlinePage.setInput(null);
82     super.dispose();
83   }
84
85   /** The <code>PHPEditor</code> implementation of this 
86    * <code>AbstractTextEditor</code> method performs any extra 
87    * revert behavior required by the php editor.
88    */
89   public void doRevertToSaved() {
90     super.doRevertToSaved();
91     if (fOutlinePage != null)
92       fOutlinePage.update();
93   }
94
95   /** The <code>PHPEditor</code> implementation of this 
96    * <code>AbstractTextEditor</code> method performs any extra 
97    * save behavior required by the php editor.
98    */
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();
105       if (a != null)
106         a.run();
107     }
108     if (fOutlinePage != null)
109       fOutlinePage.update();
110   }
111
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.
115    */
116   public void doSaveAs() {
117     super.doSaveAs();
118     if (fOutlinePage != null)
119       fOutlinePage.update();
120   }
121
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.
125    */
126   public void doSetInput(IEditorInput input) throws CoreException {
127     super.doSetInput(input);
128     if (fOutlinePage != null)
129       fOutlinePage.setInput(input);
130   }
131
132   /** The <code>JavaEditor</code> implementation of this 
133    * <code>AbstractTextEditor</code> method adds any 
134    * JavaEditor specific entries.
135    */
136   public void editorContextMenuAboutToShow(MenuManager menu) {
137     super.editorContextMenuAboutToShow(menu);
138     addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
139     addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
140   }
141
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 
145    * outline page.
146    */
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());
153       }
154       return fOutlinePage;
155     }
156     return super.getAdapter(required);
157   }
158
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);
165   }
166
167   private void openContextHelp(String word) {
168     open(word);
169   }
170
171   public static void open(String word) {
172     IHelp help = WorkbenchHelp.getHelpSupport();
173     if (help != null) {
174       IHelpResource helpResource = new PHPFunctionHelpResource(word);
175       WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
176     } else {
177       //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
178     }
179   }
180
181   private String getFunctionName(IDocument doc, int pos) {
182     Point word = PHPWordExtractor.findWord(doc, pos);
183     if (word != null) {
184       try {
185         return doc.get(word.x, word.y).replace('_', '-');
186       } catch (BadLocationException e) {
187       }
188     }
189     return "";
190   }
191
192   /* (non-Javadoc)
193    * Method declared on AbstractTextEditor
194    */
195   protected void initializeEditor() {
196
197     PHPEditorEnvironment.connect(this);
198
199     setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
200     setRangeIndicator(new DefaultRangeIndicator());
201     setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
202     setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
203     // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
204
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());
210         }
211         if (getSourceViewer() != null) {
212           getSourceViewer().invalidateTextPresentation();
213         }
214       }
215     });
216   }
217 }