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