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.phpdt.internal.ui.viewsupport.ListContentProvider;
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;
21 import net.sourceforge.phpeclipse.ui.WebUI;
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.core.runtime.IPath;
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.viewers.LabelProvider;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.dialogs.ListSelectionDialog;
36 public class OpenDeclarationEditorAction {
38 private PHPEditor fEditor;
40 private IProject fProject;
42 private boolean isIncludeString;
44 public OpenDeclarationEditorAction(PHPEditor editor) {
47 isIncludeString = false;
53 protected void openSelectedElement(ITextSelection selection) {
54 IDocument doc = fEditor.getDocumentProvider().getDocument(
55 fEditor.getEditorInput());
56 int pos = selection.getOffset();
57 openSelectedPosition(doc, pos);
60 protected void openSelectedPosition(IDocument doc, int position) {
61 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
62 fProject = f.getProject();
63 // System.out.println(selection.getText());
64 String identifierOrInclude = getIdentifierOrInclude(doc, position);
65 // System.out.println(word);
66 if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
67 if (isIncludeString) {
68 openIncludeFile(identifierOrInclude);
70 openIdentifierDeclaration(f, identifierOrInclude);
78 private void openIncludeFile(String filename) {
79 if (filename != null && !filename.equals("")) {
81 IFile currentFile = ((IFileEditorInput) fEditor
82 .getEditorInput()).getFile();
83 IPath path = PHPFileUtil.determineFilePath(filename,
84 currentFile, fProject);
86 //IFile file = PHPFileUtil.createFile(path, fProject);
87 //if (file != null && file.exists()) {
88 // PHPeclipsePlugin.getDefault().openFileInTextEditor(
89 // file.getLocation().toString());
92 WebUI.getDefault().openFileInTextEditor(
96 } catch (Exception e) {
102 IdentifierIndexManager indexManager = WebUI
103 .getDefault().getIndexManager(fProject);
104 // filename = StringUtil.replaceRegExChars(filename);
105 List list = indexManager.getFileList(filename);
106 if (list != null && list.size() > 0) {
107 // String workspaceLocation =
108 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
109 String workspaceLocation = fProject.getFullPath()
111 + java.io.File.separatorChar;
113 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
114 WebUI.getDefault().getWorkbench()
115 .getActiveWorkbenchWindow().getShell(),
116 list, new ListContentProvider(),
117 new LabelProvider(), "Select the includes to open.");
118 listSelectionDialog.setTitle("Multiple includes found");
119 if (listSelectionDialog.open() == Window.OK) {
120 Object[] locations = listSelectionDialog.getResult();
121 if (locations != null) {
123 for (int i = 0; i < locations.length; i++) {
124 // PHPIdentifierLocation location =
125 // (PHPIdentifierLocation)
127 String openFilename = workspaceLocation
128 + ((String) locations[i]);
130 .openFileInTextEditor(openFilename);
132 } catch (CoreException e) {
133 // TODO Auto-generated catch block
140 } catch (Exception e) {
151 private void openIdentifierDeclaration(IFile f, String identiifer) {
152 if (identiifer != null && !identiifer.equals("")) {
153 IdentifierIndexManager indexManager = WebUI.getDefault()
154 .getIndexManager(fProject);
155 List locationsList = indexManager.getLocations(identiifer);
156 if (locationsList != null && locationsList.size() > 0) {
158 // String workspaceLocation =
159 // PHPeclipsePlugin.getWorkspace().getRoot()
160 // .getLocation().toString();
162 String workspaceLocation = fProject.getFullPath().toString()
163 + java.io.File.separatorChar;
164 // TODO show all entries of the list in a dialog box
165 // at the moment always the first entry will be opened
166 if (locationsList.size() > 1) {
167 // determine all includes:
168 IncludesScanner includesScanner = new IncludesScanner(
169 fProject, (IFileEditorInput) fEditor
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
178 if (exactIncludeSet.contains(includeName.getFilename())) {
180 .setMatch(PHPIdentifierLocation.EXACT_MATCH);
183 .setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
186 Collections.sort(locationsList);
188 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
189 WebUI.getDefault().getWorkbench()
190 .getActiveWorkbenchWindow().getShell(),
191 locationsList, new ListContentProvider(),
193 "Select the resources to open.");
194 listSelectionDialog.setTitle("Multiple declarations found");
195 if (listSelectionDialog.open() == Window.OK) {
196 Object[] locations = listSelectionDialog.getResult();
197 if (locations != null) {
199 for (int i = 0; i < locations.length; i++) {
200 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
201 String filename = workspaceLocation
202 + location.getFilename();
203 // System.out.println(filename);
204 if (location.getOffset() >= 0) {
206 .openFileAndGotoOffset(
208 location.getOffset(),
209 identiifer.length());
212 .openFileAndFindString(
213 filename, identiifer);
216 } catch (CoreException e) {
217 // TODO Auto-generated catch block
224 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
226 String filename = workspaceLocation
227 + location.getFilename();
228 // System.out.println(filename);
229 if (location.getOffset() >= 0) {
231 .openFileAndGotoOffset(filename,
232 location.getOffset(),
233 identiifer.length());
237 .openFileAndFindString(filename, identiifer);
239 } catch (CoreException e) {
240 // TODO Auto-generated catch block
248 private String getIdentifierOrInclude(IDocument doc, int pos) {
249 // private String getPHPIncludeText(IDocument doc, int pos) {
253 isIncludeString = false;
255 // try to find an include string
257 char character = ' ';
259 while (position >= 0) {
260 character = doc.getChar(position);
261 if ((character == '\"') || (character == '\'')
262 || (character == '\r') || (character == '\n'))
266 if ((character == '\"') || (character == '\'')) {
270 int length = doc.getLength();
272 while (position < length) {
273 character = doc.getChar(position);
274 if ((character == '\"') || (character == '\'')
275 || (character == '\r') || (character == '\n'))
279 if ((character == '\"') || (character == '\'')) {
284 word = new Point(start, end - start); // include name
286 isIncludeString = true;
291 // try to find an identifier
293 word = PHPWordExtractor.findWord(doc, pos); // identifier found
294 isIncludeString = false;
296 } catch (BadLocationException x) {
301 return doc.get(word.x, word.y);
302 } catch (BadLocationException e) {