1 11 12 package org.eclipse.ui.ide.undo; 13 14 import java.net.URI ; 15 16 import org.eclipse.core.filesystem.URIUtil; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IProjectDescription; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IAdaptable; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.Platform; 29 import org.eclipse.core.runtime.Status; 30 import org.eclipse.core.runtime.SubProgressMonitor; 31 import org.eclipse.ui.internal.ide.undo.ProjectDescription; 32 import org.eclipse.ui.internal.ide.undo.UndoMessages; 33 34 45 public class CopyProjectOperation extends AbstractCopyOrMoveResourcesOperation { 46 47 private URI projectLocation; 48 49 private IProject originalProject; 50 51 private ProjectDescription originalProjectDescription; 52 53 67 public CopyProjectOperation(IProject project, String name, URI location, 68 String label) { 69 super(new IResource[] { project }, new Path(name), label); 70 Assert.isLegal(project != null); 71 originalProject = project; 72 if (location != null 73 && URIUtil.toPath(location).equals(Platform.getLocation())) { 74 projectLocation = null; 75 } else { 76 projectLocation = location; 77 } 78 } 79 80 84 private IProject getProposedProjectHandle() { 85 if (destination.segmentCount() == 1) { 86 return getWorkspace().getRoot().getProject( 87 destination.lastSegment()); 88 } 89 return null; 90 } 91 92 100 protected IStatus computeMoveOrCopyStatus() { 101 IStatus status = Status.OK_STATUS; 102 IProject project = getProposedProjectHandle(); 103 if (project == null) { 104 return getErrorStatus(UndoMessages.AbstractResourcesOperation_NotEnoughInfo); 105 } 106 if (projectLocation != null) { 107 status = getWorkspace().validateProjectLocationURI(project, 108 projectLocation); 109 } 110 if (status.isOK()) { 111 return super.computeMoveOrCopyStatus(); 112 } 113 return status; 114 } 115 116 124 protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo) 125 throws CoreException { 126 IProject newProject = copyProject(originalProject, destination, 127 projectLocation, monitor); 128 setTargetResources(new IResource[] { newProject }); 129 setResourceDescriptions(new ResourceDescription[0]); 130 } 131 132 140 protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo) 141 throws CoreException { 142 WorkspaceUndoUtil.delete(resources, new SubProgressMonitor(monitor, 1), 144 uiInfo, true); 145 setTargetResources(new IResource[] { originalProject }); 147 setResourceDescriptions(new ResourceDescription[0]); 148 } 149 150 156 protected boolean isDestinationPathValid(IResource resource, int index) { 157 return true; 159 } 160 161 167 protected String getProposedName(IResource resource, int index) { 168 return destination.lastSegment(); 169 } 170 171 177 protected boolean updateResourceChangeDescriptionFactory( 178 IResourceChangeDescriptionFactory factory, int operation) { 179 boolean update = false; 180 if (operation == UNDO) { 181 for (int i = 0; i < resources.length; i++) { 182 update = true; 183 IResource resource = resources[i]; 184 factory.delete(resource); 185 } 186 } else { 187 factory.copy(originalProject, 188 getDestinationPath(originalProject, 0)); 189 } 190 return update; 191 } 192 193 201 public IStatus computeUndoableStatus(IProgressMonitor monitor) { 202 IStatus status = super.computeUndoableStatus(monitor); 203 if (!status.isOK()) { 204 return status; 205 } 206 if (originalProject == null 210 || !originalProjectDescription.verifyExistence(true)) { 211 markInvalid(); 212 return getErrorStatus(UndoMessages.CopyResourcesOperation_NotAllowedDueToDataLoss); 213 } 214 if (status.isOK()) { 216 status = computeDeleteStatus(); 217 } 218 return status; 219 } 220 221 224 IProject copyProject(IProject project, IPath destinationPath, 225 URI locationURI, IProgressMonitor monitor) throws CoreException { 226 monitor 227 .setTaskName(UndoMessages.AbstractCopyOrMoveResourcesOperation_copyProjectProgress); 228 229 boolean open = project.isOpen(); 230 if (!open) { 231 project.open(null); 234 } 235 originalProjectDescription = new ProjectDescription(project); 236 IProjectDescription description = project.getDescription(); 237 238 description.setName(destinationPath.lastSegment()); 240 description.setLocationURI(locationURI); 241 242 project.copy(description, IResource.FORCE | IResource.SHALLOW, monitor); 243 244 if (!open) { 246 project.close(null); 247 } 248 return (IProject) getWorkspace().getRoot().findMember(destinationPath); 250 } 251 } 252 | Popular Tags |