KickJava   Java API By Example, From Geeks To Geeks.

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


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.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.SubProgressMonitor;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceVisitor;
24 import org.eclipse.core.resources.ResourcesPlugin;
25
26 import org.eclipse.ui.ide.undo.ResourceDescription;
27
28 import org.eclipse.ltk.core.refactoring.Change;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30
31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
32 import org.eclipse.jdt.internal.corext.util.Messages;
33
34 public class DeleteFolderChange extends AbstractDeleteChange {
35     
36     private final IPath fPath;
37     private final boolean fIsExecuteChange;
38     
39     public DeleteFolderChange(IFolder folder, boolean isExecuteChange) {
40         this(getFolderPath(folder), isExecuteChange);
41     }
42     
43     public DeleteFolderChange(IPath path, boolean isExecuteChange) {
44         fPath= path;
45         fIsExecuteChange= isExecuteChange;
46     }
47     
48     public static IPath getFolderPath(IFolder folder){
49         return folder.getFullPath().removeFirstSegments(ResourcesPlugin.getWorkspace().getRoot().getFullPath().segmentCount());
50     }
51     
52     public static IFolder getFolder(IPath path){
53         return ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
54     }
55
56     public String JavaDoc getName() {
57         return Messages.format(RefactoringCoreMessages.DeleteFolderChange_0, fPath.lastSegment());
58     }
59     
60     public Object JavaDoc getModifiedElement() {
61         return getFolder(fPath);
62     }
63
64     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
65         if (fIsExecuteChange) {
66             // no need to do additional checking since the dialog
67
// already prompts the user if there are dirty
68
// or read only files in the folder. The change is
69
// currently not used as a undo/redo change
70
return super.isValid(pm, NONE);
71         } else {
72             return super.isValid(pm, READ_ONLY | DIRTY);
73         }
74     }
75
76     protected Change doDelete(IProgressMonitor pm) throws CoreException{
77         IFolder folder= getFolder(fPath);
78         Assert.isTrue(folder.exists());
79         pm.beginTask("", 3); //$NON-NLS-1$
80
folder.accept(new IResourceVisitor() {
81             public boolean visit(IResource resource) throws CoreException {
82                 if (resource instanceof IFile) {
83                     // progress is covered outside.
84
saveFileIfNeeded((IFile)resource, new NullProgressMonitor());
85                 }
86                 return true;
87             }
88         }, IResource.DEPTH_INFINITE, false);
89         pm.worked(1);
90         
91         ResourceDescription resourceDescription = ResourceDescription.fromResource(folder);
92         folder.delete(false, true, new SubProgressMonitor(pm, 1));
93         resourceDescription.recordStateFromHistory(folder, new SubProgressMonitor(pm, 1));
94         pm.done();
95         
96         return new UndoDeleteResourceChange(resourceDescription);
97     }
98 }
99
100
Popular Tags