KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > AdaptableHierarchicalResourceList


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.internal.ccvs.ui;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.ui.model.WorkbenchContentProvider;
23
24 /**
25  * This class acts as an adaptable list that will return the resources in the
26  * hierarchy indicated by their paths
27  */

28 public class AdaptableHierarchicalResourceList extends AdaptableResourceList {
29     private IContainer root;
30
31     /**
32      * Constructor for AdaptableHierarchicalResourceList.
33      * @param resources
34      */

35     public AdaptableHierarchicalResourceList(IContainer root, IResource[] resources) {
36         super(resources);
37         this.root = root;
38     }
39
40     /**
41      * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
42      */

43     public Object JavaDoc[] getChildren(Object JavaDoc o) {
44         return getChildenFor(root);
45     }
46
47     private IResource[] getChildenFor(IContainer parent) {
48         Set JavaDoc children = new HashSet JavaDoc();
49         IPath parentPath = parent.getFullPath();
50         for (int i = 0; i < resources.length; i++) {
51             IResource resource = resources[i];
52             IPath resourcePath = resource.getFullPath();
53             if (parent instanceof IWorkspaceRoot) {
54                 children.add(((IWorkspaceRoot)parent).getProject(resourcePath.segment(0)));
55             } else if (parentPath.isPrefixOf(resourcePath)) {
56                 IPath parentRelativePath = resourcePath.removeFirstSegments(parentPath.segmentCount());
57                 if (parentRelativePath.segmentCount() == 1) {
58                     children.add(resource);
59                 } else if (parentRelativePath.segmentCount() > 1) {
60                     children.add(parent.getFolder(new Path(null, parentRelativePath.segment(0))));
61                 }
62             }
63         }
64         return (IResource[]) children.toArray(new IResource[children.size()]);
65     }
66     
67     /**
68      * Returns a content provider for <code>IResource</code>s that returns
69      * only children of the given resource type.
70      */

71     public ITreeContentProvider getTreeContentProvider() {
72         return new WorkbenchContentProvider() {
73             public Object JavaDoc[] getChildren(Object JavaDoc o) {
74                 if (o instanceof IContainer) {
75                     return getChildenFor((IContainer) o);
76                 } else {
77                     return super.getChildren(o);
78                 }
79             }
80         };
81     }
82     
83     public void setResources(IResource[] resources) {
84         this.resources = resources;
85     }
86
87     /**
88      * Sets the root.
89      * @param root The root to set
90      */

91     public void setRoot(IContainer root) {
92         this.root = root;
93     }
94
95 }
96
Popular Tags