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 CopyResourcesOperation extends 46 AbstractCopyOrMoveResourcesOperation { 47 48 IResource[] originalResources; 49 50 ResourceDescription[] snapshotResourceDescriptions; 51 52 64 public CopyResourcesOperation(IResource resource, IPath newPath, 65 String label) { 66 super(new IResource[] { resource }, new IPath[] { newPath }, label); 67 setOriginalResources(new IResource[] { resource }); 68 } 69 70 83 public CopyResourcesOperation(IResource[] resources, IPath destinationPath, 84 String label) { 85 super(resources, destinationPath, label); 86 setOriginalResources(this.resources); 87 } 88 89 105 public CopyResourcesOperation(IResource[] resources, 106 IPath[] destinationPaths, String label) { 107 super(resources, destinationPaths, label); 108 setOriginalResources(this.resources); 109 } 110 111 119 protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo) 120 throws CoreException { 121 copy(monitor, uiInfo); 122 } 123 124 140 protected void copy(IProgressMonitor monitor, IAdaptable uiInfo) 141 throws CoreException { 142 143 monitor.beginTask("", 2000); monitor 145 .setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); 146 List resourcesAtDestination = new ArrayList (); 147 List overwrittenResources = new ArrayList (); 148 149 for (int i = 0; i < resources.length; i++) { 150 ResourceDescription[] overwrites; 153 overwrites = WorkspaceUndoUtil.copy( 154 new IResource[] { resources[i] }, getDestinationPath( 155 resources[i], i), resourcesAtDestination, 156 new SubProgressMonitor(monitor, 1000 / resources.length), 157 uiInfo, true); 158 for (int j = 0; j < overwrites.length; j++) { 160 overwrittenResources.add(overwrites[j]); 161 } 162 } 163 164 if (resourceDescriptions != null) { 166 for (int i = 0; i < resourceDescriptions.length; i++) { 167 if (resourceDescriptions[i] != null) { 168 resourceDescriptions[i] 169 .createResource(new SubProgressMonitor(monitor, 170 1000 / resourceDescriptions.length)); 171 } 172 } 173 } 174 175 setResourceDescriptions((ResourceDescription[]) overwrittenResources 177 .toArray(new ResourceDescription[overwrittenResources.size()])); 178 179 setTargetResources((IResource[]) resourcesAtDestination 182 .toArray(new IResource[resourcesAtDestination.size()])); 183 monitor.done(); 184 } 185 186 195 protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo) 196 throws CoreException { 197 monitor.beginTask("", 2); monitor 199 .setTaskName(UndoMessages.AbstractResourcesOperation_CopyingResourcesProgress); 200 WorkspaceUndoUtil.delete(resources, new SubProgressMonitor(monitor, 1), 202 uiInfo, true); 203 WorkspaceUndoUtil.recreate(resourceDescriptions, 205 new SubProgressMonitor(monitor, 1), uiInfo); 206 setResourceDescriptions(new ResourceDescription[0]); 207 setTargetResources(originalResources); 211 monitor.done(); 212 } 213 214 220 protected boolean updateResourceChangeDescriptionFactory( 221 IResourceChangeDescriptionFactory factory, int operation) { 222 boolean update = false; 223 if (operation == UNDO) { 224 for (int i = 0; i < resources.length; i++) { 225 update = true; 226 IResource resource = resources[i]; 227 factory.delete(resource); 228 } 229 for (int i = 0; i < resourceDescriptions.length; i++) { 230 update = true; 231 IResource resource = resourceDescriptions[i] 232 .createResourceHandle(); 233 factory.create(resource); 234 } 235 } else { 236 for (int i = 0; i < resources.length; i++) { 237 update = true; 238 IResource resource = resources[i]; 239 factory.copy(resource, getDestinationPath(resource, i)); 240 } 241 } 242 return update; 243 } 244 245 253 public IStatus computeUndoableStatus(IProgressMonitor monitor) { 254 IStatus status = super.computeUndoableStatus(monitor); 255 if (!status.isOK()) { 256 return status; 257 } 258 if (originalResources == null) { 262 markInvalid(); 263 return getErrorStatus(UndoMessages.CopyResourcesOperation_NotAllowedDueToDataLoss); 264 } 265 for (int i = 0; i < snapshotResourceDescriptions.length; i++) { 266 if (!snapshotResourceDescriptions[i].verifyExistence(true)) { 267 markInvalid(); 268 return getErrorStatus(UndoMessages.CopyResourcesOperation_NotAllowedDueToDataLoss); 269 } 270 } 271 if (status.isOK()) { 273 status = computeDeleteStatus(); 274 } 275 if (status.isOK() && resourceDescriptions != null 278 && resourceDescriptions.length > 0) { 279 status = computeCreateStatus(true); 280 } 281 282 return status; 283 } 284 285 290 private void setOriginalResources(IResource[] originals) { 291 originalResources = originals; 292 snapshotResourceDescriptions = new ResourceDescription[originals.length]; 293 for (int i = 0; i < originals.length; i++) { 294 snapshotResourceDescriptions[i] = ResourceDescription 295 .fromResource(originals[i]); 296 } 297 } 298 } 299 | Popular Tags |