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;
12 import java.util.Collections;
13 import java.util.List;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
18 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
19 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
20 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.ITextSelection;
29 import org.eclipse.jface.text.TextSelection;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.LabelProvider;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.ui.IEditorActionDelegate;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.IFileEditorInput;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.actions.ActionDelegate;
39 import org.eclipse.ui.dialogs.ListSelectionDialog;
40 import org.eclipse.ui.internal.dialogs.ListContentProvider;
42 public class PHPOpenDeclarationEditorAction extends ActionDelegate implements
43 IEditorActionDelegate {
45 // class Include implements Comparable {
46 // final public static int UNDEFINED_MATCH = 0;
47 // final public static int PATTERN_MATCH = 1;
48 // final public static int EXACT_MATCH = 2;
54 // public Include(String name, int match) {
62 // * @see java.lang.Object#toString()
64 // public String toString() {
66 // case UNDEFINED_MATCH:
68 // case PATTERN_MATCH:
69 // return "[pattern included] " + fName;
71 // return "[included] " + fName;
77 // * @return Returns the name.
79 // public String getName() {
83 // * @see java.lang.Comparable#compareTo(java.lang.Object)
85 // public int compareTo(Object o) {
86 // Include i = (Include)o;
87 // if (fMatch>i.fMatch) {
89 // } else if (fMatch<i.fMatch) {
92 // return fName.compareTo(i.fName);
96 private IWorkbenchWindow fWindow;
98 private PHPEditor fEditor;
100 private IProject fProject;
102 public void dispose() {
105 public void init(IWorkbenchWindow window) {
106 this.fWindow = window;
109 public void selectionChanged(IAction action, ISelection selection) {
110 if (!selection.isEmpty()) {
111 if (selection instanceof TextSelection) {
112 action.setEnabled(true);
113 } else if (fWindow.getActivePage() != null
114 && fWindow.getActivePage().getActivePart() != null) {
120 public void run(IAction action) {
121 if (fEditor == null) {
122 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
123 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
124 fEditor = (PHPEditor) targetEditor;
127 if (fEditor != null) {
128 // determine the current Project from a (file-based) Editor
129 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
130 fProject = f.getProject();
131 // System.out.println(fProject.toString());
133 ITextSelection selection = (ITextSelection) fEditor
134 .getSelectionProvider().getSelection();
135 IDocument doc = fEditor.getDocumentProvider().getDocument(
136 fEditor.getEditorInput());
137 int pos = selection.getOffset();
138 // System.out.println(selection.getText());
139 String word = getPHPIdentifier(doc, pos);
140 // System.out.println(word);
141 if (word != null && !word.equals("")) {
142 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
143 .getIndexManager(fProject);
144 List locationsList = indexManager.getLocations(word);
145 if (locationsList != null && locationsList.size() > 0) {
147 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
148 // .getLocation().toString();
150 String workspaceLocation = fProject.getLocation().toString()+File.separatorChar;
151 // TODO show all entries of the list in a dialog box
152 // at the moment always the first entry will be opened
153 if (locationsList.size() > 1) {
154 // determine all includes:
155 IncludesScanner includesScanner = new IncludesScanner(fProject,
156 (IFileEditorInput) fEditor.getEditorInput());
157 includesScanner.addFile(f);
158 Set exactIncludeSet = includesScanner.getSet();
160 PHPIdentifierLocation includeName;
161 for (int i = 0; i < locationsList.size(); i++) {
162 includeName = (PHPIdentifierLocation) locationsList.get(i);
163 if (exactIncludeSet.contains(includeName.getFilename())) {
164 includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
166 includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
169 Collections.sort(locationsList);
171 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
172 PHPeclipsePlugin.getDefault().getWorkbench()
173 .getActiveWorkbenchWindow().getShell(), locationsList,
174 new ListContentProvider(), new LabelProvider(),
175 "Select the resources to open.");
176 listSelectionDialog.setTitle("Multiple declarations found");
177 if (listSelectionDialog.open() == Window.OK) {
178 Object[] locations = listSelectionDialog.getResult();
179 if (locations != null) {
181 for (int i = 0; i < locations.length; i++) {
182 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
183 String filename = workspaceLocation
184 + location.getFilename();
185 // System.out.println(filename);
186 if (location.getOffset() >= 0) {
187 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(
188 filename, location.getOffset(), word.length());
190 PHPeclipsePlugin.getDefault().openFileAndFindString(
194 } catch (CoreException e) {
195 // TODO Auto-generated catch block
202 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
204 String filename = workspaceLocation + location.getFilename();
205 // System.out.println(filename);
206 if (location.getOffset() >= 0) {
207 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename,
208 location.getOffset(), word.length());
210 PHPeclipsePlugin.getDefault().openFileAndFindString(filename,
213 } catch (CoreException e) {
214 // TODO Auto-generated catch block
223 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
224 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
225 fEditor = (PHPEditor) targetEditor;
229 private String getPHPIdentifier(IDocument doc, int pos) {
230 Point word = PHPWordExtractor.findWord(doc, pos);
233 return doc.get(word.x, word.y);
234 } catch (BadLocationException e) {