Skip to main content
summaryrefslogtreecommitdiffstats
blob: c595c95c8bfca0ea71624eea02bc8b98f02b9689 (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
package org.eclipse.team.ui.sync;

/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.compare.structuremergeviewer.IDiffContainer;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.core.sync.IRemoteSyncElement;
import org.eclipse.team.internal.ui.Policy;

/**
 * This class contains a set of resources that are slated to be
 * synchronized.  It performs various operations on the
 * set in preparation for catchup/release.
 */
public class SyncSet {
	private HashSet set;
	
	/**
	 * Creates a new sync set on the nodes in the given selection.
	 */
	public SyncSet(IStructuredSelection nodeSelection) {
		this.set = new HashSet(nodeSelection.size() + 1);
		collectNodes(nodeSelection);
	}

	/**
	 * Collects all nodes to which this action will apply.  This means
	 * all nodes in the selection, plus all their children.
	 */
	private void collectNodes(IStructuredSelection selection) {
		Object[] nodes = selection.toArray();
		for (int i = 0; i < nodes.length; i++) {
			recursivelyAdd((ITeamNode)nodes[i]);
		}
	}
	
	/**
	 * Adds all parent creations for the given node to the sync set.
	 */
	private void collectParentCreations(ITeamNode node) {
		IDiffElement parent = node.getParent();
		if (parent != null && parent instanceof ITeamNode) {
			if (parent.getKind() != IRemoteSyncElement.IN_SYNC) {
				set.add(parent);
				collectParentCreations((ITeamNode)parent);
			}
		}
	}

	/**
	 * Returns all nodes in the set that have changes
	 */
	public ITeamNode[] getChangedNodes() {
		ArrayList nodeList = new ArrayList(set.size());
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			int dir = node.getChangeDirection();
			// We assume changed nodes of the wrong type have been culled
			// during set creation.
			if (dir != Differencer.NO_CHANGE) {
				nodeList.add(node);
			}
		}
		ITeamNode[] nodes = new ITeamNode[nodeList.size()];
		nodeList.toArray(nodes);
		return nodes;
	}
	
	/**
	 * Returns the resources from all the nodes in this set.
	 */
	public IResource[] getResources() {
		ITeamNode[] changed = getChangedNodes();
		IResource[] resources = new IResource[changed.length];
		for (int i = 0; i < changed.length; i++) {
			resources[i] = changed[i].getResource();
		}
		return resources;
	}
	
	/**
	 * Returns a message for the status line describing this sync set.
	 */
	public String getStatusLineMessage() {
		int incoming = 0;
		int outgoing = 0;
		int conflicts = 0;
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode next = (ITeamNode)it.next();
			switch (next.getChangeDirection()) {
				case IRemoteSyncElement.INCOMING:
					incoming++;
					break;
				case IRemoteSyncElement.OUTGOING:
					outgoing++;
					break;
				case IRemoteSyncElement.CONFLICTING:
					conflicts++;
					break;
			}
		}
		StringBuffer result = new StringBuffer();
		
		if (conflicts == 0) {
			result.append(Policy.bind("SyncSet.noConflicts"));
		} else {
			result.append(Policy.bind("SyncSet.conflicts", new Object[] {Integer.toString(conflicts)} ));
		}
		if (incoming == 0) {
			result.append(Policy.bind("SyncSet.noIncomings"));
		} else {
			result.append(Policy.bind("SyncSet.incomings", new Object[] {Integer.toString(incoming)} ));
		}
		if (outgoing == 0) {
			result.append(Policy.bind("SyncSet.noOutgoings"));
		} else {
			result.append(Policy.bind("SyncSet.outgoings", new Object[] {Integer.toString(outgoing)} ));
		}
		return result.toString();
	}
	
	/**
	 * Returns true if there are any conflicting nodes in the set, and
	 * false otherwise.
	 */
	public boolean hasConflicts() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			if (((ITeamNode)it.next()).getChangeDirection() == IRemoteSyncElement.CONFLICTING) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns true if this sync set has incoming changes.
	 * Note that conflicts are not considered to be incoming changes.
	 */
	public boolean hasIncomingChanges() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			if (((ITeamNode)it.next()).getChangeDirection() == IRemoteSyncElement.INCOMING) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Returns true if this sync set has outgoing changes.
	 * Note that conflicts are not considered to be outgoing changes.
	 */
	public boolean hasOutgoingChanges() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			if (((ITeamNode)it.next()).getChangeDirection() == IRemoteSyncElement.OUTGOING) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Returns true if this sync set has auto-mergeable conflicts.
	 */
	public boolean hasAutoMergeableConflicts() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			if ((node.getKind() & IRemoteSyncElement.AUTOMERGE_CONFLICT) != 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Adds the given node, plus all its children, to the given set.
	 */
	private void recursivelyAdd(ITeamNode node) {
		// Add the node and recurse
		if (set.add(node)) {
			if (node instanceof IDiffContainer) {
				IDiffElement[] children = ((IDiffContainer)node).getChildren();
				for (int i = 0; i < children.length; i++) {
					if (children[i] instanceof ITeamNode) {
						recursivelyAdd((ITeamNode)children[i]);
					}
				}
			}
			// Add any created parents (can't release or load a 
			// resource creation without including new parents)
			collectParentCreations(node);
		}
	}
	
	/**
	 * Removes all conflicting nodes from this set.
	 */
	public void removeConflictingNodes() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			if (node.getChangeDirection() == IRemoteSyncElement.CONFLICTING) {
				it.remove();
			}
		}
	}
	/**
	 * Removes all outgoing nodes from this set.
	 */
	public void removeOutgoingNodes() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			if (node.getChangeDirection() == IRemoteSyncElement.OUTGOING) {
				it.remove();
			}
		}
	}
	/**
	 * Removes all incoming nodes from this set.
	 */
	public void removeIncomingNodes() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			if (node.getChangeDirection() == IRemoteSyncElement.INCOMING) {
				it.remove();
			}
		}
	}
	/**
	 * Removes all conflicting nodes from this set that are not auto-mergeable
	 */
	public void removeNonMergeableNodes() {
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			if ((node.getKind() & IRemoteSyncElement.MANUAL_CONFLICT) != 0) {
				it.remove();
			}
		}
	}
	
	/**
	 * Removes all nodes that aren't applicable for the direction.
	 */
	public void removeNonApplicableNodes(int direction) {
		for (Iterator it = set.iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			int nodeDirection = node.getKind() & IRemoteSyncElement.DIRECTION_MASK;
			if (nodeDirection != IRemoteSyncElement.CONFLICTING) {
				if (nodeDirection != direction) {
					it.remove();
				}
			}
		}
	}
	
	/**
	 * Remove the given node from the sync set
	 */
	public void remove(ITeamNode node) {
		set.remove(node);
	}
}

Back to the top