Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1ee041588a2504dc0f273a67f39268df011c1be (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
109
110
111
112
113
114
115
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * 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.core.resources.mapping.ResourceMapping;
import org.eclipse.core.resources.mapping.ResourceTraversal;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.mapping.ISynchronizationScope;
import org.eclipse.team.core.mapping.ISynchronizationScopeChangeListener;

/**
 * An abstract implementation of an {@link ISynchronizationScope}.
 *
 * @since 3.2
 */
public abstract class AbstractSynchronizationScope implements ISynchronizationScope {

	private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.mapping.ISynchronizationScope#getRoots()
	 */
	public IResource[] getRoots() {
		List result = new ArrayList();
		ResourceTraversal[] traversals = getTraversals();
		for (int i = 0; i < traversals.length; i++) {
			ResourceTraversal traversal = traversals[i];
			IResource[] resources = traversal.getResources();
			for (int j = 0; j < resources.length; j++) {
				IResource resource = resources[j];
				accumulateRoots(result, resource);
			}
		}
		return (IResource[]) result.toArray(new IResource[result.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.mapping.ISynchronizationScope#contains(org.eclipse.core.resources.IResource)
	 */
	public boolean contains(IResource resource) {
		ResourceTraversal[] traversals = getTraversals();
		for (int i = 0; i < traversals.length; i++) {
			ResourceTraversal traversal = traversals[i];
			if (traversal.contains(resource))
				return true;
		}
		return false;
	}

	/*
	 * Add the resource to the list if it isn't there already
	 * or is not a child of an existing resource.
	 */
	private void accumulateRoots(List roots, IResource resource) {
		IPath resourcePath = resource.getFullPath();
		for (Iterator iter = roots.iterator(); iter.hasNext();) {
			IResource root = (IResource) iter.next();
			IPath rootPath = root.getFullPath();
			// If there is a higher resource in the collection, skip this one
			if (rootPath.isPrefixOf(resourcePath))
				return;
			// If there are lower resources, remove them
			if (resourcePath.isPrefixOf(rootPath))
				iter.remove();
		}
		// There were no higher resources, so add this one
		roots.add(resource);
	}

	/**
	 * Fire the scope change event
	 * @param newTraversals the new traversals (may be empty)
	 * @param newMappings the new mappings (may be empty)
	 */
	public void fireTraversalsChangedEvent(final ResourceTraversal[] newTraversals, final ResourceMapping[] newMappings) {
		Object[] allListeners = listeners.getListeners();
		for (int i = 0; i < allListeners.length; i++) {
			final Object listener = allListeners[i];
			SafeRunner.run(new ISafeRunnable() {
				public void run() throws Exception {
					((ISynchronizationScopeChangeListener)listener).scopeChanged(AbstractSynchronizationScope.this, newMappings, newTraversals);
				}
				public void handleException(Throwable exception) {
					// Logged by Platform
				}
			});
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.mapping.ISynchronizationScope#addPropertyChangeListener(org.eclipse.core.runtime.Preferences.IPropertyChangeListener)
	 */
	public void addScopeChangeListener(ISynchronizationScopeChangeListener listener) {
		listeners.add(listener);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.mapping.ISynchronizationScope#removePropertyChangeListener(org.eclipse.core.runtime.Preferences.IPropertyChangeListener)
	 */
	public void removeScopeChangeListener(ISynchronizationScopeChangeListener listener) {
		listeners.remove(listener);
	}

}

Back to the top