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;
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;
/**
* 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).
protected JavaModelOperation(IJavaElement[] elementsToProcess, IJavaElement[] parentElements, boolean force) {
fElementsToProcess = elementsToProcess;
fParentElements= parentElements;
- fForce= force;
+ this.force= force;
}
/**
* A common constructor for all Java Model operations.
*/
protected JavaModelOperation(IJavaElement[] elements, boolean force) {
fElementsToProcess = elements;
- fForce= force;
+ this.force= force;
}
/**
*/
protected JavaModelOperation(IJavaElement element, boolean force) {
fElementsToProcess = new IJavaElement[]{element};
- fForce= force;
+ this.force= force;
}
/*
* @see IProgressMonitor
*/
public void beginTask(String name, int totalWork) {
- if (fMonitor != null) {
- fMonitor.beginTask(name, totalWork);
+ if (progressMonitor != null) {
+ progressMonitor.beginTask(name, totalWork);
}
}
/**
* @see IProgressMonitor
*/
public void done() {
- if (fMonitor != null) {
- fMonitor.done();
+ if (progressMonitor != null) {
+ progressMonitor.done();
}
}
/*
*
* @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
*/
* 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;
}
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;
}
*/
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 {
*/
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();
} 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));
}
}
}
+ /**
+ * 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++];
* @see IProgressMonitor
*/
public void setCanceled(boolean b) {
- if (fMonitor != null) {
- fMonitor.setCanceled(b);
+ if (progressMonitor != null) {
+ progressMonitor.setCanceled(b);
}
}
/**
* @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);
}
}
/**
* @see IProgressMonitor
*/
public void worked(int work) {
- if (fMonitor != null) {
- fMonitor.worked(work);
+ if (progressMonitor != null) {
+ progressMonitor.worked(work);
checkCanceled();
}
}