KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceModelTraversalCalculator


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  *******************************************************************************/

11 package org.eclipse.team.internal.ui.mapping;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.resources.mapping.ModelProvider;
17 import org.eclipse.core.resources.mapping.ResourceTraversal;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.viewers.TreePath;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.team.core.diff.IDiff;
23 import org.eclipse.team.core.mapping.IResourceDiffTree;
24 import org.eclipse.team.core.mapping.ISynchronizationContext;
25 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
26 import org.eclipse.team.internal.core.subscribers.ChangeSet;
27 import org.eclipse.team.internal.core.subscribers.DiffChangeSet;
28 import org.eclipse.team.internal.ui.*;
29 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
30 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
31
32 public class ResourceModelTraversalCalculator {
33
34     public static final String JavaDoc PROP_TRAVERSAL_CALCULATOR = "org.eclipse.team.ui.resourceModelraversalCalculator"; //$NON-NLS-1$
35
private static ResourceModelTraversalCalculator instance;
36     private final ISynchronizePageConfiguration configuration;
37
38     public ResourceModelTraversalCalculator(
39             ISynchronizePageConfiguration configuration) {
40                 this.configuration = configuration;
41     }
42
43     public ResourceModelTraversalCalculator() {
44         configuration = null;
45     }
46
47     public int getLayoutDepth(IResource resource, TreePath path) {
48         if (isFlatPageLayout()) {
49             return (resource instanceof IWorkspaceRoot) ? IResource.DEPTH_INFINITE : IResource.DEPTH_ZERO;
50         }
51         if (resource.getType() == IResource.PROJECT)
52             return IResource.DEPTH_INFINITE;
53         if (resource.getType() == IResource.FILE)
54             return IResource.DEPTH_ZERO;
55         if (path != null && hasNonResource(path))
56             return IResource.DEPTH_INFINITE;
57         if (getLayout().equals(IPreferenceIds.FLAT_LAYOUT)) {
58             return IResource.DEPTH_ZERO;
59         } else if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT)) {
60             return IResource.DEPTH_ONE;
61         }
62         return IResource.DEPTH_INFINITE;
63     }
64     
65     public String JavaDoc getLayout() {
66         return TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCVIEW_DEFAULT_LAYOUT);
67     }
68     
69     public Object JavaDoc[] filterChildren(IResourceDiffTree diffTree, IResource resource, Object JavaDoc parentOrPath, Object JavaDoc[] children) {
70         if (parentOrPath instanceof TreePath) {
71             TreePath tp = (TreePath) parentOrPath;
72             if (hasNonResource(tp)) {
73                 return getTreeChildren(diffTree, resource, children);
74             }
75         }
76         if (isFlatPageLayout()) {
77             if (resource instanceof IWorkspaceRoot) {
78                 return diffTree.getAffectedResources();
79             } else {
80                 return new Object JavaDoc[0];
81             }
82         } else if (getLayout().equals(IPreferenceIds.FLAT_LAYOUT) && resource.getType() == IResource.PROJECT) {
83             return getFlatChildren(diffTree, resource);
84         } else if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT) && resource.getType() == IResource.PROJECT) {
85             return getCompressedChildren(diffTree, (IProject)resource, children);
86         } else if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT) && resource.getType() == IResource.FOLDER) {
87             return getCompressedChildren(diffTree, (IFolder)resource, children);
88         }
89         return getTreeChildren(diffTree, resource, children);
90     }
91     
92     private boolean hasNonResource(TreePath parentPath) {
93         for (int i = 0; i < parentPath.getSegmentCount(); i++) {
94             Object JavaDoc o = parentPath.getSegment(i);
95             if (!(o instanceof IResource) && !(o instanceof ModelProvider) && !(o instanceof ChangeSet)) {
96                 return true;
97             }
98         }
99         return false;
100     }
101     
102     private Object JavaDoc[] getCompressedChildren(IResourceDiffTree diffTree, IProject project, Object JavaDoc[] children) {
103         Set result = new HashSet();
104         IDiff[] diffs = diffTree.getDiffs(project, IResource.DEPTH_INFINITE);
105         for (int i = 0; i < diffs.length; i++) {
106             IDiff diff = diffs[i];
107             IResource resource = diffTree.getResource(diff);
108             if (resource.getType() == IResource.FILE) {
109                 IContainer parent = resource.getParent();
110                 if (parent.getType() == IResource.FOLDER)
111                     result.add(parent);
112                 else
113                     result.add(resource);
114             } else if (resource.getType() == IResource.FOLDER)
115                 result.add(resource);
116         }
117         return result.toArray();
118     }
119
120     /*
121      * Only return the files that are direct children of the folder
122      */

123     private Object JavaDoc[] getCompressedChildren(IResourceDiffTree diffTree, IFolder folder, Object JavaDoc[] children) {
124         Set result = new HashSet();
125         for (int i = 0; i < children.length; i++) {
126             Object JavaDoc object = children[i];
127             if (object instanceof IResource) {
128                 IResource resource = (IResource) object;
129                 if (resource.getType() == IResource.FILE)
130                     result.add(resource);
131             }
132         }
133         IDiff[] diffs = diffTree.getDiffs(folder, IResource.DEPTH_ONE);
134         for (int i = 0; i < diffs.length; i++) {
135             IDiff diff = diffs[i];
136             IResource resource = diffTree.getResource(diff);
137             if (resource.getType() == IResource.FILE)
138                 result.add(resource);
139         }
140         return result.toArray();
141     }
142
143     private Object JavaDoc[] getFlatChildren(IResourceDiffTree diffTree, IResource resource) {
144         Object JavaDoc[] allChildren;
145         IDiff[] diffs = diffTree.getDiffs(resource, IResource.DEPTH_INFINITE);
146         ArrayList result = new ArrayList();
147         for (int i = 0; i < diffs.length; i++) {
148             IDiff diff = diffs[i];
149             result.add(diffTree.getResource(diff));
150         }
151         allChildren = result.toArray();
152         return allChildren;
153     }
154
155     private Object JavaDoc[] getTreeChildren(IResourceDiffTree diffTree, IResource resource, Object JavaDoc[] children) {
156         Set result = new HashSet();
157         for (int i = 0; i < children.length; i++) {
158             Object JavaDoc object = children[i];
159             result.add(object);
160         }
161         IResource[] setChildren = getChildren(diffTree, resource);
162         for (int i = 0; i < setChildren.length; i++) {
163             IResource child = setChildren[i];
164             result.add(child);
165         }
166         Object JavaDoc[] allChildren = result.toArray(new Object JavaDoc[result.size()]);
167         return allChildren;
168     }
169
170     public static IResource[] getChildren(IResourceDiffTree diffTree, IResource resource) {
171         Set result = new HashSet();
172         IPath[] childPaths = diffTree.getChildren(resource.getFullPath());
173         for (int i = 0; i < childPaths.length; i++) {
174             IPath path = childPaths[i];
175             IDiff delta = diffTree.getDiff(path);
176             IResource child;
177             if (delta == null) {
178                 // the path has descendent deltas so it must be a folder
179
if (path.segmentCount() == 1) {
180                     child = ((IWorkspaceRoot)resource).getProject(path.lastSegment());
181                 } else {
182                     child = ((IContainer)resource).getFolder(new Path(path.lastSegment()));
183                 }
184             } else {
185                 child = diffTree.getResource(delta);
186             }
187             result.add(child);
188         }
189         return (IResource[]) result.toArray(new IResource[result.size()]);
190     }
191
192     public ResourceTraversal[] getTraversals(DiffChangeSet dcs, TreePath tp) {
193         IResource[] resources = getResource(dcs, tp);
194         return new ResourceTraversal[] { new ResourceTraversal(resources, IResource.DEPTH_ZERO, IResource.NONE) };
195     }
196
197     private IResource[] getResource(DiffChangeSet dcs, TreePath tp) {
198         if (tp.getSegmentCount() == 1 && tp.getFirstSegment() == dcs) {
199             return dcs.getResources();
200         }
201         Set result = new HashSet();
202         Object JavaDoc o = tp.getLastSegment();
203         if (o instanceof IResource) {
204             IResource resource = (IResource) o;
205             int depth = getLayoutDepth(resource, tp);
206             IDiff[] diffs = dcs.getDiffTree().getDiffs(resource, depth);
207             for (int i = 0; i < diffs.length; i++) {
208                 IDiff diff = diffs[i];
209                 IResource r = ResourceDiffTree.getResourceFor(diff);
210                 if (r != null)
211                     result.add(r);
212             }
213         }
214         return (IResource[]) result.toArray(new IResource[result.size()]);
215     }
216
217     public ResourceTraversal[] getTraversals(IResource resource, TreePath tp) {
218         return new ResourceTraversal[] { new ResourceTraversal(new IResource[] { resource }, getLayoutDepth(resource, tp), IResource.NONE) };
219     }
220
221     public boolean isResourcePath(TreePath path) {
222         for (int i = 0; i < path.getSegmentCount(); i++) {
223             Object JavaDoc o = path.getSegment(i);
224             if (!(o instanceof IResource)) {
225                 return false;
226             }
227         }
228         return true;
229     }
230     
231     public String JavaDoc getLabel(Object JavaDoc elementOrPath) {
232         if (elementOrPath instanceof TreePath && hasNonResource((TreePath)elementOrPath)) {
233             return null;
234         }
235         Object JavaDoc element = internalGetElement(elementOrPath);
236         Object JavaDoc parent = internalGetElementParent(elementOrPath);
237         if (element instanceof IResource) {
238             IResource resource = (IResource) element;
239             if (isFlatPageLayout()) {
240                 IPath path = resource.getFullPath();
241                 if (!path.isEmpty())
242                     return NLS.bind(TeamUIMessages.ResourceModelLabelProvider_0, resource.getName(), path.toString());
243             }
244             if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT)
245                     && resource.getType() == IResource.FOLDER
246                     && (parent == null || parent instanceof IProject)) {
247                 return resource.getProjectRelativePath().toString();
248             }
249             if (getLayout().equals(IPreferenceIds.FLAT_LAYOUT)
250                     && resource.getType() == IResource.FILE
251                     && (parent == null || parent instanceof IProject)) {
252                 IPath parentPath = resource.getProjectRelativePath().removeLastSegments(1);
253                 if (!parentPath.isEmpty())
254                     return NLS.bind(TeamUIMessages.ResourceModelLabelProvider_0, resource.getName(), parentPath.toString());
255             }
256         }
257         return null;
258     }
259     
260     public boolean isCompressedFolder(Object JavaDoc elementOrPath) {
261         if (elementOrPath instanceof TreePath && hasNonResource((TreePath)elementOrPath)) {
262             return false;
263         }
264         Object JavaDoc element = internalGetElement(elementOrPath);
265         Object JavaDoc parent = internalGetElementParent(elementOrPath);
266         if (element instanceof IResource) {
267             IResource resource = (IResource) element;
268             // Only use the compressed folder icon if the parent is not known
269
// or the parent is a project
270
return getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT)
271                 && resource.getType() == IResource.FOLDER
272                 && (parent == null || parent instanceof IProject);
273         }
274         return false;
275     }
276     
277     private TreePath internalGetPath(Object JavaDoc elementOrPath) {
278         if (elementOrPath instanceof TreePath) {
279             return (TreePath) elementOrPath;
280         }
281         return null;
282     }
283     
284     private Object JavaDoc internalGetElement(Object JavaDoc elementOrPath) {
285         if (elementOrPath instanceof TreePath) {
286             TreePath tp = (TreePath) elementOrPath;
287             return tp.getLastSegment();
288         }
289         return elementOrPath;
290     }
291     
292     private Object JavaDoc internalGetElementParent(Object JavaDoc elementOrPath) {
293         if (elementOrPath instanceof TreePath) {
294             TreePath tp = (TreePath) elementOrPath;
295             if (tp.getSegmentCount() > 1) {
296                 return tp.getSegment(tp.getSegmentCount() - 2);
297             }
298             
299         }
300         return null;
301     }
302
303     public boolean hasChildren(ISynchronizationContext context, Object JavaDoc elementOrPath) {
304         Object JavaDoc element = internalGetElement(elementOrPath);
305         if (element instanceof IContainer) {
306             IContainer container = (IContainer) element;
307             // For containers check to see if the delta contains any children
308
if (context != null) {
309                 int depth = getLayoutDepth(container, internalGetPath(elementOrPath));
310                 if (depth == IResource.DEPTH_ZERO)
311                     return false;
312                 IResourceDiffTree tree = context.getDiffTree();
313                 IResource[] members = tree.members(container);
314                 if (members.length > 0) {
315                     if (depth == IResource.DEPTH_INFINITE)
316                         return true;
317                     for (int i = 0; i < members.length; i++) {
318                         IResource resource = members[i];
319                         if (resource.getType() == IResource.FILE)
320                             return true;
321                     }
322                 }
323             }
324         }
325         return false;
326     }
327     
328     public TreePath getParentPath(ISynchronizationContext context, ModelProvider provider, Object JavaDoc element) {
329         if (element instanceof IResource) {
330             IResource resource = (IResource) element;
331             TreePath treePath = getProviderRootPath(context, provider);
332             if (resource.getType() == IResource.ROOT){
333                 return null;
334             }
335             if (resource.getType() == IResource.PROJECT){
336                 return treePath;
337             }
338             if (getLayout().equals(IPreferenceIds.FLAT_LAYOUT)) {
339                 return treePath.createChildPath(resource.getProject());
340             } else if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT) && resource.getType() == IResource.FOLDER) {
341                 return treePath.createChildPath(resource.getProject());
342             } else if (getLayout().equals(IPreferenceIds.COMPRESSED_LAYOUT) && resource.getType() == IResource.FILE) {
343                 if (resource.getParent().getType() == IResource.PROJECT)
344                     return treePath.createChildPath(resource.getProject());
345                 return treePath.createChildPath(resource.getProject()).createChildPath(resource.getParent());
346             }
347             IResource parent = resource.getParent();
348             IResource[] resourcePath = new IResource[parent.getFullPath().segmentCount()];
349             for (int i = resourcePath.length - 1; i >= 0; i--) {
350                 resourcePath[i] = parent;
351                 parent = parent.getParent();
352             }
353             for (int i = 0; i < resourcePath.length; i++) {
354                 IResource r = resourcePath[i];
355                 treePath = treePath.createChildPath(r);
356             }
357             return treePath;
358         }
359         return null;
360     }
361     
362     private TreePath getProviderRootPath(ISynchronizationContext context, ModelProvider provider) {
363         if (context == null)
364             return TreePath.EMPTY.createChildPath(provider);
365         return TreePath.EMPTY;
366     }
367     
368     private boolean isFlatPageLayout() {
369         if (configuration != null) {
370             String JavaDoc p = (String JavaDoc)configuration.getProperty(ITeamContentProviderManager.PROP_PAGE_LAYOUT);
371             return p != null && p.equals(ITeamContentProviderManager.FLAT_LAYOUT);
372         }
373         return false;
374     }
375
376     public synchronized static ResourceModelTraversalCalculator getDefault() {
377         if (instance == null)
378             instance = new ResourceModelTraversalCalculator();
379         return instance;
380     }
381     
382     public synchronized static ResourceModelTraversalCalculator getTraversalCalculator(ISynchronizePageConfiguration configuration) {
383         if (configuration == null)
384             return ResourceModelTraversalCalculator.getDefault();
385         ResourceModelTraversalCalculator tc = (ResourceModelTraversalCalculator)configuration.getProperty(ResourceModelTraversalCalculator.PROP_TRAVERSAL_CALCULATOR);
386         if (tc == null) {
387             tc = new ResourceModelTraversalCalculator(configuration);
388             configuration.setProperty(ResourceModelTraversalCalculator.PROP_TRAVERSAL_CALCULATOR, tc);
389         }
390         return tc;
391     }
392     
393 }
394
Popular Tags