Initial version from the webtools project; sligthly modified for phpeclipse
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenDeclarationEditorAction.java
index a582f1f..43b848a 100644 (file)
@@ -1,16 +1,17 @@
-/**********************************************************************
-Copyright (c) 2000, 2002 IBM Corp. and others.
-All rights reserved. This program and the accompanying materials
-are made available under the terms of the Common Public License v1.0
-which accompanies this distribution, and is available at
-http://www.eclipse.org/legal/cpl-v10.html
-
-Contributors:
-    Klaus Hartlage - www.eclipseproject.de
-**********************************************************************/
+/*******************************************************************************
+ * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
+ * program and the accompanying materials are made available under the terms of
+ * the Common Public License v1.0 which accompanies this distribution, and is
+ * available at http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors: Klaus Hartlage - www.eclipseproject.de
+ ******************************************************************************/
 package net.sourceforge.phpeclipse.actions;
 
+import java.io.File;
+import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
@@ -38,10 +39,64 @@ import org.eclipse.ui.actions.ActionDelegate;
 import org.eclipse.ui.dialogs.ListSelectionDialog;
 import org.eclipse.ui.internal.dialogs.ListContentProvider;
 
-public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IEditorActionDelegate {
+public class PHPOpenDeclarationEditorAction extends ActionDelegate implements
+    IEditorActionDelegate {
+
+//  class Include implements Comparable {
+//    final public static int UNDEFINED_MATCH = 0;
+//    final public static int PATTERN_MATCH = 1;
+//    final public static int EXACT_MATCH = 2;
+//
+//    String fName;
+//
+//    int fMatch;
+//
+//    public Include(String name, int match) {
+//      fName = name;
+//      fMatch = match;
+//    }
+//
+//    /*
+//     * (non-Javadoc)
+//     * 
+//     * @see java.lang.Object#toString()
+//     */
+//    public String toString() {
+//      switch (fMatch) {
+//      case UNDEFINED_MATCH:
+//        return fName;
+//      case PATTERN_MATCH:
+//        return "[pattern included] " + fName;
+//      case EXACT_MATCH:
+//        return "[included] " + fName;
+//      }
+//      return fName;
+//    }
+//
+//    /**
+//     * @return Returns the name.
+//     */
+//    public String getName() {
+//      return fName;
+//    }
+//    /* (non-Javadoc)
+//     * @see java.lang.Comparable#compareTo(java.lang.Object)
+//     */
+//    public int compareTo(Object o) {
+//      Include i = (Include)o;
+//      if (fMatch>i.fMatch) {
+//        return 1;
+//      } else if (fMatch<i.fMatch) {
+//        return -1;
+//      }
+//      return fName.compareTo(i.fName);
+//    }
+//  }
 
   private IWorkbenchWindow fWindow;
+
   private PHPEditor fEditor;
+
   private IProject fProject;
 
   public void dispose() {
@@ -55,7 +110,8 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
     if (!selection.isEmpty()) {
       if (selection instanceof TextSelection) {
         action.setEnabled(true);
-      } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
+      } else if (fWindow.getActivePage() != null
+          && fWindow.getActivePage().getActivePart() != null) {
         //
       }
     }
@@ -74,26 +130,48 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
       fProject = f.getProject();
       //      System.out.println(fProject.toString());
 
-      ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
-      IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
+      ITextSelection selection = (ITextSelection) fEditor
+          .getSelectionProvider().getSelection();
+      IDocument doc = fEditor.getDocumentProvider().getDocument(
+          fEditor.getEditorInput());
       int pos = selection.getOffset();
-    //  System.out.println(selection.getText());
+      //  System.out.println(selection.getText());
       String word = getPHPIdentifier(doc, pos);
       //      System.out.println(word);
       if (word != null && !word.equals("")) {
-        IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
-        List list = indexManager.getLocations(word);
-        if (list != null && list.size() > 0) {
-          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+        IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
+            .getIndexManager(fProject);
+        List locationsList = indexManager.getLocations(word);
+        if (locationsList != null && locationsList.size() > 0) {
+
+//          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
+//              .getLocation().toString();
+          
+          String workspaceLocation = fProject.getLocation().toString()+File.separatorChar;
           // TODO show all entries of the list in a dialog box
           // at the moment always the first entry will be opened
-          if (list.size() > 1) {
-            ListSelectionDialog listSelectionDialog =
-              new ListSelectionDialog(
-                PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
-                list,
-                new ListContentProvider(),
-                new LabelProvider(),
+          if (locationsList.size() > 1) {
+            // determine all includes:
+            IncludesScanner includesScanner = new IncludesScanner(fProject,
+                (IFileEditorInput) fEditor.getEditorInput());
+            includesScanner.addFile(f);
+            Set exactIncludeSet = includesScanner.getSet();
+
+            PHPIdentifierLocation includeName;
+            for (int i = 0; i < locationsList.size(); i++) {
+              includeName = (PHPIdentifierLocation) locationsList.get(i);
+              if (exactIncludeSet.contains(includeName.getFilename())) {
+                includeName.setMatch(PHPIdentifierLocation.EXACT_MATCH);
+              } else {
+                includeName.setMatch(PHPIdentifierLocation.UNDEFINED_MATCH);
+              }
+            }
+            Collections.sort(locationsList);
+            
+            ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
+                PHPeclipsePlugin.getDefault().getWorkbench()
+                    .getActiveWorkbenchWindow().getShell(), locationsList,
+                new ListContentProvider(), new LabelProvider(),
                 "Select the resources to open.");
             listSelectionDialog.setTitle("Multiple declarations found");
             if (listSelectionDialog.open() == Window.OK) {
@@ -102,12 +180,15 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
                 try {
                   for (int i = 0; i < locations.length; i++) {
                     PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
-                    String filename = workspaceLocation + location.getFilename();
+                    String filename = workspaceLocation
+                        + location.getFilename();
                     //                                 System.out.println(filename);
                     if (location.getOffset() >= 0) {
-                      PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
+                      PHPeclipsePlugin.getDefault().openFileAndGotoOffset(
+                          filename, location.getOffset(), word.length());
                     } else {
-                      PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
+                      PHPeclipsePlugin.getDefault().openFileAndFindString(
+                          filename, word);
                     }
                   }
                 } catch (CoreException e) {
@@ -118,13 +199,16 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
             }
           } else {
             try {
-              PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0);
+              PHPIdentifierLocation location = (PHPIdentifierLocation) locationsList
+                  .get(0);
               String filename = workspaceLocation + location.getFilename();
               //                                       System.out.println(filename);
               if (location.getOffset() >= 0) {
-                PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
+                PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename,
+                    location.getOffset(), word.length());
               } else {
-                PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
+                PHPeclipsePlugin.getDefault().openFileAndFindString(filename,
+                    word);
               }
             } catch (CoreException e) {
               // TODO Auto-generated catch block
@@ -152,4 +236,4 @@ public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IE
     }
     return "";
   }
-}
+}
\ No newline at end of file