1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 import org.eclipse.core.runtime.SubProgressMonitor; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IFolder; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IResourceVisitor; 24 import org.eclipse.core.resources.ResourcesPlugin; 25 26 import org.eclipse.ui.ide.undo.ResourceDescription; 27 28 import org.eclipse.ltk.core.refactoring.Change; 29 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 30 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 32 import org.eclipse.jdt.internal.corext.util.Messages; 33 34 public class DeleteFolderChange extends AbstractDeleteChange { 35 36 private final IPath fPath; 37 private final boolean fIsExecuteChange; 38 39 public DeleteFolderChange(IFolder folder, boolean isExecuteChange) { 40 this(getFolderPath(folder), isExecuteChange); 41 } 42 43 public DeleteFolderChange(IPath path, boolean isExecuteChange) { 44 fPath= path; 45 fIsExecuteChange= isExecuteChange; 46 } 47 48 public static IPath getFolderPath(IFolder folder){ 49 return folder.getFullPath().removeFirstSegments(ResourcesPlugin.getWorkspace().getRoot().getFullPath().segmentCount()); 50 } 51 52 public static IFolder getFolder(IPath path){ 53 return ResourcesPlugin.getWorkspace().getRoot().getFolder(path); 54 } 55 56 public String getName() { 57 return Messages.format(RefactoringCoreMessages.DeleteFolderChange_0, fPath.lastSegment()); 58 } 59 60 public Object getModifiedElement() { 61 return getFolder(fPath); 62 } 63 64 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 65 if (fIsExecuteChange) { 66 return super.isValid(pm, NONE); 71 } else { 72 return super.isValid(pm, READ_ONLY | DIRTY); 73 } 74 } 75 76 protected Change doDelete(IProgressMonitor pm) throws CoreException{ 77 IFolder folder= getFolder(fPath); 78 Assert.isTrue(folder.exists()); 79 pm.beginTask("", 3); folder.accept(new IResourceVisitor() { 81 public boolean visit(IResource resource) throws CoreException { 82 if (resource instanceof IFile) { 83 saveFileIfNeeded((IFile)resource, new NullProgressMonitor()); 85 } 86 return true; 87 } 88 }, IResource.DEPTH_INFINITE, false); 89 pm.worked(1); 90 91 ResourceDescription resourceDescription = ResourceDescription.fromResource(folder); 92 folder.delete(false, true, new SubProgressMonitor(pm, 1)); 93 resourceDescription.recordStateFromHistory(folder, new SubProgressMonitor(pm, 1)); 94 pm.done(); 95 96 return new UndoDeleteResourceChange(resourceDescription); 97 } 98 } 99 100 | Popular Tags |