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.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.core.runtime.jobs.ISchedulingRule; 24 import org.eclipse.core.runtime.jobs.MultiRule; 25 import org.eclipse.ui.actions.ReadOnlyStateChecker; 26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 27 import org.eclipse.ui.internal.ide.undo.UndoMessages; 28 29 41 abstract class AbstractResourcesOperation extends AbstractWorkspaceOperation { 42 43 47 protected ResourceDescription[] resourceDescriptions; 48 49 54 private static boolean isDescendantOf(IResource subResource, IResource superResource) { 55 return ! subResource.equals(superResource) && superResource.getFullPath().isPrefixOf(subResource.getFullPath()); 56 } 57 58 59 67 AbstractResourcesOperation(IResource[] resources, String label) { 68 super(label); 69 this.addContext(WorkspaceUndoUtil.getWorkspaceUndoContext()); 70 71 setTargetResources(resources); 72 } 73 74 82 AbstractResourcesOperation(ResourceDescription[] resourceDescriptions, 83 String label) { 84 super(label); 85 addContext(WorkspaceUndoUtil.getWorkspaceUndoContext()); 86 setResourceDescriptions(resourceDescriptions); 87 } 88 89 108 protected void delete(IProgressMonitor monitor, IAdaptable uiInfo, 109 boolean deleteContent) throws CoreException { 110 setResourceDescriptions(WorkspaceUndoUtil.delete(resources, monitor, 111 uiInfo, deleteContent)); 112 setTargetResources(new IResource[0]); 113 } 114 115 130 protected void recreate(IProgressMonitor monitor, IAdaptable uiInfo) 131 throws CoreException { 132 setTargetResources(WorkspaceUndoUtil.recreate(resourceDescriptions, 133 monitor, uiInfo)); 134 setResourceDescriptions(new ResourceDescription[0]); 135 } 136 137 157 protected IStatus computeCreateStatus(boolean allowOverwrite) { 158 if (resourceDescriptions == null || resourceDescriptions.length == 0) { 159 markInvalid(); 160 return getErrorStatus(UndoMessages.AbstractResourcesOperation_NotEnoughInfo); 161 } 162 for (int i = 0; i < resourceDescriptions.length; i++) { 163 if (!resourceDescriptions[i].isValid()) { 165 markInvalid(); 166 return getErrorStatus(UndoMessages.AbstractResourcesOperation_InvalidRestoreInfo); 167 } else if (!allowOverwrite 168 && resourceDescriptions[i].verifyExistence(false)) { 169 markInvalid(); 171 return getErrorStatus(UndoMessages.AbstractResourcesOperation_ResourcesAlreadyExist); 172 } 173 } 174 return Status.OK_STATUS; 175 } 176 177 193 protected IStatus computeDeleteStatus() { 194 if (resources == null || resources.length == 0) { 195 markInvalid(); 196 return getErrorStatus(UndoMessages.AbstractResourcesOperation_NotEnoughInfo); 197 } 198 if (!resourcesExist()) { 199 markInvalid(); 200 return getErrorStatus(UndoMessages.AbstractResourcesOperation_ResourcesDoNotExist); 201 } 202 return checkReadOnlyResources(resources); 203 } 204 205 209 IStatus checkReadOnlyResources(IResource[] resourcesToCheck) { 210 if (!quietCompute) { 213 ReadOnlyStateChecker checker = new ReadOnlyStateChecker( 214 getShell(null), 215 IDEWorkbenchMessages.DeleteResourceAction_title1, 216 IDEWorkbenchMessages.DeleteResourceAction_readOnlyQuestion); 217 checker.setIgnoreLinkedResources(true); 218 IResource[] approvedResources = checker 219 .checkReadOnlyResources(resourcesToCheck); 220 if (approvedResources.length == 0) { 221 return Status.CANCEL_STATUS; 223 } 224 setTargetResources(approvedResources); 226 } 227 return Status.OK_STATUS; 228 } 229 230 237 protected void setResourceDescriptions(ResourceDescription[] descriptions) { 238 if (descriptions == null) { 239 resourceDescriptions = new ResourceDescription[0]; 240 } else { 241 resourceDescriptions = descriptions; 242 } 243 } 244 245 250 protected void appendDescriptiveText(StringBuffer text) { 251 super.appendDescriptiveText(text); 252 text.append(" resourceDescriptions: "); text.append(resourceDescriptions); 254 text.append('\''); 255 } 256 257 263 protected ISchedulingRule computeCreateSchedulingRule() { 264 ISchedulingRule[] ruleArray = new ISchedulingRule[resourceDescriptions.length * 3]; 265 266 for (int i = 0; i < resourceDescriptions.length; i++) { 267 IResource resource = resourceDescriptions[i].createResourceHandle(); 268 ruleArray[i * 3] = getWorkspaceRuleFactory().createRule(resource); 270 ruleArray[i * 3 + 1] = getWorkspaceRuleFactory().modifyRule( 272 resource); 273 ruleArray[i * 3 + 2] = getWorkspaceRuleFactory().charsetRule( 275 resource); 276 277 } 278 return MultiRule.combine(ruleArray); 279 } 280 281 287 protected ISchedulingRule computeDeleteSchedulingRule() { 288 ISchedulingRule[] ruleArray = new ISchedulingRule[resources.length * 2]; 289 for (int i = 0; i < resources.length; i++) { 290 ruleArray[i * 2] = getWorkspaceRuleFactory().deleteRule( 291 resources[i]); 292 ruleArray[i * 2 + 1] = getWorkspaceRuleFactory().modifyRule( 295 resources[i]); 296 } 297 return MultiRule.combine(ruleArray); 298 299 } 300 301 306 protected void setTargetResources(IResource[] targetResources) { 307 List subResources = new ArrayList (); 310 for (int i = 0; i < targetResources.length; i++) { 311 IResource subResource = targetResources[i]; 312 for (int j = 0; j < targetResources.length; j++) { 313 IResource superResource = targetResources[j]; 314 if (isDescendantOf(subResource, superResource)) 315 subResources.add(subResource); 316 } 317 } 318 IResource[] nestedResourcesRemoved = new IResource[targetResources.length 319 - subResources.size()]; 320 int j = 0; 321 for (int i = 0; i < targetResources.length; i++) { 322 if (!subResources.contains(targetResources[i])) { 323 nestedResourcesRemoved[j] = targetResources[i]; 324 j++; 325 } 326 } 327 super.setTargetResources(nestedResourcesRemoved); 328 } 329 } 330 | Popular Tags |