changes to install phpeclipse under wampp distribution
[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 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.help.IHelp;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.text.TextSelection;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.ui.IEditorActionDelegate;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.actions.ActionDelegate;
34 import org.eclipse.ui.help.WorkbenchHelp;
35
36 public class PHPEclipseShowContextHelp extends ActionDelegate implements IEditorActionDelegate {
37
38   private IWorkbenchWindow window;
39   private PHPEditor editor;
40
41   public void dispose() {
42   }
43
44   public void init(IWorkbenchWindow window) {
45     this.window = window;
46   }
47
48   public void selectionChanged(IAction action, ISelection selection) {
49     if (!selection.isEmpty()) {
50       if (selection instanceof TextSelection) {
51         action.setEnabled(true);
52       } else if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
53         //
54       }
55     }
56   }
57
58   public void run(IAction action) {
59     if (editor == null) {
60       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
61       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
62         editor = (PHPEditor) targetEditor;
63       }
64     }
65     if (editor != null) {
66       ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
67       IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
68       int pos = selection.getOffset();
69       String word = getFunctionName(doc, pos);
70       openContextHelp(word);
71
72     }
73   }
74
75   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
76     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
77       editor = (PHPEditor) targetEditor;
78     }
79   }
80
81   public static void openContextHelp(String word) {
82     IPreferenceStore store = PHPHelpPlugin.getDefault().getPreferenceStore();
83     if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
84       String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word };
85       MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND));
86       try {
87         Runtime runtime = Runtime.getRuntime();
88         String command = form.format(arguments);
89
90         runtime.exec(command);
91       } catch (IOException e) {
92       }
93     } else {
94       IHelp help = WorkbenchHelp.getHelpSupport();
95       if (help != null) {
96         PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word);
97         WorkbenchHelp.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 }