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.jface.viewers.LabelProvider;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IEditorActionDelegate;
34 import org.eclipse.ui.IEditorPart;
35 import org.eclipse.ui.IFileEditorInput;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.actions.ActionDelegate;
38 import org.eclipse.ui.dialogs.ListSelectionDialog;
39 import org.eclipse.ui.internal.dialogs.ListContentProvider;
41 public class PHPOpenDeclarationEditorActon extends ActionDelegate implements IEditorActionDelegate {
43 private IWorkbenchWindow fWindow;
44 private PHPEditor fEditor;
45 private IProject fProject;
47 public void dispose() {
50 public void init(IWorkbenchWindow window) {
51 this.fWindow = window;
54 public void selectionChanged(IAction action, ISelection selection) {
55 if (!selection.isEmpty()) {
56 if (selection instanceof TextSelection) {
57 action.setEnabled(true);
58 } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
64 public void run(IAction action) {
65 if (fEditor == null) {
66 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
67 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
68 fEditor = (PHPEditor) targetEditor;
71 if (fEditor != null) {
72 // determine the current Project from a (file-based) Editor
73 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
74 fProject = f.getProject();
75 // System.out.println(fProject.toString());
77 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
78 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
79 int pos = selection.getOffset();
80 String word = getPHPIdentifier(doc, pos);
81 // System.out.println(word);
82 if (word != null && !word.equals("")) {
83 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
84 List list = indexManager.getLocations(word);
85 if (list != null && list.size() > 0) {
86 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
87 // TODO show all entries of the list in a dialog box
88 // at the moment always the first entry will be opened
89 if (list.size() > 1) {
90 ListSelectionDialog listSelectionDialog =
91 new ListSelectionDialog(
92 PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
94 new ListContentProvider(),
96 "Select the resources to open.");
97 listSelectionDialog.setTitle("Multiple declarations found");
98 if (listSelectionDialog.open() == Window.OK) {
99 Object[] locations = listSelectionDialog.getResult();
100 if (locations != null) {
102 for (int i = 0; i < locations.length; i++) {
103 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
104 String filename = workspaceLocation + location.getFilename();
105 // System.out.println(filename);
106 PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word);
108 } catch (CoreException e) {
109 // TODO Auto-generated catch block
116 PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0);
117 String filename = workspaceLocation + location.getFilename();
118 // System.out.println(filename);
119 PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word);
121 } catch (CoreException e) {
122 // TODO Auto-generated catch block
131 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
132 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
133 fEditor = (PHPEditor) targetEditor;
137 // public static void openContextHelp(String word) {
138 // IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
139 // if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
140 // String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word };
141 // MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND));
143 // Runtime runtime = Runtime.getRuntime();
144 // String command = form.format(arguments);
146 // runtime.exec(command);
147 // } catch (IOException e) {
150 // IHelp help = WorkbenchHelp.getHelpSupport();
151 // if (help != null) {
152 // PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word);
153 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
155 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
160 private String getPHPIdentifier(IDocument doc, int pos) {
161 Point word = PHPWordExtractor.findWord(doc, pos);
164 return doc.get(word.x, word.y);
165 } catch (BadLocationException e) {