Skip to main content
summaryrefslogtreecommitdiffstats
blob: b67b5e848df212f05693722cd0cb34f59fdbc9dc (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
/*******************************************************************************
 * 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 java.util.*;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.subscribers.FastSyncInfoFilter.SyncInfoDirectionFilter;
import org.eclipse.team.internal.core.Policy;
import org.eclipse.team.internal.core.subscribers.SyncSetChangedEvent;

public class MutableSyncInfoSet extends SyncInfoSet {

	public MutableSyncInfoSet() {
		super();
	}
	
	public MutableSyncInfoSet(SyncInfo[] infos) {
		super(infos);
	}
	
	public synchronized void remove(IResource local) {
		IPath path = local.getFullPath();
		SyncInfo info = (SyncInfo)resources.remove(path);
		changes.removed(local);
		statistics.remove(info);
		removeFromParents(local, local);
	}
	
	public void removeAll(IResource[] resources) {
		for (int i = 0; i < resources.length; i++) {
			remove(resources[i]);			
		}
	}

	public synchronized void add(SyncInfo info) {
		internalAdd(info);
	}
	
	public void addAll(SyncInfoSet set) {
		SyncInfo[] infos = set.members();
		for (int i = 0; i < infos.length; i++) {
			add(infos[i]);
		}
	}
	
	public synchronized void changed(SyncInfo info) {
		internalAddSyncInfo(info);
		changes.changed(info);
	}

	/**
	 * Reset the sync set so it is empty
	 */
	public synchronized void clear() {
		resources.clear();
		parents.clear();
		changes.reset();
		statistics.clear();
	}

	public synchronized void removeAllChildren(IResource resource) {
		// The parent map contains a set of all out-of-sync children
		Set allChildren = (Set)parents.get(resource.getFullPath());
		if (allChildren == null) return;
		IResource [] removed = (IResource[]) allChildren.toArray(new IResource[allChildren.size()]);
		for (int i = 0; i < removed.length; i++) {
			remove(removed[i]);
		}
	}

	/**
	 * This method is invoked by a SyncSetInput provider when the 
	 * provider is starting to provide new input to the SyncSet
	 */
	public void beginInput() {
		synchronized(this) {
			resetChanges();
		}
	}

	/**
	 * This method is invoked by a SyncSetInput provider when the 
	 * provider is done providing new input to the SyncSet
	 */
	public void endInput(IProgressMonitor monitor) {
		fireChanges(monitor);
	}

	private void fireChanges(final IProgressMonitor monitor) {
		// Use a synchronized block to ensure that the event we send is static
		final SyncSetChangedEvent event;
		synchronized(this) {
			event = changes;
			resetChanges();
		}
		// Ensure that the list of listeners is not changed while events are fired.
		// Copy the listeners so that addition/removal is not blocked by event listeners
		if(event.isEmpty() && ! event.isReset()) return;
		ISyncSetChangedListener[] allListeners;
		synchronized(listeners) {
			allListeners = (ISyncSetChangedListener[]) listeners.toArray(new ISyncSetChangedListener[listeners.size()]);
		}
		// Fire the events using an ISafeRunnable
		monitor.beginTask(null, 100 * allListeners.length);
		for (int i = 0; i < allListeners.length; i++) {
			final ISyncSetChangedListener listener = allListeners[i];
			Platform.run(new ISafeRunnable() {
				public void handleException(Throwable exception) {
					// don't log the exception....it is already being logged in Platform#run
				}
				public void run() throws Exception {
					listener.syncSetChanged(event, Policy.subMonitorFor(monitor, 100));
	
				}
			});
		}
		monitor.done();
	}

	private boolean removeFromParents(IResource resource, IResource parent) {
		if (parent.getType() == IResource.ROOT) {
			return false;
		}
		// this flag is used to indicate if the parent was removed from the set
		boolean removedParent = false;
		if (parent.getType() == IResource.FILE) {
			// the file will be removed
			removedParent = true;
		} else {
			Set children = (Set)parents.get(parent.getFullPath());
			if (children != null) {
				children.remove(resource);
				if (children.isEmpty()) {
					parents.remove(parent.getFullPath());
					removedParent = true;
				}
			}
		}
		//	if the parent wasn't removed and the resource was, record it
		if (!removeFromParents(resource, parent.getParent()) && removedParent) {
			changes.removedRoot(parent);
		}
		return removedParent;
	}
	
	/**
	 * Removes all conflicting nodes from this set.
	 */
	public void removeConflictingNodes() {
		rejectNodes(new SyncInfoDirectionFilter(SyncInfo.CONFLICTING));
	}
	/**
	 * Removes all outgoing nodes from this set.
	 */
	public void removeOutgoingNodes() {
		rejectNodes(new SyncInfoDirectionFilter(SyncInfo.OUTGOING));
	}
	/**
	 * Removes all incoming nodes from this set.
	 */
	public void removeIncomingNodes() {
		rejectNodes(new SyncInfoDirectionFilter(SyncInfo.INCOMING));
	}
	
	/**
	 * Removes all nodes from this set that are not auto-mergeable conflicts
	 */
	public void removeNonMergeableNodes() {
		SyncInfo[] infos = members();
		for (int i = 0; i < infos.length; i++) {
			SyncInfo info = infos[i];
			if ((info.getKind() & SyncInfo.MANUAL_CONFLICT) != 0) {
				remove(info.getLocal());
			} else if ((info.getKind() & SyncInfo.DIRECTION_MASK) != SyncInfo.CONFLICTING) {
				remove(info.getLocal());
			}
		}
	}
	
	/**
	 * Indicate whether the set has nodes matching the given filter
	 */
	public boolean hasNodes(FastSyncInfoFilter filter) {
		SyncInfo[] infos = members();
		for (int i = 0; i < infos.length; i++) {
			SyncInfo info = infos[i];
			if (info != null && filter.select(info)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Removes all nodes from this set that do not match the given filter
	 */
	public void selectNodes(FastSyncInfoFilter filter) {
		SyncInfo[] infos = members();
		for (int i = 0; i < infos.length; i++) {
			SyncInfo info = infos[i];
			if (info == null || !filter.select(info)) {
				remove(info.getLocal());
			}
		}
	}
	
	/**
	 * Removes all nodes from this set that match the given filter
	 */
	public void rejectNodes(FastSyncInfoFilter filter) {
		SyncInfo[] infos = members();
		for (int i = 0; i < infos.length; i++) {
			SyncInfo info = infos[i];
			if (info != null && filter.select(info)) {
				remove(info.getLocal());
			}
		}
	}
	
	/**
	 * Return all nodes in this set that match the given filter
	 */
	public SyncInfo[] getNodes(FastSyncInfoFilter filter) {
		List result = new ArrayList();
		SyncInfo[] infos = members();
		for (int i = 0; i < infos.length; i++) {
			SyncInfo info = infos[i];
			if (info != null && filter.select(info)) {
				result.add(info);
			}
		}
		return (SyncInfo[]) result.toArray(new SyncInfo[result.size()]);
	}
}

Back to the top