1 12 package org.eclipse.core.internal.localstore; 13 14 import java.util.Iterator ; 15 import java.util.List ; 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 skipList; 31 protected MultiStatus status; 32 33 36 private int ticks; 37 38 public DeleteVisitor(List 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 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 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 if (node.existsInWorkspace()) 67 target.deleteResource(true, status); 68 } catch (CoreException e) { 69 status.add(e.getStatus()); 70 try { 72 target.refreshLocal(IResource.DEPTH_INFINITE, null); 73 } catch (CoreException e1) { 74 } 76 } 77 } 78 79 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 if (target.isLinked() || node.isSymbolicLink()) 109 return; 110 if (node.isFolder()) { 111 monitor.subTask(NLS.bind(Messages.localstore_deleting, target.getFullPath())); 112 for (Iterator 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 |