Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a20605b1617ccaa5514d049824e9ce3649ce328 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core.subscribers;

import java.util.*;

import org.eclipse.core.resources.IResource;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.*;
import org.eclipse.team.core.synchronize.*;
import org.eclipse.team.internal.core.TeamPlugin;

/**
 * WorkingSet filter for a SyncSet.
 */
public class SyncInfoWorkingSetFilter extends FastSyncInfoFilter {

	private IResource[] resources;

	@Override
	public boolean select(SyncInfo info) {
		// if there's no set, the resource is included
		if (isEmpty()) return true;
		return isIncluded(info.getLocal());
	}

	/*
	 * Answer true if the given resource is included in the working set
	 */
	private boolean isIncluded(IResource resource) {
		// otherwise, if their is a parent of the resource in the set,
		// it is included
		for (int i = 0; i < resources.length; i++) {
			IResource setResource = resources[i];
			if (isParent(setResource, resource)) {
				return true;
			}
		}
		return false;
	}

	private boolean isParent(IResource parent, IResource child) {
		return (parent.getFullPath().isPrefixOf(child.getFullPath()));
	}

	public IResource[] getRoots(Subscriber subscriber) {
		IResource[] roots = subscriber.roots();
		if (isEmpty()) return roots;

		// filter the roots by the selected working set
		Set<IResource> result = new HashSet<>();
		for (int i = 0; i < roots.length; i++) {
			IResource resource = roots[i];
			result.addAll(Arrays.asList(getIntersectionWithSet(subscriber, resource)));
		}
		return result.toArray(new IResource[result.size()]);
	}

	/*
	 * Answer the intersection between the given resource and it's children
	 * and the receiver's working set.
	 */
	private IResource[] getIntersectionWithSet(Subscriber subscriber, IResource resource) {
		List<IResource> result = new ArrayList<>();
		for (int i = 0; i < resources.length; i++) {
			IResource setResource = resources[i];
			if (setResource != null) {
				if (isParent(resource, setResource)) {
					try {
						if (subscriber.isSupervised(setResource)) {
							result.add(setResource);
						}
					} catch (TeamException e) {
						// Log the exception and add the resource to the list
						TeamPlugin.log(e);
						result.add(setResource);
					}
				} else if (isParent(setResource, resource)) {
					result.add(resource);
				}
			}
		}
		return result.toArray(new IResource[result.size()]);
	}

	public void setWorkingSet(IResource[] resources) {
		this.resources = resources;
	}

	public IResource[] getWorkingSet() {
		return this.resources;
	}

	private boolean isEmpty() {
		return resources == null || resources.length == 0;
	}
}

Back to the top