avoid exception during creation of identifier list
[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.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;
20
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 net.sourceforge.phpdt.internal.ui.viewsupport.ListContentProvider;
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(fEditor.getEditorInput());
54     int pos = selection.getOffset();
55     openSelectedPosition(doc, pos);
56   }
57
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);
67       } else {
68         openIdentifierDeclaration(f, identifierOrInclude);
69       }
70     }
71   }
72   
73   /**
74    * @param filename
75    */
76   private void openIncludeFile(String filename) {
77     if (filename != null && !filename.equals("")) {
78       try {
79         IFile currentFile = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
80         IPath path = PHPFileUtil.determineFilePath(filename, currentFile, fProject);
81         if (path != null) {
82          IFile file = PHPFileUtil.createFile(path, fProject);
83           if (file != null && file.exists()) {
84             PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
85             return;
86           }
87         }
88       } catch (Exception e) {
89         // ignore
90       }
91
92       try {
93
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;
100
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) {
108               try {
109                 for (int i = 0; i < locations.length; i++) {
110                   //                    PHPIdentifierLocation location = (PHPIdentifierLocation)
111                   // locations[i];
112                   String openFilename = workspaceLocation + ((String) locations[i]);
113                   PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
114                 }
115               } catch (CoreException e) {
116                 // TODO Auto-generated catch block
117                 e.printStackTrace();
118               }
119             }
120           }
121
122         }
123       } catch (Exception e) {
124       }
125
126     }
127     return;
128   }
129
130   /**
131    * @param f
132    * @param identiifer
133    */
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) {
139
140         //          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
141         //              .getLocation().toString();
142
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();
151
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);
157             } else {
158               includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
159             }
160           }
161           Collections.sort(locationsList);
162
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) {
170               try {
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());
177                   } else {
178                     PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
179                   }
180                 }
181               } catch (CoreException e) {
182                 // TODO Auto-generated catch block
183                 e.printStackTrace();
184               }
185             }
186           }
187         } else {
188           try {
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());
194             } else {
195               PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
196             }
197           } catch (CoreException e) {
198             // TODO Auto-generated catch block
199             e.printStackTrace();
200           }
201         }
202       }
203     }
204   }
205
206   private String getIdentifierOrInclude(IDocument doc, int pos) {
207     //    private String getPHPIncludeText(IDocument doc, int pos) {
208     Point word = null;
209     int start = -1;
210     int end = -1;
211     isIncludeString = false;
212     try {
213       //    try to find an include string
214       int position = pos;
215       char character = ' ';
216
217       while (position >= 0) {
218         character = doc.getChar(position);
219         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
220           break;
221         --position;
222       }
223       if ((character == '\"') || (character == '\'')) {
224         start = position;
225
226         position = pos;
227         int length = doc.getLength();
228         character = ' ';
229         while (position < length) {
230           character = doc.getChar(position);
231           if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
232             break;
233           ++position;
234         }
235         if ((character == '\"') || (character == '\'')) {
236           start++;
237           end = position;
238
239           if (end > start) {
240             word = new Point(start, end - start); // include name found
241             isIncludeString = true;
242           }
243         }
244       }
245
246       // try to find an identifier
247       if (word == null) {
248         word = PHPWordExtractor.findWord(doc, pos); // identifier found
249         isIncludeString = false;
250       }
251     } catch (BadLocationException x) {
252     }
253
254     if (word != null) {
255       try {
256         return doc.get(word.x, word.y);
257       } catch (BadLocationException e) {
258       }
259     }
260     return "";
261   }
262
263 }