delete finally
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenDeclarationEditorAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
3  * program and the accompanying materials are made available under the terms of
4  * the Common Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/cpl-v10.html
6  * 
7  * Contributors: Klaus Hartlage - www.eclipseproject.de
8  ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
11 import java.io.File;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Set;
15
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.jface.action.IAction;
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.text.TextSelection;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.LabelProvider;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.ui.IEditorActionDelegate;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.IFileEditorInput;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.actions.ActionDelegate;
39 import org.eclipse.ui.dialogs.ListSelectionDialog;
40 import org.eclipse.ui.internal.dialogs.ListContentProvider;
41
42 public class PHPOpenDeclarationEditorAction extends ActionDelegate implements
43     IEditorActionDelegate {
44
45 //  class Include implements Comparable {
46 //    final public static int UNDEFINED_MATCH = 0;
47 //    final public static int PATTERN_MATCH = 1;
48 //    final public static int EXACT_MATCH = 2;
49 //
50 //    String fName;
51 //
52 //    int fMatch;
53 //
54 //    public Include(String name, int match) {
55 //      fName = name;
56 //      fMatch = match;
57 //    }
58 //
59 //    /*
60 //     * (non-Javadoc)
61 //     * 
62 //     * @see java.lang.Object#toString()
63 //     */
64 //    public String toString() {
65 //      switch (fMatch) {
66 //      case UNDEFINED_MATCH:
67 //        return fName;
68 //      case PATTERN_MATCH:
69 //        return "[pattern included] " + fName;
70 //      case EXACT_MATCH:
71 //        return "[included] " + fName;
72 //      }
73 //      return fName;
74 //    }
75 //
76 //    /**
77 //     * @return Returns the name.
78 //     */
79 //    public String getName() {
80 //      return fName;
81 //    }
82 //    /* (non-Javadoc)
83 //     * @see java.lang.Comparable#compareTo(java.lang.Object)
84 //     */
85 //    public int compareTo(Object o) {
86 //      Include i = (Include)o;
87 //      if (fMatch>i.fMatch) {
88 //        return 1;
89 //      } else if (fMatch<i.fMatch) {
90 //        return -1;
91 //      }
92 //      return fName.compareTo(i.fName);
93 //    }
94 //  }
95
96   private IWorkbenchWindow fWindow;
97
98   private PHPEditor fEditor;
99
100   private IProject fProject;
101
102   public void dispose() {
103   }
104
105   public void init(IWorkbenchWindow window) {
106     this.fWindow = window;
107   }
108
109   public void selectionChanged(IAction action, ISelection selection) {
110     if (!selection.isEmpty()) {
111       if (selection instanceof TextSelection) {
112         action.setEnabled(true);
113       } else if (fWindow.getActivePage() != null
114           && fWindow.getActivePage().getActivePart() != null) {
115         //
116       }
117     }
118   }
119
120   public void run(IAction action) {
121     if (fEditor == null) {
122       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
123       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
124         fEditor = (PHPEditor) targetEditor;
125       }
126     }
127     if (fEditor != null) {
128       // determine the current Project from a (file-based) Editor
129       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
130       fProject = f.getProject();
131       //      System.out.println(fProject.toString());
132
133       ITextSelection selection = (ITextSelection) fEditor
134           .getSelectionProvider().getSelection();
135       IDocument doc = fEditor.getDocumentProvider().getDocument(
136           fEditor.getEditorInput());
137       int pos = selection.getOffset();
138       //  System.out.println(selection.getText());
139       String word = getPHPIdentifier(doc, pos);
140       //      System.out.println(word);
141       if (word != null && !word.equals("")) {
142         IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
143             .getIndexManager(fProject);
144         List locationsList = indexManager.getLocations(word);
145         if (locationsList != null && locationsList.size() > 0) {
146
147 //          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
148 //              .getLocation().toString();
149           
150           String workspaceLocation = fProject.getLocation().toString()+File.separatorChar;
151           // TODO show all entries of the list in a dialog box
152           // at the moment always the first entry will be opened
153           if (locationsList.size() > 1) {
154             // determine all includes:
155             IncludesScanner includesScanner = new IncludesScanner(fProject,
156                 (IFileEditorInput) fEditor.getEditorInput());
157             includesScanner.addFile(f);
158             Set exactIncludeSet = includesScanner.getSet();
159
160             PHPIdentifierLocation includeName;
161             for (int i = 0; i < locationsList.size(); i++) {
162               includeName = (PHPIdentifierLocation) locationsList.get(i);
163               if (exactIncludeSet.contains(includeName.getFilename())) {
164                 includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
165               } else {
166                 includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
167               }
168             }
169             Collections.sort(locationsList);
170             
171             ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
172                 PHPeclipsePlugin.getDefault().getWorkbench()
173                     .getActiveWorkbenchWindow().getShell(), locationsList,
174                 new ListContentProvider(), new LabelProvider(),
175                 "Select the resources to open.");
176             listSelectionDialog.setTitle("Multiple declarations found");
177             if (listSelectionDialog.open() == Window.OK) {
178               Object[] locations = listSelectionDialog.getResult();
179               if (locations != null) {
180                 try {
181                   for (int i = 0; i < locations.length; i++) {
182                     PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
183                     String filename = workspaceLocation
184                         + location.getFilename();
185                     //                                  System.out.println(filename);
186                     if (location.getOffset() >= 0) {
187                       PHPeclipsePlugin.getDefault().openFileAndGotoOffset(
188                           filename, location.getOffset(), word.length());
189                     } else {
190                       PHPeclipsePlugin.getDefault().openFileAndFindString(
191                           filename, word);
192                     }
193                   }
194                 } catch (CoreException e) {
195                   // TODO Auto-generated catch block
196                   e.printStackTrace();
197                 }
198               }
199             }
200           } else {
201             try {
202               PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
203                   .get(0);
204               String filename = workspaceLocation + location.getFilename();
205               //                                        System.out.println(filename);
206               if (location.getOffset() >= 0) {
207                 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename,
208                     location.getOffset(), word.length());
209               } else {
210                 PHPeclipsePlugin.getDefault().openFileAndFindString(filename,
211                     word);
212               }
213             } catch (CoreException e) {
214               // TODO Auto-generated catch block
215               e.printStackTrace();
216             }
217           }
218         }
219       }
220     }
221   }
222
223   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
224     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
225       fEditor = (PHPEditor) targetEditor;
226     }
227   }
228
229   private String getPHPIdentifier(IDocument doc, int pos) {
230     Point word = PHPWordExtractor.findWord(doc, pos);
231     if (word != null) {
232       try {
233         return doc.get(word.x, word.y);
234       } catch (BadLocationException e) {
235       }
236     }
237     return "";
238   }
239 }