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.IProgressMonitor; 16 import org.eclipse.core.runtime.SubProgressMonitor; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.ui.ide.undo.ResourceDescription; 22 23 import org.eclipse.ltk.core.refactoring.Change; 24 import org.eclipse.ltk.core.refactoring.NullChange; 25 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 26 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IPackageFragment; 30 import org.eclipse.jdt.core.ISourceManipulation; 31 import org.eclipse.jdt.core.JavaCore; 32 33 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 34 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 35 import org.eclipse.jdt.internal.corext.util.Messages; 36 37 41 public class DeleteSourceManipulationChange extends AbstractDeleteChange { 42 43 private final String fHandle; 44 private final boolean fIsExecuteChange; 45 46 public DeleteSourceManipulationChange(ISourceManipulation sm, boolean isExecuteChange) { 47 Assert.isNotNull(sm); 48 fHandle= getJavaElement(sm).getHandleIdentifier(); 49 fIsExecuteChange= isExecuteChange; 50 } 51 52 55 public String getName() { 56 return Messages.format(RefactoringCoreMessages.DeleteSourceManipulationChange_0, getElementName()); 57 } 58 59 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 60 ISourceManipulation element= getSourceManipulation(); 61 if (fIsExecuteChange) { 62 if (element instanceof ICompilationUnit) { 63 return super.isValid(pm, NONE); 66 } else { 67 return super.isValid(pm, DIRTY); 68 } 69 } else { 70 return super.isValid(pm, READ_ONLY | DIRTY); 71 } 72 } 73 74 private String getElementName() { 75 IJavaElement javaElement= getJavaElement(getSourceManipulation()); 76 if (JavaElementUtil.isDefaultPackage(javaElement)) 77 return RefactoringCoreMessages.DeleteSourceManipulationChange_1; 78 return javaElement.getElementName(); 79 } 80 81 84 public Object getModifiedElement() { 85 return JavaCore.create(fHandle); 86 } 87 88 91 protected Change doDelete(IProgressMonitor pm) throws CoreException { 92 ISourceManipulation element= getSourceManipulation(); 93 if (element instanceof ICompilationUnit) { 97 pm.beginTask("", 2); ICompilationUnit unit= (ICompilationUnit)element; 99 saveCUnitIfNeeded(unit, new SubProgressMonitor(pm, 1)); 100 101 IResource resource= unit.getResource(); 102 ResourceDescription resourceDescription = ResourceDescription.fromResource(resource); 103 element.delete(false, new SubProgressMonitor(pm, 1)); 104 resourceDescription.recordStateFromHistory(resource, new SubProgressMonitor(pm, 1)); 105 return new UndoDeleteResourceChange(resourceDescription); 106 107 } else if (element instanceof IPackageFragment) { 108 ICompilationUnit[] units= ((IPackageFragment)element).getCompilationUnits(); 109 pm.beginTask("", units.length + 1); for (int i = 0; i < units.length; i++) { 111 saveCUnitIfNeeded(units[i], new SubProgressMonitor(pm, 1)); 113 } 114 element.delete(false, new SubProgressMonitor(pm, 1)); 115 return new NullChange(); 117 } else { 118 element.delete(false, pm); 119 return null; } 121 } 122 123 private ISourceManipulation getSourceManipulation() { 124 return (ISourceManipulation) getModifiedElement(); 125 } 126 127 private static IJavaElement getJavaElement(ISourceManipulation sm) { 128 return (IJavaElement)sm; 130 } 131 132 private static void saveCUnitIfNeeded(ICompilationUnit unit, IProgressMonitor pm) throws CoreException { 133 saveFileIfNeeded((IFile)unit.getResource(), pm); 134 } 135 } 136 137 | Popular Tags |