0b5690c032aabeccf563b834de70eac33e5913c0
[phpeclipse.git] /
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 import net.sourceforge.phpeclipse.ui.WebUI;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.ITextSelection;
30 import org.eclipse.jface.viewers.LabelProvider;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.dialogs.ListSelectionDialog;
35
36 public class OpenDeclarationEditorAction {
37
38         private PHPEditor fEditor;
39
40         private IProject fProject;
41
42         private boolean isIncludeString;
43
44         public OpenDeclarationEditorAction(PHPEditor editor) {
45                 fEditor = editor;
46                 fProject = null;
47                 isIncludeString = false;
48         }
49
50         /**
51          * @param selection
52          */
53         protected void openSelectedElement(ITextSelection selection) {
54                 IDocument doc = fEditor.getDocumentProvider().getDocument(
55                                 fEditor.getEditorInput());
56                 int pos = selection.getOffset();
57                 openSelectedPosition(doc, pos);
58         }
59
60         protected void openSelectedPosition(IDocument doc, int position) {
61                 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
62                 fProject = f.getProject();
63                 // System.out.println(selection.getText());
64                 String identifierOrInclude = getIdentifierOrInclude(doc, position);
65                 // System.out.println(word);
66                 if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
67                         if (isIncludeString) {
68                                 openIncludeFile(identifierOrInclude);
69                         } else {
70                                 openIdentifierDeclaration(f, identifierOrInclude);
71                         }
72                 }
73         }
74
75         /**
76          * @param filename
77          */
78         private void openIncludeFile(String filename) {
79                 if (filename != null && !filename.equals("")) {
80                         try {
81                                 IFile currentFile = ((IFileEditorInput) fEditor
82                                                 .getEditorInput()).getFile();
83                                 IPath path = PHPFileUtil.determineFilePath(filename,
84                                                 currentFile, fProject);
85                                 if (path != null) {
86                                         //IFile file = PHPFileUtil.createFile(path, fProject);
87                                         //if (file != null && file.exists()) {
88                                         //      PHPeclipsePlugin.getDefault().openFileInTextEditor(
89                                         //                      file.getLocation().toString());
90                                         //      return;
91                                         //}
92                                         WebUI.getDefault().openFileInTextEditor(
93                                                         path.toString());
94                                         return;
95                                 }
96                         } catch (Exception e) {
97                                 // ignore
98                         }
99
100                         try {
101
102                                 IdentifierIndexManager indexManager = WebUI
103                                                 .getDefault().getIndexManager(fProject);
104                                 // filename = StringUtil.replaceRegExChars(filename);
105                                 List list = indexManager.getFileList(filename);
106                                 if (list != null && list.size() > 0) {
107                                         // String workspaceLocation =
108                                         // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
109                                         String workspaceLocation = fProject.getFullPath()
110                                                         .toString()
111                                                         + java.io.File.separatorChar;
112
113                                         ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
114                                                         WebUI.getDefault().getWorkbench()
115                                                                         .getActiveWorkbenchWindow().getShell(),
116                                                         list, new ListContentProvider(),
117                                                         new LabelProvider(), "Select the includes to open.");
118                                         listSelectionDialog.setTitle("Multiple includes found");
119                                         if (listSelectionDialog.open() == Window.OK) {
120                                                 Object[] locations = listSelectionDialog.getResult();
121                                                 if (locations != null) {
122                                                         try {
123                                                                 for (int i = 0; i < locations.length; i++) {
124                                                                         // PHPIdentifierLocation location =
125                                                                         // (PHPIdentifierLocation)
126                                                                         // locations[i];
127                                                                         String openFilename = workspaceLocation
128                                                                                         + ((String) locations[i]);
129                                                                         WebUI.getDefault()
130                                                                                         .openFileInTextEditor(openFilename);
131                                                                 }
132                                                         } catch (CoreException e) {
133                                                                 // TODO Auto-generated catch block
134                                                                 e.printStackTrace();
135                                                         }
136                                                 }
137                                         }
138
139                                 }
140                         } catch (Exception e) {
141                         }
142
143                 }
144                 return;
145         }
146
147         /**
148          * @param f
149          * @param identiifer
150          */
151         private void openIdentifierDeclaration(IFile f, String identiifer) {
152                 if (identiifer != null && !identiifer.equals("")) {
153                         IdentifierIndexManager indexManager = WebUI.getDefault()
154                                         .getIndexManager(fProject);
155                         List locationsList = indexManager.getLocations(identiifer);
156                         if (locationsList != null && locationsList.size() > 0) {
157
158                                 // String workspaceLocation =
159                                 // PHPeclipsePlugin.getWorkspace().getRoot()
160                                 // .getLocation().toString();
161
162                                 String workspaceLocation = fProject.getFullPath().toString()
163                                                 + java.io.File.separatorChar;
164                                 // TODO show all entries of the list in a dialog box
165                                 // at the moment always the first entry will be opened
166                                 if (locationsList.size() > 1) {
167                                         // determine all includes:
168                                         IncludesScanner includesScanner = new IncludesScanner(
169                                                         fProject, (IFileEditorInput) fEditor
170                                                                         .getEditorInput());
171                                         includesScanner.addFile(f);
172                                         Set exactIncludeSet = includesScanner.getSet();
173
174                                         PHPIdentifierLocation includeName;
175                                         for (int i = 0; i < locationsList.size(); i++) {
176                                                 includeName = (PHPIdentifierLocation) locationsList
177                                                                 .get(i);
178                                                 if (exactIncludeSet.contains(includeName.getFilename())) {
179                                                         includeName
180                                                                         .setMatch(PHPIdentifierLocation.EXACT_MATCH);
181                                                 } else {
182                                                         includeName
183                                                                         .setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
184                                                 }
185                                         }
186                                         Collections.sort(locationsList);
187
188                                         ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
189                                                         WebUI.getDefault().getWorkbench()
190                                                                         .getActiveWorkbenchWindow().getShell(),
191                                                         locationsList, new ListContentProvider(),
192                                                         new LabelProvider(),
193                                                         "Select the resources to open.");
194                                         listSelectionDialog.setTitle("Multiple declarations found");
195                                         if (listSelectionDialog.open() == Window.OK) {
196                                                 Object[] locations = listSelectionDialog.getResult();
197                                                 if (locations != null) {
198                                                         try {
199                                                                 for (int i = 0; i < locations.length; i++) {
200                                                                         PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
201                                                                         String filename = workspaceLocation
202                                                                                         + location.getFilename();
203                                                                         // System.out.println(filename);
204                                                                         if (location.getOffset() >= 0) {
205                                                                                 WebUI.getDefault()
206                                                                                                 .openFileAndGotoOffset(
207                                                                                                                 filename,
208                                                                                                                 location.getOffset(),
209                                                                                                                 identiifer.length());
210                                                                         } else {
211                                                                                 WebUI.getDefault()
212                                                                                                 .openFileAndFindString(
213                                                                                                                 filename, identiifer);
214                                                                         }
215                                                                 }
216                                                         } catch (CoreException e) {
217                                                                 // TODO Auto-generated catch block
218                                                                 e.printStackTrace();
219                                                         }
220                                                 }
221                                         }
222                                 } else {
223                                         try {
224                                                 PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
225                                                                 .get(0);
226                                                 String filename = workspaceLocation
227                                                                 + location.getFilename();
228                                                 // System.out.println(filename);
229                                                 if (location.getOffset() >= 0) {
230                                                         WebUI.getDefault()
231                                                                         .openFileAndGotoOffset(filename,
232                                                                                         location.getOffset(),
233                                                                                         identiifer.length());
234                                                 } else {
235                                                         WebUI
236                                                                         .getDefault()
237                                                                         .openFileAndFindString(filename, identiifer);
238                                                 }
239                                         } catch (CoreException e) {
240                                                 // TODO Auto-generated catch block
241                                                 e.printStackTrace();
242                                         }
243                                 }
244                         }
245                 }
246         }
247
248         private String getIdentifierOrInclude(IDocument doc, int pos) {
249                 // private String getPHPIncludeText(IDocument doc, int pos) {
250                 Point word = null;
251                 int start = -1;
252                 int end = -1;
253                 isIncludeString = false;
254                 try {
255                         // try to find an include string
256                         int position = pos;
257                         char character = ' ';
258
259                         while (position >= 0) {
260                                 character = doc.getChar(position);
261                                 if ((character == '\"') || (character == '\'')
262                                                 || (character == '\r') || (character == '\n'))
263                                         break;
264                                 --position;
265                         }
266                         if ((character == '\"') || (character == '\'')) {
267                                 start = position;
268
269                                 position = pos;
270                                 int length = doc.getLength();
271                                 character = ' ';
272                                 while (position < length) {
273                                         character = doc.getChar(position);
274                                         if ((character == '\"') || (character == '\'')
275                                                         || (character == '\r') || (character == '\n'))
276                                                 break;
277                                         ++position;
278                                 }
279                                 if ((character == '\"') || (character == '\'')) {
280                                         start++;
281                                         end = position;
282
283                                         if (end > start) {
284                                                 word = new Point(start, end - start); // include name
285                                                                                                                                 // found
286                                                 isIncludeString = true;
287                                         }
288                                 }
289                         }
290
291                         // try to find an identifier
292                         if (word == null) {
293                                 word = PHPWordExtractor.findWord(doc, pos); // identifier found
294                                 isIncludeString = false;
295                         }
296                 } catch (BadLocationException x) {
297                 }
298
299                 if (word != null) {
300                         try {
301                                 return doc.get(word.x, word.y);
302                         } catch (BadLocationException e) {
303                         }
304                 }
305                 return "";
306         }
307
308 }