Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dbf9c7b75eac9cca9fb90141412d306b21ab0562 (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
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2005, 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.core.mapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.RepositoryProviderType;
import org.eclipse.team.internal.core.IRepositoryProviderListener;
import org.eclipse.team.internal.core.RepositoryProviderManager;
import org.eclipse.team.internal.core.TeamPlugin;

/**
 * Supports the tracking of related changes for the purpose of grouping then using an {@link IChangeGroupingRequestor}.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @see IChangeGroupingRequestor
 * @since 3.3
 */
public abstract class ChangeTracker {

	private Map<IProject, IChangeGroupingRequestor> trackedProjects = new HashMap<>(); // Map IProject->IChangeGroupingRequestor
	private boolean disposed;
	private ChangeListener changeListener = new ChangeListener();

	private class ChangeListener implements IResourceChangeListener, IRepositoryProviderListener {
		/**
		 * Handle a resource change event.
		 * Update the set of projects for which we can track changes
		 * by listening for project changes and project description changes.
		 * @param event the change event
		 */
		@Override
		public void resourceChanged(IResourceChangeEvent event) {
			if (disposed) return;
			IResourceDelta delta = event.getDelta();
			IResourceDelta[] projectDeltas = delta.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.CHANGED | IResourceDelta.REMOVED);
			for (IResourceDelta projectDelta : projectDeltas) {
				IResource resource = projectDelta.getResource();
				if (resource.getType() == IResource.PROJECT) {
					IProject project = (IProject)resource;
					if (isProjectOfInterest(project)) {
						if (isProjectTracked(project)) {
							IResource[] resources = getProjectChanges(project, projectDelta);
							if (resources.length > 0)
								handleChanges(project, resources);
						} else {
							trackProject(project);
						}
					} else {
						stopTrackingProject(project);
					}
				}
			}
		}

		/**
		 * When a project is shared, start tracking it if it is of interest.
		 * @param provider the repository provider
		 */
		@Override
		public void providerMapped(RepositoryProvider provider) {
			if (disposed) return;
			if (isProjectOfInterest(provider.getProject())) {
				trackProject(provider.getProject());
			}
		}

		/**
		 * When a project is no longer shared, stop tracking the project.
		 * @param project the project
		 */
		@Override
		public void providerUnmapped(IProject project) {
			if (disposed) return;
			stopTrackingProject(project);
		}
	}

	/**
	 * Create a change tracker
	 */
	public ChangeTracker() {
		super();
	}

	/**
	 * Start tracking changes. This registers listeners with the workspace
	 * and team.
	 */
	public void start() {
		ResourcesPlugin.getWorkspace().addResourceChangeListener(changeListener, IResourceChangeEvent.POST_CHANGE);
		RepositoryProviderManager.getInstance().addListener(changeListener);
		IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		for (IProject project : allProjects) {
			if (isProjectOfInterest(project))
				trackProject(project);
		}
	}

	/**
	 * Remove any listeners for this tracker. Subclasses
	 * may extend this method but must call this method if they do.
	 */
	public void dispose() {
		disposed = true;
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(changeListener);
		RepositoryProviderManager.getInstance().removeListener(changeListener);
	}

	private IResource[] getProjectChanges(IProject project, IResourceDelta projectDelta) {
		final List<IResource> result = new ArrayList<>();
		try {
			projectDelta.accept(new IResourceDeltaVisitor() {
				@Override
				public boolean visit(IResourceDelta delta) throws CoreException {
					if (isResourceOfInterest(delta.getResource()) & isChangeOfInterest(delta)) {
						result.add(delta.getResource());
					}
					return true;
				}
			});
		} catch (CoreException e) {
			TeamPlugin.log(e);
		}
		return result.toArray(new IResource[result.size()]);
	}

	/**
	 * Return whether the given delta represents a change of interest.
	 * @param delta the delta
	 * @return whether the given delta represents a change of interest
	 */
	protected boolean isChangeOfInterest(IResourceDelta delta) {
		return (delta.getKind() & (IResourceDelta.ADDED | IResourceDelta.REMOVED | IResourceDelta.CHANGED)) > 0;
	}

	/**
	 * Stop tracking changes for the given project. Subclasses
	 * may extend but must call this method.
	 * @param project the project
	 */
	protected void stopTrackingProject(IProject project) {
		trackedProjects.remove(project);
	}

	/**
	 * Return whether the given project is being tracked.
	 * @param project the project
	 * @return whether the given project is being tracked
	 */
	protected final boolean isProjectTracked(IProject project) {
		return trackedProjects.containsKey(project);
	}

	/**
	 * Return whether the given project is of interest to this
	 * tracker. By default, <code>true</code> is returned if the
	 * project is accessible. Subclasses may extend but should
	 * still check for project accessibility either by calling
	 * {@link IResource#isAccessible()} or by invoking the
	 * overridden method.
	 * @param project the project
	 * @return whether the given project is of interest to this
	 * tracker
	 */
	protected boolean isProjectOfInterest(IProject project) {
		return project.isAccessible();
	}

	/**
	 * Return whether the given resource is of interest to the tracker.
	 * @param resource the resource
	 * @return whether the given resource is of interest to the tracker
	 */
	protected abstract boolean isResourceOfInterest(IResource resource);

	/**
	 * The given resources of interest have changed in the given project.
	 * @param project the project
	 * @param resources the resources
	 */
	protected abstract void handleChanges(IProject project, IResource[] resources);

	/**
	 * Resources of interest in the given project have changed but the
	 * specific changes are not known. Implementors must search the project for
	 * changes of interest.
	 * @param project the project
	 */
	protected abstract void handleProjectChange(IProject project);

	/**
	 * Track the given project if it has a change set collector. If the project
	 * does not have a collector, the project is not tracked.
	 * @param project the project
	 * @return whether the project is being tracked
	 */
	protected final boolean trackProject(IProject project) {
		if (RepositoryProvider.isShared(project)) {
			try {
				String currentId = project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY);
				if (currentId != null) {
					RepositoryProviderType type = RepositoryProviderType.getProviderType(currentId);
					if (type != null) {
						IChangeGroupingRequestor collector = getCollector(type);
						if (collector != null) {
							trackedProjects.put(project, collector);
							// Ensure that an appropriate change set exists if needed
							// We can do this here because we know that the number of files
							// to test is small.
							projectTracked(project);
							return true;
						}
					}
				}
			} catch (CoreException e) {
				TeamPlugin.log(e);
			}
		}
		return false;
	}

	private IChangeGroupingRequestor getCollector(RepositoryProviderType type) {
		if (type != null) {
			IChangeGroupingRequestor o = type.getAdapter(IChangeGroupingRequestor.class);
			if (o != null) {
				return o;
			}
		}
		return null;
	}

	/**
	 * Callback made from {@link #trackProject(IProject)} when a project is tracked.
	 * By default, {@link #handleProjectChange(IProject)} is called by subclasses may override.
	 * @param project the project
	 */
	protected void projectTracked(IProject project) {
		handleProjectChange(project);
	}

	/**
	 * Group the given modified file into a change set with the given name.
	 * @param project the project
	 * @param name the unique name used to identify the change set
	 * @param files the change files to be grouped
	 * @throws CoreException
	 */
	protected void ensureGrouped(IProject project, String name, IFile[] files) throws CoreException {
		IChangeGroupingRequestor collector = getCollector(project);
		if (collector != null) {
			collector.ensureChangesGrouped(project, files, name);
		}
	}

	private IChangeGroupingRequestor getCollector(IProject project) {
		return trackedProjects.get(project);
	}

	/**
	 * Return whether the given file is modified with respect to the
	 * repository provider associated with the file's project.
	 * @param file the file
	 * @return whether the given file is modified
	 * @throws CoreException
	 */
	protected boolean isModified(IFile file) throws CoreException {
		IChangeGroupingRequestor collector = getCollector(file.getProject());
		if (collector != null)
			return collector.isModified(file);
		return false;
	}

}

Back to the top