1 11 12 package org.eclipse.ui.ide.undo; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.SubProgressMonitor; 25 import org.eclipse.ui.internal.ide.undo.UndoMessages; 26 27 45 public class MoveResourcesOperation extends 46 AbstractCopyOrMoveResourcesOperation { 47 48 IResource[] originalResources; 49 50 IPath originalDestination; 51 52 IPath[] originalDestinationPaths; 53 54 66 public MoveResourcesOperation(IResource[] resources, IPath destinationPath, 67 String label) { 68 super(resources, destinationPath, label); 69 originalResources = this.resources; 70 originalDestination = this.destination; 71 originalDestinationPaths = this.destinationPaths; 72 } 73 74 86 public MoveResourcesOperation(IResource resource, IPath newPath, 87 String label) { 88 super(new IResource[] { resource }, new IPath[] { newPath }, label); 89 originalResources = this.resources; 90 originalDestination = this.destination; 91 originalDestinationPaths = this.destinationPaths; 92 } 93 94 102 protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo) 103 throws CoreException { 104 move(monitor, uiInfo); 105 } 106 107 122 protected void move(IProgressMonitor monitor, IAdaptable uiInfo) 123 throws CoreException { 124 125 monitor.beginTask("", 2000); monitor 127 .setTaskName(UndoMessages.AbstractResourcesOperation_MovingResources); 128 List resourcesAtDestination = new ArrayList (); 129 List undoDestinationPaths = new ArrayList (); 130 List overwrittenResources = new ArrayList (); 131 132 for (int i = 0; i < resources.length; i++) { 133 ResourceDescription[] overwrites; 136 overwrites = WorkspaceUndoUtil.move( 137 new IResource[] { resources[i] }, getDestinationPath( 138 resources[i], i), resourcesAtDestination, 139 undoDestinationPaths, new SubProgressMonitor(monitor, 140 1000 / resources.length), uiInfo, true); 141 142 for (int j = 0; j < overwrites.length; j++) { 144 overwrittenResources.add(overwrites[j]); 145 } 146 } 147 148 if (resourceDescriptions != null) { 150 for (int i = 0; i < resourceDescriptions.length; i++) { 151 if (resourceDescriptions[i] != null) { 152 resourceDescriptions[i] 153 .createResource(new SubProgressMonitor(monitor, 154 1000 / resourceDescriptions.length)); 155 } 156 } 157 } 158 159 setResourceDescriptions((ResourceDescription[]) overwrittenResources 161 .toArray(new ResourceDescription[overwrittenResources.size()])); 162 163 setTargetResources((IResource[]) resourcesAtDestination 166 .toArray(new IResource[resourcesAtDestination.size()])); 167 destinationPaths = (IPath[]) undoDestinationPaths 169 .toArray(new IPath[undoDestinationPaths.size()]); 170 destination = null; 171 172 monitor.done(); 173 } 174 175 183 protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo) 184 throws CoreException { 185 move(monitor, uiInfo); 187 setTargetResources(originalResources); 194 this.resourceDescriptions = new ResourceDescription[0]; 195 this.destination = originalDestination; 196 this.destinationPaths = originalDestinationPaths; 197 } 198 199 205 protected boolean updateResourceChangeDescriptionFactory( 206 IResourceChangeDescriptionFactory factory, int operation) { 207 for (int i = 0; i < resources.length; i++) { 208 IResource resource = resources[i]; 209 factory.move(resource, getDestinationPath(resource, i)); 210 } 211 return true; 212 } 213 214 221 public IStatus computeUndoableStatus(IProgressMonitor monitor) { 222 IStatus status = super.computeUndoableStatus(monitor); 223 if (status.isOK()) { 224 status = computeMoveOrCopyStatus(); 225 } 226 return status; 227 } 228 } 229 | Popular Tags |