Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9194937ba8ecaaab30ad7bc9a88d6ffc08800a1e (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
/*******************************************************************************
 * Copyright (c) 2006, 2015 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.viewsupport;

import java.util.HashMap;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.ui.IWorkingSet;

public class WorkingSetFilter {

	private static final Object ACCEPT = new Object();
	private static final Object REJECT = new Object();

	private HashMap<IPath, Object> fResourceFilter = null;

	public synchronized boolean isPartOfWorkingSet(ICElement elem) {
		if (fResourceFilter == null) {
			return true;
		}
		if (elem == null) {
			return false;
		}
		IPath path = elem.getPath();
		if (path == null) {
			return false;
		}
		Object check = fResourceFilter.get(path);
		if (check == null) {
			check = checkWorkingSet(path);
			fResourceFilter.put(path, check);
		}
		return check == ACCEPT;
	}

	public synchronized boolean isPartOfWorkingSet(IPath resourceOrExternalPath) {
		if (fResourceFilter == null) {
			return true;
		}
		if (resourceOrExternalPath == null) {
			return false;
		}
		Object check = fResourceFilter.get(resourceOrExternalPath);
		if (check == null) {
			check = checkWorkingSet(resourceOrExternalPath);
			fResourceFilter.put(resourceOrExternalPath, check);
		}
		return check == ACCEPT;
	}

	private synchronized Object checkWorkingSet(IPath path) {
		if (path.segmentCount() == 0) {
			return REJECT;
		}

		Object result = fResourceFilter.get(path);
		if (result == null) {
			result = checkWorkingSet(path.removeLastSegments(1));
			fResourceFilter.put(path, result);
		}
		return result;
	}

	public synchronized void setWorkingSet(IWorkingSet workingSetFilter) {
		if (workingSetFilter == null) {
			fResourceFilter = null;
		} else {
			IAdaptable[] input = workingSetFilter.getElements();
			fResourceFilter = new HashMap<IPath, Object>();
			for (int i = 0; i < input.length; i++) {
				IAdaptable adaptable = input[i];
				IResource res = adaptable.getAdapter(IResource.class);
				if (res != null) {
					fResourceFilter.put(res.getFullPath(), ACCEPT);
				}
			}
		}
	}
}

Back to the top