Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cee1880dcf79257ba9cd397e9f9a331494876a7 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.core.subscribers;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.core.Assert;
import org.eclipse.team.internal.core.Policy;
import org.eclipse.team.internal.core.subscribers.SubscriberEventHandler;
import org.eclipse.team.internal.core.subscribers.SyncSetInputFromSubscriber;

/**
 * This collector maintains a {@link SyncInfoSet} for a particular team subscriber keeping
 * it up-to-date with both incoming changes and outgoing changes as they occur for 
 * resources in the workspace. The collector can be configured to consider all the subscriber's
 * roots or only a subset.
 * <p>
 * The advantage of this collector is that it processes both resource and team
 * subscriber deltas in a background thread.
 * </p>
 * @since 3.0
 */
public final class TeamSubscriberSyncInfoCollector implements IResourceChangeListener, ITeamResourceChangeListener {

	private SyncSetInputFromSubscriber set;
	private SubscriberEventHandler eventHandler;
	private TeamSubscriber subscriber;
	private IResource[] roots;

	/**
	 * Create a collector on the subscriber that collects out-of-sync resources
	 * for all roots of the subscriber.
	 * @param subscriber the TeamSubscriber
	 */
	public TeamSubscriberSyncInfoCollector(TeamSubscriber subscriber) {
		this(subscriber, null /* use the subscriber roots */);
	}
	
	/**
	 * Create a collector that collects out-of-sync resources that are children of
	 * the given roots. If the roots are <code>null</code>, then all out-of-sync resources
	 * from the subscriber are collected. An empty array of roots will cause no resources
	 * to be collected.
	 * @param subscriber the TeamSubscriber
	 * @param roots the roots of the out-of-sync resources to be collected
	 */
	public TeamSubscriberSyncInfoCollector(TeamSubscriber subscriber, IResource[] roots) {
		this.roots = roots;
		this.subscriber = subscriber;
		Assert.isNotNull(subscriber);
		set = new SyncSetInputFromSubscriber(subscriber);
		eventHandler = new SubscriberEventHandler(set);

		ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
		subscriber.addListener(this);
	}
	
	/**
	 * Return the set that provides access to the out-of-sync resources for the collector's
	 * subscriber. The set will contain only those resources that are children of the roots
	 * of the collector unless the roots of the colletor has been set to <code>null</code>
	 * in which case all out-of-sync resources from the subscriber are collected.
	 * @return a SyncInfoSet containing out-of-sync resources
	 */
	public SyncInfoSet getSyncInfoSet() {
		return set.getSyncSet();
	}

	/**
	 * This causes the calling thread to wait any background collection of out-of-sync resources
	 * to stop before returning.
	 * @param monitor a progress monitor
	 */
	public void waitForCollector(IProgressMonitor monitor) {
		monitor.worked(1);
		// wait for the event handler to process changes.
		while(eventHandler.getEventHandlerJob().getState() != Job.NONE) {
			monitor.worked(1);
			try {
				Thread.sleep(10);		
			} catch (InterruptedException e) {
			}
		}
		monitor.worked(1);
	}
	
	/**
	 * Clears this collector's <code>SyncInfoSet</code> and causes it to be recreated from the
	 * associated <code>TeamSubscriber</code>. The reset may occur in the background. If the
	 * caller wishes to wait for the reset to complete, they should call \
	 * {@link waitForCollector(IProgressMonitor)}.
	 * @param monitor a progress monitor
	 * @throws TeamException
	 */
	public void reset(IProgressMonitor monitor) throws TeamException {
		monitor.beginTask(null, 100);
		set.reset(Policy.subMonitorFor(monitor, 100));
		eventHandler.initialize(getRoots());
		monitor.done();
	}

	/**
	 * Returns the <code>TeamSubscriber</code> associated with this collector.
	 * 
	 * @return the <code>TeamSubscriber</code> associated with this collector.
	 */
	public TeamSubscriber getTeamSubscriber() {
		return subscriber;
	}

	/**
	 * Disposes of the background job associated with this collector and deregisters
	 * all it's listeners. This method must be called when the collector is no longer
	 * referenced and could be garbage collected.
	 */
	public void dispose() {
		eventHandler.shutdown();

		set.disconnect();

		getTeamSubscriber().removeListener(this);		
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
	}

	/**
	 * Process the resource delta and posts all necessary events to the background
	 * event handler.
	 * 
	 * @param delta the resource delta to analyse
	 */
	private void processDelta(IResourceDelta delta) {
		IResource resource = delta.getResource();
		int kind = delta.getKind();

		if (resource.getType() == IResource.PROJECT) {
			// Handle a deleted project
			if (((kind & IResourceDelta.REMOVED) != 0)) {
				eventHandler.remove(resource);
				return;
			}
			// Handle a closed project
			if ((delta.getFlags() & IResourceDelta.OPEN) != 0 && !((IProject) resource).isOpen()) {
				eventHandler.remove(resource);
				return;
			}
			// Only interested in projects mapped to the provider
			if (!isAncestorOfRoot(resource)) {
				// If the project has any entries in the sync set, remove them
				if (getSyncInfoSet().hasMembers(resource)) {
					eventHandler.remove(resource);
				}
				return;
			}
		}

		boolean visitChildren = false;
		if (isDescendantOfRoot(resource)) {
			visitChildren = true;
			// If the resource has changed type, remove the old resource handle
			// and add the new one
			if ((delta.getFlags() & IResourceDelta.TYPE) != 0) {
				eventHandler.remove(resource);
				eventHandler.change(resource, IResource.DEPTH_INFINITE);
			}
	
			// Check the flags for changes the SyncSet cares about.
			// Notice we don't care about MARKERS currently.
			int changeFlags = delta.getFlags();
			if ((changeFlags & (IResourceDelta.OPEN | IResourceDelta.CONTENT)) != 0) {
				eventHandler.change(resource, IResource.DEPTH_ZERO);
			}
	
			// Check the kind and deal with those we care about
			if ((delta.getKind() & (IResourceDelta.REMOVED | IResourceDelta.ADDED)) != 0) {
				eventHandler.change(resource, IResource.DEPTH_ZERO);
			}
		}

		// Handle changed children
		if (visitChildren || isAncestorOfRoot(resource)) {
			IResourceDelta[] affectedChildren = delta.getAffectedChildren(IResourceDelta.CHANGED | IResourceDelta.REMOVED | IResourceDelta.ADDED);
			for (int i = 0; i < affectedChildren.length; i++) {
				processDelta(affectedChildren[i]);
			}
		}
	}

	private boolean isAncestorOfRoot(IResource parent) {
		// Always traverse into projects in case a root was removed
		if (parent.getType() == IResource.ROOT) return true;
		IResource[] roots = getRoots();
		for (int i = 0; i < roots.length; i++) {
			IResource resource = roots[i];
			if (parent.getFullPath().isPrefixOf(resource.getFullPath())) {
				return true;
			}
		}
		return false;
	}

	private boolean isDescendantOfRoot(IResource resource) {
		IResource[] roots = getRoots();
		for (int i = 0; i < roots.length; i++) {
			IResource root = roots[i];
			if (root.getFullPath().isPrefixOf(resource.getFullPath())) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Return the roots that are being considered by this collector.
	 * By default, the collector is interested in the roots of its
	 * subscriber. However, the set can be reduced using {@link setRoots(IResource)).
	 * @return
	 */
	public IResource[] getRoots() {
		if (roots == null) {
			return getTeamSubscriber().roots();
		} else {
			return roots;
		}
	}
	
	/*
	 * Returns whether the collector is configured to collect for
	 * all roots of the subscriber or not
	 * @return <code>true</code> if the collector is considering all 
	 * roots of the subscriber and <code>false</code> otherwise
	 */
	private boolean isAllRootsIncluded() {
		return roots == null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
	 */
	public void resourceChanged(IResourceChangeEvent event) {
		processDelta(event.getDelta());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.team.core.sync.ITeamResourceChangeListener#teamResourceChanged(org.eclipse.team.core.sync.TeamDelta[])
	 */
	public void teamResourceChanged(TeamDelta[] deltas) {
		for (int i = 0; i < deltas.length; i++) {
			switch (deltas[i].getFlags()) {
				case TeamDelta.SYNC_CHANGED :
					if (isAllRootsIncluded() || isDescendantOfRoot(deltas[i].getResource())) {
						eventHandler.change(deltas[i].getResource(), IResource.DEPTH_ZERO);
					}
					break;
				case TeamDelta.ROOT_REMOVED :
					eventHandler.remove(deltas[i].getResource());
					break;
				case TeamDelta.ROOT_ADDED :
					if (isAllRootsIncluded() || isDescendantOfRoot(deltas[i].getResource())) {
						eventHandler.change(deltas[i].getResource(), IResource.DEPTH_INFINITE);
					}
					break;
			}
		}
	}
	
	/**
	 * Set the roots that are to be considered by the collector. The provided
	 * resources should be either a subset of the roots of the collector's subscriber
	 * of children of those roots. Other resources can be provided but will be ignored.
	 * Setting the roots to <code>null</code> will cause the roots of the subscriber
	 * to be used
	 * @param roots The roots to be considered or <code>null</code>.
	 */
	public void setRoots(IResource[] roots) {
		this.roots = roots;
	}
}

Back to the top