Switched to Eclipse 2.1 development; eliminated JTidy package => now standalone plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / phphelp / actions / PHPEclipseShowContextHelp.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpdt.phphelp.actions;
13
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;
30
31 public class PHPEclipseShowContextHelp extends ActionDelegate implements IEditorActionDelegate {
32
33   private IWorkbenchWindow window;
34   private PHPEditor editor;
35
36   public void dispose() {
37   }
38
39   public void init(IWorkbenchWindow window) {
40     this.window = window;
41   }
42
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) {
48         //
49       }
50     }
51   }
52
53   public void run(IAction action) {
54     if (editor == null) {
55       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
56       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
57         editor = (PHPEditor) targetEditor;
58       }
59     }
60     if (editor != null) {
61       ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
62       IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
63       int pos = selection.getOffset();
64       String word = getFunctionName(doc, pos);
65       openContextHelp(word);
66
67     }
68   }
69
70   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
71     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
72       editor = (PHPEditor) targetEditor;
73     }
74   }
75
76   public static void openContextHelp(String word) {
77     IHelp help = WorkbenchHelp.getHelpSupport();
78     if (help != null) {
79       IHelpResource helpResource = new PHPFunctionHelpResource(word);
80       WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
81     } else {
82       //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
83     }
84   }
85
86   private String getFunctionName(IDocument doc, int pos) {
87     Point word = PHPWordExtractor.findWord(doc, pos);
88     if (word != null) {
89       try {
90         return doc.get(word.x, word.y).replace('_', '-');
91       } catch (BadLocationException e) {
92       }
93     }
94     return "";
95   }
96 }