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;
10 import java.util.Collections;
11 import java.util.List;
14 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
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.internal.resources.File;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextSelection;
31 import org.eclipse.jface.text.TextSelection;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.swt.graphics.Point;
36 import org.eclipse.ui.IEditorActionDelegate;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IFileEditorInput;
39 import org.eclipse.ui.IWorkbenchWindow;
40 import org.eclipse.ui.actions.ActionDelegate;
41 import org.eclipse.ui.dialogs.ListSelectionDialog;
42 import org.eclipse.ui.internal.dialogs.ListContentProvider;
44 public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IEditorActionDelegate {
46 private IWorkbenchWindow fWindow;
48 private PHPEditor fEditor;
50 private IProject fProject;
52 private boolean isIncludeString;
54 public void dispose() {
57 public void init(IWorkbenchWindow window) {
58 this.fWindow = window;
61 public void selectionChanged(IAction action, ISelection selection) {
62 if (!selection.isEmpty()) {
63 if (selection instanceof TextSelection) {
64 action.setEnabled(true);
65 } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
71 public void run(IAction action) {
72 if (fEditor == null) {
73 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
74 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
75 fEditor = (PHPEditor) targetEditor;
78 if (fEditor != null) {
79 // determine the current Project from a (file-based) Editor
80 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
81 fProject = f.getProject();
82 // System.out.println(fProject.toString());
84 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
85 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
86 int pos = selection.getOffset();
87 // System.out.println(selection.getText());
88 String identifierOrInclude = getIdentifierOrInclude(doc, pos);
89 // System.out.println(word);
90 if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
91 if (isIncludeString) {
92 openIncludeFile(identifierOrInclude);
94 openIdentifierDeclaration(f, identifierOrInclude);
103 private void openIncludeFile(String filename) {
104 if (filename != null && !filename.equals("")) {
106 IFile currentFile = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
107 IPath path = PHPFileUtil.determineFilePath(filename, currentFile, fProject);
109 // String projectPath = fProject.getLocation().toString();
110 // String filePath = path.toString().substring(projectPath.length()+1);
111 // IFile file = fProject.getFile(filePath);
112 IFile file = PHPFileUtil.createFile(path, fProject);
113 // IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
114 if (file != null && file.exists()) {
115 PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
119 } catch (Exception e) {
125 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
126 // filename = StringUtil.replaceRegExChars(filename);
127 List list = indexManager.getFileList(filename);
128 if (list != null && list.size() > 0) {
129 //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
130 String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
132 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
133 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
134 "Select the includes to open.");
135 listSelectionDialog.setTitle("Multiple includes found");
136 if (listSelectionDialog.open() == Window.OK) {
137 Object[] locations = listSelectionDialog.getResult();
138 if (locations != null) {
140 for (int i = 0; i < locations.length; i++) {
141 // PHPIdentifierLocation location = (PHPIdentifierLocation)
143 String openFilename = workspaceLocation + ((String) locations[i]);
144 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
146 } catch (CoreException e) {
147 // TODO Auto-generated catch block
154 } catch (Exception e) {
165 private void openIdentifierDeclaration(IFile f, String identiifer) {
166 if (identiifer != null && !identiifer.equals("")) {
167 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
168 List locationsList = indexManager.getLocations(identiifer);
169 if (locationsList != null && locationsList.size() > 0) {
171 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
172 // .getLocation().toString();
174 String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
175 // TODO show all entries of the list in a dialog box
176 // at the moment always the first entry will be opened
177 if (locationsList.size() > 1) {
178 // determine all includes:
179 IncludesScanner includesScanner = new IncludesScanner(fProject, (IFileEditorInput) fEditor.getEditorInput());
180 includesScanner.addFile(f);
181 Set exactIncludeSet = includesScanner.getSet();
183 PHPIdentifierLocation includeName;
184 for (int i = 0; i < locationsList.size(); i++) {
185 includeName = (PHPIdentifierLocation) locationsList.get(i);
186 if (exactIncludeSet.contains(includeName.getFilename())) {
187 includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
189 includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
192 Collections.sort(locationsList);
194 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
195 .getActiveWorkbenchWindow().getShell(), locationsList, new ListContentProvider(), new LabelProvider(),
196 "Select the resources to open.");
197 listSelectionDialog.setTitle("Multiple declarations found");
198 if (listSelectionDialog.open() == Window.OK) {
199 Object[] locations = listSelectionDialog.getResult();
200 if (locations != null) {
202 for (int i = 0; i < locations.length; i++) {
203 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
204 String filename = workspaceLocation + location.getFilename();
205 // System.out.println(filename);
206 if (location.getOffset() >= 0) {
207 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
209 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
212 } catch (CoreException e) {
213 // TODO Auto-generated catch block
220 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList.get(0);
221 String filename = workspaceLocation + location.getFilename();
222 // System.out.println(filename);
223 if (location.getOffset() >= 0) {
224 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
226 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
228 } catch (CoreException e) {
229 // TODO Auto-generated catch block
237 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
238 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
239 fEditor = (PHPEditor) targetEditor;
243 private String getIdentifierOrInclude(IDocument doc, int pos) {
244 // private String getPHPIncludeText(IDocument doc, int pos) {
248 isIncludeString = false;
250 // try to find an include string
252 char character = ' ';
254 while (position >= 0) {
255 character = doc.getChar(position);
256 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
260 if ((character == '\"') || (character == '\'')) {
264 int length = doc.getLength();
266 while (position < length) {
267 character = doc.getChar(position);
268 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
272 if ((character == '\"') || (character == '\'')) {
277 word = new Point(start, end - start); // include name found
278 isIncludeString = true;
283 // try to find an identifier
285 word = PHPWordExtractor.findWord(doc, pos); // identifier found
286 isIncludeString = false;
288 } catch (BadLocationException x) {
293 return doc.get(word.x, word.y);
294 } catch (BadLocationException e) {
302 // Point word = PHPWordExtractor.findWord(doc, pos);
303 // if (word != null) {
305 // return doc.get(word.x, word.y);
306 // } catch (BadLocationException e) {
311 private IContainer getWorkingLocation(IFileEditorInput editorInput) {
312 if (editorInput == null || editorInput.getFile() == null) {
315 return editorInput.getFile().getParent();
318 // private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
319 // IContainer container = getWorkingLocation(editorInput);
320 // String fullPath = project.getLocation().toString();
321 // IFile file = null;
322 // if (relativeFilename.startsWith("../")) {
323 // Path path = new Path(relativeFilename);
324 // file = container.getFile(path);
327 // int index = relativeFilename.lastIndexOf('/');
330 // Path path = new Path(relativeFilename);
331 // file = project.getFile(path);
332 // if (file.exists()) {
337 // Path path = new Path(relativeFilename);
338 // file = container.getFile(path);