Fixing some bugs and making the plug-in compatible with Java 1.4
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / OpenDeclarationEditorAction.java
index aa7e725..9c35add 100644 (file)
@@ -12,6 +12,7 @@ import java.util.List;
 import java.util.Set;
 
 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
+import net.sourceforge.phpdt.internal.ui.viewsupport.ListContentProvider;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
@@ -30,234 +31,277 @@ import org.eclipse.jface.window.Window;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.dialogs.ListSelectionDialog;
-import org.eclipse.ui.internal.dialogs.ListContentProvider;
 
 public class OpenDeclarationEditorAction {
 
-  private PHPEditor fEditor;
+       private PHPEditor fEditor;
 
-  private IProject fProject;
+       private IProject fProject;
 
-  private boolean isIncludeString;
+       private boolean isIncludeString;
 
-  public OpenDeclarationEditorAction(PHPEditor editor) {
-    fEditor = editor;
-    fProject = null;
-    isIncludeString = false;
-  }
+       public OpenDeclarationEditorAction(PHPEditor editor) {
+               fEditor = editor;
+               fProject = null;
+               isIncludeString = false;
+       }
 
-  /**
-   * @param selection
-   */
-  protected void openSelectedElement(ITextSelection selection) {
-    IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
-    int pos = selection.getOffset();
-    openSelectedPosition(doc, pos);
-  }
+       /**
+        * @param selection
+        */
+       protected void openSelectedElement(ITextSelection selection) {
+               IDocument doc = fEditor.getDocumentProvider().getDocument(
+                               fEditor.getEditorInput());
+               int pos = selection.getOffset();
+               openSelectedPosition(doc, pos);
+       }
 
-  protected void openSelectedPosition(IDocument doc, int position) {
-    IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
-    fProject = f.getProject();
-    //  System.out.println(selection.getText());
-    String identifierOrInclude = getIdentifierOrInclude(doc, position);
-    //      System.out.println(word);
-    if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
-      if (isIncludeString) {
-        openIncludeFile(identifierOrInclude);
-      } else {
-        openIdentifierDeclaration(f, identifierOrInclude);
-      }
-    }
-  }
-  
-  /**
-   * @param filename
-   */
-  private void openIncludeFile(String filename) {
-    if (filename != null && !filename.equals("")) {
-      try {
-        IFile currentFile = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
-        IPath path = PHPFileUtil.determineFilePath(filename, currentFile, fProject);
-        if (path != null) {
-         IFile file = PHPFileUtil.createFile(path, fProject);
-          if (file != null && file.exists()) {
-            PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
-            return;
-          }
-        }
-      } catch (Exception e) {
-        // ignore
-      }
+       protected void openSelectedPosition(IDocument doc, int position) {
+               IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
+               fProject = f.getProject();
+               // System.out.println(selection.getText());
+               String identifierOrInclude = getIdentifierOrInclude(doc, position);
+               // System.out.println(word);
+               if (identifierOrInclude != null && !identifierOrInclude.equals("")) {
+                       if (isIncludeString) {
+                               openIncludeFile(identifierOrInclude);
+                       } else {
+                               openIdentifierDeclaration(f, identifierOrInclude);
+                       }
+               }
+       }
 
-      try {
+       /**
+        * @param filename
+        */
+       private void openIncludeFile(String filename) {
+               if (filename != null && !filename.equals("")) {
+                       try {
+                               IFile currentFile = ((IFileEditorInput) fEditor
+                                               .getEditorInput()).getFile();
+                               IPath path = PHPFileUtil.determineFilePath(filename,
+                                               currentFile, fProject);
+                               if (path != null) {
+                                       //IFile file = PHPFileUtil.createFile(path, fProject);
+                                       //if (file != null && file.exists()) {
+                                       //      PHPeclipsePlugin.getDefault().openFileInTextEditor(
+                                       //                      file.getLocation().toString());
+                                       //      return;
+                                       //}
+                                       PHPeclipsePlugin.getDefault().openFileInTextEditor(
+                                                       path.toString());
+                                       return;
+                               }
+                       } catch (Exception e) {
+                               // ignore
+                       }
 
-        IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
-        //            filename = StringUtil.replaceRegExChars(filename);
-        List list = indexManager.getFileList(filename);
-        if (list != null && list.size() > 0) {
-          //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
-          String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
+                       try {
 
-          ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
-              .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
-              "Select the includes to open.");
-          listSelectionDialog.setTitle("Multiple includes found");
-          if (listSelectionDialog.open() == Window.OK) {
-            Object[] locations = listSelectionDialog.getResult();
-            if (locations != null) {
-              try {
-                for (int i = 0; i < locations.length; i++) {
-                  //                    PHPIdentifierLocation location = (PHPIdentifierLocation)
-                  // locations[i];
-                  String openFilename = workspaceLocation + ((String) locations[i]);
-                  PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
-                }
-              } catch (CoreException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-              }
-            }
-          }
+                               IdentifierIndexManager indexManager = PHPeclipsePlugin
+                                               .getDefault().getIndexManager(fProject);
+                               // filename = StringUtil.replaceRegExChars(filename);
+                               List list = indexManager.getFileList(filename);
+                               if (list != null && list.size() > 0) {
+                                       // String workspaceLocation =
+                                       // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
+                                       String workspaceLocation = fProject.getLocation()
+                                                       .toString()
+                                                       + java.io.File.separatorChar;
 
-        }
-      } catch (Exception e) {
-      }
+                                       ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
+                                                       PHPeclipsePlugin.getDefault().getWorkbench()
+                                                                       .getActiveWorkbenchWindow().getShell(),
+                                                       list, new ListContentProvider(),
+                                                       new LabelProvider(), "Select the includes to open.");
+                                       listSelectionDialog.setTitle("Multiple includes found");
+                                       if (listSelectionDialog.open() == Window.OK) {
+                                               Object[] locations = listSelectionDialog.getResult();
+                                               if (locations != null) {
+                                                       try {
+                                                               for (int i = 0; i < locations.length; i++) {
+                                                                       // PHPIdentifierLocation location =
+                                                                       // (PHPIdentifierLocation)
+                                                                       // locations[i];
+                                                                       String openFilename = workspaceLocation
+                                                                                       + ((String) locations[i]);
+                                                                       PHPeclipsePlugin.getDefault()
+                                                                                       .openFileInTextEditor(openFilename);
+                                                               }
+                                                       } catch (CoreException e) {
+                                                               // TODO Auto-generated catch block
+                                                               e.printStackTrace();
+                                                       }
+                                               }
+                                       }
 
-    }
-    return;
-  }
+                               }
+                       } catch (Exception e) {
+                       }
 
-  /**
-   * @param f
-   * @param identiifer
-   */
-  private void openIdentifierDeclaration(IFile f, String identiifer) {
-    if (identiifer != null && !identiifer.equals("")) {
-      IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
-      List locationsList = indexManager.getLocations(identiifer);
-      if (locationsList != null && locationsList.size() > 0) {
+               }
+               return;
+       }
 
-        //          String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
-        //              .getLocation().toString();
+       /**
+        * @param f
+        * @param identiifer
+        */
+       private void openIdentifierDeclaration(IFile f, String identiifer) {
+               if (identiifer != null && !identiifer.equals("")) {
+                       IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault()
+                                       .getIndexManager(fProject);
+                       List locationsList = indexManager.getLocations(identiifer);
+                       if (locationsList != null && locationsList.size() > 0) {
 
-        String workspaceLocation = fProject.getLocation().toString() + java.io.File.separatorChar;
-        // TODO show all entries of the list in a dialog box
-        // at the moment always the first entry will be opened
-        if (locationsList.size() > 1) {
-          // determine all includes:
-          IncludesScanner includesScanner = new IncludesScanner(fProject, (IFileEditorInput) fEditor.getEditorInput());
-          includesScanner.addFile(f);
-          Set exactIncludeSet = includesScanner.getSet();
+                               // String workspaceLocation =
+                               // PHPeclipsePlugin.getWorkspace().getRoot()
+                               // .getLocation().toString();
 
-          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);
+                               String workspaceLocation = fProject.getLocation().toString()
+                                               + java.io.File.separatorChar;
+                               // TODO show all entries of the list in a dialog box
+                               // at the moment always the first entry will be opened
+                               if (locationsList.size() > 1) {
+                                       // determine all includes:
+                                       IncludesScanner includesScanner = new IncludesScanner(
+                                                       fProject, (IFileEditorInput) fEditor
+                                                                       .getEditorInput());
+                                       includesScanner.addFile(f);
+                                       Set exactIncludeSet = includesScanner.getSet();
 
-          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) {
-            Object[] locations = listSelectionDialog.getResult();
-            if (locations != null) {
-              try {
-                for (int i = 0; i < locations.length; i++) {
-                  PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
-                  String filename = workspaceLocation + location.getFilename();
-                  //                                   System.out.println(filename);
-                  if (location.getOffset() >= 0) {
-                    PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), identiifer.length());
-                  } else {
-                    PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
-                  }
-                }
-              } catch (CoreException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-              }
-            }
-          }
-        } else {
-          try {
-            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(), identiifer.length());
-            } else {
-              PHPeclipsePlugin.getDefault().openFileAndFindString(filename, identiifer);
-            }
-          } catch (CoreException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-          }
-        }
-      }
-    }
-  }
+                                       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);
 
-  private String getIdentifierOrInclude(IDocument doc, int pos) {
-    //    private String getPHPIncludeText(IDocument doc, int pos) {
-    Point word = null;
-    int start = -1;
-    int end = -1;
-    isIncludeString = false;
-    try {
-      //    try to find an include string
-      int position = pos;
-      char character = ' ';
+                                       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) {
+                                               Object[] locations = listSelectionDialog.getResult();
+                                               if (locations != null) {
+                                                       try {
+                                                               for (int i = 0; i < locations.length; i++) {
+                                                                       PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
+                                                                       String filename = workspaceLocation
+                                                                                       + location.getFilename();
+                                                                       // System.out.println(filename);
+                                                                       if (location.getOffset() >= 0) {
+                                                                               PHPeclipsePlugin.getDefault()
+                                                                                               .openFileAndGotoOffset(
+                                                                                                               filename,
+                                                                                                               location.getOffset(),
+                                                                                                               identiifer.length());
+                                                                       } else {
+                                                                               PHPeclipsePlugin.getDefault()
+                                                                                               .openFileAndFindString(
+                                                                                                               filename, identiifer);
+                                                                       }
+                                                               }
+                                                       } catch (CoreException e) {
+                                                               // TODO Auto-generated catch block
+                                                               e.printStackTrace();
+                                                       }
+                                               }
+                                       }
+                               } else {
+                                       try {
+                                               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(),
+                                                                                       identiifer.length());
+                                               } else {
+                                                       PHPeclipsePlugin
+                                                                       .getDefault()
+                                                                       .openFileAndFindString(filename, identiifer);
+                                               }
+                                       } catch (CoreException e) {
+                                               // TODO Auto-generated catch block
+                                               e.printStackTrace();
+                                       }
+                               }
+                       }
+               }
+       }
 
-      while (position >= 0) {
-        character = doc.getChar(position);
-        if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
-          break;
-        --position;
-      }
-      if ((character == '\"') || (character == '\'')) {
-        start = position;
+       private String getIdentifierOrInclude(IDocument doc, int pos) {
+               // private String getPHPIncludeText(IDocument doc, int pos) {
+               Point word = null;
+               int start = -1;
+               int end = -1;
+               isIncludeString = false;
+               try {
+                       // try to find an include string
+                       int position = pos;
+                       char character = ' ';
 
-        position = pos;
-        int length = doc.getLength();
-        character = ' ';
-        while (position < length) {
-          character = doc.getChar(position);
-          if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
-            break;
-          ++position;
-        }
-        if ((character == '\"') || (character == '\'')) {
-          start++;
-          end = position;
+                       while (position >= 0) {
+                               character = doc.getChar(position);
+                               if ((character == '\"') || (character == '\'')
+                                               || (character == '\r') || (character == '\n'))
+                                       break;
+                               --position;
+                       }
+                       if ((character == '\"') || (character == '\'')) {
+                               start = position;
 
-          if (end > start) {
-            word = new Point(start, end - start); // include name found
-            isIncludeString = true;
-          }
-        }
-      }
+                               position = pos;
+                               int length = doc.getLength();
+                               character = ' ';
+                               while (position < length) {
+                                       character = doc.getChar(position);
+                                       if ((character == '\"') || (character == '\'')
+                                                       || (character == '\r') || (character == '\n'))
+                                               break;
+                                       ++position;
+                               }
+                               if ((character == '\"') || (character == '\'')) {
+                                       start++;
+                                       end = position;
 
-      // try to find an identifier
-      if (word == null) {
-        word = PHPWordExtractor.findWord(doc, pos); // identifier found
-        isIncludeString = false;
-      }
-    } catch (BadLocationException x) {
-    }
+                                       if (end > start) {
+                                               word = new Point(start, end - start); // include name
+                                                                                                                               // found
+                                               isIncludeString = true;
+                                       }
+                               }
+                       }
 
-    if (word != null) {
-      try {
-        return doc.get(word.x, word.y);
-      } catch (BadLocationException e) {
-      }
-    }
-    return "";
-  }
+                       // try to find an identifier
+                       if (word == null) {
+                               word = PHPWordExtractor.findWord(doc, pos); // identifier found
+                               isIncludeString = false;
+                       }
+               } catch (BadLocationException x) {
+               }
+
+               if (word != null) {
+                       try {
+                               return doc.get(word.x, word.y);
+                       } catch (BadLocationException e) {
+                       }
+               }
+               return "";
+       }
 
 }
\ No newline at end of file