KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > localstore > DeleteVisitor


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  * Matt McCutchen - fix for bug 174492
11  *******************************************************************************/

12 package org.eclipse.core.internal.localstore;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.filesystem.*;
17 import org.eclipse.core.filesystem.provider.FileInfo;
18 import org.eclipse.core.internal.resources.ICoreConstants;
19 import org.eclipse.core.internal.resources.Resource;
20 import org.eclipse.core.internal.utils.Messages;
21 import org.eclipse.core.internal.utils.Policy;
22 import org.eclipse.core.resources.*;
23 import org.eclipse.core.runtime.*;
24 import org.eclipse.osgi.util.NLS;
25
26 public class DeleteVisitor implements IUnifiedTreeVisitor, ICoreConstants {
27     protected boolean force;
28     protected boolean keepHistory;
29     protected IProgressMonitor monitor;
30     protected List JavaDoc skipList;
31     protected MultiStatus status;
32
33     /**
34      * The number of tickets available on the progress monitor
35      */

36     private int ticks;
37
38     public DeleteVisitor(List JavaDoc skipList, int flags, IProgressMonitor monitor, int ticks) {
39         this.skipList = skipList;
40         this.ticks = ticks;
41         this.force = (flags & IResource.FORCE) != 0;
42         this.keepHistory = (flags & IResource.KEEP_HISTORY) != 0;
43         this.monitor = monitor;
44         status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_DELETE_LOCAL, Messages.localstore_deleteProblem, null);
45     }
46
47     /**
48      * Deletes a file from both the workspace resource tree and the file system.
49      */

50     protected void delete(UnifiedTreeNode node, boolean shouldKeepHistory) {
51         Resource target = (Resource) node.getResource();
52         try {
53             final boolean deleteLocalFile = !target.isLinked() && node.existsInFileSystem();
54             IFileStore localFile = deleteLocalFile ? node.getStore() : null;
55             if (deleteLocalFile && shouldKeepHistory)
56                 recursiveKeepHistory(target.getLocalManager().getHistoryStore(), node);
57             node.removeChildrenFromTree();
58             //delete from disk
59
int work = ticks < 0 ? 0 : ticks;
60             ticks -= work;
61             if (deleteLocalFile)
62                 localFile.delete(EFS.NONE, Policy.subMonitorFor(monitor, work));
63             else
64                 monitor.worked(work);
65             //delete from tree
66
if (node.existsInWorkspace())
67                 target.deleteResource(true, status);
68         } catch (CoreException e) {
69             status.add(e.getStatus());
70             // delete might have been partly successful, so refresh to ensure in sync
71
try {
72                 target.refreshLocal(IResource.DEPTH_INFINITE, null);
73             } catch (CoreException e1) {
74                 //ignore secondary failure - we are just trying to cleanup from first failure
75
}
76         }
77     }
78
79     /**
80      * Only consider path in equality in order to handle gender changes
81      */

82     protected boolean equals(IResource one, IResource another) {
83         return one.getFullPath().equals(another.getFullPath());
84     }
85
86     public MultiStatus getStatus() {
87         return status;
88     }
89
90     protected boolean isAncestor(IResource one, IResource another) {
91         return one.getFullPath().isPrefixOf(another.getFullPath()) && !equals(one, another);
92     }
93
94     protected boolean isAncestorOfResourceToSkip(IResource resource) {
95         if (skipList == null)
96             return false;
97         for (int i = 0; i < skipList.size(); i++) {
98             IResource target = (IResource) skipList.get(i);
99             if (isAncestor(resource, target))
100                 return true;
101         }
102         return false;
103     }
104
105     private void recursiveKeepHistory(IHistoryStore store, UnifiedTreeNode node) {
106         final IResource target = node.getResource();
107         //we don't delete linked content, so no need to keep history
108
if (target.isLinked() || node.isSymbolicLink())
109             return;
110         if (node.isFolder()) {
111             monitor.subTask(NLS.bind(Messages.localstore_deleting, target.getFullPath()));
112             for (Iterator JavaDoc children = node.getChildren(); children.hasNext();)
113                 recursiveKeepHistory(store, (UnifiedTreeNode) children.next());
114         } else {
115             IFileInfo info = node.fileInfo;
116             if (info == null)
117                 info = new FileInfo(node.getLocalName());
118             store.addState(target.getFullPath(), node.getStore(), info, true);
119         }
120         monitor.worked(1);
121         ticks--;
122     }
123
124     protected void removeFromSkipList(IResource resource) {
125         if (skipList != null)
126             skipList.remove(resource);
127     }
128
129     protected boolean shouldSkip(IResource resource) {
130         if (skipList == null)
131             return false;
132         for (int i = 0; i < skipList.size(); i++)
133             if (equals(resource, (IResource) skipList.get(i)))
134                 return true;
135         return false;
136     }
137
138     public boolean visit(UnifiedTreeNode node) {
139         Policy.checkCanceled(monitor);
140         Resource target = (Resource) node.getResource();
141         if (shouldSkip(target)) {
142             removeFromSkipList(target);
143             int skipTicks = target.countResources(IResource.DEPTH_INFINITE, false);
144             monitor.worked(skipTicks);
145             ticks -= skipTicks;
146             return false;
147         }
148         if (isAncestorOfResourceToSkip(target))
149             return true;
150         delete(node, keepHistory);
151         return false;
152     }
153 }
154
Popular Tags