1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: www.phpeclipse.de
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.actions;
11 import java.util.Collections;
12 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.IContainer;
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.core.runtime.Path;
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 IEditorActionDelegate {
45 private IWorkbenchWindow fWindow;
47 private PHPEditor fEditor;
49 private IProject fProject;
51 private boolean isIncludeString;
53 public void dispose() {
56 public void init(IWorkbenchWindow window) {
57 this.fWindow = window;
60 public void selectionChanged(IAction action, ISelection selection) {
61 if (!selection.isEmpty()) {
62 if (selection instanceof TextSelection) {
63 action.setEnabled(true);
64 } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
70 public void run(IAction action) {
71 if (fEditor == null) {
72 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
73 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
74 fEditor = (PHPEditor) targetEditor;
77 if (fEditor != null) {
78 // determine the current Project from a (file-based) Editor
79 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
80 fProject = f.getProject();
81 // System.out.println(fProject.toString());
83 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
84 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
85 int pos = selection.getOffset();
86 // System.out.println(selection.getText());
87 String identifierOrInclude = getIdentifierOrInclude(doc, pos);
88 // System.out.println(word);
89 if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
90 if (isIncludeString) {
91 openIncludeFile(identifierOrInclude);
93 openIdentifierDeclaration(f, identifierOrInclude);
102 private void openIncludeFile(String filename) {
103 if (filename != null && !filename.equals("")) {
105 IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
106 if (file != null && file.exists()) {
107 PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
110 } catch (Exception e) {
116 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
117 // filename = StringUtil.replaceRegExChars(filename);
118 List list = indexManager.getFileList(filename);
119 if (list != null && list.size() > 0) {
120 //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
121 String workspaceLocation = fProject.getLocation().toString() + File.separatorChar;
123 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
124 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
125 "Select the includes to open.");
126 listSelectionDialog.setTitle("Multiple includes found");
127 if (listSelectionDialog.open() == Window.OK) {
128 Object[] locations = listSelectionDialog.getResult();
129 if (locations != null) {
131 for (int i = 0; i < locations.length; i++) {
132 // PHPIdentifierLocation location = (PHPIdentifierLocation)
134 String openFilename = workspaceLocation + ((String) locations[i]);
135 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
137 } catch (CoreException e) {
138 // TODO Auto-generated catch block
145 } catch (Exception e) {
156 private void openIdentifierDeclaration(IFile f, String identiifer) {
157 if (identiifer != null && !identiifer.equals("")) {
158 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
159 List locationsList = indexManager.getLocations(identiifer);
160 if (locationsList != null && locationsList.size() > 0) {
162 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
163 // .getLocation().toString();
165 String workspaceLocation = fProject.getLocation().toString() + File.separatorChar;
166 // TODO show all entries of the list in a dialog box
167 // at the moment always the first entry will be opened
168 if (locationsList.size() > 1) {
169 // determine all includes:
170 IncludesScanner includesScanner = new IncludesScanner(fProject, (IFileEditorInput) fEditor.getEditorInput());
171 includesScanner.addFile(f);
172 Set exactIncludeSet = includesScanner.getSet();
174 PHPIdentifierLocation includeName;
175 for (int i = 0; i < locationsList.size(); i++) {
176 includeName = (PHPIdentifierLocation) locationsList.get(i);
177 if (exactIncludeSet.contains(includeName.getFilename())) {
178 includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
180 includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
183 Collections.sort(locationsList);
185 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
186 .getActiveWorkbenchWindow().getShell(), locationsList, new ListContentProvider(), new LabelProvider(),
187 "Select the resources to open.");
188 listSelectionDialog.setTitle("Multiple declarations found");
189 if (listSelectionDialog.open() == Window.OK) {
190 Object[] locations = listSelectionDialog.getResult();
191 if (locations != null) {
193 for (int i = 0; i < locations.length; i++) {
194 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
195 String filename = workspaceLocation + location.getFilename();
196 // System.out.println(filename);
197 if (location.getOffset() >= 0) {
198 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
200 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
203 } catch (CoreException e) {
204 // TODO Auto-generated catch block
211 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList.get(0);
212 String filename = workspaceLocation + location.getFilename();
213 // System.out.println(filename);
214 if (location.getOffset() >= 0) {
215 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
217 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
219 } catch (CoreException e) {
220 // TODO Auto-generated catch block
228 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
229 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
230 fEditor = (PHPEditor) targetEditor;
234 private String getIdentifierOrInclude(IDocument doc, int pos) {
235 // private String getPHPIncludeText(IDocument doc, int pos) {
239 isIncludeString = false;
241 // try to find an include string
243 char character = ' ';
245 while (position >= 0) {
246 character = doc.getChar(position);
247 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
251 if ((character == '\"') || (character == '\'')) {
255 int length = doc.getLength();
257 while (position < length) {
258 character = doc.getChar(position);
259 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
263 if ((character == '\"') || (character == '\'')) {
268 word = new Point(start, end - start); // include name found
269 isIncludeString = true;
274 // try to find an identifier
276 word = PHPWordExtractor.findWord(doc, pos); // identifier found
277 isIncludeString = false;
279 } catch (BadLocationException x) {
284 return doc.get(word.x, word.y);
285 } catch (BadLocationException e) {
293 // Point word = PHPWordExtractor.findWord(doc, pos);
294 // if (word != null) {
296 // return doc.get(word.x, word.y);
297 // } catch (BadLocationException e) {
302 private IContainer getWorkingLocation(IFileEditorInput editorInput) {
303 if (editorInput == null || editorInput.getFile() == null) {
306 return editorInput.getFile().getParent();
309 private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
310 IContainer container = getWorkingLocation(editorInput);
311 String fullPath = project.getLocation().toString();
313 if (relativeFilename.startsWith("../")) {
314 Path path = new Path(relativeFilename);
315 file = container.getFile(path);
318 int index = relativeFilename.lastIndexOf('/');
321 Path path = new Path(relativeFilename);
322 file = project.getFile(path);
328 Path path = new Path(relativeFilename);
329 file = container.getFile(path);