new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / EditorUtility.java
index 2cff713..b593805 100644 (file)
 package net.sourceforge.phpeclipse.phpeditor;
 
 
+import net.sourceforge.phpdt.core.ICompilationUnit;
 import net.sourceforge.phpdt.core.IJavaElement;
+import net.sourceforge.phpdt.core.IMember;
+import net.sourceforge.phpdt.core.IWorkingCopy;
 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.IResource;
 import org.eclipse.jface.action.Action;
 import org.eclipse.swt.SWT;
 import org.eclipse.ui.IEditorDescriptor;
@@ -43,11 +49,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 +65,11 @@ public class EditorUtility {
        public static IEditorPart isOpenInEditor(Object inputElement) {
                IEditorInput input= null;
                
-//             try {
+               try {
                        input= getEditorInput(inputElement);
-//             } catch (JavaModelException x) {
-//                     JavaPlugin.log(x.getStatus());
-//             }
+               } catch (JavaModelException x) {
+                       PHPeclipsePlugin.log(x.getStatus());
+               }
                
                if (input != null) {
                        IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
@@ -108,11 +114,11 @@ 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) {
@@ -159,29 +165,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 +224,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 +239,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 +278,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