1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IWorkspace; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.OperationCanceledException; 22 import org.eclipse.core.runtime.SubProgressMonitor; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.ide.undo.AbstractWorkspaceOperation; 26 import org.eclipse.ui.ide.undo.MoveResourcesOperation; 27 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 28 29 37 public class MoveFilesAndFoldersOperation extends CopyFilesAndFoldersOperation { 38 39 45 public MoveFilesAndFoldersOperation(Shell shell) { 46 super(shell); 47 } 48 49 56 protected boolean canPerformAutoRename() { 57 return false; 58 } 59 60 75 protected void copy(IResource[] resources, IPath destination, 76 IProgressMonitor subMonitor) throws CoreException { 77 for (int i = 0; i < resources.length; i++) { 78 IResource source = resources[i]; 79 IPath destinationPath = destination.append(source.getName()); 80 IWorkspace workspace = source.getWorkspace(); 81 IWorkspaceRoot workspaceRoot = workspace.getRoot(); 82 IResource existing = workspaceRoot.findMember(destinationPath); 83 if (source.getType() == IResource.FOLDER && existing != null) { 84 if (homogenousResources(source, existing)) { 87 IResource[] children = ((IContainer) source).members(); 88 copy(children, destinationPath, subMonitor); 89 delete(source, subMonitor); 90 } else { 91 delete(existing, new SubProgressMonitor(subMonitor, 0)); 94 source.move(destinationPath, IResource.SHALLOW 95 | IResource.KEEP_HISTORY, new SubProgressMonitor( 96 subMonitor, 0)); 97 } 98 } else { 99 if (existing != null) { 102 if (homogenousResources(source, existing)) { 103 moveExisting(source, existing, subMonitor); 104 } else { 105 delete(existing, new SubProgressMonitor(subMonitor, 0)); 108 source.move(destinationPath, IResource.SHALLOW 109 | IResource.KEEP_HISTORY, 110 new SubProgressMonitor(subMonitor, 0)); 111 } 112 } else { 113 source.move(destinationPath, IResource.SHALLOW 114 | IResource.KEEP_HISTORY, new SubProgressMonitor( 115 subMonitor, 0)); 116 } 117 subMonitor.worked(1); 118 if (subMonitor.isCanceled()) { 119 throw new OperationCanceledException(); 120 } 121 } 122 } 123 } 124 125 132 protected String getDeepCheckQuestion(IResource source) { 133 return NLS 134 .bind( 135 IDEWorkbenchMessages.CopyFilesAndFoldersOperation_deepMoveQuestion, 136 source.getFullPath().makeRelative()); 137 } 138 139 144 protected String getOperationTitle() { 145 return IDEWorkbenchMessages.MoveFilesAndFoldersOperation_operationTitle; 146 } 147 148 153 protected String getProblemsMessage() { 154 return IDEWorkbenchMessages.MoveFilesAndFoldersOperation_problemMessage; 155 } 156 157 162 protected String getProblemsTitle() { 163 return IDEWorkbenchMessages.MoveFilesAndFoldersOperation_moveFailedTitle; 164 } 165 166 173 protected boolean getValidateConflictSource() { 174 return true; 175 } 176 177 191 private void moveExisting(IResource source, IResource existing, 192 IProgressMonitor subMonitor) throws CoreException { 193 IFile existingFile = getFile(existing); 194 195 if (existingFile != null) { 196 IFile sourceFile = getFile(source); 197 198 if (sourceFile != null) { 199 existingFile.setContents(sourceFile.getContents(), 200 IResource.KEEP_HISTORY, new SubProgressMonitor( 201 subMonitor, 0)); 202 delete(sourceFile, subMonitor); 203 } 204 } 205 } 206 207 213 public String validateDestination(IContainer destination, 214 IResource[] sourceResources) { 215 IPath destinationLocation = destination.getLocation(); 216 217 for (int i = 0; i < sourceResources.length; i++) { 218 IResource sourceResource = sourceResources[i]; 219 220 if (sourceResource.getParent().equals(destination)) { 222 return NLS 223 .bind( 224 IDEWorkbenchMessages.MoveFilesAndFoldersOperation_sameSourceAndDest, 225 sourceResource.getName()); 226 } 227 if (destinationLocation != null) { 229 IPath sourceLocation = sourceResource.getLocation(); 230 IPath destinationResource = destinationLocation 231 .append(sourceResource.getName()); 232 if (sourceLocation != null 233 && sourceLocation.isPrefixOf(destinationResource)) { 234 return NLS 235 .bind( 236 IDEWorkbenchMessages.MoveFilesAndFoldersOperation_sameSourceAndDest, 237 sourceResource.getName()); 238 } 239 } 240 } 241 return super.validateDestination(destination, sourceResources); 242 } 243 244 249 protected boolean isMove() { 250 return true; 251 } 252 253 265 protected AbstractWorkspaceOperation getUndoableCopyOrMoveOperation( 266 IResource[] resources, IPath destinationPath) { 267 return new MoveResourcesOperation(resources, destinationPath, 268 IDEWorkbenchMessages.CopyFilesAndFoldersOperation_moveTitle); 269 270 } 271 } 272 | Popular Tags |