Skip to main content
summaryrefslogtreecommitdiffstats
blob: aec6b7a32f071c4611e9fd9753f7cb1238e55657 (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 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.internal.ccvs.ui.sync;

 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.core.sync.IRemoteSyncElement;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ui.sync.ITeamNode;
import org.eclipse.team.internal.ui.sync.SyncSet;

/**
 * This class contains a set of CVS resources that are slated to be
 * synchronized. This adds CVS specific handling to the common sync set
 * class, specifically to deal with non-added outgoing changes.
 */
public class CVSSyncSet extends SyncSet {
	
	/**
	 * Creates a new sync set on the nodes in the given selection.
	 */
	public CVSSyncSet(IStructuredSelection nodeSelection) {
		super(nodeSelection);
	}
	
	public ITeamNode[] getNonAddedNodes() throws CVSException {
		List result = new ArrayList();
		ITeamNode[] changedNodes = getChangedNodes();
		for (int i = 0; i < changedNodes.length; i++) {
			ITeamNode node = changedNodes[i];
			ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(node.getResource());
			if (cvsResource.isFolder()) {
				if (!((ICVSFolder)cvsResource).isCVSFolder()) {
					result.add(node);
				}
			} else if (!cvsResource.isManaged()) {
				result.add(node);
			}
		}
		return (ITeamNode[])result.toArray(new ITeamNode[result.size()]);
	}
	
	public boolean hasNonAddedChanges() throws CVSException {
		ITeamNode[] changedNodes = getChangedNodes();
		for (int i = 0; i < changedNodes.length; i++) {
			ITeamNode node = changedNodes[i];
			ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(node.getResource());
			if (cvsResource.exists()) {
				if (cvsResource.isFolder()) {
					if (!((ICVSFolder)cvsResource).isCVSFolder()) {
						return true;
					}
				} else if (!cvsResource.isManaged()) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean removeNonAddedChanges() {
		for (Iterator it = getSyncSet().iterator(); it.hasNext();) {
			try {
				ITeamNode node = (ITeamNode)it.next();
				ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(node.getResource());
				if (cvsResource.exists()) {
					if(cvsResource.isFolder()) {
						if(!((ICVSFolder)cvsResource).isCVSFolder()) {
							it.remove();
						}
					} else {
						if(!cvsResource.isManaged()) {
							it.remove();
						}
					}
				}
			} catch (CVSException e) {
				// isManaged or isCVSFolder threw an exception
				// Log it and continue
				CVSUIPlugin.log(e);
			}
		}
		return false;
	}
	
	public boolean removeNonAddedResources(IResource[] remove) {
		for (Iterator it = getSyncSet().iterator(); it.hasNext();) {
			ITeamNode node = (ITeamNode)it.next();
			IResource resource = node.getResource();
			boolean included = false;
			for (int j = 0; j < remove.length; j++) {
				IResource resourceToRemove = remove[j];
				if (resource.equals(resourceToRemove)) {
					included = true;
					break;
				}
			}
			if (included)
				it.remove();
		}
		return false;
	}
	
	public boolean removeAddedChanges() {
		for (Iterator it = getSyncSet().iterator(); it.hasNext();) {
			try {
				ITeamNode node = (ITeamNode)it.next();
				ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(node.getResource());
				if(cvsResource.isFolder()) {
					if(((ICVSFolder)cvsResource).isCVSFolder()) {
						it.remove();
					}
				} else {
					if(cvsResource.isManaged()) {
						it.remove();
					}
				}
			} catch (CVSException e) {
				// isManaged or isCVSFolder threw an exception
				// Log it and continue
				CVSUIPlugin.log(e);
			}
		}
		return false;
	}

	/**
	 * Returns a message for the status line describing this sync set.
	 * 
	 * Override the method in SyncSet to add information about new resources
	 */
	public String getStatusLineMessage() {
		int incoming = 0;
		int outgoing = 0;
		int conflicts = 0;
		int newResources = 0;
		ITeamNode[] nodes = getChangedNodes();
		for (int i = 0; i < nodes.length; i++) {
			ITeamNode next = nodes[i];
			switch (next.getChangeDirection()) {
				case IRemoteSyncElement.INCOMING:
					incoming++;
					break;
				case IRemoteSyncElement.OUTGOING:
					outgoing++;
					ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(next.getResource());
					try {
						if (cvsResource.exists()) {
							if (cvsResource.isFolder()) {
								if (!((ICVSFolder)cvsResource).isCVSFolder()) {
									newResources++;
								}
							} else if (!cvsResource.isManaged()) {
								newResources++;
							}
						}
					} catch (CVSException e) {
						CVSUIPlugin.log(e);
					}
					break;
				case IRemoteSyncElement.CONFLICTING:
					conflicts++;
					break;
			}
		}
		StringBuffer result = new StringBuffer();
		
		if (conflicts == 0) {
			result.append(Policy.bind("CVSSyncSet.noConflicts")); //$NON-NLS-1$
		} else {
			result.append(Policy.bind("CVSSyncSet.conflicts", new Object[] {Integer.toString(conflicts)} )); //$NON-NLS-1$
		}
		if (incoming == 0) {
			result.append(Policy.bind("CVSSyncSet.noIncomings")); //$NON-NLS-1$
		} else {
			result.append(Policy.bind("CVSSyncSet.incomings", new Object[] {Integer.toString(incoming)} )); //$NON-NLS-1$
		}
		if (outgoing == 0) {
			result.append(Policy.bind("CVSSyncSet.noOutgoings")); //$NON-NLS-1$
		} else {
			result.append(Policy.bind("CVSSyncSet.outgoings", new Object[] {Integer.toString(outgoing)} )); //$NON-NLS-1$
		}
		if (newResources == 0) {
			result.append(Policy.bind("CVSSyncSet.noNew")); //$NON-NLS-1$
		} else {
			result.append(Policy.bind("CVSSyncSet.new", new Object[] {Integer.toString(newResources)} )); //$NON-NLS-1$
		}

		return result.toString();
	}
}

Back to the top