KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > mapping > provider > ResourceDiffTree


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.core.mapping.provider;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.resources.mapping.ResourceTraversal;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.team.core.diff.*;
20 import org.eclipse.team.core.diff.provider.DiffTree;
21 import org.eclipse.team.core.mapping.IResourceDiff;
22 import org.eclipse.team.core.mapping.IResourceDiffTree;
23
24 /**
25  * Implementation of {@link IResourceDiffTree}.
26  * <p>
27  * This class is not intended to be subclassed by clients.
28  *
29  * @since 3.2
30  */

31 public class ResourceDiffTree extends DiffTree implements IResourceDiffTree {
32
33     /**
34      * Get the resource for the diff node that was obtained from an
35      * {@link IResourceDiffTree}.
36      * @param node the diff node.
37      * @return the resource for the diff node
38      */

39     public static IResource getResourceFor(IDiff node) {
40         if (node instanceof IResourceDiff) {
41             IResourceDiff rd = (IResourceDiff) node;
42             return rd.getResource();
43         }
44         if (node instanceof IThreeWayDiff) {
45             IThreeWayDiff twd = (IThreeWayDiff) node;
46             IDiff child = twd.getLocalChange();
47             if (child != null)
48                 return getResourceFor(child);
49             child = twd.getRemoteChange();
50             if (child != null)
51                 return getResourceFor(child);
52         }
53         Assert.isLegal(false);
54         return null;
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.team.core.diff.IResourceDiffTree#getDiff(org.eclipse.core.resources.IResource)
59      */

60     public IDiff getDiff(IResource resource) {
61         return getDiff(resource.getFullPath());
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.team.core.diff.IResourceDiffTree#getResource(org.eclipse.team.core.diff.IDiffNode)
66      */

67     public IResource getResource(IDiff diff) {
68         if (diff instanceof IThreeWayDiff) {
69             IThreeWayDiff twd = (IThreeWayDiff) diff;
70             IResourceDiff localChange = ((IResourceDiff)twd.getLocalChange());
71             if (localChange != null)
72                 return localChange.getResource();
73             return ((IResourceDiff)twd.getRemoteChange()).getResource();
74         } else {
75             return ((IResourceDiff)diff).getResource();
76         }
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.team.core.diff.IResourceDiffTree#accept(org.eclipse.team.core.diff.IDiffVisitor, org.eclipse.core.resources.mapping.ResourceTraversal[])
81      */

82     public void accept(ResourceTraversal[] traversals, IDiffVisitor visitor) {
83         for (int i = 0; i < traversals.length; i++) {
84             ResourceTraversal traversal = traversals[i];
85             IResource[] resources = traversal.getResources();
86             for (int j = 0; j < resources.length; j++) {
87                 IResource resource = resources[j];
88                 accept(resource.getFullPath(), visitor, traversal.getDepth());
89             }
90         }
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.team.core.mapping.IResourceDiffTree#getDiffs(org.eclipse.core.resources.mapping.ResourceTraversal[])
95      */

96     public IDiff[] getDiffs(final ResourceTraversal[] traversals) {
97         final Set result = new HashSet();
98         for (int i = 0; i < traversals.length; i++) {
99             ResourceTraversal traversal = traversals[i];
100             IResource[] resources = traversal.getResources();
101             for (int j = 0; j < resources.length; j++) {
102                 IResource resource = resources[j];
103                 internalGetDiffs(resource, traversal.getDepth(), result);
104             }
105         }
106         return (IDiff[]) result.toArray(new IDiff[result.size()]);
107     }
108     
109     /* (non-Javadoc)
110      * @see org.eclipse.team.core.mapping.IResourceDiffTree#getDiffs(org.eclipse.core.resources.IResource, int)
111      */

112     public IDiff[] getDiffs(IResource resource, int depth) {
113         final Set result = new HashSet();
114         internalGetDiffs(resource, depth, result);
115         return (IDiff[]) result.toArray(new IDiff[result.size()]);
116     }
117
118     private void internalGetDiffs(IResource resource, int depth, final Set result) {
119         accept(resource.getFullPath(), new IDiffVisitor() {
120             public boolean visit(IDiff diff) {
121                 return result.add(diff);
122             }
123         }, depth);
124     }
125
126     private IResource internalGetResource(IPath fullPath, boolean container) {
127         if (container) {
128             if (fullPath.segmentCount() == 1)
129                 return ResourcesPlugin.getWorkspace().getRoot().getProject(fullPath.segment(0));
130             return ResourcesPlugin.getWorkspace().getRoot().getFolder(fullPath);
131         }
132         return ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.team.core.diff.IResourceDiffTree#members(org.eclipse.core.resources.IResource)
137      */

138     public IResource[] members(IResource resource) {
139         List result = new ArrayList();
140         IPath[] paths = getChildren(resource.getFullPath());
141         for (int i = 0; i < paths.length; i++) {
142             IPath path = paths[i];
143             IDiff node = getDiff(path);
144             if (node == null) {
145                 result.add(internalGetResource(path, true));
146             } else {
147                 result.add(getResource(node));
148             }
149         }
150         return (IResource[]) result.toArray(new IResource[result.size()]);
151     }
152
153     /* (non-Javadoc)
154      * @see org.eclipse.team.core.mapping.IResourceDiffTree#getAffectedResources()
155      */

156     public IResource[] getAffectedResources() {
157         List result = new ArrayList();
158         IDiff[] nodes = getDiffs();
159         for (int i = 0; i < nodes.length; i++) {
160             IDiff node = nodes[i];
161             result.add(getResource(node));
162         }
163         return (IResource[]) result.toArray(new IResource[result.size()]);
164     }
165     
166     /* (non-Javadoc)
167      * @see org.eclipse.team.core.mapping.provider.DiffTree#add(org.eclipse.team.core.diff.IDiffNode)
168      */

169     public void add(IDiff delta) {
170         Assert.isTrue(delta instanceof IResourceDiff || delta instanceof IThreeWayDiff);
171         super.add(delta);
172     }
173     
174     /**
175      * Remove the diff associated with the given resource from
176      * the tree.
177      * @param resource the resource
178      */

179     public void remove(IResource resource) {
180         remove(resource.getFullPath());
181     }
182
183     public boolean hasMatchingDiffs(ResourceTraversal[] traversals, final FastDiffFilter filter) {
184         final RuntimeException JavaDoc found = new RuntimeException JavaDoc();
185         try {
186             accept(traversals, new IDiffVisitor() {
187                 public boolean visit(IDiff delta) {
188                     if (filter.select(delta)) {
189                         throw found;
190                     }
191                     return false;
192                 }
193             
194             });
195         } catch (RuntimeException JavaDoc e) {
196             if (e == found)
197                 return true;
198             throw e;
199         }
200         return false;
201     }
202 }
203
Popular Tags