Adds a psf for the 1.3.x branch and duplicates the 1.2.x psf as trunk, which is what...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / OpenDeclarationEditorAction.java
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
5  * 
6  * Contributors: www.phpeclipse.de
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.actions;
9
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Set;
13
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
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;
34
35 public class OpenDeclarationEditorAction {
36
37         private PHPEditor fEditor;
38
39         private IProject fProject;
40
41         private boolean isIncludeString;
42
43         public OpenDeclarationEditorAction(PHPEditor editor) {
44                 fEditor = editor;
45                 fProject = null;
46                 isIncludeString = false;
47         }
48
49         /**
50          * @param selection
51          */
52         protected void openSelectedElement(ITextSelection selection) {
53                 IDocument doc = fEditor.getDocumentProvider().getDocument(
54                                 fEditor.getEditorInput());
55                 int pos = selection.getOffset();
56                 openSelectedPosition(doc, pos);
57         }
58
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);
68                         } else {
69                                 openIdentifierDeclaration(f, identifierOrInclude);
70                         }
71                 }
72         }
73
74         /**
75          * @param filename
76          */
77         private void openIncludeFile(String filename) {
78                 if (filename != null && !filename.equals("")) {
79                         try {
80                                 IFile currentFile = ((IFileEditorInput) fEditor
81                                                 .getEditorInput()).getFile();
82                                 IPath path = PHPFileUtil.determineFilePath(filename,
83                                                 currentFile, fProject);
84                                 if (path != null) {
85                                         //IFile file = PHPFileUtil.createFile(path, fProject);
86                                         //if (file != null && file.exists()) {
87                                         //      PHPeclipsePlugin.getDefault().openFileInTextEditor(
88                                         //                      file.getLocation().toString());
89                                         //      return;
90                                         //}
91                                         PHPeclipsePlugin.getDefault().openFileInTextEditor(
92                                                         path.toString());
93                                         return;
94                                 }
95                         } catch (Exception e) {
96                                 // ignore
97                         }
98
99                         try {
100
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.getFullPath()
109                                                         .toString()
110                                                         + java.io.File.separatorChar;
111
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) {
121                                                         try {
122                                                                 for (int i = 0; i < locations.length; i++) {
123                                                                         // PHPIdentifierLocation location =
124                                                                         // (PHPIdentifierLocation)
125                                                                         // locations[i];
126                                                                         String openFilename = workspaceLocation
127                                                                                         + ((String) locations[i]);
128                                                                         PHPeclipsePlugin.getDefault()
129                                                                                         .openFileInTextEditor(openFilename);
130                                                                 }
131                                                         } catch (CoreException e) {
132                                                                 // TODO Auto-generated catch block
133                                                                 e.printStackTrace();
134                                                         }
135                                                 }
136                                         }
137
138                                 }
139                         } catch (Exception e) {
140                         }
141
142                 }
143                 return;
144         }
145
146         /**
147          * @param f
148          * @param identiifer
149          */
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) {
156
157                                 // String workspaceLocation =
158                                 // PHPeclipsePlugin.getWorkspace().getRoot()
159                                 // .getLocation().toString();
160
161                                 String workspaceLocation = fProject.getFullPath().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
169                                                                         .getEditorInput());
170                                         includesScanner.addFile(f);
171                                         Set exactIncludeSet = includesScanner.getSet();
172
173                                         PHPIdentifierLocation includeName;
174                                         for (int i = 0; i < locationsList.size(); i++) {
175                                                 includeName = (PHPIdentifierLocation) locationsList
176                                                                 .get(i);
177                                                 if (exactIncludeSet.contains(includeName.getFilename())) {
178                                                         includeName
179                                                                         .setMatch(PHPIdentifierLocation.EXACT_MATCH);
180                                                 } else {
181                                                         includeName
182                                                                         .setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
183                                                 }
184                                         }
185                                         Collections.sort(locationsList);
186
187                                         ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
188                                                         PHPeclipsePlugin.getDefault().getWorkbench()
189                                                                         .getActiveWorkbenchWindow().getShell(),
190                                                         locationsList, new ListContentProvider(),
191                                                         new LabelProvider(),
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) {
197                                                         try {
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(
206                                                                                                                 filename,
207                                                                                                                 location.getOffset(),
208                                                                                                                 identiifer.length());
209                                                                         } else {
210                                                                                 PHPeclipsePlugin.getDefault()
211                                                                                                 .openFileAndFindString(
212                                                                                                                 filename, identiifer);
213                                                                         }
214                                                                 }
215                                                         } catch (CoreException e) {
216                                                                 // TODO Auto-generated catch block
217                                                                 e.printStackTrace();
218                                                         }
219                                                 }
220                                         }
221                                 } else {
222                                         try {
223                                                 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
224                                                                 .get(0);
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());
233                                                 } else {
234                                                         PHPeclipsePlugin
235                                                                         .getDefault()
236                                                                         .openFileAndFindString(filename, identiifer);
237                                                 }
238                                         } catch (CoreException e) {
239                                                 // TODO Auto-generated catch block
240                                                 e.printStackTrace();
241                                         }
242                                 }
243                         }
244                 }
245         }
246
247         private String getIdentifierOrInclude(IDocument doc, int pos) {
248                 // private String getPHPIncludeText(IDocument doc, int pos) {
249                 Point word = null;
250                 int start = -1;
251                 int end = -1;
252                 isIncludeString = false;
253                 try {
254                         // try to find an include string
255                         int position = pos;
256                         char character = ' ';
257
258                         while (position >= 0) {
259                                 character = doc.getChar(position);
260                                 if ((character == '\"') || (character == '\'')
261                                                 || (character == '\r') || (character == '\n'))
262                                         break;
263                                 --position;
264                         }
265                         if ((character == '\"') || (character == '\'')) {
266                                 start = position;
267
268                                 position = pos;
269                                 int length = doc.getLength();
270                                 character = ' ';
271                                 while (position < length) {
272                                         character = doc.getChar(position);
273                                         if ((character == '\"') || (character == '\'')
274                                                         || (character == '\r') || (character == '\n'))
275                                                 break;
276                                         ++position;
277                                 }
278                                 if ((character == '\"') || (character == '\'')) {
279                                         start++;
280                                         end = position;
281
282                                         if (end > start) {
283                                                 word = new Point(start, end - start); // include name
284                                                                                                                                 // found
285                                                 isIncludeString = true;
286                                         }
287                                 }
288                         }
289
290                         // try to find an identifier
291                         if (word == null) {
292                                 word = PHPWordExtractor.findWord(doc, pos); // identifier found
293                                 isIncludeString = false;
294                         }
295                 } catch (BadLocationException x) {
296                 }
297
298                 if (word != null) {
299                         try {
300                                 return doc.get(word.x, word.y);
301                         } catch (BadLocationException e) {
302                         }
303                 }
304                 return "";
305         }
306
307 }