KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > mapping > ChangeDescription


1 /*******************************************************************************
2  * Copyright (c) 2006 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.core.internal.resources.mapping;
12
13 import java.util.*;
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.runtime.IPath;
16
17 /**
18  * A description of the changes found in a delta
19  */

20 public class ChangeDescription {
21
22     private List addedRoots = new ArrayList();
23     private List changedFiles = new ArrayList();
24     private List closedProjects = new ArrayList();
25     private List copiedRoots = new ArrayList();
26     private List movedRoots = new ArrayList();
27     private List removedRoots = new ArrayList();
28
29     private IResource createSourceResource(IResourceDelta delta) {
30         IPath sourcePath = delta.getMovedFromPath();
31         IResource resource = delta.getResource();
32         IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
33         switch (resource.getType()) {
34             case IResource.PROJECT :
35                 return wsRoot.getProject(sourcePath.segment(0));
36             case IResource.FOLDER :
37                 return wsRoot.getFolder(sourcePath);
38             case IResource.FILE :
39                 return wsRoot.getFile(sourcePath);
40         }
41         return null;
42     }
43
44     private void ensureResourceCovered(IResource resource, List list) {
45         IPath path = resource.getFullPath();
46         for (Iterator iter = list.iterator(); iter.hasNext();) {
47             IResource root = (IResource) iter.next();
48             if (root.getFullPath().isPrefixOf(path)) {
49                 return;
50             }
51         }
52         list.add(resource);
53     }
54
55     public IResource[] getRootResources() {
56         Set result = new HashSet();
57         result.addAll(addedRoots);
58         result.addAll(changedFiles);
59         result.addAll(closedProjects);
60         result.addAll(copiedRoots);
61         result.addAll(movedRoots);
62         result.addAll(removedRoots);
63         return (IResource[]) result.toArray(new IResource[result.size()]);
64     }
65
66     private void handleAdded(IResourceDelta delta) {
67         if ((delta.getFlags() & IResourceDelta.MOVED_FROM) != 0) {
68             handleMove(delta);
69         } else if ((delta.getFlags() & IResourceDelta.COPIED_FROM) != 0) {
70             handleCopy(delta);
71         } else {
72             ensureResourceCovered(delta.getResource(), addedRoots);
73         }
74     }
75
76     private void handleChange(IResourceDelta delta) {
77         if ((delta.getFlags() & IResourceDelta.REPLACED) != 0) {
78             // A replace was added in place of a removed resource
79
handleAdded(delta);
80         } else if (delta.getResource().getType() == IResource.FILE) {
81             ensureResourceCovered(delta.getResource(), changedFiles);
82         }
83     }
84
85     private void handleCopy(IResourceDelta delta) {
86         if ((delta.getFlags() & IResourceDelta.COPIED_FROM) != 0) {
87             IResource source = createSourceResource(delta);
88             ensureResourceCovered(source, copiedRoots);
89         }
90     }
91
92     private void handleMove(IResourceDelta delta) {
93         if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
94             movedRoots.add(delta.getResource());
95         } else if ((delta.getFlags() & IResourceDelta.MOVED_FROM) != 0) {
96             IResource source = createSourceResource(delta);
97             ensureResourceCovered(source, movedRoots);
98         }
99     }
100
101     private void handleRemoved(IResourceDelta delta) {
102         if ((delta.getFlags() & IResourceDelta.OPEN) != 0) {
103             closedProjects.add(delta.getResource());
104         } else if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
105             handleMove(delta);
106         } else {
107             ensureResourceCovered(delta.getResource(), removedRoots);
108         }
109     }
110
111     /**
112      * Record the change and return whether any child changes should be visited.
113      * @param delta the change
114      * @return whether any child changes should be visited
115      */

116     public boolean recordChange(IResourceDelta delta) {
117         switch (delta.getKind()) {
118             case IResourceDelta.ADDED :
119                 handleAdded(delta);
120                 return true; // Need to traverse children to look for moves or other changes under added roots
121
case IResourceDelta.REMOVED :
122                 handleRemoved(delta);
123                 // No need to look for further changes under a remove (such as moves).
124
// Changes will be discovered in corresponding destination delta
125
return false;
126             case IResourceDelta.CHANGED :
127                 handleChange(delta);
128                 return true;
129         }
130         return true;
131     }
132
133 }
134
Popular Tags