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.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.search.internal.ui.util.ListContentProvider;
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(fEditor.getEditorInput());
54 int pos = selection.getOffset();
55 openSelectedPosition(doc, pos);
58 protected void openSelectedPosition(IDocument doc, int position) {
59 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
60 fProject = f.getProject();
61 // System.out.println(selection.getText());
62 String identifierOrInclude = getIdentifierOrInclude(doc, position);
63 // System.out.println(word);
64 if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
65 if (isIncludeString) {
66 openIncludeFile(identifierOrInclude);
68 openIdentifierDeclaration(f, identifierOrInclude);
76 private void openIncludeFile(String filename) {
77 if (filename != null && !filename.equals("")) {
79 IFile currentFile = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
80 IPath path = PHPFileUtil.determineFilePath(filename, currentFile, fProject);
82 IFile file = PHPFileUtil.createFile(path, fProject);
83 if (file != null && file.exists()) {
84 PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
88 } catch (Exception e) {
94 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
95 // filename = StringUtil.replaceRegExChars(filename);
96 List list = indexManager.getFileList(filename);
97 if (list != null && list.size() > 0) {
98 //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
99 String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
101 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
102 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
103 "Select the includes to open.");
104 listSelectionDialog.setTitle("Multiple includes found");
105 if (listSelectionDialog.open() == Window.OK) {
106 Object[] locations = listSelectionDialog.getResult();
107 if (locations != null) {
109 for (int i = 0; i < locations.length; i++) {
110 // PHPIdentifierLocation location = (PHPIdentifierLocation)
112 String openFilename = workspaceLocation + ((String) locations[i]);
113 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
115 } catch (CoreException e) {
116 // TODO Auto-generated catch block
123 } catch (Exception e) {
134 private void openIdentifierDeclaration(IFile f, String identiifer) {
135 if (identiifer != null && !identiifer.equals("")) {
136 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
137 List locationsList = indexManager.getLocations(identiifer);
138 if (locationsList != null && locationsList.size() > 0) {
140 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
141 // .getLocation().toString();
143 String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
144 // TODO show all entries of the list in a dialog box
145 // at the moment always the first entry will be opened
146 if (locationsList.size() > 1) {
147 // determine all includes:
148 IncludesScanner includesScanner = new IncludesScanner(fProject, (IFileEditorInput) fEditor.getEditorInput());
149 includesScanner.addFile(f);
150 Set exactIncludeSet = includesScanner.getSet();
152 PHPIdentifierLocation includeName;
153 for (int i = 0; i < locationsList.size(); i++) {
154 includeName = (PHPIdentifierLocation) locationsList.get(i);
155 if (exactIncludeSet.contains(includeName.getFilename())) {
156 includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
158 includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
161 Collections.sort(locationsList);
163 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
164 .getActiveWorkbenchWindow().getShell(), locationsList, new ListContentProvider(), new LabelProvider(),
165 "Select the resources to open.");
166 listSelectionDialog.setTitle("Multiple declarations found");
167 if (listSelectionDialog.open() == Window.OK) {
168 Object[] locations = listSelectionDialog.getResult();
169 if (locations != null) {
171 for (int i = 0; i < locations.length; i++) {
172 PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
173 String filename = workspaceLocation + location.getFilename();
174 // System.out.println(filename);
175 if (location.getOffset() >= 0) {
176 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
178 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
181 } catch (CoreException e) {
182 // TODO Auto-generated catch block
189 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList.get(0);
190 String filename = workspaceLocation + location.getFilename();
191 // System.out.println(filename);
192 if (location.getOffset() >= 0) {
193 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
195 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
197 } catch (CoreException e) {
198 // TODO Auto-generated catch block
206 private String getIdentifierOrInclude(IDocument doc, int pos) {
207 // private String getPHPIncludeText(IDocument doc, int pos) {
211 isIncludeString = false;
213 // try to find an include string
215 char character = ' ';
217 while (position >= 0) {
218 character = doc.getChar(position);
219 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
223 if ((character == '\"') || (character == '\'')) {
227 int length = doc.getLength();
229 while (position < length) {
230 character = doc.getChar(position);
231 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
235 if ((character == '\"') || (character == '\'')) {
240 word = new Point(start, end - start); // include name found
241 isIncludeString = true;
246 // try to find an identifier
248 word = PHPWordExtractor.findWord(doc, pos); // identifier found
249 isIncludeString = false;
251 } catch (BadLocationException x) {
256 return doc.get(word.x, word.y);
257 } catch (BadLocationException e) {