1 11 package org.eclipse.core.internal.resources.mapping; 12 13 import org.eclipse.core.internal.utils.Policy; 14 import org.eclipse.core.resources.*; 15 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 19 22 public class ResourceChangeDescriptionFactory implements IResourceChangeDescriptionFactory { 23 24 private ProposedResourceDelta root = new ProposedResourceDelta(ResourcesPlugin.getWorkspace().getRoot()); 25 26 32 private ProposedResourceDelta buildDeleteDelta(ProposedResourceDelta parentDelta, IResource resource) { 33 ProposedResourceDelta delta = parentDelta.getChild(resource.getName()); 35 if (delta == null) { 36 delta = new ProposedResourceDelta(resource); 37 parentDelta.add(delta); 38 } 39 delta.setKind(IResourceDelta.REMOVED); 40 if (resource.getType() == IResource.FILE) 41 return delta; 42 try { 44 IResource[] members = ((IContainer) resource).members(); 45 int childCount = members.length; 46 if (childCount > 0) { 47 ProposedResourceDelta[] childDeltas = new ProposedResourceDelta[childCount]; 48 for (int i = 0; i < childCount; i++) 49 childDeltas[i] = buildDeleteDelta(delta, members[i]); 50 } 51 } catch (CoreException e) { 52 } 54 return delta; 55 } 56 57 60 public void change(IFile file) { 61 ProposedResourceDelta delta = getDelta(file); 62 if (delta.getKind() == 0) 63 delta.setKind(IResourceDelta.CHANGED); 64 if (delta.getKind() == IResourceDelta.CHANGED 66 || (delta.getFlags() & IResourceDelta.MOVED_FROM) != 0 67 || (delta.getFlags() & IResourceDelta.COPIED_FROM) != 0) 68 delta.addFlags(IResourceDelta.CONTENT); 69 } 70 71 74 public void close(IProject project) { 75 delete(project); 76 ProposedResourceDelta delta = getDelta(project); 77 delta.addFlags(IResourceDelta.OPEN); 78 } 79 80 83 public void copy(IResource resource, IPath destination) { 84 moveOrCopyDeep(resource, destination, false ); 85 } 86 87 90 public void create(IResource resource) { 91 getDelta(resource).setKind(IResourceDelta.ADDED); 92 } 93 94 97 public void delete(IResource resource) { 98 if (resource.getType() == IResource.ROOT) { 99 IProject[] projects = ((IWorkspaceRoot)resource).getProjects(); 101 for (int i = 0; i < projects.length; i++) 102 buildDeleteDelta(root, projects[i]); 103 } else { 104 buildDeleteDelta(getDelta(resource.getParent()), resource); 105 } 106 } 107 108 private void fail(CoreException e) { 109 Policy.log(e.getStatus().getSeverity(), "An internal error occurred while accumulating a change description.", e); } 111 112 115 public IResourceDelta getDelta() { 116 return root; 117 } 118 119 ProposedResourceDelta getDelta(IResource resource) { 120 ProposedResourceDelta delta = (ProposedResourceDelta) root.findMember(resource.getFullPath()); 121 if (delta != null) { 122 return delta; 123 } 124 ProposedResourceDelta parent = getDelta(resource.getParent()); 125 delta = new ProposedResourceDelta(resource); 126 parent.add(delta); 127 return delta; 128 } 129 130 137 protected IResource getDestinationResource(IResource source, IPath sourcePrefix, IPath destinationPrefix) { 138 IPath relativePath = source.getFullPath().removeFirstSegments(sourcePrefix.segmentCount()); 139 IPath destinationPath = destinationPrefix.append(relativePath); 140 IResource destination; 141 IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); 142 switch (source.getType()) { 143 case IResource.FILE : 144 destination = wsRoot.getFile(destinationPath); 145 break; 146 case IResource.FOLDER : 147 destination = wsRoot.getFolder(destinationPath); 148 break; 149 case IResource.PROJECT : 150 destination = wsRoot.getProject(destinationPath.segment(0)); 151 break; 152 default : 153 destination = null; 155 } 156 return destination; 157 } 158 159 162 public void move(IResource resource, IPath destination) { 163 moveOrCopyDeep(resource, destination, true ); 164 } 165 166 175 boolean moveOrCopy(IResource resource, final IPath sourcePrefix, final IPath destinationPrefix, final boolean move) { 176 ProposedResourceDelta sourceDelta = getDelta(resource); 177 if (sourceDelta.getKind() == IResourceDelta.REMOVED) { 178 return false; 181 } 182 IResource destinationResource = getDestinationResource(resource, sourcePrefix, destinationPrefix); 183 ProposedResourceDelta destinationDelta = getDelta(destinationResource); 184 if ((destinationDelta.getKind() & (IResourceDelta.ADDED | IResourceDelta.CHANGED)) > 0) { 185 return false; 188 } 189 IPath fromPath = resource.getFullPath(); 191 boolean wasAdded = false; 192 final int sourceFlags = sourceDelta.getFlags(); 193 if (move) { 194 if (sourceDelta.getKind() == IResourceDelta.ADDED) { 196 if ((sourceFlags & IResourceDelta.MOVED_FROM) != 0) { 197 fromPath = sourceDelta.getMovedFromPath(); 200 sourceDelta.setMovedFromPath(null); 201 } 202 sourceDelta.setKind(0); 205 wasAdded = true; 206 } else { 207 sourceDelta.setKind(IResourceDelta.REMOVED); 209 sourceDelta.setFlags(IResourceDelta.MOVED_TO); 210 sourceDelta.setMovedToPath(destinationPrefix.append(fromPath.removeFirstSegments(sourcePrefix.segmentCount()))); 211 } 212 } 213 if (destinationDelta.getKind() == IResourceDelta.REMOVED) { 215 destinationDelta.setKind(IResourceDelta.CHANGED); 217 destinationDelta.addFlags(IResourceDelta.REPLACED); 218 } else { 219 destinationDelta.setKind(IResourceDelta.ADDED); 220 } 221 if (!wasAdded || !fromPath.equals(resource.getFullPath())) { 222 destinationDelta.addFlags(move ? IResourceDelta.MOVED_FROM : IResourceDelta.COPIED_FROM); 224 destinationDelta.setMovedFromPath(fromPath); 225 if (move) 227 destinationDelta.addFlags(sourceFlags); 228 } 229 230 return true; 231 } 232 233 237 private void moveOrCopyDeep(IResource resource, IPath destination, final boolean move) { 238 final IPath sourcePrefix = resource.getFullPath(); 239 final IPath destinationPrefix = destination; 240 try { 241 if (resource.isAccessible()) { 243 resource.accept(new IResourceVisitor() { 244 public boolean visit(IResource child) { 245 return moveOrCopy(child, sourcePrefix, destinationPrefix, move); 246 } 247 }); 248 } else { 249 moveOrCopy(resource, sourcePrefix, destination, move); 251 } 252 } catch (CoreException e) { 253 fail(e); 254 } 255 } 256 } 257 | Popular Tags |