1 11 12 package org.eclipse.ui.ide.undo; 13 14 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.ui.internal.ide.undo.UndoMessages; 22 23 33 abstract class AbstractCopyOrMoveResourcesOperation extends 34 AbstractResourcesOperation { 35 36 protected IPath[] destinationPaths = null; 38 39 protected IPath destination = null; 41 42 59 AbstractCopyOrMoveResourcesOperation(IResource[] resources, 60 IPath[] destinationPaths, String label) { 61 super(resources, label); 62 if (this.resources == null || destinationPaths == null) 64 throw new IllegalArgumentException ("The resource and destination paths may not be null"); if (this.resources.length != resources.length) 69 throw new IllegalArgumentException ("The resource list contained descendants that cannot be moved to separate destination paths"); if (this.resources.length != destinationPaths.length) { 72 throw new IllegalArgumentException ("The resource and destination paths must be the same length"); } 74 for (int i=0; i<this.resources.length; i++) { 75 if (this.resources[i] == null) { 76 throw new IllegalArgumentException ("The resources array may not contain null resources"); } 78 if (destinationPaths[i] == null) { 79 throw new IllegalArgumentException ("The destination paths array may not contain null paths"); } 81 } 82 this.destinationPaths = destinationPaths; 83 } 84 85 98 AbstractCopyOrMoveResourcesOperation(IResource[] resources, 99 IPath destinationPath, String label) { 100 super(resources, label); 101 destination = destinationPath; 102 } 103 104 113 AbstractCopyOrMoveResourcesOperation(IResource[] resources, String label) { 114 super(resources, label); 115 } 116 117 118 137 protected IStatus computeMoveOrCopyStatus() { 138 if (resources == null) { 141 markInvalid(); 142 return getErrorStatus(UndoMessages.AbstractResourcesOperation_NotEnoughInfo); 143 } 144 for (int i = 0; i < resources.length; i++) { 145 IResource resource = resources[i]; 146 if (!resource.exists()) { 148 markInvalid(); 149 return getErrorStatus(UndoMessages.AbstractCopyOrMoveResourcesOperation_ResourceDoesNotExist); 150 } 151 152 if (!isDestinationPathValid(resource, i)) { 154 markInvalid(); 155 return getErrorStatus(UndoMessages.AbstractCopyOrMoveResourcesOperation_SameNameOrLocation); 156 } 157 IStatus status = getWorkspace().validateName( 159 getProposedName(resource, i), resource.getType()); 160 if (status.getSeverity() == IStatus.ERROR) { 161 markInvalid(); 162 } 163 if (!status.isOK()) { 164 return status; 165 } 166 } 167 return Status.OK_STATUS; 168 } 169 170 180 protected IPath getDestinationPath(IResource resource, int index) { 181 if (destinationPaths != null) { 182 return destinationPaths[index]; 183 } 184 return destination.append(resource.getName()); 185 186 } 187 188 193 protected void appendDescriptiveText(StringBuffer text) { 194 super.appendDescriptiveText(text); 195 text.append(" destination: "); text.append(destination); 197 text.append(", destinationPaths: "); text.append(destinationPaths); 199 text.append('\''); 200 } 201 202 212 protected boolean isDestinationPathValid(IResource resource, int index) { 213 return !resource.getFullPath().equals( 214 getDestinationPath(resource, index)); 215 } 216 217 226 protected String getProposedName(IResource resource, int index) { 227 return getDestinationPath(resource, index).lastSegment(); 228 } 229 230 237 public IStatus computeExecutionStatus(IProgressMonitor monitor) { 238 IStatus status = super.computeExecutionStatus(monitor); 239 if (status.isOK()) { 240 status = computeMoveOrCopyStatus(); 241 } 242 return status; 243 } 244 245 252 public IStatus computeRedoableStatus(IProgressMonitor monitor) { 253 IStatus status = super.computeRedoableStatus(monitor); 254 if (status.isOK()) { 255 status = computeMoveOrCopyStatus(); 256 } 257 return status; 258 } 259 } 260 | Popular Tags |