01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.changes;
11:
12: import org.eclipse.core.runtime.Assert;
13: import org.eclipse.core.runtime.CoreException;
14: import org.eclipse.core.runtime.IProgressMonitor;
15: import org.eclipse.core.runtime.SubProgressMonitor;
16:
17: import org.eclipse.core.resources.IFile;
18:
19: import org.eclipse.ui.ide.undo.ResourceDescription;
20:
21: import org.eclipse.ltk.core.refactoring.Change;
22: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
23:
24: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
25: import org.eclipse.jdt.internal.corext.util.Messages;
26:
27: public class DeleteFileChange extends AbstractDeleteChange {
28:
29: private final boolean fIsExecuteChange;
30: private final IFile fFile;
31:
32: public DeleteFileChange(IFile file, boolean executeChange) {
33: Assert.isNotNull(file, "file"); //$NON-NLS-1$
34: fIsExecuteChange = executeChange;
35: fFile = file;
36: }
37:
38: private IFile getFile() {
39: return fFile;
40: }
41:
42: /* non java-doc
43: * @see IChange#getName()
44: */
45: public String getName() {
46: return Messages.format(
47: RefactoringCoreMessages.DeleteFileChange_1, fFile
48: .getName());
49: }
50:
51: public RefactoringStatus isValid(IProgressMonitor pm)
52: throws CoreException {
53: if (fIsExecuteChange) {
54: // no need for checking since we already prompt the
55: // user if the file is dirty or read only
56: return super .isValid(pm, NONE);
57: } else {
58: return super .isValid(pm, READ_ONLY | DIRTY);
59: }
60: }
61:
62: /* non java-doc
63: * @see IChange#getModifiedLanguageElement()
64: */
65: public Object getModifiedElement() {
66: return getFile();
67: }
68:
69: /* non java-doc
70: * @see DeleteChange#doDelete(IProgressMonitor)
71: */
72: protected Change doDelete(IProgressMonitor pm) throws CoreException {
73: IFile file = getFile();
74: Assert.isNotNull(file);
75: Assert.isTrue(file.exists());
76: pm.beginTask("", 3); //$NON-NLS-1$
77: saveFileIfNeeded(file, new SubProgressMonitor(pm, 1));
78:
79: ResourceDescription resourceDescription = ResourceDescription
80: .fromResource(file);
81: file.delete(false, true, new SubProgressMonitor(pm, 1));
82: resourceDescription.recordStateFromHistory(file,
83: new SubProgressMonitor(pm, 1));
84: pm.done();
85:
86: return new UndoDeleteResourceChange(resourceDescription);
87: }
88: }
|