1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ccvs.ui;
15 
16 import java.util.HashSet;
17 import java.util.Set;
18 
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.ui.model.WorkbenchContentProvider;
24 
25 /**
26  * This class acts as an adaptable list that will return the resources in the
27  * hierarchy indicated by their paths
28  */
29 public class AdaptableHierarchicalResourceList extends AdaptableResourceList {
30 	private IContainer root;
31 
32 	/**
33 	 * Constructor for AdaptableHierarchicalResourceList.
34 	 * @param resources
35 	 */
AdaptableHierarchicalResourceList(IContainer root, IResource[] resources)36 	public AdaptableHierarchicalResourceList(IContainer root, IResource[] resources) {
37 		super(resources);
38 		this.root = root;
39 	}
40 
41 	@Override
getChildren(Object o)42 	public Object[] getChildren(Object o) {
43 		return getChildenFor(root);
44 	}
45 
getChildenFor(IContainer parent)46 	private IResource[] getChildenFor(IContainer parent) {
47 		Set children = new HashSet();
48 		IPath parentPath = parent.getFullPath();
49 		for (IResource resource : resources) {
50 			IPath resourcePath = resource.getFullPath();
51 			if (parent instanceof IWorkspaceRoot) {
52 				children.add(((IWorkspaceRoot)parent).getProject(resourcePath.segment(0)));
53 			} else if (parentPath.isPrefixOf(resourcePath)) {
54 				IPath parentRelativePath = resourcePath.removeFirstSegments(parentPath.segmentCount());
55 				if (parentRelativePath.segmentCount() == 1) {
56 					children.add(resource);
57 				} else if (parentRelativePath.segmentCount() > 1) {
58 					children.add(parent.getFolder(new Path(null, parentRelativePath.segment(0))));
59 				}
60 			}
61 		}
62 		return (IResource[]) children.toArray(new IResource[children.size()]);
63 	}
64 
65 	/**
66 	 * Returns a content provider for <code>IResource</code>s that returns
67 	 * only children of the given resource type.
68 	 */
getTreeContentProvider()69 	public ITreeContentProvider getTreeContentProvider() {
70 		return new WorkbenchContentProvider() {
71 			@Override
72 			public Object[] getChildren(Object o) {
73 				if (o instanceof IContainer) {
74 					return getChildenFor((IContainer) o);
75 				} else {
76 					return super.getChildren(o);
77 				}
78 			}
79 		};
80 	}
81 
82 	public void setResources(IResource[] resources) {
83 		this.resources = resources;
84 	}
85 
86 	/**
87 	 * Sets the root.
88 	 * @param root The root to set
89 	 */
90 	public void setRoot(IContainer root) {
91 		this.root = root;
92 	}
93 
94 }
95