1 /*******************************************************************************
2 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
3 * program and the accompanying materials are made available under the terms of
4 * the Common Public License v1.0 which accompanies this distribution, and is
5 * available at http://www.eclipse.org/legal/cpl-v10.html
7 * Contributors: Klaus Hartlage - www.eclipseproject.de
8 ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
19 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
20 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
21 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.ITextSelection;
30 import org.eclipse.jface.text.TextSelection;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.LabelProvider;
33 import org.eclipse.jface.window.Window;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.ui.IEditorActionDelegate;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IFileEditorInput;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.actions.ActionDelegate;
40 import org.eclipse.ui.dialogs.ListSelectionDialog;
41 import org.eclipse.ui.internal.dialogs.ListContentProvider;
43 public class PHPOpenDeclarationEditorAction extends ActionDelegate implements
44 IEditorActionDelegate {
46 // class Include implements Comparable {
47 // final public static int UNDEFINED_MATCH = 0;
48 // final public static int PATTERN_MATCH = 1;
49 // final public static int EXACT_MATCH = 2;
55 // public Include(String name, int match) {
63 // * @see java.lang.Object#toString()
65 // public String toString() {
67 // case UNDEFINED_MATCH:
69 // case PATTERN_MATCH:
70 // return "[pattern included] " + fName;
72 // return "[included] " + fName;
78 // * @return Returns the name.
80 // public String getName() {
84 // * @see java.lang.Comparable#compareTo(java.lang.Object)
86 // public int compareTo(Object o) {
87 // Include i = (Include)o;
88 // if (fMatch>i.fMatch) {
90 // } else if (fMatch<i.fMatch) {
93 // return fName.compareTo(i.fName);
97 private IWorkbenchWindow fWindow;
99 private PHPEditor fEditor;
101 private IProject fProject;
103 public void dispose() {
106 public void init(IWorkbenchWindow window) {
107 this.fWindow = window;
110 public void selectionChanged(IAction action, ISelection selection) {
111 if (!selection.isEmpty()) {
112 if (selection instanceof TextSelection) {
113 action.setEnabled(true);
114 } else if (fWindow.getActivePage() != null
115 && fWindow.getActivePage().getActivePart() != null) {
121 public void run(IAction action) {
122 if (fEditor == null) {
123 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
124 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
125 fEditor = (PHPEditor) targetEditor;
128 if (fEditor != null) {
129 // determine the current Project from a (file-based) Editor
130 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
131 fProject = f.getProject();
132 // System.out.println(fProject.toString());
134 ITextSelection selection = (ITextSelection) fEditor
135 .getSelectionProvider().getSelection();
136 IDocument doc = fEditor.getDocumentProvider().getDocument(
137 fEditor.getEditorInput());
138 int pos = selection.getOffset();
139 // System.out.println(selection.getText());
140 String word = getPHPIdentifier(doc, pos);
141 // System.out.println(word);
142 if (word != null && !word.equals("")) {
143 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
144 .getIndexManager(fProject);
145 List locationsList = indexManager.getLocations(word);
146 if (locationsList != null && locationsList.size() > 0) {
148 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
149 .getLocation().toString();
150 // TODO show all entries of the list in a dialog box
151 // at the moment always the first entry will be opened
152 if (locationsList.size() > 1) {
153 // determine all includes:
154 IncludesScanner includesScanner = new IncludesScanner(fProject,
155 (IFileEditorInput) fEditor.getEditorInput());
156 includesScanner.addFile(f);
157 Set exactIncludeSet = includesScanner.getSet();
159 PHPIdentifierLocation includeName;
160 for (int i = 0; i < locationsList.size(); i++) {
161 includeName = (PHPIdentifierLocation) locationsList.get(i);
162 if (exactIncludeSet.contains(includeName.getFilename())) {
163 includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
165 includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
168 Collections.sort(locationsList);
170 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
171 PHPeclipsePlugin.getDefault().getWorkbench()
172 .getActiveWorkbenchWindow().getShell(), locationsList,
173 new ListContentProvider(), new LabelProvider(),
174 "Select the resources to open.");
175 listSelectionDialog.setTitle("Multiple declarations found");
176 if (listSelectionDialog.open() == Window.OK) {
177 Object[] locations = listSelectionDialog.getResult();
178 if (locations != null) {
180 for (int i = 0; i < locations.length; i++) {
181 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
182 String filename = workspaceLocation
183 + location.getFilename();
184 // System.out.println(filename);
185 if (location.getOffset() >= 0) {
186 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(
187 filename, location.getOffset(), word.length());
189 PHPeclipsePlugin.getDefault().openFileAndFindString(
193 } catch (CoreException e) {
194 // TODO Auto-generated catch block
201 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
203 String filename = workspaceLocation + location.getFilename();
204 // System.out.println(filename);
205 if (location.getOffset() >= 0) {
206 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename,
207 location.getOffset(), word.length());
209 PHPeclipsePlugin.getDefault().openFileAndFindString(filename,
212 } catch (CoreException e) {
213 // TODO Auto-generated catch block
222 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
223 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
224 fEditor = (PHPEditor) targetEditor;
228 private String getPHPIdentifier(IDocument doc, int pos) {
229 Point word = PHPWordExtractor.findWord(doc, pos);
232 return doc.get(word.x, word.y);
233 } catch (BadLocationException e) {