1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.changes;
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;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
21 import org.eclipse.ui.ide.undo.ResourceDescription;
23 import org.eclipse.ltk.core.refactoring.Change;
24 import org.eclipse.ltk.core.refactoring.NullChange;
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.IPackageFragment;
29 import org.eclipse.jdt.core.ISourceManipulation;
30 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
33 import org.eclipse.jdt.internal.corext.util.Messages;
35 import org.eclipse.jdt.ui.JavaElementLabels;
38 * Caveat: undo of package fragment deletes is provided by a wrapping
39 * UndoablePackageDeleteChange. This change returns a NullChange as undo for package fragments.
41 public class DeleteSourceManipulationChange extends AbstractDeleteChange {
43 private final String fHandle;
45 public DeleteSourceManipulationChange(ISourceManipulation sm, boolean isExecuteChange) {
47 fHandle= getJavaElement(sm).getHandleIdentifier();
49 if (isExecuteChange) {
50 if (sm instanceof ICompilationUnit) {
51 // don't check anything in this case. We have a warning dialog
52 // already presented to the user that the file is dirty.
53 setValidationMethod(VALIDATE_DEFAULT);
55 setValidationMethod(VALIDATE_NOT_DIRTY);
58 setValidationMethod(VALIDATE_NOT_DIRTY | VALIDATE_NOT_READ_ONLY);
63 * @see IChange#getName()
66 public String getName() {
67 IJavaElement javaElement= getJavaElement(getSourceManipulation());
68 return Messages.format(RefactoringCoreMessages.DeleteSourceManipulationChange_0, JavaElementLabels.getElementLabel(javaElement, JavaElementLabels.ALL_DEFAULT));
72 * @see IChange#getModifiedLanguageElement()
75 public Object getModifiedElement() {
76 return JavaCore.create(fHandle);
80 * @see org.eclipse.jdt.internal.corext.refactoring.base.JDTChange#getModifiedResource()
83 protected IResource getModifiedResource() {
84 IJavaElement elem= JavaCore.create(fHandle);
86 return elem.getResource();
92 * @see DeleteChange#doDelete(IProgressMonitor)
95 protected Change doDelete(IProgressMonitor pm) throws CoreException {
96 ISourceManipulation element= getSourceManipulation();
97 // we have to save dirty compilation units before deleting them. Otherwise
98 // we will end up showing ghost compilation units in the package explorer
99 // since the primary working copy still exists.
100 if (element instanceof ICompilationUnit) {
101 pm.beginTask("", 2); //$NON-NLS-1$
102 ICompilationUnit unit= (ICompilationUnit)element;
103 saveCUnitIfNeeded(unit, new SubProgressMonitor(pm, 1));
105 IResource resource= unit.getResource();
106 ResourceDescription resourceDescription = ResourceDescription.fromResource(resource);
107 element.delete(false, new SubProgressMonitor(pm, 1));
108 resourceDescription.recordStateFromHistory(resource, new SubProgressMonitor(pm, 1));
109 return new UndoDeleteResourceChange(resourceDescription);
111 } else if (element instanceof IPackageFragment) {
112 ICompilationUnit[] units= ((IPackageFragment)element).getCompilationUnits();
113 pm.beginTask("", units.length + 1); //$NON-NLS-1$
114 for (int i = 0; i < units.length; i++) {
115 // fix https://bugs.eclipse.org/bugs/show_bug.cgi?id=66835
116 saveCUnitIfNeeded(units[i], new SubProgressMonitor(pm, 1));
118 element.delete(false, new SubProgressMonitor(pm, 1));
119 return new NullChange(); // caveat: real undo implemented by UndoablePackageDeleteChange
122 element.delete(false, pm);
123 return null; //should not happen
127 private ISourceManipulation getSourceManipulation() {
128 return (ISourceManipulation) getModifiedElement();
131 private static IJavaElement getJavaElement(ISourceManipulation sm) {
132 //all known ISourceManipulations are IJavaElements
133 return (IJavaElement)sm;
136 private static void saveCUnitIfNeeded(ICompilationUnit unit, IProgressMonitor pm) throws CoreException {
137 saveFileIfNeeded((IFile)unit.getResource(), pm);