3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaModelOperation.java
index a04b4f6..a780950 100644 (file)
@@ -24,6 +24,7 @@ import net.sourceforge.phpdt.core.IPackageFragment;
 import net.sourceforge.phpdt.core.IWorkingCopy;
 import net.sourceforge.phpdt.core.JavaModelException;
 import net.sourceforge.phpdt.internal.core.util.PerThreadObject;
+import net.sourceforge.phpdt.internal.core.util.Util;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
@@ -32,12 +33,16 @@ import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IResourceStatus;
 import org.eclipse.core.resources.IWorkspace;
 import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
+import net.sourceforge.phpdt.internal.core.DeltaProcessor;
+import net.sourceforge.phpdt.internal.core.JavaModelManager;
 
 
 /**
@@ -100,27 +105,27 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * empty result if no elements are created, or if this
         * operation is not actually executed.
         */
-       protected static IJavaElement[] fgEmptyResult= new IJavaElement[] {};
+       protected static IJavaElement[] NO_ELEMENTS= new IJavaElement[] {};
 
 
        /**
         * The elements created by this operation - empty
         * until the operation actually creates elements.
         */
-       protected IJavaElement[] fResultElements= fgEmptyResult;
+       protected IJavaElement[] resultElements= NO_ELEMENTS;
 
        /**
         * The progress monitor passed into this operation
         */
-       protected IProgressMonitor fMonitor= null;
+       protected IProgressMonitor progressMonitor= null;
        /**
         * A flag indicating whether this operation is nested.
         */
-       protected boolean fNested = false;
+       protected boolean isNested = false;
        /**
         * Conflict resolution policy - by default do not force (fail on a conflict).
         */
-       protected boolean fForce= false;
+       protected boolean force= false;
 
        /*
         * A per thread stack of java model operations (PerThreadObject of ArrayList).
@@ -147,14 +152,14 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
        protected JavaModelOperation(IJavaElement[] elementsToProcess, IJavaElement[] parentElements, boolean force) {
                fElementsToProcess = elementsToProcess;
                fParentElements= parentElements;
-               fForce= force;
+               force= force;
        }
        /**
         * A common constructor for all Java Model operations.
         */
        protected JavaModelOperation(IJavaElement[] elements, boolean force) {
                fElementsToProcess = elements;
-               fForce= force;
+               force= force;
        }
        
        /**
@@ -168,7 +173,7 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         */
        protected JavaModelOperation(IJavaElement element, boolean force) {
                fElementsToProcess = new IJavaElement[]{element};
-               fForce= force;
+               force= force;
        }
        
        /*
@@ -213,8 +218,8 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * @see IProgressMonitor
         */
        public void beginTask(String name, int totalWork) {
-               if (fMonitor != null) {
-                       fMonitor.beginTask(name, totalWork);
+               if (progressMonitor != null) {
+                       progressMonitor.beginTask(name, totalWork);
                }
        }
        /**
@@ -352,8 +357,8 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * @see IProgressMonitor
         */
        public void done() {
-               if (fMonitor != null) {
-                       fMonitor.done();
+               if (progressMonitor != null) {
+                       progressMonitor.done();
                }
        }
        /*
@@ -374,20 +379,21 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         *
         * @exception JavaModelException The operation has failed.
         */
-       protected void execute() throws JavaModelException {
-               IJavaModelStatus status= verify();
-               if (status.isOK()) {
-                       // if first time here, computes the root infos before executing the operation
-                       DeltaProcessor deltaProcessor = JavaModelManager.getJavaModelManager().deltaProcessor;
-                       if (deltaProcessor.roots == null) {
-//                     TODO khartlage temp-del
-                         deltaProcessor.initializeRoots();
-                       }
-                       executeOperation();
-               } else {
-                       throw new JavaModelException(status);
-               }
-       }
+//     protected void execute() throws JavaModelException {
+//             IJavaModelStatus status= verify();
+//             if (status.isOK()) {
+//                     // if first time here, computes the root infos before executing the operation
+//                     DeltaProcessor deltaProcessor = JavaModelManager.getJavaModelManager().deltaProcessor;
+//                     if (deltaProcessor.roots == null) {
+////                   TODO khartlage temp-del
+//                       deltaProcessor.initializeRoots();
+//                     }
+//                     executeOperation();
+//             } else {
+//                     throw new JavaModelException(status);
+//             }
+//     }
+       
        /**
         * Convenience method to run an operation within this operation
         */
@@ -516,15 +522,23 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * Returns the elements created by this operation.
         */
        public IJavaElement[] getResultElements() {
-               return fResultElements;
+               return resultElements;
+       }
+       /*
+        * Returns the scheduling rule for this operation (i.e. the resource that needs to be locked 
+        * while this operation is running.
+        * Subclasses can override.
+        */
+       protected ISchedulingRule getSchedulingRule() {
+               return ResourcesPlugin.getWorkspace().getRoot();
        }
        /**
         * Creates and returns a subprogress monitor if appropriate.
         */
        protected IProgressMonitor getSubProgressMonitor(int workAmount) {
                IProgressMonitor sub = null;
-               if (fMonitor != null) {
-                       sub = new SubProgressMonitor(fMonitor, workAmount, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
+               if (progressMonitor != null) {
+                       sub = new SubProgressMonitor(progressMonitor, workAmount, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
                }
                return sub;
        }
@@ -537,16 +551,16 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
                return !this.isReadOnly() && this.getAttribute(HAS_MODIFIED_RESOURCE_ATTR) == TRUE; 
        }
        public void internalWorked(double work) {
-               if (fMonitor != null) {
-                       fMonitor.internalWorked(work);
+               if (progressMonitor != null) {
+                       progressMonitor.internalWorked(work);
                }
        }
        /**
         * @see IProgressMonitor
         */
        public boolean isCanceled() {
-               if (fMonitor != null) {
-                       return fMonitor.isCanceled();
+               if (progressMonitor != null) {
+                       return progressMonitor.isCanceled();
                }
                return false;
        }
@@ -584,8 +598,8 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         */
        protected void moveResources(IResource[] resources, IPath destinationPath) throws JavaModelException {
                IProgressMonitor subProgressMonitor = null;
-               if (fMonitor != null) {
-                       subProgressMonitor = new SubProgressMonitor(fMonitor, resources.length, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
+               if (progressMonitor != null) {
+                       subProgressMonitor = new SubProgressMonitor(progressMonitor, resources.length, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
                }
                IWorkspace workspace = resources[0].getWorkspace();
                try {
@@ -697,12 +711,17 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         */
        public void run(IProgressMonitor monitor) throws CoreException {
                JavaModelManager manager = JavaModelManager.getJavaModelManager();
-               int previousDeltaCount = manager.javaModelDeltas.size();
+               DeltaProcessor deltaProcessor = manager.getDeltaProcessor();
+               int previousDeltaCount = deltaProcessor.javaModelDeltas.size();
                try {
-                       fMonitor = monitor;
+                       progressMonitor = monitor;
                        pushOperation(this);
                        try {
-                               this.execute();
+                               // computes the root infos before executing the operation
+                               // noop if aready initialized
+                               JavaModelManager.getJavaModelManager().deltaState.initializeRoots();
+                               
+                               executeOperation();
                        } finally {
                                if (this.isTopLevelOperation()) {
                                        this.runPostActions();
@@ -711,7 +730,7 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
                } finally {
 
                        try {
-//                             TODO khartlage temp-del
+//                             TODO jsurfer temp-del
                                // update JavaModel using deltas that were recorded during this operation
 //                             for (int i = previousDeltaCount, size = manager.javaModelDeltas.size(); i < size; i++) {
 //                                     manager.updateJavaModel((IJavaElementDelta)manager.javaModelDeltas.get(i));
@@ -732,6 +751,38 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
                        } 
                }
        }
+       /**
+        * Main entry point for Java Model operations. Runs a Java Model Operation as an IWorkspaceRunnable
+        * if not read-only.
+        */
+       public void runOperation(IProgressMonitor monitor) throws JavaModelException {
+               IJavaModelStatus status= verify();
+               if (!status.isOK()) {
+                       throw new JavaModelException(status);
+               }
+               try {
+                       if (isReadOnly()) {
+                               run(monitor);
+                       } else {
+                               // Use IWorkspace.run(...) to ensure that a build will be done in autobuild mode.
+                               // Note that if the tree is locked, this will throw a CoreException, but this is ok
+                               // as this operation is modifying the tree (not read-only) and a CoreException will be thrown anyway.
+                ResourcesPlugin.getWorkspace().run(this, getSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
+                       }
+               } catch (CoreException ce) {
+                       if (ce instanceof JavaModelException) {
+                               throw (JavaModelException)ce;
+                       } else {
+                               if (ce.getStatus().getCode() == IResourceStatus.OPERATION_FAILED) {
+                                       Throwable e= ce.getStatus().getException();
+                                       if (e instanceof JavaModelException) {
+                                               throw (JavaModelException) e;
+                                       }
+                               }
+                               throw new JavaModelException(ce);
+                       }
+               }
+       }
        protected void runPostActions() throws JavaModelException {
                while (this.actionsStart <= this.actionsEnd) {
                        IPostAction postAction = this.actions[this.actionsStart++];
@@ -755,8 +806,8 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * @see IProgressMonitor
         */
        public void setCanceled(boolean b) {
-               if (fMonitor != null) {
-                       fMonitor.setCanceled(b);
+               if (progressMonitor != null) {
+                       progressMonitor.setCanceled(b);
                }
        }
        /**
@@ -764,22 +815,22 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * @see CreateElementInCUOperation#checkCanceled
         */
        protected void setNested(boolean nested) {
-               fNested = nested;
+               isNested = nested;
        }
        /**
         * @see IProgressMonitor
         */
        public void setTaskName(String name) {
-               if (fMonitor != null) {
-                       fMonitor.setTaskName(name);
+               if (progressMonitor != null) {
+                       progressMonitor.setTaskName(name);
                }
        }
        /**
         * @see IProgressMonitor
         */
        public void subTask(String name) {
-               if (fMonitor != null) {
-                       fMonitor.subTask(name);
+               if (progressMonitor != null) {
+                       progressMonitor.subTask(name);
                }
        }
        /**
@@ -800,8 +851,8 @@ public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgres
         * @see IProgressMonitor
         */
        public void worked(int work) {
-               if (fMonitor != null) {
-                       fMonitor.worked(work);
+               if (progressMonitor != null) {
+                       progressMonitor.worked(work);
                        checkCanceled();
                }
        }