first PHP parser version (doesn't work actually)
[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.phpeditor.php.PHPWordExtractor;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.help.IHelp;
18 import org.eclipse.help.IHelpResource;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.text.source.ISourceViewer;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.editors.text.TextEditor;
28 import org.eclipse.ui.help.WorkbenchHelp;
29 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
30 import org.eclipse.ui.texteditor.TextOperationAction;
31 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
32 import org.eclipse.jface.action.IAction;
33 /**
34  * Java specific text editor.
35  */
36 public class PHPEditor extends TextEditor {
37
38   /** The outline page */
39   private PHPContentOutlinePage fOutlinePage;
40
41   /**
42    * Default constructor.
43    */
44   public PHPEditor() {
45     super();
46   }
47
48   /** The <code>JavaEditor</code> implementation of this 
49    * <code>AbstractTextEditor</code> method extend the 
50    * actions to add those specific to the receiver
51    */
52   protected void createActions() {
53     super.createActions();
54     setAction(
55       "ContentAssistProposal",
56       new TextOperationAction(
57         PHPEditorMessages.getResourceBundle(),
58         "ContentAssistProposal.",
59         this,
60         ISourceViewer.CONTENTASSIST_PROPOSALS));
61     setAction(
62       "ContentAssistTip",
63       new TextOperationAction(
64         PHPEditorMessages.getResourceBundle(),
65         "ContentAssistTip.",
66         this,
67         ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
68   }
69
70   /** The <code>JavaEditor</code> implementation of this 
71    * <code>AbstractTextEditor</code> method performs any extra 
72    * disposal actions required by the java editor.
73    */
74   public void dispose() {
75     PHPEditorEnvironment.disconnect(this);
76     if (fOutlinePage != null)
77       fOutlinePage.setInput(null);
78     super.dispose();
79   }
80
81   /** The <code>JavaEditor</code> implementation of this 
82    * <code>AbstractTextEditor</code> method performs any extra 
83    * revert behavior required by the java editor.
84    */
85   public void doRevertToSaved() {
86     super.doRevertToSaved();
87     if (fOutlinePage != null)
88       fOutlinePage.update();
89   }
90
91   /** The <code>JavaEditor</code> implementation of this 
92    * <code>AbstractTextEditor</code> method performs any extra 
93    * save behavior required by the java editor.
94    */
95   public void doSave(IProgressMonitor monitor) {
96     super.doSave(monitor);
97     // compile or not, according to the user preferences
98     IAction a = PHPParserAction.getInstance();
99     if (a != null)
100       a.run();
101     if (fOutlinePage != null)
102       fOutlinePage.update();
103   }
104
105   /** The <code>JavaEditor</code> implementation of this 
106    * <code>AbstractTextEditor</code> method performs any extra 
107    * save as behavior required by the java editor.
108    */
109   public void doSaveAs() {
110     super.doSaveAs();
111     if (fOutlinePage != null)
112       fOutlinePage.update();
113   }
114
115   /** The <code>JavaEditor</code> implementation of this 
116    * <code>AbstractTextEditor</code> method performs sets the 
117    * input of the outline page after AbstractTextEditor has set input.
118    */
119   public void doSetInput(IEditorInput input) throws CoreException {
120     super.doSetInput(input);
121     if (fOutlinePage != null)
122       fOutlinePage.setInput(input);
123   }
124
125   /** The <code>JavaEditor</code> implementation of this 
126    * <code>AbstractTextEditor</code> method adds any 
127    * JavaEditor specific entries.
128    */
129   public void editorContextMenuAboutToShow(MenuManager menu) {
130     super.editorContextMenuAboutToShow(menu);
131     addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
132     addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
133   }
134
135   /** The <code>JavaEditor</code> implementation of this 
136    * <code>AbstractTextEditor</code> method performs gets
137    * the java content outline page if request is for a an 
138    * outline page.
139    */
140   public Object getAdapter(Class required) {
141     if (IContentOutlinePage.class.equals(required)) {
142       if (fOutlinePage == null) {
143         fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
144         if (getEditorInput() != null)
145           fOutlinePage.setInput(getEditorInput());
146       }
147       return fOutlinePage;
148     }
149     return super.getAdapter(required);
150   }
151
152   public void openContextHelp() {
153     IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
154     ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
155     int pos = selection.getOffset();
156     String word = getFunctionName(doc, pos);
157     openContextHelp(word);
158   }
159
160   private void openContextHelp(String word) {
161     open(word);
162   }
163
164   public static void open(String word) {
165     IHelp help = WorkbenchHelp.getHelpSupport();
166     if (help != null) {
167       IHelpResource helpResource = new PHPFunctionHelpResource(word);
168       WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
169     } else {
170       //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
171     }
172   }
173
174   private String getFunctionName(IDocument doc, int pos) {
175     Point word = PHPWordExtractor.findWord(doc, pos);
176     if (word != null) {
177       try {
178         return doc.get(word.x, word.y).replace('_', '-');
179       } catch (BadLocationException e) {
180       }
181     }
182     return "";
183   }
184
185   /* (non-Javadoc)
186    * Method declared on AbstractTextEditor
187    */
188   protected void initializeEditor() {
189
190     PHPEditorEnvironment.connect(this);
191
192     setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
193     setRangeIndicator(new DefaultRangeIndicator());
194     setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
195     setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
196   }
197 }