]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/DeleteSourceManipulationChange.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / changes / DeleteSourceManipulationChange.java
CommitLineData
1b2798f6
EK
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
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.changes;
12
13import org.eclipse.core.runtime.Assert;
14import org.eclipse.core.runtime.CoreException;
15import org.eclipse.core.runtime.IProgressMonitor;
16import org.eclipse.core.runtime.SubProgressMonitor;
17
18import org.eclipse.core.resources.IFile;
19import org.eclipse.core.resources.IResource;
20
21import org.eclipse.ui.ide.undo.ResourceDescription;
22
23import org.eclipse.ltk.core.refactoring.Change;
24import org.eclipse.ltk.core.refactoring.NullChange;
25
26import org.eclipse.jdt.core.ICompilationUnit;
27import org.eclipse.jdt.core.IJavaElement;
28import org.eclipse.jdt.core.IPackageFragment;
29import org.eclipse.jdt.core.ISourceManipulation;
30import org.eclipse.jdt.core.JavaCore;
31
32import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
33import org.eclipse.jdt.internal.corext.util.Messages;
34
35import org.eclipse.jdt.ui.JavaElementLabels;
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 */
41public class DeleteSourceManipulationChange extends AbstractDeleteChange {
42
43 private final String fHandle;
44
45 public DeleteSourceManipulationChange(ISourceManipulation sm, boolean isExecuteChange) {
46 Assert.isNotNull(sm);
47 fHandle= getJavaElement(sm).getHandleIdentifier();
48
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);
54 } else {
55 setValidationMethod(VALIDATE_NOT_DIRTY);
56 }
57 } else {
58 setValidationMethod(VALIDATE_NOT_DIRTY | VALIDATE_NOT_READ_ONLY);
59 }
60 }
61
62 /*
63 * @see IChange#getName()
64 */
65 @Override
66 public String getName() {
67 IJavaElement javaElement= getJavaElement(getSourceManipulation());
68 return Messages.format(RefactoringCoreMessages.DeleteSourceManipulationChange_0, JavaElementLabels.getElementLabel(javaElement, JavaElementLabels.ALL_DEFAULT));
69 }
70
71 /*
72 * @see IChange#getModifiedLanguageElement()
73 */
74 @Override
75 public Object getModifiedElement() {
76 return JavaCore.create(fHandle);
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.jdt.internal.corext.refactoring.base.JDTChange#getModifiedResource()
81 */
82 @Override
83 protected IResource getModifiedResource() {
84 IJavaElement elem= JavaCore.create(fHandle);
85 if (elem != null) {
86 return elem.getResource();
87 }
88 return null;
89 }
90
91 /*
92 * @see DeleteChange#doDelete(IProgressMonitor)
93 */
94 @Override
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));
104
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);
110
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));
117 }
118 element.delete(false, new SubProgressMonitor(pm, 1));
119 return new NullChange(); // caveat: real undo implemented by UndoablePackageDeleteChange
120
121 } else {
122 element.delete(false, pm);
123 return null; //should not happen
124 }
125 }
126
127 private ISourceManipulation getSourceManipulation() {
128 return (ISourceManipulation) getModifiedElement();
129 }
130
131 private static IJavaElement getJavaElement(ISourceManipulation sm) {
132 //all known ISourceManipulations are IJavaElements
133 return (IJavaElement)sm;
134 }
135
136 private static void saveCUnitIfNeeded(ICompilationUnit unit, IProgressMonitor pm) throws CoreException {
137 saveFileIfNeeded((IFile)unit.getResource(), pm);
138 }
139}
140