1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpdt.phphelp.actions;
14 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
16 import org.eclipse.help.IHelp;
17 import org.eclipse.help.IHelpResource;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextSelection;
22 import org.eclipse.jface.text.TextSelection;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.ui.IEditorActionDelegate;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.actions.ActionDelegate;
29 import org.eclipse.ui.help.WorkbenchHelp;
31 public class PHPEclipseShowContextHelp extends ActionDelegate implements IEditorActionDelegate {
33 private IWorkbenchWindow window;
34 private PHPEditor editor;
36 public void dispose() {
39 public void init(IWorkbenchWindow window) {
43 public void selectionChanged(IAction action, ISelection selection) {
44 if (!selection.isEmpty()) {
45 if (selection instanceof TextSelection) {
46 action.setEnabled(true);
47 } else if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
53 public void run(IAction action) {
55 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
56 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
57 editor = (PHPEditor) targetEditor;
61 editor.openContextHelp();
65 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
66 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
67 editor = (PHPEditor) targetEditor;
68 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
69 ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
70 int pos = selection.getOffset();
71 String word = getFunctionName(doc, pos);
72 openContextHelp(word);
76 public static void openContextHelp(String word) {
77 IHelp help = WorkbenchHelp.getHelpSupport();
79 IHelpResource helpResource = new PHPFunctionHelpResource(word);
80 WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
82 // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
86 private String getFunctionName(IDocument doc, int pos) {
87 Point word = PHPWordExtractor.findWord(doc, pos);
90 return doc.get(word.x, word.y).replace('_', '-');
91 } catch (BadLocationException e) {