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 Klaus Hartlage - www.eclipseproject.de
10 **********************************************************************/
11 package net.sourceforge.phpeclipse.actions;
13 import java.util.List;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
17 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
18 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.TextSelection;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.ui.IEditorActionDelegate;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.actions.ActionDelegate;
37 public class PHPOpenDeclarationEditorActon extends ActionDelegate implements IEditorActionDelegate {
39 private IWorkbenchWindow fWindow;
40 private PHPEditor fEditor;
41 private IProject fProject;
43 public void dispose() {
46 public void init(IWorkbenchWindow window) {
47 this.fWindow = window;
50 public void selectionChanged(IAction action, ISelection selection) {
51 if (!selection.isEmpty()) {
52 if (selection instanceof TextSelection) {
53 action.setEnabled(true);
54 } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
60 public void run(IAction action) {
61 if (fEditor == null) {
62 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
63 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
64 fEditor = (PHPEditor) targetEditor;
67 if (fEditor != null) {
68 // determine the current Project from a (file-based) Editor
69 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
70 fProject = f.getProject();
71 // System.out.println(fProject.toString());
73 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
74 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
75 int pos = selection.getOffset();
76 String word = getPHPIdentifier(doc, pos);
77 // System.out.println(word);
78 if (word!=null && ! word.equals("")) {
79 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
80 List list = indexManager.getLocations(word);
81 if (list!=null && list.size()>0) {
82 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
83 // TODO show all entries of the list in a dialog box
84 // at the moment allways the first entry will be opened
85 PHPIdentifierLocation location = (PHPIdentifierLocation)list.get(0);
86 String filename = workspaceLocation + location.getFilename();
87 // System.out.println(filename);
89 PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word);
90 } catch (CoreException e) {
91 // TODO Auto-generated catch block
99 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
100 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
101 fEditor = (PHPEditor) targetEditor;
105 // public static void openContextHelp(String word) {
106 // IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
107 // if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
108 // String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word };
109 // MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND));
111 // Runtime runtime = Runtime.getRuntime();
112 // String command = form.format(arguments);
114 // runtime.exec(command);
115 // } catch (IOException e) {
118 // IHelp help = WorkbenchHelp.getHelpSupport();
119 // if (help != null) {
120 // PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word);
121 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
123 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
128 private String getPHPIdentifier(IDocument doc, int pos) {
129 Point word = PHPWordExtractor.findWord(doc, pos);
132 return doc.get(word.x, word.y);
133 } catch (BadLocationException e) {