Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 906d810a6e6b51d9648af495857aaf08564f70ef (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
/*******************************************************************************
 * Copyright (c) 2000, 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.internal.ui.synchronize;

import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.*;
import org.eclipse.core.resources.IEncodedStorage;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.TeamUIPlugin;

/**
 * A diff node used to display the synchronization state for resources described by
 * existing {@link SyncInfo} objects. The synchronization state for a node can
 * change after it has been created. Since it implements the <code>ITypedElement</code>
 * and <code>ICompareInput</code> interfaces it can be used directly to
 * display the compare result in a <code>DiffTreeViewer</code> and as the
 * input to any other compare/merge viewer.
 * <p>
 * Clients typically use this class as is, but may subclass if required.
 * </p>
 * @see DiffTreeViewer
 * @see Differencer
 */
public class SyncInfoModelElement extends SynchronizeModelElement {

	private ITypedElement ancestor;
	private SyncInfo info;

	/**
	 * Construct a <code>SyncInfoModelElement</code> for the given resource.
	 *
	 * @param parent
	 * @param info
	 */
	public SyncInfoModelElement(IDiffContainer parent, SyncInfo info) {
		super(parent);

		Assert.isNotNull(info);
		this.info = info;
		// update state
		setKind(info.getKind());
		// local
		setLeft(createLocalTypeElement(info));
		// remote
		setRight(createRemoteTypeElement(info));
		// base
		setAncestor(createBaseTypeElement(info));

		fireChange();
	}

	/**
	 * Update this element with a changed sync info. The remote and base handles have to be updated
	 * with the new handles in the sync info.
	 *
	 * @param info the new sync info
	 */
	public void update(SyncInfo info) {
		this.info = info;
		// update state
		setKind(info.getKind());

		// Remote
		RemoteResourceTypedElement rightEl = (RemoteResourceTypedElement)getRight();
		IResourceVariant remote = info.getRemote();
		if(rightEl == null && remote != null) {
			setRight(createRemoteTypeElement(info));
		} else if(rightEl != null) {
			if(remote == null) {
				setRight(null);
			} else {
				setRight(createRemoteTypeElement(info));
			}
		}
		// Base
		RemoteResourceTypedElement ancestorEl = (RemoteResourceTypedElement)getAncestor();
		IResourceVariant base = info.getBase();
		if(ancestorEl == null && base != null) {
			setAncestor(createBaseTypeElement(info));
		} else if(ancestorEl != null) {
			if(base == null) {
				setAncestor(null);
			} else {
				setAncestor(createBaseTypeElement(info));
			}
		}

		fireChange();
	}

	@Override
	public int getKind() {
		SyncInfo info = getSyncInfo();
		if (info != null) {
			return info.getKind();
		} else {
			return SyncInfo.IN_SYNC;
		}
	}

	/**
	 * We have to track the base because <code>DiffNode</code> doesn't provide a
	 * setter. See:
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52261
	 */
	@Override
	public void setAncestor(ITypedElement ancestor) {
		this.ancestor = ancestor;
	}

	@Override
	public ITypedElement getAncestor() {
		return this.ancestor;
	}

	@Override
	public String getName() {
		IResource resource = getResource();
		if(resource != null) {
			return resource.getName();
		} else {
			return super.getName();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if(adapter == SyncInfo.class) {
			return (T) getSyncInfo();
		}
		return super.getAdapter(adapter);
	}

	/**
	 * Helper method that returns the resource associated with this node. A node is not
	 * required to have an associated local resource.
	 * @return the resource associated with this node or <code>null</code> if the local
	 * contributor is not a resource.
	 */
	@Override
	public IResource getResource() {
		return info.getLocal();
	}

	@Override
	public String toString() {
		return getResource().getFullPath().toString();
	}

	/**
	 * Cache the contents for the base and remote.
	 * @param monitor
	 */
	public void cacheContents(IProgressMonitor monitor) throws TeamException {
		ITypedElement base = getAncestor();
		ITypedElement remote = getRight();
		int work = Math.min((remote== null ? 0 : 50) + (base == null ? 0 : 50), 10);
		monitor.beginTask(null, work);
		try {
			if (base != null && base instanceof RemoteResourceTypedElement) {
				((RemoteResourceTypedElement)base).cacheContents(Policy.subMonitorFor(monitor, 50));
			}
			if (remote != null && remote instanceof RemoteResourceTypedElement) {
				((RemoteResourceTypedElement)remote).cacheContents(Policy.subMonitorFor(monitor, 50));
			}
		} catch (CoreException e) {
			throw TeamException.asTeamException(e);
		} finally {
			monitor.done();
		}
	}

	public SyncInfo getSyncInfo() {
		return info;
	}

	/**
	 * Create an ITypedElement for the given local resource. The returned ITypedElement
	 * will prevent editing of outgoing deletions.
	 */
	private static ITypedElement createTypeElement(final IResource resource, final int kind) {
		if(resource != null) {
			return new LocalResourceTypedElement(resource);
		}
		return null;
	}

	/**
	 * Create an ITypedElement for the given remote resource. The contents for the remote resource
	 * will be retrieved from the given IStorage which is a local cache used to buffer the remote contents
	 */
	protected static ITypedElement createTypeElement(IResourceVariant remoteResource, String encoding) {
		return new RemoteResourceTypedElement(remoteResource,encoding);
	}

	protected static ITypedElement createRemoteTypeElement(SyncInfo info) {
		if(info != null && info.getRemote() != null) {
			return createTypeElement(info.getRemote(), getEncoding(info.getLocal()));
		}
		return null;
	}

	private static String getEncoding(IResource local) {
		if (local instanceof IEncodedStorage) {
			IEncodedStorage es = (IEncodedStorage) local;
			try {
				return es.getCharset();
			} catch (CoreException e) {
				TeamUIPlugin.log(e);
			}
		}
		return null;
	}

	protected static ITypedElement createLocalTypeElement(SyncInfo info) {
		if(info != null && info.getLocal() != null) {
			return createTypeElement(info.getLocal(), info.getKind());
		}
		return null;
	}

	protected static ITypedElement createBaseTypeElement(SyncInfo info) {
		if(info != null && info.getBase() != null) {
			return createTypeElement(info.getBase(), getEncoding(info.getLocal()));
		}
		return null;
	}
}

Back to the top