Patches from user robekras to show the variables view
[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     www.phpeclipse.de
11 **********************************************************************/
12 package net.sourceforge.phpdt.phphelp.actions;
13
14 import java.io.IOException;
15 import java.text.MessageFormat;
16
17 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
18 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.ITextSelection;
26 import org.eclipse.jface.text.TextSelection;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.ui.IEditorActionDelegate;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.actions.ActionDelegate;
33 import org.eclipse.ui.help.WorkbenchHelp;
34
35 public class PHPEclipseShowContextHelp extends ActionDelegate implements IEditorActionDelegate {
36
37   private IWorkbenchWindow window;
38   private PHPEditor editor;
39
40   public void dispose() {
41   }
42
43   public void init(IWorkbenchWindow window) {
44     this.window = window;
45   }
46
47   public void selectionChanged(IAction action, ISelection selection) {
48     if (!selection.isEmpty()) {
49       if (selection instanceof TextSelection) {
50         action.setEnabled(true);
51       } else if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
52         //
53       }
54     }
55   }
56
57   public void run(IAction action) {
58     if (editor == null) {
59       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
60       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
61         editor = (PHPEditor) targetEditor;
62       }
63     }
64     if (editor != null) {
65       ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
66       IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
67       int pos = selection.getOffset();
68       String word = getFunctionName(doc, pos);
69       openContextHelp(word);
70
71     }
72   }
73
74   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
75     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
76       editor = (PHPEditor) targetEditor;
77     }
78   }
79
80   public static void openContextHelp(String word) {
81     IPreferenceStore store = PHPHelpPlugin.getDefault().getPreferenceStore();
82     if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
83       String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word };
84       MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND));
85       try {
86         Runtime runtime = Runtime.getRuntime();
87         String command = form.format(arguments);
88
89         runtime.exec(command);
90       } catch (IOException e) {
91       }
92     } else {
93 //      IHelp help = WorkbenchHelp.getHelpSupport();
94 //      if (help != null) {
95         PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word);
96         WorkbenchHelp.displayHelpResource(helpResource.getHref());
97         //getHelpSupport().displayHelpResource(helpResource);
98 //      } else {
99         //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
100 //      }
101     }
102   }
103
104   private String getFunctionName(IDocument doc, int pos) {
105     Point word = PHPWordExtractor.findWord(doc, pos);
106     if (word != null) {
107       try {
108         return doc.get(word.x, word.y).replace('_', '-');
109       } catch (BadLocationException e) {
110       }
111     }
112     return "";
113   }
114 }