Goto line/Goto Matching Bracket Shortcuts should work
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / EditorUtility.java
index 2cff713..b951472 100644 (file)
 package net.sourceforge.phpeclipse.phpeditor;
 
 
+import net.sourceforge.phpdt.core.ICompilationUnit;
 import net.sourceforge.phpdt.core.IJavaElement;
+import net.sourceforge.phpdt.core.IJavaProject;
+import net.sourceforge.phpdt.core.IMember;
+import net.sourceforge.phpdt.core.IWorkingCopy;
+import net.sourceforge.phpdt.core.JavaCore;
 import net.sourceforge.phpdt.core.JavaModelException;
+import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
+import net.sourceforge.phpdt.ui.JavaUI;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.jface.action.Action;
 import org.eclipse.swt.SWT;
 import org.eclipse.ui.IEditorDescriptor;
@@ -27,6 +36,7 @@ import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
 import org.eclipse.ui.part.FileEditorInput;
 import org.eclipse.ui.texteditor.ITextEditor;
 
@@ -43,11 +53,11 @@ public class EditorUtility {
        
        public static boolean isEditorInput(Object element, IEditorPart editor) {
                if (editor != null) {
-//                     try {
+                       try {
                                return editor.getEditorInput().equals(getEditorInput(element));
-//                     } catch (JavaModelException x) {
-//                             JavaPlugin.log(x.getStatus());
-//                     }
+                       } catch (JavaModelException x) {
+                               PHPeclipsePlugin.log(x.getStatus());
+                       }
                }
                return false;
        }
@@ -59,11 +69,11 @@ public class EditorUtility {
        public static IEditorPart isOpenInEditor(Object inputElement) {
                IEditorInput input= null;
                
-//             try {
-                       input= getEditorInput(inputElement);
-//             } catch (JavaModelException x) {
-//                     JavaPlugin.log(x.getStatus());
-//             }
+               try {
+                       input = getEditorInput(inputElement);
+               } catch (JavaModelException x) {
+                       PHPeclipsePlugin.log(x.getStatus());
+               }
                
                if (input != null) {
                        IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
@@ -108,22 +118,22 @@ public class EditorUtility {
        /** 
         * Selects a Java Element in an editor
         */     
-//     public static void revealInEditor(IEditorPart part, IJavaElement element) {
-//             if (element != null && part instanceof PHPEditor) {
-//                     ((PHPEditor) part).setSelection(element);
-//             }
-//     }
+       public static void revealInEditor(IEditorPart part, IJavaElement element) {
+               if (element != null && part instanceof PHPEditor) {
+                       ((PHPEditor) part).setSelection(element);
+               }
+       }
        
        private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
-               if (file != null) {
-                       IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
-                       if (p != null) {
-                               IEditorPart editorPart= p.openEditor(file, null, activate);
-                               initializeHighlightRange(editorPart);
-                               return editorPart;
-                       }
-               }
-               return null;
+         if (file != null) {
+           IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
+           if (p != null) {
+             IEditorPart editorPart= IDE.openEditor(p, file, activate);
+             initializeHighlightRange(editorPart);
+             return editorPart;
+           }
+         }
+         return null;
        }
 
        private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
@@ -159,29 +169,28 @@ public class EditorUtility {
                return null;
        }
        
-//     private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
-//             while (element != null) {
-//                     if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy()) 
-//                             element= ((IWorkingCopy) element).getOriginalElement();
-//                             
-//                     if (element instanceof ICompilationUnit) {
-//                             ICompilationUnit unit= (ICompilationUnit) element;
-//                                     IResource resource= unit.getResource();
-//                                     if (resource instanceof IFile)
-//                                             return new FileEditorInput((IFile) resource);
-//                     }
-//                     
+       private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
+               while (element != null) {
+                       if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy()) 
+                               element= ((IWorkingCopy) element).getOriginalElement();
+                               
+                       if (element instanceof ICompilationUnit) {
+                               ICompilationUnit unit= (ICompilationUnit) element;
+                                       IResource resource= unit.getResource();
+                                       if (resource instanceof IFile)
+                                               return new FileEditorInput((IFile) resource);
+                       }
+                       
 //                     if (element instanceof IClassFile)
 //                             return new InternalClassFileEditorInput((IClassFile) element);
 //                     
-//                     element= element.getParent();
-//             }
-//             
-//             return null;
-//     }       
+                       element= element.getParent();
+               }
+               
+               return null;
+       }       
 
-       public static IEditorInput getEditorInput(Object input) {
-//             throws JavaModelException {
+       public static IEditorInput getEditorInput(Object input) throws JavaModelException {
        
                if (input instanceof IJavaElement)
                        return getEditorInput((IJavaElement) input);
@@ -219,14 +228,14 @@ public class EditorUtility {
         * @param cu the original compilation unit (or another working copy)
         * @return the working copy of the compilation unit, or null if not found
         */     
-//     public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
-//             if (cu == null)
-//                     return null;
-//             if (cu.isWorkingCopy())
-//                     return cu;
-//                     
-//             return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
-//     }
+       public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
+               if (cu == null)
+                       return null;
+               if (cu.isWorkingCopy())
+                       return cu;
+                       
+               return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
+       }
        
        /** 
         * Gets the working copy of an member opened in an editor
@@ -234,38 +243,38 @@ public class EditorUtility {
         * @param member the original member or a member in a working copy
         * @return the corresponding member in the shared working copy or <code>null</code> if not found
         */     
-//     public static IMember getWorkingCopy(IMember member) throws JavaModelException {
-//             ICompilationUnit cu= member.getCompilationUnit();
-//             if (cu != null) {
-//                     ICompilationUnit workingCopy= getWorkingCopy(cu);
-//                     if (workingCopy != null) {
-//                             return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
-//                     }
-//             }
-//             return null;
-//     }
+       public static IMember getWorkingCopy(IMember member) throws JavaModelException {
+               ICompilationUnit cu= member.getCompilationUnit();
+               if (cu != null) {
+                       ICompilationUnit workingCopy= getWorkingCopy(cu);
+                       if (workingCopy != null) {
+                               return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
+                       }
+               }
+               return null;
+       }
        
        /**
         * Returns the compilation unit for the given java element.
         * @param element the java element whose compilation unit is searched for
         * @return the compilation unit of the given java element
         */
-//     private static ICompilationUnit getCompilationUnit(IJavaElement element) {
-//             
-//             if (element == null)
-//                     return null;
-//                     
-//             if (element instanceof IMember)
-//                     return ((IMember) element).getCompilationUnit();
-//             
-//             int type= element.getElementType();
-//             if (IJavaElement.COMPILATION_UNIT == type)
-//                     return (ICompilationUnit) element;
-//             if (IJavaElement.CLASS_FILE == type)
-//                     return null;
-//                     
-//             return getCompilationUnit(element.getParent());
-//     }
+       private static ICompilationUnit getCompilationUnit(IJavaElement element) {
+               
+               if (element == null)
+                       return null;
+                       
+               if (element instanceof IMember)
+                       return ((IMember) element).getCompilationUnit();
+               
+               int type= element.getElementType();
+               if (IJavaElement.COMPILATION_UNIT == type)
+                       return (ICompilationUnit) element;
+               if (IJavaElement.CLASS_FILE == type)
+                       return null;
+                       
+               return getCompilationUnit(element.getParent());
+       }
        
        /** 
         * Returns the working copy of the given java element.
@@ -273,28 +282,28 @@ public class EditorUtility {
         * @param reconcile indicates whether the working copy must be reconcile prior to searching it
         * @return the working copy of the given element or <code>null</code> if none
         */     
-//     public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
-//             ICompilationUnit unit= getCompilationUnit(element);
-//             if (unit == null)
-//                     return null;
-//                     
-//             if (unit.isWorkingCopy())
-//                     return element;
-//                     
-//             ICompilationUnit workingCopy= getWorkingCopy(unit);
-//             if (workingCopy != null) {
-//                     if (reconcile) {
-//                             synchronized (workingCopy) {
-//                                     workingCopy.reconcile();
-//                                     return JavaModelUtil.findInCompilationUnit(workingCopy, element);
-//                             }
-//                     } else {
-//                                     return JavaModelUtil.findInCompilationUnit(workingCopy, element);
-//                     }
-//             }
-//             
-//             return null;
-//     }
+       public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
+               ICompilationUnit unit= getCompilationUnit(element);
+               if (unit == null)
+                       return null;
+                       
+               if (unit.isWorkingCopy())
+                       return element;
+                       
+               ICompilationUnit workingCopy= getWorkingCopy(unit);
+               if (workingCopy != null) {
+                       if (reconcile) {
+                               synchronized (workingCopy) {
+                                       workingCopy.reconcile();
+                                       return JavaModelUtil.findInCompilationUnit(workingCopy, element);
+                               }
+                       } else {
+                                       return JavaModelUtil.findInCompilationUnit(workingCopy, element);
+                       }
+               }
+               
+               return null;
+       }
 
        /**
         * Maps the localized modifier name to a code in the same
@@ -359,4 +368,29 @@ public class EditorUtility {
                        return newModifierString;
                return PHPEditorMessages.getFormattedString("EditorUtility.concatModifierStrings", new String[] {modifierString, newModifierString}); //$NON-NLS-1$
        }
+       
+       /**
+        * Returns the Java project for a given editor input or <code>null</code> if no corresponding
+        * Java project exists.
+        * 
+        * @param input the editor input
+        * @return the corresponding Java project
+        * 
+        * @since 3.0
+        */
+       public static IJavaProject getJavaProject(IEditorInput input) {
+               IJavaProject jProject= null;
+               if (input instanceof IFileEditorInput) {
+                       IProject project= ((IFileEditorInput)input).getFile().getProject();
+                       if (project != null) {
+                               jProject= JavaCore.create(project);
+                               if (!jProject.exists())
+                                       jProject= null;
+                       }
+               } 
+//             else if (input instanceof IClassFileEditorInput) {
+//                     jProject= ((IClassFileEditorInput)input).getClassFile().getJavaProject();
+//             }
+               return jProject;
+       }
 }