KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.core.runtime.Assert;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.SubProgressMonitor;
17
18 import org.eclipse.core.resources.IFile;
19
20 import org.eclipse.ui.ide.undo.ResourceDescription;
21
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
24
25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
26 import org.eclipse.jdt.internal.corext.util.Messages;
27
28 public class DeleteFileChange extends AbstractDeleteChange {
29
30     private final IPath fPath;
31     private final boolean fIsExecuteChange;
32     
33     public DeleteFileChange(IFile file, boolean executeChange) {
34         Assert.isNotNull(file, "file"); //$NON-NLS-1$
35
fPath= Utils.getResourcePath(file);
36         fIsExecuteChange= executeChange;
37     }
38     
39     private IFile getFile(){
40         return Utils.getFile(fPath);
41     }
42     
43     /* non java-doc
44      * @see IChange#getName()
45      */

46     public String JavaDoc getName() {
47         return Messages.format(RefactoringCoreMessages.DeleteFileChange_1, fPath.lastSegment());
48     }
49
50     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
51         if (fIsExecuteChange) {
52             // no need for checking since we already prompt the
53
// user if the file is dirty or read only
54
return super.isValid(pm, NONE);
55         } else {
56             return super.isValid(pm, READ_ONLY | DIRTY);
57         }
58     }
59
60     /* non java-doc
61      * @see IChange#getModifiedLanguageElement()
62      */

63     public Object JavaDoc getModifiedElement() {
64         return getFile();
65     }
66
67     /* non java-doc
68      * @see DeleteChange#doDelete(IProgressMonitor)
69      */

70     protected Change doDelete(IProgressMonitor pm) throws CoreException {
71         IFile file= getFile();
72         Assert.isNotNull(file);
73         Assert.isTrue(file.exists());
74         pm.beginTask("", 3); //$NON-NLS-1$
75
saveFileIfNeeded(file, new SubProgressMonitor(pm, 1));
76         
77         ResourceDescription resourceDescription = ResourceDescription.fromResource(file);
78         file.delete(false, true, new SubProgressMonitor(pm, 1));
79         resourceDescription.recordStateFromHistory(file, new SubProgressMonitor(pm, 1));
80         pm.done();
81         
82         return new UndoDeleteResourceChange(resourceDescription);
83     }
84 }
85
86
Popular Tags