Updated feature for build.
[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                                 }
92                         } catch (Exception e) {
93                                 // ignore
94                         }
95
96                         try {
97
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()
106                                                         .toString()
107                                                         + java.io.File.separatorChar;
108
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) {
118                                                         try {
119                                                                 for (int i = 0; i < locations.length; i++) {
120                                                                         // PHPIdentifierLocation location =
121                                                                         // (PHPIdentifierLocation)
122                                                                         // locations[i];
123                                                                         String openFilename = workspaceLocation
124                                                                                         + ((String) locations[i]);
125                                                                         PHPeclipsePlugin.getDefault()
126                                                                                         .openFileInTextEditor(openFilename);
127                                                                 }
128                                                         } catch (CoreException e) {
129                                                                 // TODO Auto-generated catch block
130                                                                 e.printStackTrace();
131                                                         }
132                                                 }
133                                         }
134
135                                 }
136                         } catch (Exception e) {
137                         }
138
139                 }
140                 return;
141         }
142
143         /**
144          * @param f
145          * @param identiifer
146          */
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) {
153
154                                 // String workspaceLocation =
155                                 // PHPeclipsePlugin.getWorkspace().getRoot()
156                                 // .getLocation().toString();
157
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
166                                                                         .getEditorInput());
167                                         includesScanner.addFile(f);
168                                         Set exactIncludeSet = includesScanner.getSet();
169
170                                         PHPIdentifierLocation includeName;
171                                         for (int i = 0; i < locationsList.size(); i++) {
172                                                 includeName = (PHPIdentifierLocation) locationsList
173                                                                 .get(i);
174                                                 if (exactIncludeSet.contains(includeName.getFilename())) {
175                                                         includeName
176                                                                         .setMatch(PHPIdentifierLocation.EXACT_MATCH);
177                                                 } else {
178                                                         includeName
179                                                                         .setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
180                                                 }
181                                         }
182                                         Collections.sort(locationsList);
183
184                                         ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
185                                                         PHPeclipsePlugin.getDefault().getWorkbench()
186                                                                         .getActiveWorkbenchWindow().getShell(),
187                                                         locationsList, new ListContentProvider(),
188                                                         new LabelProvider(),
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) {
194                                                         try {
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(
203                                                                                                                 filename,
204                                                                                                                 location.getOffset(),
205                                                                                                                 identiifer.length());
206                                                                         } else {
207                                                                                 PHPeclipsePlugin.getDefault()
208                                                                                                 .openFileAndFindString(
209                                                                                                                 filename, identiifer);
210                                                                         }
211                                                                 }
212                                                         } catch (CoreException e) {
213                                                                 // TODO Auto-generated catch block
214                                                                 e.printStackTrace();
215                                                         }
216                                                 }
217                                         }
218                                 } else {
219                                         try {
220                                                 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
221                                                                 .get(0);
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());
230                                                 } else {
231                                                         PHPeclipsePlugin
232                                                                         .getDefault()
233                                                                         .openFileAndFindString(filename, identiifer);
234                                                 }
235                                         } catch (CoreException e) {
236                                                 // TODO Auto-generated catch block
237                                                 e.printStackTrace();
238                                         }
239                                 }
240                         }
241                 }
242         }
243
244         private String getIdentifierOrInclude(IDocument doc, int pos) {
245                 // private String getPHPIncludeText(IDocument doc, int pos) {
246                 Point word = null;
247                 int start = -1;
248                 int end = -1;
249                 isIncludeString = false;
250                 try {
251                         // try to find an include string
252                         int position = pos;
253                         char character = ' ';
254
255                         while (position >= 0) {
256                                 character = doc.getChar(position);
257                                 if ((character == '\"') || (character == '\'')
258                                                 || (character == '\r') || (character == '\n'))
259                                         break;
260                                 --position;
261                         }
262                         if ((character == '\"') || (character == '\'')) {
263                                 start = position;
264
265                                 position = pos;
266                                 int length = doc.getLength();
267                                 character = ' ';
268                                 while (position < length) {
269                                         character = doc.getChar(position);
270                                         if ((character == '\"') || (character == '\'')
271                                                         || (character == '\r') || (character == '\n'))
272                                                 break;
273                                         ++position;
274                                 }
275                                 if ((character == '\"') || (character == '\'')) {
276                                         start++;
277                                         end = position;
278
279                                         if (end > start) {
280                                                 word = new Point(start, end - start); // include name
281                                                                                                                                 // found
282                                                 isIncludeString = true;
283                                         }
284                                 }
285                         }
286
287                         // try to find an identifier
288                         if (word == null) {
289                                 word = PHPWordExtractor.findWord(doc, pos); // identifier found
290                                 isIncludeString = false;
291                         }
292                 } catch (BadLocationException x) {
293                 }
294
295                 if (word != null) {
296                         try {
297                                 return doc.get(word.x, word.y);
298                         } catch (BadLocationException e) {
299                         }
300                 }
301                 return "";
302         }
303
304 }