Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3d9b67e0e6df8fa81ea074837367778590f7438c (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
/*******************************************************************************
 * 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.views;

import java.util.*;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.subscribers.*;
import org.eclipse.team.internal.core.Assert;
import org.eclipse.team.internal.ui.*;
import org.eclipse.team.ui.ISharedImages;
import org.eclipse.team.ui.synchronize.SyncInfoDiffNode;
import org.eclipse.ui.internal.WorkbenchColors;

/**
 * Decorates the text and labels from a <code>SyncInfoLabelProvider</code>
 * with the proper sync kind indications. 
 * This class provides a facility for subclasses to define annotations
 * on the labels and icons of adaptable objects by overriding
 * <code>decorateText()</code> and <code>decorateImage</code>.
 * 
 * @see SyncInfoSetCompareConfiguration#getLabelProvider(SyncInfoLabelProvider)
 * @since 3.0
 */
public class SyncInfoDecoratingLabelProvider extends LabelProvider implements ITableLabelProvider, IColorProvider {
	
	//column constants
	private static final int COL_RESOURCE = 0;
	private static final int COL_PARENT = 1;
	private boolean working = false;
	
	// cache for folder images that have been overlayed with conflict icon
	private Map fgImageCache;
	
	// Keep track of the compare provider and sync info label provider
	// so they can be properly disposed
	CompareConfiguration compareConfig = new CompareConfiguration();
	SyncInfoLabelProvider syncInfoLabelProvider;

	public SyncInfoDecoratingLabelProvider() {
		this(new SyncInfoLabelProvider());
	}
	
	public SyncInfoDecoratingLabelProvider(SyncInfoLabelProvider syncInfoLabelProvider) {
		Assert.isNotNull(syncInfoLabelProvider);
		JobStatusHandler.addJobListener(new IJobListener() {
			public void started(QualifiedName jobType) {
				working = true;
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						// TODO: What this is this supposed to be?
						synchronized (this) {
							fireLabelProviderChanged(new LabelProviderChangedEvent(SyncInfoDecoratingLabelProvider.this));
						}
					}
				});
			}
			public void finished(QualifiedName jobType) {
				working = false;
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						synchronized (this) {
							fireLabelProviderChanged(new LabelProviderChangedEvent(SyncInfoDecoratingLabelProvider.this));
						}
					}
				});
			}
		}, TeamSubscriber.SUBSCRIBER_JOB_TYPE);
		
		// The label provider may of been created after the subscriber job has been
		// started.
		this.working = JobStatusHandler.hasRunningJobs(TeamSubscriber.SUBSCRIBER_JOB_TYPE);
		this.syncInfoLabelProvider = syncInfoLabelProvider;
	}

	protected String decorateText(String input, Object element) {
		return input;
	}
	
	protected Image decorateImage(Image base, Object element) {
		return base;
	}
	
	public String getText(Object element) {
		String name = syncInfoLabelProvider.getText(element);
		if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.SYNCVIEW_VIEW_SYNCINFO_IN_LABEL)) {
			SyncInfo info = syncInfoLabelProvider.getSyncInfo(element);
			if (info != null && info.getKind() != SyncInfo.IN_SYNC) {
				String syncKindString = SyncInfo.kindToString(info.getKind());
				name = Policy.bind("TeamSubscriberSyncPage.labelWithSyncKind", name, syncKindString); //$NON-NLS-1$
			}
		}
		return decorateText(name, element);
	}
	
	/**
	 * An image is decorated by at most 3 different plugins. 
	 * 1. ask the sync info label decorator for the default icon for the resource
	 * 2. ask the compare plugin for the sync kind overlay
	 * 3. overlay the conflicting image on folders/projects containing conflicts 
	 */
	public Image getImage(Object element) {
		Image decoratedImage = null;
		IResource resource = syncInfoLabelProvider.getResource(element);
		Image image = syncInfoLabelProvider.getImage(element);
		decoratedImage = getCompareImage(image, element);	
		decoratedImage = propagateConflicts(decoratedImage, element, resource);
		return decorateImage(decoratedImage, element);
	}
	
	private Image getCompareImage(Image base, Object element) {
		int kind = getSyncKind(element);
		switch (kind & SyncInfo.DIRECTION_MASK) {
			case SyncInfo.OUTGOING:
				kind = (kind &~ SyncInfo.OUTGOING) | SyncInfo.INCOMING;
				break;
			case SyncInfo.INCOMING:
				kind = (kind &~ SyncInfo.INCOMING) | SyncInfo.OUTGOING;
				break;
		}	
		return compareConfig.getImage(base, kind);
	}
	
	private Image propagateConflicts(Image base, Object element, IResource resource) {
		if(element instanceof SyncInfoDiffNode && resource.getType() != IResource.FILE) {
			// if the folder is already conflicting then don't bother propagating the conflict
			int kind = getSyncKind(element);
			if((kind & SyncInfo.DIRECTION_MASK) != SyncInfo.CONFLICTING) {
				if(((SyncInfoDiffNode)element).hasDecendantConflicts()) {
					ImageDescriptor overlay = new OverlayIcon(
	   					base, 
	   					new ImageDescriptor[] { TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_CONFLICT_OVR)}, 
	   					new int[] {OverlayIcon.BOTTOM_LEFT}, 
	   					new Point(base.getBounds().width, base.getBounds().height));
	  
					if(fgImageCache == null) {
	   					fgImageCache = new HashMap(10);
	 				}
	 				Image conflictDecoratedImage = (Image) fgImageCache.get(overlay);
	 				if (conflictDecoratedImage == null) {
	   					conflictDecoratedImage = overlay.createImage();
	   					fgImageCache.put(overlay, conflictDecoratedImage);
				 	}
					return conflictDecoratedImage;
				}
			}
		}
		return base;
	}
	
	private int getSyncKind(Object element) {
		SyncInfo info = syncInfoLabelProvider.getSyncInfo(element);
		if (info != null) {
			return info.getKind();
		}
		return SyncInfo.IN_SYNC;
	}
	   
	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
	 */
	public void dispose() {
		super.dispose();
		syncInfoLabelProvider.dispose();
		compareConfig.dispose();
		if(fgImageCache != null) {
			Iterator it = fgImageCache.values().iterator();
			while (it.hasNext()) {
				Image element = (Image) it.next();
				element.dispose();				
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
	 */
	public Image getColumnImage(Object element, int columnIndex) {
		if (columnIndex == COL_RESOURCE) {
			return getImage(element);
		} else if (columnIndex == COL_PARENT) {
			return null;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
	 */
	public String getColumnText(Object element, int columnIndex) {
		if (columnIndex == COL_RESOURCE) {
			return getText(element);
		} else if (columnIndex == COL_PARENT) {
			IResource resource = syncInfoLabelProvider.getResource(element);
			return resource.getParent().getFullPath().toString();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
	 */
	public Color getForeground(Object element) {	
		if (working)  {
			return WorkbenchColors.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
		} else  {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
	 */
	public Color getBackground(Object element) {
		return null;
	}
}

Back to the top