Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3763b09b11c42654c046f9c1701fa55b7bbecc27 (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
/*******************************************************************************
 * 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.ui.synchronize.viewers;

import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.ResourceNode;
import org.eclipse.compare.structuremergeviewer.*;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.*;
import org.eclipse.team.core.variants.*;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
import org.eclipse.team.internal.ui.synchronize.RemoteResourceTypedElement;

/**
 * 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>
 * You can access the {@link SyncInfoSet} this node was created from for quick access
 * to the underlying sync state model.
 * </p>
 * <p>
 * TODO: mention node builders and syncinfocompareinput and syncinfodifftree viewer
 * Clients typically use this class as is, but may subclass if required.
 * @see DiffTreeViewer
 * @see Differencer
 */
public class SyncInfoModelElement extends SynchronizeModelElement {
		
	private ITypedElement ancestor;
	private SyncInfo info;
	
	/**
	 * Construct a <code>SyncInfoModelElement</code> for the given resource. The {@link SyncInfoSet} 
	 * that contains sync states for this resource must also be provided. This set is used
	 * to access the underlying sync state model that is the basis for this node this helps for
	 * providing quick access to the logical containment
	 * 
	 * @param set The set associated with the diff tree veiwer
	 * @param resource The resource for the node
	 */
	public SyncInfoModelElement(IDiffContainer parent, SyncInfo info) {
		super(parent);
		update(info);
	}

	public void update(SyncInfo info) {
		this.info = info;
		// update state
		setKind(info.getKind());		
		// local
		setLeft(createLocalTypeElement(info));
		// remote
		setRight(createRemoteTypeElement(info));
		// base
		setAncestor(createBaseTypeElement(info));
		fireChange();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.compare.structuremergeviewer.DiffElement#getKind()
	 */
	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
	 */
	public void setAncestor(ITypedElement ancestor) {
		this.ancestor = ancestor;
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.compare.structuremergeviewer.DiffNode#getAncestor()
	 */
	public ITypedElement getAncestor() {
		return this.ancestor;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.compare.structuremergeviewer.DiffNode#getName()
	 */
	public String getName() {
		IResource resource = getResource();
		if(resource != null) {
			return resource.getName();
		} else {
			return super.getName();
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class adapter) {
		if(adapter == SyncInfo.class) {
			return 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.
	 */
	public IResource getResource() {
		ITypedElement element = getLeft();
		if(element instanceof ResourceNode) {
			return ((ResourceNode)element).getResource();
		}
		return null;
	}
	
	/**
	 * Return true if the receiver's Subscriber and Resource are equal to that of object.
	 * @param object The object to test
	 * @return true has the same subsriber and resource
	 */
	public boolean equals(Object object) {
		return this==object;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		IResource resource = getResource();
		if (resource == null) {
			return super.hashCode();
		}
		return resource.hashCode();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return getResource() != null ? getResource().getFullPath().toString() : getName();
	}
	
	/**
	 * 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));
			}
		} 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) {
				public boolean isEditable() {
						if(! resource.exists() && SyncInfo.getDirection(kind) == SyncInfo.OUTGOING && SyncInfo.getChange(kind) == SyncInfo.DELETION) {
							return false;
						}
						return super.isEditable();
					}
				};
		}
		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) {
		return new RemoteResourceTypedElement(remoteResource);
	}

	protected static ITypedElement createRemoteTypeElement(SyncInfo info) {
		if(info != null && info.getRemote() != null) {
			return createTypeElement(info.getRemote());
		}
		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());
		}
		return null;
	}
}

Back to the top