diff options
| author | Stefan Xenos | 2015-09-14 20:19:54 +0000 |
|---|---|---|
| committer | Stefan Xenos | 2015-09-15 16:18:30 +0000 |
| commit | ea9d71a488dcd539b8b3a84d7ddc8b2acbca7bf5 (patch) | |
| tree | de3983a433361d81660727db9111c030433a306c | |
| parent | 6c7e1896954772f2aa54c41be2bb0eccea2fbe32 (diff) | |
| download | eclipse.platform.ui-ea9d71a488dcd539b8b3a84d7ddc8b2acbca7bf5.tar.gz eclipse.platform.ui-ea9d71a488dcd539b8b3a84d7ddc8b2acbca7bf5.tar.xz eclipse.platform.ui-ea9d71a488dcd539b8b3a84d7ddc8b2acbca7bf5.zip | |
Bug 475785 - Remove usage of SubProgressMonitor in org.eclipse.ui.ide.undo
Change-Id: I3c7f95dc73e7d690b63d8134536a06e2a4422ecc
Signed-off-by: Stefan Xenos <sxenos@gmail.com>
4 files changed, 177 insertions, 263 deletions
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyProjectOperation.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyProjectOperation.java index 8053a11a7c8..c515118dad1 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyProjectOperation.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyProjectOperation.java @@ -27,7 +27,7 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.ui.internal.ide.undo.ProjectDescription; import org.eclipse.ui.internal.ide.undo.UndoMessages; @@ -130,9 +130,9 @@ public class CopyProjectOperation extends AbstractCopyOrMoveResourcesOperation { @Override protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException { + SubMonitor subMonitor = SubMonitor.convert(monitor, 1); // Delete the project that was copied - WorkspaceUndoUtil.delete(resources, new SubProgressMonitor(monitor, 1), - uiInfo, true); + WorkspaceUndoUtil.delete(resources, subMonitor.newChild(1), uiInfo, true); // Set the target resource to the original setTargetResources(new IResource[] { originalProject }); setResourceDescriptions(new ResourceDescription[0]); diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyResourcesOperation.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyResourcesOperation.java index 8c4617c218e..350de9925b9 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyResourcesOperation.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/CopyResourcesOperation.java @@ -21,7 +21,7 @@ import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.ui.internal.ide.undo.UndoMessages; /** @@ -140,9 +140,9 @@ public class CopyResourcesOperation extends protected void copy(IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException { - monitor.beginTask("", 2000); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); + SubMonitor subMonitor = SubMonitor.convert(monitor, + resources.length + (resourceDescriptions != null ? resourceDescriptions.length : 0)); + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); List resourcesAtDestination = new ArrayList(); List overwrittenResources = new ArrayList(); @@ -150,11 +150,8 @@ public class CopyResourcesOperation extends // Copy the resources and record the overwrites that would // be restored if this operation were reversed ResourceDescription[] overwrites; - overwrites = WorkspaceUndoUtil.copy( - new IResource[] { resources[i] }, getDestinationPath( - resources[i], i), resourcesAtDestination, - new SubProgressMonitor(monitor, 1000 / resources.length), - uiInfo, true, fCreateGroups, fCreateLinks, + overwrites = WorkspaceUndoUtil.copy(new IResource[] { resources[i] }, getDestinationPath(resources[i], i), + resourcesAtDestination, subMonitor.newChild(1), uiInfo, true, fCreateGroups, fCreateLinks, fRelativeToVariable); // Accumulate the overwrites into the full list for (int j = 0; j < overwrites.length; j++) { @@ -166,9 +163,7 @@ public class CopyResourcesOperation extends if (resourceDescriptions != null) { for (int i = 0; i < resourceDescriptions.length; i++) { if (resourceDescriptions[i] != null) { - resourceDescriptions[i] - .createResource(new SubProgressMonitor(monitor, - 1000 / resourceDescriptions.length)); + resourceDescriptions[i].createResource(subMonitor.newChild(1)); } } } @@ -181,7 +176,6 @@ public class CopyResourcesOperation extends // location. setTargetResources((IResource[]) resourcesAtDestination .toArray(new IResource[resourcesAtDestination.size()])); - monitor.done(); } /* @@ -191,15 +185,12 @@ public class CopyResourcesOperation extends @Override protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException { - monitor.beginTask("", 2); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); + SubMonitor subMonitor = SubMonitor.convert(monitor, 2); + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); // undoing a copy is first deleting the copied resources... - WorkspaceUndoUtil.delete(resources, new SubProgressMonitor(monitor, 1), - uiInfo, true); + WorkspaceUndoUtil.delete(resources, subMonitor.newChild(1), uiInfo, true); // then restoring any overwritten by the previous copy... - WorkspaceUndoUtil.recreate(resourceDescriptions, - new SubProgressMonitor(monitor, 1), uiInfo); + WorkspaceUndoUtil.recreate(resourceDescriptions, subMonitor.newChild(1), uiInfo); setResourceDescriptions(new ResourceDescription[0]); // then setting the target resources back to the original ones. // Note that the destination paths never changed since they diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/MoveResourcesOperation.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/MoveResourcesOperation.java index f4ab6c40821..0b2f8413ae0 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/MoveResourcesOperation.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/MoveResourcesOperation.java @@ -21,7 +21,7 @@ import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.ui.internal.ide.undo.UndoMessages; /** @@ -119,10 +119,9 @@ public class MoveResourcesOperation extends */ protected void move(IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException { - - monitor.beginTask("", 2000); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_MovingResources); + SubMonitor subMonitor = SubMonitor.convert(monitor, + resources.length + (resourceDescriptions != null ? resourceDescriptions.length : 0)); + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_MovingResources); List resourcesAtDestination = new ArrayList(); List undoDestinationPaths = new ArrayList(); List overwrittenResources = new ArrayList(); @@ -131,11 +130,8 @@ public class MoveResourcesOperation extends // Move the resources and record the overwrites that would // be restored if this operation were reversed ResourceDescription[] overwrites; - overwrites = WorkspaceUndoUtil.move( - new IResource[] { resources[i] }, getDestinationPath( - resources[i], i), resourcesAtDestination, - undoDestinationPaths, new SubProgressMonitor(monitor, - 1000 / resources.length), uiInfo, true); + overwrites = WorkspaceUndoUtil.move(new IResource[] { resources[i] }, getDestinationPath(resources[i], i), + resourcesAtDestination, undoDestinationPaths, subMonitor.newChild(1), uiInfo, true); // Accumulate the overwrites into the full list for (int j = 0; j < overwrites.length; j++) { @@ -147,9 +143,7 @@ public class MoveResourcesOperation extends if (resourceDescriptions != null) { for (int i = 0; i < resourceDescriptions.length; i++) { if (resourceDescriptions[i] != null) { - resourceDescriptions[i] - .createResource(new SubProgressMonitor(monitor, - 1000 / resourceDescriptions.length)); + resourceDescriptions[i].createResource(subMonitor.newChild(1)); } } } @@ -166,8 +160,6 @@ public class MoveResourcesOperation extends destinationPaths = (IPath[]) undoDestinationPaths .toArray(new IPath[undoDestinationPaths.size()]); destination = null; - - monitor.done(); } /* diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/WorkspaceUndoUtil.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/WorkspaceUndoUtil.java index c9e05bd8566..eca0776c73c 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/WorkspaceUndoUtil.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/undo/WorkspaceUndoUtil.java @@ -38,7 +38,7 @@ import org.eclipse.core.runtime.MultiStatus; import org.eclipse.core.runtime.NullProgressMonitor; 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.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.osgi.util.NLS; @@ -175,63 +175,53 @@ public class WorkspaceUndoUtil { * @throws CoreException * propagates any CoreExceptions thrown from the resources API */ - static ResourceDescription[] delete(IResource[] resourcesToDelete, - IProgressMonitor monitor, IAdaptable uiInfo, boolean deleteContent) - throws CoreException { - final List exceptions = new ArrayList(); + static ResourceDescription[] delete(IResource[] resourcesToDelete, IProgressMonitor mon, IAdaptable uiInfo, + boolean deleteContent) throws CoreException { + SubMonitor subMonitor = SubMonitor.convert(mon, resourcesToDelete.length); + + final List<CoreException> exceptions = new ArrayList<>(); boolean forceOutOfSyncDelete = false; ResourceDescription[] returnedResourceDescriptions = new ResourceDescription[resourcesToDelete.length]; - monitor.beginTask("", resourcesToDelete.length); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_DeleteResourcesProgress); - try { - for (int i = 0; i < resourcesToDelete.length; ++i) { - if (monitor.isCanceled()) { - throw new OperationCanceledException(); - } - IResource resource = resourcesToDelete[i]; - try { - returnedResourceDescriptions[i] = delete(resource, - new SubProgressMonitor(monitor, 1), uiInfo, - forceOutOfSyncDelete, deleteContent); - } catch (CoreException e) { - if (resource.getType() == IResource.FILE) { - IStatus[] children = e.getStatus().getChildren(); - if (children.length == 1 - && children[0].getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) { - int result = queryDeleteOutOfSync(resource, uiInfo); - - if (result == IDialogConstants.YES_ID) { - // retry the delete with a force out of sync - delete(resource, new SubProgressMonitor( - monitor, 1), uiInfo, true, - deleteContent); - } else if (result == IDialogConstants.YES_TO_ALL_ID) { - // all future attempts should force out of - // sync - forceOutOfSyncDelete = true; - delete(resource, new SubProgressMonitor( - monitor, 1), uiInfo, - forceOutOfSyncDelete, deleteContent); - } else if (result == IDialogConstants.CANCEL_ID) { - throw new OperationCanceledException(); - } else { - exceptions.add(e); - } + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_DeleteResourcesProgress); + for (int i = 0; i < resourcesToDelete.length; ++i) { + if (subMonitor.isCanceled()) { + throw new OperationCanceledException(); + } + IResource resource = resourcesToDelete[i]; + try { + returnedResourceDescriptions[i] = delete(resource, subMonitor.newChild(1), uiInfo, + forceOutOfSyncDelete, deleteContent); + } catch (CoreException e) { + if (resource.getType() == IResource.FILE) { + IStatus[] children = e.getStatus().getChildren(); + if (children.length == 1 && children[0].getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) { + int result = queryDeleteOutOfSync(resource, uiInfo); + + if (result == IDialogConstants.YES_ID) { + // retry the delete with a force out of sync + delete(resource, subMonitor.newChild(1), uiInfo, true, deleteContent); + } else if (result == IDialogConstants.YES_TO_ALL_ID) { + // all future attempts should force out of + // sync + forceOutOfSyncDelete = true; + delete(resource, subMonitor.newChild(1), uiInfo, forceOutOfSyncDelete, + deleteContent); + } else if (result == IDialogConstants.CANCEL_ID) { + throw new OperationCanceledException(); } else { exceptions.add(e); } } else { exceptions.add(e); } + } else { + exceptions.add(e); } } - IStatus result = createResult(exceptions); - if (!result.isOK()) { - throw new CoreException(result); - } - } finally { - monitor.done(); + } + IStatus result = createResult(exceptions); + if (!result.isOK()) { + throw new CoreException(result); } return returnedResourceDescriptions; } @@ -267,9 +257,8 @@ public class WorkspaceUndoUtil { * @throws CoreException * propagates any CoreExceptions thrown from the resources API */ - static ResourceDescription[] copy(IResource[] resources, IPath destination, - List resourcesAtDestination, IProgressMonitor monitor, - IAdaptable uiInfo, boolean pathIncludesName) throws CoreException { + static ResourceDescription[] copy(IResource[] resources, IPath destination, List<IResource> resourcesAtDestination, + IProgressMonitor monitor, IAdaptable uiInfo, boolean pathIncludesName) throws CoreException { return copy(resources, destination, resourcesAtDestination, monitor, uiInfo, pathIncludesName, false, false, null); } @@ -318,18 +307,14 @@ public class WorkspaceUndoUtil { * @throws CoreException * propagates any CoreExceptions thrown from the resources API */ - static ResourceDescription[] copy(IResource[] resources, IPath destination, - List resourcesAtDestination, IProgressMonitor monitor, - IAdaptable uiInfo, boolean pathIncludesName, boolean createVirtual, - boolean createLinks, String relativeToVariable) - throws CoreException { - - monitor.beginTask("", resources.length); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); - List overwrittenResources = new ArrayList(); - for (int i = 0; i < resources.length; i++) { - IResource source = resources[i]; + static ResourceDescription[] copy(IResource[] resources, IPath destination, List<IResource> resourcesAtDestination, + IProgressMonitor monitor, IAdaptable uiInfo, boolean pathIncludesName, boolean createVirtual, + boolean createLinks, String relativeToVariable) throws CoreException { + SubMonitor subMonitor = SubMonitor.convert(monitor, resources.length); + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); + List<ResourceDescription> overwrittenResources = new ArrayList<>(); + for (IResource source : resources) { + SubMonitor iterationProgress = subMonitor.newChild(1).setWorkRemaining(100); IPath destinationPath; if (pathIncludesName) { destinationPath = destination; @@ -351,7 +336,7 @@ public class WorkspaceUndoUtil { children = filterNonLinkedResources(children); ResourceDescription[] overwritten = copy(children, destinationPath, resourcesAtDestination, - new SubProgressMonitor(monitor, 1), uiInfo, false, + iterationProgress, uiInfo, false, createVirtual, createLinks, relativeToVariable); // We don't record the copy since this recursive call will // do so. Just record the overwrites. @@ -361,37 +346,28 @@ public class WorkspaceUndoUtil { } else { // delete the destination folder, copying a linked folder // over an unlinked one or vice versa. Fixes bug 28772. - ResourceDescription[] deleted = delete( - new IResource[] { existing }, - new SubProgressMonitor(monitor, 0), uiInfo, false); - if ((createLinks || createVirtual) - && (source.isLinked() == false) + ResourceDescription[] deleted = delete(new IResource[] { existing }, iterationProgress.newChild(1), + uiInfo, false); + iterationProgress.setWorkRemaining(100); + if ((createLinks || createVirtual) && (source.isLinked() == false) && (source.isVirtual() == false)) { IFolder folder = workspaceRoot.getFolder(destinationPath); if (createVirtual) { - folder.create(IResource.VIRTUAL, true, - new SubProgressMonitor(monitor, 1)); - IResource[] members = ((IContainer) source) - .members(); + folder.create(IResource.VIRTUAL, true, iterationProgress.newChild(1)); + IResource[] members = ((IContainer) source).members(); if (members.length > 0) { - overwrittenResources.addAll(Arrays.asList(copy( - members, destinationPath, - resourcesAtDestination, - new SubProgressMonitor(monitor, 1), - uiInfo, false, createVirtual, - createLinks, relativeToVariable))); + overwrittenResources.addAll(Arrays.asList(copy(members, destinationPath, + resourcesAtDestination, iterationProgress.newChild(99), uiInfo, false, + createVirtual, createLinks, relativeToVariable))); } } else - folder.createLink(createRelativePath(source - .getLocationURI(), relativeToVariable, folder), 0, - new SubProgressMonitor(monitor, 1)); + folder.createLink(createRelativePath(source.getLocationURI(), relativeToVariable, folder), + 0, iterationProgress.newChild(100)); } else - source.copy(destinationPath, IResource.SHALLOW, - new SubProgressMonitor(monitor, 1)); + source.copy(destinationPath, IResource.SHALLOW, iterationProgress.newChild(100)); // Record the copy - resourcesAtDestination.add(getWorkspace().getRoot() - .findMember(destinationPath)); + resourcesAtDestination.add(getWorkspace().getRoot().findMember(destinationPath)); for (int j = 0; j < deleted.length; j++) { overwrittenResources.add(deleted[j]); } @@ -405,37 +381,29 @@ public class WorkspaceUndoUtil { // destination ResourceDescription[] deleted = delete( new IResource[] { existing }, - new SubProgressMonitor(monitor, 0), uiInfo, + iterationProgress.newChild(1), uiInfo, false); + iterationProgress.setWorkRemaining(100); if (source.getType() == IResource.FILE) { IFile file = workspaceRoot.getFile(destinationPath); file.createLink(createRelativePath( source.getLocationURI(), relativeToVariable, file), 0, - new SubProgressMonitor(monitor, 1)); + iterationProgress.newChild(100)); } else { IFolder folder = workspaceRoot .getFolder(destinationPath); if (createVirtual) { - folder.create(IResource.VIRTUAL, true, - new SubProgressMonitor(monitor, 1)); - IResource[] members = ((IContainer) source) - .members(); + folder.create(IResource.VIRTUAL, true, iterationProgress.newChild(1)); + IResource[] members = ((IContainer) source).members(); if (members.length > 0) { - overwrittenResources.addAll(Arrays - .asList(copy(members, - destinationPath, - resourcesAtDestination, - new SubProgressMonitor( - monitor, 1), - uiInfo, false, - createVirtual, createLinks, - relativeToVariable))); - + overwrittenResources.addAll(Arrays.asList(copy(members, destinationPath, + resourcesAtDestination, iterationProgress.newChild(99), uiInfo, false, + createVirtual, createLinks, relativeToVariable))); } } else - folder.createLink(createRelativePath( - source.getLocationURI(), relativeToVariable, folder), - 0, new SubProgressMonitor(monitor, 1)); + folder.createLink( + createRelativePath(source.getLocationURI(), relativeToVariable, folder), 0, + iterationProgress.newChild(100)); } resourcesAtDestination.add(getWorkspace().getRoot() .findMember(destinationPath)); @@ -444,9 +412,8 @@ public class WorkspaceUndoUtil { } } else { if (source.isLinked() == existing.isLinked()) { - overwrittenResources.add(copyOverExistingResource( - source, existing, new SubProgressMonitor( - monitor, 1), uiInfo, false)); + overwrittenResources.add(copyOverExistingResource(source, existing, + iterationProgress.newChild(100), uiInfo, false)); // Record the "copy" resourcesAtDestination.add(existing); } else { @@ -455,10 +422,9 @@ public class WorkspaceUndoUtil { // 28772. ResourceDescription[] deleted = delete( new IResource[] { existing }, - new SubProgressMonitor(monitor, 0), uiInfo, + iterationProgress.newChild(1), uiInfo, false); - source.copy(destinationPath, IResource.SHALLOW, - new SubProgressMonitor(monitor, 1)); + source.copy(destinationPath, IResource.SHALLOW, iterationProgress.newChild(99)); // Record the copy resourcesAtDestination.add(getWorkspace().getRoot() .findMember(destinationPath)); @@ -482,33 +448,25 @@ public class WorkspaceUndoUtil { IFile file = workspaceRoot.getFile(destinationPath); file.createLink(createRelativePath( source.getLocationURI(), relativeToVariable, file), 0, - new SubProgressMonitor(monitor, 1)); + iterationProgress.newChild(100)); } else { IFolder folder = workspaceRoot .getFolder(destinationPath); if (createVirtual) { - folder.create(IResource.VIRTUAL, true, - new SubProgressMonitor(monitor, 1)); + folder.create(IResource.VIRTUAL, true, iterationProgress.newChild(1)); IResource[] members = ((IContainer) source).members(); if (members.length > 0) { - overwrittenResources.addAll(Arrays - .asList(copy(members, - destinationPath, - resourcesAtDestination, - new SubProgressMonitor( - monitor, 1), - uiInfo, false, - createVirtual, createLinks, - relativeToVariable))); - + overwrittenResources.addAll(Arrays.asList(copy(members, destinationPath, + resourcesAtDestination, iterationProgress.newChild(99), uiInfo, false, + createVirtual, createLinks, relativeToVariable))); } } else - folder.createLink(createRelativePath(source.getLocationURI(), relativeToVariable, folder), - 0, new SubProgressMonitor(monitor, 1)); + folder.createLink( + createRelativePath(source.getLocationURI(), relativeToVariable, folder), 0, + iterationProgress.newChild(100)); } } else - source.copy(destinationPath, IResource.SHALLOW, - new SubProgressMonitor(monitor, 1)); + source.copy(destinationPath, IResource.SHALLOW, iterationProgress.newChild(100)); // Record the copy. If we had to generate a parent // folder, that should be recorded as part of the copy if (generatedParent == null) { @@ -519,13 +477,12 @@ public class WorkspaceUndoUtil { } } - if (monitor.isCanceled()) { + if (subMonitor.isCanceled()) { throw new OperationCanceledException(); } } } - monitor.done(); - return (ResourceDescription[]) overwrittenResources + return overwrittenResources .toArray(new ResourceDescription[overwrittenResources.size()]); } @@ -564,13 +521,13 @@ public class WorkspaceUndoUtil { * A list used to record each moved resource. * @param reverseDestinations * A list used to record each moved resource's original location - * @param monitor + * @param mon * the progress monitor used to show progress * @param uiInfo - * the IAdaptable (or <code>null</code>) provided by the - * caller in order to supply UI information for prompting the - * user if necessary. When this parameter is not - * <code>null</code>, it contains an adapter for the + * the IAdaptable (or <code>null</code>) provided by the caller + * in order to supply UI information for prompting the user if + * necessary. When this parameter is not <code>null</code>, it + * contains an adapter for the * org.eclipse.swt.widgets.Shell.class * @param pathIncludesName * a boolean that indicates whether the specified path includes @@ -584,16 +541,15 @@ public class WorkspaceUndoUtil { * @throws CoreException * propagates any CoreExceptions thrown from the resources API */ - static ResourceDescription[] move(IResource[] resources, IPath destination, - List resourcesAtDestination, List reverseDestinations, - IProgressMonitor monitor, IAdaptable uiInfo, - boolean pathIncludesName) throws CoreException { + static ResourceDescription[] move(IResource[] resources, IPath destination, List<IResource> resourcesAtDestination, + List<IPath> reverseDestinations, IProgressMonitor mon, IAdaptable uiInfo, boolean pathIncludesName) + throws CoreException { - monitor.beginTask("", resources.length); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_MovingResources); - List overwrittenResources = new ArrayList(); + SubMonitor subMonitor = SubMonitor.convert(mon, resources.length); + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_MovingResources); + List<ResourceDescription> overwrittenResources = new ArrayList<>(); for (int i = 0; i < resources.length; i++) { + SubMonitor iterationProgress = subMonitor.newChild(1); IResource source = resources[i]; IPath destinationPath; if (pathIncludesName) { @@ -607,36 +563,31 @@ public class WorkspaceUndoUtil { // The resource is a folder and it exists in the destination. // Move its children to the existing destination. if (source.isLinked() == existing.isLinked()) { - IResource[] children = ((IContainer) source).members(); - // move only linked resource children (267173) - if (source.isLinked() && source.getLocation().equals(existing.getLocation())) - children = filterNonLinkedResources(children); - ResourceDescription[] overwritten = move(children, - destinationPath, resourcesAtDestination, - reverseDestinations, new SubProgressMonitor( - monitor, 1), uiInfo, false); - // We don't record the moved resources since the recursive - // call has done so. Just record the overwrites. - for (int j = 0; j < overwritten.length; j++) { - overwrittenResources.add(overwritten[j]); - } + IResource[] children = ((IContainer) source).members(); + // move only linked resource children (267173) + if (source.isLinked() && source.getLocation().equals(existing.getLocation())) + children = filterNonLinkedResources(children); + ResourceDescription[] overwritten = move(children, destinationPath, resourcesAtDestination, + reverseDestinations, iterationProgress.newChild(90), uiInfo, false); + // We don't record the moved resources since the recursive + // call has done so. Just record the overwrites. + for (int j = 0; j < overwritten.length; j++) { + overwrittenResources.add(overwritten[j]); + } // Delete the source. No need to record it since it // will get moved back. - delete(source, monitor, uiInfo, false, false); + delete(source, iterationProgress.newChild(10), uiInfo, false, false); } else { // delete the destination folder, moving a linked folder // over an unlinked one or vice versa. Fixes bug 28772. - ResourceDescription[] deleted = delete( - new IResource[] { existing }, - new SubProgressMonitor(monitor, 0), uiInfo, false); + ResourceDescription[] deleted = delete(new IResource[] { existing }, iterationProgress.newChild(10), + uiInfo, false); // Record the original path reverseDestinations.add(source.getFullPath()); - source.move(destinationPath, IResource.SHALLOW - | IResource.KEEP_HISTORY, new SubProgressMonitor( - monitor, 1)); + source.move(destinationPath, IResource.SHALLOW | IResource.KEEP_HISTORY, + iterationProgress.newChild(90)); // Record the resource at its destination - resourcesAtDestination.add(getWorkspace().getRoot() - .findMember(destinationPath)); + resourcesAtDestination.add(getWorkspace().getRoot().findMember(destinationPath)); for (int j = 0; j < deleted.length; j++) { overwrittenResources.add(deleted[j]); } @@ -646,21 +597,20 @@ public class WorkspaceUndoUtil { if (source.isLinked() == existing.isLinked()) { // Record the original path reverseDestinations.add(source.getFullPath()); - overwrittenResources.add(copyOverExistingResource( - source, existing, new SubProgressMonitor( - monitor, 1), uiInfo, true)); + overwrittenResources.add(copyOverExistingResource(source, existing, + iterationProgress.newChild(100), uiInfo, true)); resourcesAtDestination.add(existing); } else { // Moving a linked resource over unlinked or vice // versa. Can't use setContents here. Fixes bug 28772. ResourceDescription[] deleted = delete( new IResource[] { existing }, - new SubProgressMonitor(monitor, 0), uiInfo, + iterationProgress.newChild(1), uiInfo, false); reverseDestinations.add(source.getFullPath()); source.move(destinationPath, IResource.SHALLOW | IResource.KEEP_HISTORY, - new SubProgressMonitor(monitor, 1)); + iterationProgress.newChild(99)); // Record the resource at its destination resourcesAtDestination.add(getWorkspace().getRoot() .findMember(destinationPath)); @@ -679,9 +629,8 @@ public class WorkspaceUndoUtil { } IContainer generatedParent = generateContainers(parentPath); - source.move(destinationPath, IResource.SHALLOW - | IResource.KEEP_HISTORY, new SubProgressMonitor( - monitor, 1)); + source.move(destinationPath, IResource.SHALLOW | IResource.KEEP_HISTORY, + iterationProgress.newChild(100)); // Record the move. If we had to generate a parent // folder, that should be recorded as part of the copy if (generatedParent == null) { @@ -692,13 +641,12 @@ public class WorkspaceUndoUtil { } } - if (monitor.isCanceled()) { + if (subMonitor.isCanceled()) { throw new OperationCanceledException(); } } } - monitor.done(); - return (ResourceDescription[]) overwrittenResources + return overwrittenResources .toArray(new ResourceDescription[overwrittenResources.size()]); } @@ -709,12 +657,12 @@ public class WorkspaceUndoUtil { * @return The linked resources */ private static IResource[] filterNonLinkedResources(IResource[] resources) { - List result = new ArrayList(); + List<IResource> result = new ArrayList<>(); for (int i = 0; i < resources.length; i++) { if (resources[i].isLinked()) result.add(resources[i]); } - return (IResource[]) result.toArray(new IResource[0]); + return result.toArray(new IResource[0]); } /** @@ -736,29 +684,23 @@ public class WorkspaceUndoUtil { */ static IResource[] recreate(ResourceDescription[] resourcesToRecreate, IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException { - final List exceptions = new ArrayList(); + SubMonitor subMonitor = SubMonitor.convert(monitor, resourcesToRecreate.length); + final List<CoreException> exceptions = new ArrayList<>(); IResource[] resourcesToReturn = new IResource[resourcesToRecreate.length]; - monitor.beginTask("", resourcesToRecreate.length); //$NON-NLS-1$ - monitor - .setTaskName(UndoMessages.AbstractResourcesOperation_CreateResourcesProgress); - try { - for (int i = 0; i < resourcesToRecreate.length; ++i) { - if (monitor.isCanceled()) { - throw new OperationCanceledException(); - } - try { - resourcesToReturn[i] = resourcesToRecreate[i] - .createResource(new SubProgressMonitor(monitor, 1)); - } catch (CoreException e) { - exceptions.add(e); - } + subMonitor.setTaskName(UndoMessages.AbstractResourcesOperation_CreateResourcesProgress); + for (int i = 0; i < resourcesToRecreate.length; ++i) { + if (monitor.isCanceled()) { + throw new OperationCanceledException(); } - IStatus result = WorkspaceUndoUtil.createResult(exceptions); - if (!result.isOK()) { - throw new CoreException(result); + try { + resourcesToReturn[i] = resourcesToRecreate[i].createResource(subMonitor.newChild(1)); + } catch (CoreException e) { + exceptions.add(e); } - } finally { - monitor.done(); + } + IStatus result = WorkspaceUndoUtil.createResult(exceptions); + if (!result.isOK()) { + throw new CoreException(result); } return resourcesToReturn; } @@ -792,17 +734,18 @@ public class WorkspaceUndoUtil { IProgressMonitor monitor, IAdaptable uiInfo, boolean forceOutOfSyncDelete, boolean deleteContent) throws CoreException { + SubMonitor subMonitor = SubMonitor.convert(monitor); ResourceDescription resourceDescription = ResourceDescription .fromResource(resourceToDelete); if (resourceToDelete.getType() == IResource.PROJECT) { // it is a project - monitor + subMonitor .setTaskName(UndoMessages.AbstractResourcesOperation_DeleteResourcesProgress); IProject project = (IProject) resourceToDelete; - project.delete(deleteContent, forceOutOfSyncDelete, monitor); + project.delete(deleteContent, forceOutOfSyncDelete, subMonitor); } else { // if it's not a project, just delete it - monitor.beginTask("", 2); //$NON-NLS-1$ + subMonitor.setWorkRemaining(2); monitor .setTaskName(UndoMessages.AbstractResourcesOperation_DeleteResourcesProgress); int updateFlags; @@ -811,11 +754,8 @@ public class WorkspaceUndoUtil { } else { updateFlags = IResource.KEEP_HISTORY; } - resourceToDelete.delete(updateFlags, new SubProgressMonitor( - monitor, 1)); - resourceDescription.recordStateFromHistory(resourceToDelete, - new SubProgressMonitor(monitor, 1)); - monitor.done(); + resourceToDelete.delete(updateFlags, subMonitor.newChild(1)); + resourceDescription.recordStateFromHistory(resourceToDelete, subMonitor.newChild(1)); } return resourceDescription; @@ -834,35 +774,26 @@ public class WorkspaceUndoUtil { } IFile file = (IFile) source; IFile existingFile = (IFile) existing; - monitor - .beginTask( - UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress, - 3); + SubMonitor subMonitor = SubMonitor.convert(monitor, + UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress, deleteSourceFile ? 3 : 2); if (file != null && existingFile != null) { if (validateEdit(file, existingFile, getShell(uiInfo))) { // Remember the state of the existing file so it can be // restored. - FileDescription fileDescription = new FileDescription( - existingFile); + FileDescription fileDescription = new FileDescription(existingFile); // Reset the contents to that of the file being moved - existingFile.setContents(file.getContents(), - IResource.KEEP_HISTORY, new SubProgressMonitor(monitor, - 1)); - fileDescription.recordStateFromHistory(existingFile, - new SubProgressMonitor(monitor, 1)); + existingFile.setContents(file.getContents(), IResource.KEEP_HISTORY, subMonitor.newChild(1)); + fileDescription.recordStateFromHistory(existingFile, subMonitor.newChild(1)); // Now delete the source file if requested // We don't need to remember anything about it, because // any undo involving this operation will move the original // content back to it. if (deleteSourceFile) { - file.delete(IResource.KEEP_HISTORY, new SubProgressMonitor( - monitor, 1)); + file.delete(IResource.KEEP_HISTORY, subMonitor.newChild(1)); } - monitor.done(); return fileDescription; } } - monitor.done(); return null; } @@ -946,15 +877,15 @@ public class WorkspaceUndoUtil { * Creates and return a result status appropriate for the given list of * exceptions. */ - private static IStatus createResult(List exceptions) { + private static IStatus createResult(List<CoreException> exceptions) { if (exceptions.isEmpty()) { return Status.OK_STATUS; } final int exceptionCount = exceptions.size(); if (exceptionCount == 1) { - return ((CoreException) exceptions.get(0)).getStatus(); + return exceptions.get(0).getStatus(); } - CoreException[] children = (CoreException[]) exceptions + CoreException[] children = exceptions .toArray(new CoreException[exceptionCount]); boolean outOfSync = false; for (int i = 0; i < children.length; i++) { |
