Fixed bugs item #1240435
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / EditorUtility.java
index b593805..b951472 100644 (file)
@@ -14,14 +14,17 @@ 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;
@@ -33,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;
 
@@ -66,7 +70,7 @@ public class EditorUtility {
                IEditorInput input= null;
                
                try {
-                       input= getEditorInput(inputElement);
+                       input = getEditorInput(inputElement);
                } catch (JavaModelException x) {
                        PHPeclipsePlugin.log(x.getStatus());
                }
@@ -121,15 +125,15 @@ public class EditorUtility {
        }
        
        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 {
@@ -364,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;
+       }
 }