Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f74d3d54058f80d32e876ae9e7108cffeeb3b887 (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 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.ccvs.ui.subscriber;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.mapping.ISynchronizationContext;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.core.subscribers.SubscriberMergeContext;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.mapping.ChangeSetModelProvider;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.mappings.ModelCompareParticipant;
import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter;
import org.eclipse.team.internal.core.subscribers.DiffChangeSet;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
import org.eclipse.team.ui.TeamUI;
import org.eclipse.team.ui.mapping.SynchronizationStateTester;
import org.eclipse.team.ui.synchronize.*;


public class CVSParticipantLabelDecorator extends LabelProvider implements IPropertyChangeListener, ILabelDecorator {
	private ISynchronizePageConfiguration configuration;
	private SynchronizationStateTester tester = new SynchronizationStateTester() {
		public int getState(Object element, int stateMask, IProgressMonitor monitor) throws CoreException {
			// Disable state decoration
			return IDiff.NO_CHANGE;
		}
	};
	
	public CVSParticipantLabelDecorator(ISynchronizePageConfiguration configuration) {
		this.configuration = configuration;
		// Listen for decorator changed to refresh the viewer's labels.
		CVSUIPlugin.addPropertyChangeListener(this);
		TeamUI.addPropertyChangeListener(this);
	}
	
	public String decorateText(String input, Object elementOrPath) {
		if (!isEnabledFor(elementOrPath))
			return input;
		try {
			String text = input;
			IResource resource = getResource(elementOrPath);
			if (resource != null && resource.getType() != IResource.ROOT) {
				// Prepare the decoration but substitute revision and hide dirty indicator
				CVSDecoration decoration = getDecoration(resource);
				decoration.setRevision(getRevisionNumber(elementOrPath));
				decoration.compute();
				// Update label
				StringBuffer output = new StringBuffer(25);
				if (decoration.getPrefix() != null) {
					output.append(decoration.getPrefix());
				}
				output.append(text);
				if (decoration.getSuffix() != null) {
					output.append(decoration.getSuffix());
				}
				return output.toString();
			}
			return text;
		} catch (CoreException e) {
			return input;
		}
	}
	
	protected IResource getResource(Object element) {
		if (element instanceof ISynchronizeModelElement)
			return ((ISynchronizeModelElement) element).getResource();
		return Utils.getResource(internalGetElement(element));
	}

    protected CVSDecoration getDecoration(IResource resource) throws CoreException {
        return CVSLightweightDecorator.decorate(resource, tester);
    }

    public Image decorateImage(Image base, Object element) {
		return base;
	}
	public void propertyChange(PropertyChangeEvent event) {
		if (needsRefresh(event)) {
			Viewer viewer = configuration.getPage().getViewer();
			if(viewer instanceof StructuredViewer && !viewer.getControl().isDisposed()) {
				((StructuredViewer)viewer).refresh(true);
			}
		}
	}
	
	protected boolean needsRefresh(PropertyChangeEvent event) {
	    final String property= event.getProperty();
	    return property.equals(CVSUIPlugin.P_DECORATORS_CHANGED) || property.equals(TeamUI.GLOBAL_FILE_TYPES_CHANGED);
	}
	public void dispose() {
		CVSUIPlugin.removePropertyChangeListener(this);
		TeamUI.removePropertyChangeListener(this);
	}
	
	protected String getRevisionNumber(Object elementOrPath) {
		IResource resource = getResource(elementOrPath);
		if (resource != null && resource.getType() == IResource.FILE) {
			IResourceVariant local;
			try {
				local = (IResourceVariant) CVSWorkspaceRoot.getRemoteResourceFor(resource);
			} catch (CVSException e) {
				local = null;
			}
			if(local == null) {
				local = getBase(elementOrPath);
			}
			String localRevision = getRevisionString(local);
			StringBuffer revisionString = new StringBuffer();
			IResourceVariant remote = getRemote(elementOrPath);
			String remoteRevision = getRevisionString(remote);
			if(localRevision != null) {
				revisionString.append(localRevision);
			}
			if(remoteRevision != null) {
				revisionString.append( (localRevision != null ? " - " : "") + remoteRevision); //$NON-NLS-1$ //$NON-NLS-2$
			}
			return revisionString.toString();
		}
		return null;
	}

	private boolean isEnabledFor(Object elementOrPath) {
		return !isCompareWithChangeSets() || elementOrPath instanceof TreePath;
	}

	private boolean isCompareWithChangeSets() {
		String id = (String)configuration.getProperty(ModelSynchronizeParticipant.P_VISIBLE_MODEL_PROVIDER);
		return configuration.getParticipant() instanceof ModelCompareParticipant && id.equals(ChangeSetModelProvider.ID);
	}

	private SyncInfo getSyncInfo(Object element) {
		if (element instanceof SyncInfoModelElement) {
			SyncInfoModelElement sime = (SyncInfoModelElement) element;
			return sime.getSyncInfo();
		}
		IResource resource = getResource(element);
		if (resource != null) {
			ISynchronizeParticipant participant = configuration.getParticipant();
			if (participant instanceof ModelSynchronizeParticipant) {
				ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
				ISynchronizationContext context = msp.getContext();
				if (context instanceof SubscriberMergeContext) {
					SubscriberMergeContext smc = (SubscriberMergeContext) context;
					Subscriber subscriber = smc.getSubscriber();
					try {
						return subscriber.getSyncInfo(resource);
					} catch (TeamException e) {
						CVSUIPlugin.log(e);
					}
				}
			}
		}
		return null;
	}
	
	private IResourceVariant getBase(Object element) {
		if (element instanceof TreePath) {
			TreePath tp = (TreePath) element;
			IDiff diff = getDiff(tp);
			IFileRevision revision = Utils.getBase(diff);
			return SyncInfoToDiffConverter.asResourceVariant(revision);
		}
		SyncInfo info = getSyncInfo(element);
		if (info != null)
			return info.getBase();
		return null;
	}
	
	private IResourceVariant getRemote(Object element) {
		if (element instanceof TreePath) {
			TreePath tp = (TreePath) element;
			IDiff diff = getDiff(tp);
			IFileRevision revision = Utils.getRemote(diff);
			return SyncInfoToDiffConverter.asResourceVariant(revision);
		}
		SyncInfo info = getSyncInfo(element);
		if (info != null)
			return info.getRemote();
		return null;
	}

	private IDiff getDiff(TreePath tp) {
		Object first = tp.getFirstSegment();
		Object last = tp.getLastSegment();
		IResource resource = Utils.getResource(last);
		if (first instanceof DiffChangeSet && resource != null) {
			DiffChangeSet dcs = (DiffChangeSet) first;
			return dcs.getDiffTree().getDiff(resource);
		}
		return null;
	}

	private String getRevisionString(IResourceVariant remoteFile) {
		if(remoteFile instanceof RemoteFile) {
			return ((RemoteFile)remoteFile).getRevision();
		}
		return null;
	}
	
	private Object internalGetElement(Object elementOrPath) {
		if (elementOrPath instanceof TreePath) {
			TreePath tp = (TreePath) elementOrPath;
			return tp.getLastSegment();
		}
		return elementOrPath;
	}
}

Back to the top