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());
92 } catch (Exception e) {
98 IdentifierIndexManager indexManager = PHPeclipsePlugin
99 .getDefault().getIndexManager(fProject);
100 // filename = StringUtil.replaceRegExChars(filename);
101 List list = indexManager.getFileList(filename);
102 if (list != null && list.size() > 0) {
103 // String workspaceLocation =
104 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
105 String workspaceLocation = fProject.getLocation()
107 + java.io.File.separatorChar;
109 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
110 PHPeclipsePlugin.getDefault().getWorkbench()
111 .getActiveWorkbenchWindow().getShell(),
112 list, new ListContentProvider(),
113 new LabelProvider(), "Select the includes to open.");
114 listSelectionDialog.setTitle("Multiple includes found");
115 if (listSelectionDialog.open() == Window.OK) {
116 Object[] locations = listSelectionDialog.getResult();
117 if (locations != null) {
119 for (int i = 0; i < locations.length; i++) {
120 // PHPIdentifierLocation location =
121 // (PHPIdentifierLocation)
123 String openFilename = workspaceLocation
124 + ((String) locations[i]);
125 PHPeclipsePlugin.getDefault()
126 .openFileInTextEditor(openFilename);
128 } catch (CoreException e) {
129 // TODO Auto-generated catch block
136 } catch (Exception e) {
147 private void openIdentifierDeclaration(IFile f, String identiifer) {
148 if (identiifer != null && !identiifer.equals("")) {
149 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
150 .getIndexManager(fProject);
151 List locationsList = indexManager.getLocations(identiifer);
152 if (locationsList != null && locationsList.size() > 0) {
154 // String workspaceLocation =
155 // PHPeclipsePlugin.getWorkspace().getRoot()
156 // .getLocation().toString();
158 String workspaceLocation = fProject.getLocation().toString()
159 + java.io.File.separatorChar;
160 // TODO show all entries of the list in a dialog box
161 // at the moment always the first entry will be opened
162 if (locationsList.size() > 1) {
163 // determine all includes:
164 IncludesScanner includesScanner = new IncludesScanner(
165 fProject, (IFileEditorInput) fEditor
167 includesScanner.addFile(f);
168 Set exactIncludeSet = includesScanner.getSet();
170 PHPIdentifierLocation includeName;
171 for (int i = 0; i < locationsList.size(); i++) {
172 includeName = (PHPIdentifierLocation) locationsList
174 if (exactIncludeSet.contains(includeName.getFilename())) {
176 .setMatch(PHPIdentifierLocation.EXACT_MATCH);
179 .setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
182 Collections.sort(locationsList);
184 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
185 PHPeclipsePlugin.getDefault().getWorkbench()
186 .getActiveWorkbenchWindow().getShell(),
187 locationsList, new ListContentProvider(),
189 "Select the resources to open.");
190 listSelectionDialog.setTitle("Multiple declarations found");
191 if (listSelectionDialog.open() == Window.OK) {
192 Object[] locations = listSelectionDialog.getResult();
193 if (locations != null) {
195 for (int i = 0; i < locations.length; i++) {
196 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
197 String filename = workspaceLocation
198 + location.getFilename();
199 // System.out.println(filename);
200 if (location.getOffset() >= 0) {
201 PHPeclipsePlugin.getDefault()
202 .openFileAndGotoOffset(
204 location.getOffset(),
205 identiifer.length());
207 PHPeclipsePlugin.getDefault()
208 .openFileAndFindString(
209 filename, identiifer);
212 } catch (CoreException e) {
213 // TODO Auto-generated catch block
220 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
222 String filename = workspaceLocation
223 + location.getFilename();
224 // System.out.println(filename);
225 if (location.getOffset() >= 0) {
226 PHPeclipsePlugin.getDefault()
227 .openFileAndGotoOffset(filename,
228 location.getOffset(),
229 identiifer.length());
233 .openFileAndFindString(filename, identiifer);
235 } catch (CoreException e) {
236 // TODO Auto-generated catch block
244 private String getIdentifierOrInclude(IDocument doc, int pos) {
245 // private String getPHPIncludeText(IDocument doc, int pos) {
249 isIncludeString = false;
251 // try to find an include string
253 char character = ' ';
255 while (position >= 0) {
256 character = doc.getChar(position);
257 if ((character == '\"') || (character == '\'')
258 || (character == '\r') || (character == '\n'))
262 if ((character == '\"') || (character == '\'')) {
266 int length = doc.getLength();
268 while (position < length) {
269 character = doc.getChar(position);
270 if ((character == '\"') || (character == '\'')
271 || (character == '\r') || (character == '\n'))
275 if ((character == '\"') || (character == '\'')) {
280 word = new Point(start, end - start); // include name
282 isIncludeString = true;
287 // try to find an identifier
289 word = PHPWordExtractor.findWord(doc, pos); // identifier found
290 isIncludeString = false;
292 } catch (BadLocationException x) {
297 return doc.get(word.x, word.y);
298 } catch (BadLocationException e) {