Added context help menu for php function names. The description of the fundtion opens...
[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.PHPWordDetector;
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
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     if (fOutlinePage != null)
98       fOutlinePage.update();
99   }
100
101   /** The <code>JavaEditor</code> implementation of this 
102    * <code>AbstractTextEditor</code> method performs any extra 
103    * save as behavior required by the java editor.
104    */
105   public void doSaveAs() {
106     super.doSaveAs();
107     if (fOutlinePage != null)
108       fOutlinePage.update();
109   }
110
111   /** The <code>JavaEditor</code> implementation of this 
112    * <code>AbstractTextEditor</code> method performs sets the 
113    * input of the outline page after AbstractTextEditor has set input.
114    */
115   public void doSetInput(IEditorInput input) throws CoreException {
116     super.doSetInput(input);
117     if (fOutlinePage != null)
118       fOutlinePage.setInput(input);
119   }
120
121   /** The <code>JavaEditor</code> implementation of this 
122    * <code>AbstractTextEditor</code> method adds any 
123    * JavaEditor specific entries.
124    */
125   public void editorContextMenuAboutToShow(MenuManager menu) {
126     super.editorContextMenuAboutToShow(menu);
127     addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
128     addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
129   }
130
131   /** The <code>JavaEditor</code> implementation of this 
132    * <code>AbstractTextEditor</code> method performs gets
133    * the java content outline page if request is for a an 
134    * outline page.
135    */
136   public Object getAdapter(Class required) {
137     if (IContentOutlinePage.class.equals(required)) {
138       if (fOutlinePage == null) {
139         fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
140         if (getEditorInput() != null)
141           fOutlinePage.setInput(getEditorInput());
142       }
143       return fOutlinePage;
144     }
145     return super.getAdapter(required);
146   }
147
148   public void openContextHelp() {
149     IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
150     ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
151     int pos = selection.getOffset();
152     String word = getFunctionName(doc, pos);
153     openContextHelp(word);
154   }
155
156   private void openContextHelp(String word) {
157     open(word);
158   }
159
160   public static void open(String word) {
161     IHelp help = WorkbenchHelp.getHelpSupport();
162     if (help != null) {
163       IHelpResource helpResource = new PHPFunctionHelpResource(word);
164       WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
165     } else {
166       //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
167     }
168   }
169   
170   private String getFunctionName(IDocument doc, int pos) {
171     Point word = PHPWordDetector.findWord(doc, pos);
172     if (word != null) {
173       try {
174         return doc.get(word.x, word.y).replace('_', '-');
175       } catch (BadLocationException e) {
176       }
177     }
178     return "";
179   }
180
181   /* (non-Javadoc)
182    * Method declared on AbstractTextEditor
183    */
184   protected void initializeEditor() {
185
186     PHPEditorEnvironment.connect(this);
187
188     setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
189     setRangeIndicator(new DefaultRangeIndicator());
190     setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
191     setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
192   }
193 }