KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > DeleteSourceManipulationChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
38  * Caveat: undo of package fragment deletes is provided by a wrapping
39  * UndoablePackageDeleteChange. This change returns a NullChange as undo for package fragments.
40  */

41 public class DeleteSourceManipulationChange extends AbstractDeleteChange {
42
43     private final String JavaDoc 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     /*
53      * @see IChange#getName()
54      */

55     public String JavaDoc 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                 // don't check anything in this case. We have a warning dialog
64
// already presented to the user that the file is dirty.
65
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 JavaDoc getElementName() {
75         IJavaElement javaElement= getJavaElement(getSourceManipulation());
76         if (JavaElementUtil.isDefaultPackage(javaElement))
77             return RefactoringCoreMessages.DeleteSourceManipulationChange_1;
78         return javaElement.getElementName();
79     }
80
81     /*
82      * @see IChange#getModifiedLanguageElement()
83      */

84     public Object JavaDoc getModifiedElement() {
85         return JavaCore.create(fHandle);
86     }
87     
88     /*
89      * @see DeleteChange#doDelete(IProgressMonitor)
90      */

91     protected Change doDelete(IProgressMonitor pm) throws CoreException {
92         ISourceManipulation element= getSourceManipulation();
93         // we have to save dirty compilation units before deleting them. Otherwise
94
// we will end up showing ghost compilation units in the package explorer
95
// since the primary working copy still exists.
96
if (element instanceof ICompilationUnit) {
97             pm.beginTask("", 2); //$NON-NLS-1$
98
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); //$NON-NLS-1$
110
for (int i = 0; i < units.length; i++) {
111                 // fix https://bugs.eclipse.org/bugs/show_bug.cgi?id=66835
112
saveCUnitIfNeeded(units[i], new SubProgressMonitor(pm, 1));
113             }
114             element.delete(false, new SubProgressMonitor(pm, 1));
115             return new NullChange(); // caveat: real undo implemented by UndoablePackageDeleteChange
116

117         } else {
118             element.delete(false, pm);
119             return null; //should not happen
120
}
121     }
122         
123     private ISourceManipulation getSourceManipulation() {
124         return (ISourceManipulation) getModifiedElement();
125     }
126
127     private static IJavaElement getJavaElement(ISourceManipulation sm) {
128         //all known ISourceManipulations are IJavaElements
129
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