A massive organize imports and formatting of the sources using default Eclipse code...
[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
34 public class PHPEclipseShowContextHelp extends ActionDelegate implements
35                 IEditorActionDelegate {
36
37         private IWorkbenchWindow window;
38
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
53                                         && window.getActivePage().getActivePart() != null) {
54                                 //
55                         }
56                 }
57         }
58
59         public void run(IAction action) {
60                 if (editor == null) {
61                         IEditorPart targetEditor = window.getActivePage().getActiveEditor();
62                         if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
63                                 editor = (PHPEditor) targetEditor;
64                         }
65                 }
66                 if (editor != null) {
67                         ITextSelection selection = (ITextSelection) editor
68                                         .getSelectionProvider().getSelection();
69                         IDocument doc = editor.getDocumentProvider().getDocument(
70                                         editor.getEditorInput());
71                         if (null == window) {
72                                 window = editor.getSite().getWorkbenchWindow();
73                         }
74                         int pos = selection.getOffset();
75                         String word = getFunctionName(doc, pos);
76                         openContextHelp(word);
77                 }
78         }
79
80         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
81                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
82                         editor = (PHPEditor) targetEditor;
83                 }
84         }
85
86         public void openContextHelp(String word) {
87                 IPreferenceStore store = PHPHelpPlugin.getDefault()
88                                 .getPreferenceStore();
89                 if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
90                         String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE),
91                                         word };
92                         MessageFormat form = new MessageFormat(store
93                                         .getString(PHPHelpPlugin.PHP_CHM_COMMAND));
94                         try {
95                                 Runtime runtime = Runtime.getRuntime();
96                                 String command = form.format(arguments);
97
98                                 runtime.exec(command);
99                         } catch (IOException e) {
100                         }
101                 } else {
102                         PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(
103                                         word);
104                         window.getWorkbench().getHelpSystem().displayHelpResource(
105                                         helpResource.getHref());
106                 }
107         }
108
109         private String getFunctionName(IDocument doc, int pos) {
110                 Point word = PHPWordExtractor.findWord(doc, pos);
111                 if (word != null) {
112                         try {
113                                 return doc.get(word.x, word.y).replace('_', '-');
114                         } catch (BadLocationException e) {
115                         }
116                 }
117                 return "";
118         }
119 }