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
index 470aa0f..df0c4cc 100644 (file)
@@ -11,12 +11,21 @@ Contributors:
     IBM Corporation - Initial implementation
     Klaus Hartlage - www.eclipseproject.de
 **********************************************************************/
+import net.sourceforge.phpeclipse.phpeditor.php.PHPWordDetector;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.help.IHelp;
+import org.eclipse.help.IHelpResource;
 import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.editors.text.TextEditor;
+import org.eclipse.ui.help.WorkbenchHelp;
 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
 import org.eclipse.ui.texteditor.TextOperationAction;
 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
@@ -25,7 +34,7 @@ import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
  * Java specific text editor.
  */
 public class PHPEditor extends TextEditor {
-
+  
   /** The outline page */
   private PHPContentOutlinePage fOutlinePage;
 
@@ -136,6 +145,39 @@ public class PHPEditor extends TextEditor {
     return super.getAdapter(required);
   }
 
+  public void openContextHelp() {
+    IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
+    ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
+    int pos = selection.getOffset();
+    String word = getFunctionName(doc, pos);
+    openContextHelp(word);
+  }
+
+  private void openContextHelp(String word) {
+    open(word);
+  }
+
+  public static void open(String word) {
+    IHelp help = WorkbenchHelp.getHelpSupport();
+    if (help != null) {
+      IHelpResource helpResource = new PHPFunctionHelpResource(word);
+      WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
+    } else {
+      //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
+    }
+  }
+  
+  private String getFunctionName(IDocument doc, int pos) {
+    Point word = PHPWordDetector.findWord(doc, pos);
+    if (word != null) {
+      try {
+        return doc.get(word.x, word.y).replace('_', '-');
+      } catch (BadLocationException e) {
+      }
+    }
+    return "";
+  }
+
   /* (non-Javadoc)
    * Method declared on AbstractTextEditor
    */