Bug 479523 - Replace usage of SubProgressMonitor with SubMonitor in
eclipse.platform.text

Also using split for automatic cancelation checking


Change-Id: I0d78f0071a0b1d25683bd4404b489dd4334c6b5a
Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
diff --git a/org.eclipse.core.filebuffers/src/org/eclipse/core/filebuffers/manipulation/ContainerCreator.java b/org.eclipse.core.filebuffers/src/org/eclipse/core/filebuffers/manipulation/ContainerCreator.java
index 4dfbaaf..8045b48 100644
--- a/org.eclipse.core.filebuffers/src/org/eclipse/core/filebuffers/manipulation/ContainerCreator.java
+++ b/org.eclipse.core.filebuffers/src/org/eclipse/core/filebuffers/manipulation/ContainerCreator.java
@@ -20,7 +20,7 @@
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFolder;
@@ -68,7 +68,7 @@
 		IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
 			@Override
 			public void run(IProgressMonitor monitor) throws CoreException {
-				monitor.beginTask(FileBuffersMessages.ContainerCreator_task_creatingContainer, fContainerFullPath.segmentCount());
+				SubMonitor subMonitor = SubMonitor.convert(monitor, FileBuffersMessages.ContainerCreator_task_creatingContainer, fContainerFullPath.segmentCount());
 				if (fContainer != null)
 					return;
 
@@ -91,7 +91,7 @@
 					if (resource != null) {
 						if (resource instanceof IContainer) {
 							fContainer= (IContainer) resource;
-							monitor.worked(1);
+							subMonitor.step(1);
 						} else {
 							// fContainerFullPath specifies a file as directory
 							throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ContainerCreator_destinationMustBeAContainer, resource.getFullPath()), null));
@@ -100,15 +100,11 @@
 					else {
 						if (i == 0) {
 							IProject projectHandle= createProjectHandle(root, currentSegment);
-							IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1);
-							fContainer= createProject(projectHandle, subMonitor);
-							subMonitor.done();
+							fContainer= createProject(projectHandle, subMonitor.split(1));
 						}
 						else {
 							IFolder folderHandle= createFolderHandle(fContainer, currentSegment);
-							IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1);
-							fContainer= createFolder(folderHandle, subMonitor);
-							subMonitor.done();
+							fContainer= createFolder(folderHandle, subMonitor.split(1));
 						}
 					}
 				}
@@ -138,27 +134,9 @@
 	}
 
 	private IProject createProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
-		monitor.beginTask("", 100);//$NON-NLS-1$
-		try {
-
-			IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 50);
-			projectHandle.create(subMonitor);
-			subMonitor.done();
-
-			if (monitor.isCanceled())
-				throw new OperationCanceledException();
-
-			subMonitor= new SubProgressMonitor(monitor, 50);
-			projectHandle.open(subMonitor);
-			subMonitor.done();
-
-			if (monitor.isCanceled())
-				throw new OperationCanceledException();
-
-		} finally {
-			monitor.done();
-		}
-
+		SubMonitor subMonitor= SubMonitor.convert(monitor, 2);
+		projectHandle.create(subMonitor.split(1));
+		projectHandle.open(subMonitor.split(1));
 		return projectHandle;
 	}
 
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationAction.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationAction.java
index 0efd803..a7a0110 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationAction.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationAction.java
@@ -24,7 +24,7 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.jobs.Job;
 
 import org.eclipse.core.resources.IFile;
@@ -172,23 +172,19 @@
 				try {
 
 					int ticks= 100;
-					monitor.beginTask(fFileBufferOperation.getOperationName(), ticks);
-					try {
-						IPath[] locations;
-						if (files != null) {
-							ticks -= 30;
-							locations= generateLocations(files, new SubProgressMonitor(monitor, 30));
-						} else
-							locations= new IPath[] { location };
+					SubMonitor subMonitor= SubMonitor.convert(monitor, fFileBufferOperation.getOperationName(), ticks);
+					IPath[] locations;
+					if (files != null) {
+						ticks-= 30;
+						locations= generateLocations(files, subMonitor.split(30));
+					} else
+						locations= new IPath[] { location };
 
-						if (locations != null && locations.length > 0) {
-							FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
-							runner.execute(locations, fileBufferOperation, new SubProgressMonitor(monitor, ticks));
-						}
-						status= Status.OK_STATUS;
-					} finally {
-						monitor.done();
+					if (locations != null && locations.length > 0) {
+						FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
+						runner.execute(locations, fileBufferOperation, subMonitor.split(ticks));
 					}
+					status= Status.OK_STATUS;
 
 				} catch (OperationCanceledException e) {
 					status= new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationHandler.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationHandler.java
index 63ee227..7821fa7 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationHandler.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileBufferOperationHandler.java
@@ -28,7 +28,7 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.jobs.Job;
 
 import org.eclipse.core.resources.IFile;
@@ -57,7 +57,7 @@
  * @since 3.1
  */
 public class FileBufferOperationHandler extends AbstractHandler {
-
+	
 	private IFileBufferOperation fFileBufferOperation;
 	private IWorkbenchWindow fWindow;
 	private IResource[] fResources;
@@ -195,23 +195,19 @@
 				try {
 
 					int ticks= 100;
-					monitor.beginTask(fFileBufferOperation.getOperationName(), ticks);
-					try {
-						IPath[] locations;
-						if (files != null) {
-							ticks -= 30;
-							locations= generateLocations(files, new SubProgressMonitor(monitor, 30));
-						} else
-							locations= new IPath[] { location };
+					SubMonitor subMonitor= SubMonitor.convert(monitor, fFileBufferOperation.getOperationName(), ticks);
+					IPath[] locations;
+					if (files != null) {
+						ticks-= 30;
+						locations= generateLocations(files, subMonitor.split(30));
+					} else
+						locations= new IPath[] { location };
 
-						if (locations != null && locations.length > 0) {
-							FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
-							runner.execute(locations, fileBufferOperation, new SubProgressMonitor(monitor, ticks));
-						}
-						status= Status.OK_STATUS;
-					} finally {
-						monitor.done();
+					if (locations != null && locations.length > 0) {
+						FileBufferOperationRunner runner= new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
+						runner.execute(locations, fileBufferOperation, subMonitor.split(ticks));
 					}
+					status= Status.OK_STATUS;
 
 				} catch (OperationCanceledException e) {
 					status= new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java
index 61e8695..7307e60 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java
@@ -37,7 +37,7 @@
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.QualifiedName;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.content.IContentDescription;
 import org.eclipse.core.runtime.content.IContentType;
 import org.eclipse.core.runtime.jobs.ISchedulingRule;
@@ -639,15 +639,10 @@
 				}
 
 			} else {
-				try {
-					monitor.beginTask(TextEditorMessages.FileDocumentProvider_task_saving, 2000);
-					ContainerCreator creator = new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
-					creator.createContainer(new SubProgressMonitor(monitor, 1000));
-					file.create(stream, false, new SubProgressMonitor(monitor, 1000));
-				}
-				finally {
-					monitor.done();
-				}
+				SubMonitor subMonitor= SubMonitor.convert(monitor, TextEditorMessages.FileDocumentProvider_task_saving, 2);
+				ContainerCreator creator= new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
+				creator.createContainer(subMonitor.split(1));
+				file.create(stream, false, subMonitor.split(1));
 			}
 
 		} else {