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;
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.IPath;
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.viewers.LabelProvider;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.ui.IFileEditorInput;
33 import org.eclipse.ui.dialogs.ListSelectionDialog;
35 public class OpenDeclarationEditorAction {
37 private PHPEditor fEditor;
39 private IProject fProject;
41 private boolean isIncludeString;
43 public OpenDeclarationEditorAction(PHPEditor editor) {
46 isIncludeString = false;
52 protected void openSelectedElement(ITextSelection selection) {
53 IDocument doc = fEditor.getDocumentProvider().getDocument(
54 fEditor.getEditorInput());
55 int pos = selection.getOffset();
56 openSelectedPosition(doc, pos);
59 protected void openSelectedPosition(IDocument doc, int position) {
60 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
61 fProject = f.getProject();
62 // System.out.println(selection.getText());
63 String identifierOrInclude = getIdentifierOrInclude(doc, position);
64 // System.out.println(word);
65 if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
66 if (isIncludeString) {
67 openIncludeFile(identifierOrInclude);
69 openIdentifierDeclaration(f, identifierOrInclude);
77 private void openIncludeFile(String filename) {
78 if (filename != null && !filename.equals("")) {
80 IFile currentFile = ((IFileEditorInput) fEditor
81 .getEditorInput()).getFile();
82 IPath path = PHPFileUtil.determineFilePath(filename,
83 currentFile, fProject);
85 //IFile file = PHPFileUtil.createFile(path, fProject);
86 //if (file != null && file.exists()) {
87 // PHPeclipsePlugin.getDefault().openFileInTextEditor(
88 // file.getLocation().toString());
91 PHPeclipsePlugin.getDefault().openFileInTextEditor(
95 } catch (Exception e) {
101 IdentifierIndexManager indexManager = PHPeclipsePlugin
102 .getDefault().getIndexManager(fProject);
103 // filename = StringUtil.replaceRegExChars(filename);
104 List list = indexManager.getFileList(filename);
105 if (list != null && list.size() > 0) {
106 // String workspaceLocation =
107 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
108 String workspaceLocation = fProject.getLocation()
110 + java.io.File.separatorChar;
112 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
113 PHPeclipsePlugin.getDefault().getWorkbench()
114 .getActiveWorkbenchWindow().getShell(),
115 list, new ListContentProvider(),
116 new LabelProvider(), "Select the includes to open.");
117 listSelectionDialog.setTitle("Multiple includes found");
118 if (listSelectionDialog.open() == Window.OK) {
119 Object[] locations = listSelectionDialog.getResult();
120 if (locations != null) {
122 for (int i = 0; i < locations.length; i++) {
123 // PHPIdentifierLocation location =
124 // (PHPIdentifierLocation)
126 String openFilename = workspaceLocation
127 + ((String) locations[i]);
128 PHPeclipsePlugin.getDefault()
129 .openFileInTextEditor(openFilename);
131 } catch (CoreException e) {
132 // TODO Auto-generated catch block
139 } catch (Exception e) {
150 private void openIdentifierDeclaration(IFile f, String identiifer) {
151 if (identiifer != null && !identiifer.equals("")) {
152 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
153 .getIndexManager(fProject);
154 List locationsList = indexManager.getLocations(identiifer);
155 if (locationsList != null && locationsList.size() > 0) {
157 // String workspaceLocation =
158 // PHPeclipsePlugin.getWorkspace().getRoot()
159 // .getLocation().toString();
161 String workspaceLocation = fProject.getLocation().toString()
162 + java.io.File.separatorChar;
163 // TODO show all entries of the list in a dialog box
164 // at the moment always the first entry will be opened
165 if (locationsList.size() > 1) {
166 // determine all includes:
167 IncludesScanner includesScanner = new IncludesScanner(
168 fProject, (IFileEditorInput) fEditor
170 includesScanner.addFile(f);
171 Set exactIncludeSet = includesScanner.getSet();
173 PHPIdentifierLocation includeName;
174 for (int i = 0; i < locationsList.size(); i++) {
175 includeName = (PHPIdentifierLocation) locationsList
177 if (exactIncludeSet.contains(includeName.getFilename())) {
179 .setMatch(PHPIdentifierLocation.EXACT_MATCH);
182 .setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
185 Collections.sort(locationsList);
187 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
188 PHPeclipsePlugin.getDefault().getWorkbench()
189 .getActiveWorkbenchWindow().getShell(),
190 locationsList, new ListContentProvider(),
192 "Select the resources to open.");
193 listSelectionDialog.setTitle("Multiple declarations found");
194 if (listSelectionDialog.open() == Window.OK) {
195 Object[] locations = listSelectionDialog.getResult();
196 if (locations != null) {
198 for (int i = 0; i < locations.length; i++) {
199 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
200 String filename = workspaceLocation
201 + location.getFilename();
202 // System.out.println(filename);
203 if (location.getOffset() >= 0) {
204 PHPeclipsePlugin.getDefault()
205 .openFileAndGotoOffset(
207 location.getOffset(),
208 identiifer.length());
210 PHPeclipsePlugin.getDefault()
211 .openFileAndFindString(
212 filename, identiifer);
215 } catch (CoreException e) {
216 // TODO Auto-generated catch block
223 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
225 String filename = workspaceLocation
226 + location.getFilename();
227 // System.out.println(filename);
228 if (location.getOffset() >= 0) {
229 PHPeclipsePlugin.getDefault()
230 .openFileAndGotoOffset(filename,
231 location.getOffset(),
232 identiifer.length());
236 .openFileAndFindString(filename, identiifer);
238 } catch (CoreException e) {
239 // TODO Auto-generated catch block
247 private String getIdentifierOrInclude(IDocument doc, int pos) {
248 // private String getPHPIncludeText(IDocument doc, int pos) {
252 isIncludeString = false;
254 // try to find an include string
256 char character = ' ';
258 while (position >= 0) {
259 character = doc.getChar(position);
260 if ((character == '\"') || (character == '\'')
261 || (character == '\r') || (character == '\n'))
265 if ((character == '\"') || (character == '\'')) {
269 int length = doc.getLength();
271 while (position < length) {
272 character = doc.getChar(position);
273 if ((character == '\"') || (character == '\'')
274 || (character == '\r') || (character == '\n'))
278 if ((character == '\"') || (character == '\'')) {
283 word = new Point(start, end - start); // include name
285 isIncludeString = true;
290 // try to find an identifier
292 word = PHPWordExtractor.findWord(doc, pos); // identifier found
293 isIncludeString = false;
295 } catch (BadLocationException x) {
300 return doc.get(word.x, word.y);
301 } catch (BadLocationException e) {