Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c4f864ee25aac9ee445c3cbb9a51913970f8cfc (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package org.eclipse.team.internal.ccvs.ui;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSTeamProvider;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.core.ICVSRunnable;
import org.eclipse.team.internal.ccvs.core.IResourceStateChangeListener;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.ui.IDecoratorManager;

/**
 * Classes registered with the workbench decoration extension point. The <code>CVSDecorationRunnable</code> class
 * actually calculates the decoration, while this class is responsible for listening to the following sources
 * for indications that the decorators need updating:
 * <ul>
 * 	<li>workbench label requests (decorateText/decorateImage)
 * 	<li>workspace resource change events (resourceChanged)
 * 	<li>cvs state changes (resourceStateChanged)
 * </ul>
 * <p>
 * [Note: There are several optimization that can be implemented in this class: (1) cache something
 * so that computing the dirty state of containers does not imply traversal of all children, (2) improve
 * the queue used between the decorator and the decorator runnable such that priority can be
 * given to visible elements when decoration requests are made.]
 */
public class CVSDecorator extends LabelProvider implements ILabelDecorator, IResourceChangeListener, IResourceStateChangeListener, IDecorationNotifier {
	
	// Resources that need an icon and text computed for display to the user
	private List decoratorNeedsUpdating = new ArrayList();

	// When decorations are computed they are added to this cache via decorated() method
	private Map cache = Collections.synchronizedMap(new HashMap());

	// Updater thread, computes decoration labels and images
	private Thread decoratorUpdateThread;

	private boolean shutdown = false;

	private OverlayIconCache iconCache = new OverlayIconCache();
	
	// Keep track of deconfigured projects
	private Set deconfiguredProjects = new HashSet();
	
	private static class DecoratorOverlayIcon extends OverlayIcon {		
		public DecoratorOverlayIcon(Image base, ImageDescriptor[] overlays, int[] locations) {
			super(base, overlays, locations, new Point(base.getBounds().width, base.getBounds().height));
		}
		protected void drawOverlays(ImageDescriptor[] overlays, int[] locations) {
			Point size = getSize();
			for (int i = 0; i < overlays.length; i++) {
				ImageDescriptor overlay = overlays[i];
				ImageData overlayData = overlay.getImageData();
				switch (locations[i]) {
					case TOP_LEFT:
						drawImage(overlayData, 0, 0);			
						break;
					case TOP_RIGHT:
						drawImage(overlayData, size.x - overlayData.width, 0);			
						break;
					case BOTTOM_LEFT:
						drawImage(overlayData, 0, size.y - overlayData.height);			
						break;
					case BOTTOM_RIGHT:
						drawImage(overlayData, size.x - overlayData.width, size.y - overlayData.height);			
						break;
				}
			}
		}
	}
	
	/*
	 * Return the CVSDecorator instance that is currently enabled.
	 * Return null if we don't have a decorator or its not enabled.
	 */ 
	/* package */ static CVSDecorator getActiveCVSDecorator() {
		IDecoratorManager manager = CVSUIPlugin.getPlugin().getWorkbench().getDecoratorManager();
		if (manager.getEnabled(CVSUIPlugin.DECORATOR_ID))
			return (CVSDecorator) manager.getLabelDecorator(CVSUIPlugin.DECORATOR_ID);
		return null;
	}
	
	/*
	 * Blanket refresh the displaying of our decorator.
	 */ 
	public static void refresh() {
		CVSDecorator activeDecorator = getActiveCVSDecorator();
		
		if(activeDecorator == null)
			return;	//nothing to do, our decorator isn't active
		activeDecorator.clearCache();  //clear the cache of previous decorations so we can compute them anew
		
		//update all displaying of our decorator;
		activeDecorator.fireLabelProviderChanged(new LabelProviderChangedEvent(activeDecorator));
	}
	
	/*
	 * Answers null if a provider does not exist or the provider is not a CVS provider. These resources
	 * will be ignored by the decorator.
	 */
	private static CVSTeamProvider getCVSProviderFor(IResource resource) {
		RepositoryProvider p = RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId());
		if (p == null) {
			return null;
		}
		return (CVSTeamProvider) p;
	}	


	public CVSDecorator() {
		// thread that calculates the decoration for a resource
		decoratorUpdateThread = new Thread(new CVSDecorationRunnable(this), "CVS"); //$NON-NLS-1$
		decoratorUpdateThread.start();
		CVSProviderPlugin.addResourceStateChangeListener(this);
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
	}

	public String decorateText(String text, Object o) {
		IResource resource = getResource(o);
		if (resource == null || text == null || resource.getType() == IResource.ROOT)
			return text;
		if (getCVSProviderFor(resource) == null)
			return text;

		CVSDecoration decoration = (CVSDecoration) cache.get(resource);

		if (decoration != null) {
			String format = decoration.getFormat();
			if (format == null) {
				return text;
			} else {
				Map bindings = decoration.getBindings();
				if (bindings.isEmpty())
					return text;
				bindings.put(CVSDecoratorConfiguration.RESOURCE_NAME, text);
				return CVSDecoratorConfiguration.bind(format, bindings);
			}
		} else {
			addResourcesToBeDecorated(new IResource[] { resource });
			return text;
		}
	}

	public Image decorateImage(Image image, Object o) {
		IResource resource = getResource(o);
		if (resource == null || image == null || resource.getType() == IResource.ROOT)
			return image;
		if (getCVSProviderFor(resource) == null)
			return image;

		CVSDecoration decoration = (CVSDecoration) cache.get(resource);

		if (decoration != null) {
			List overlays = decoration.getOverlays();
			int[] locations = decoration.getLocations();
			if (overlays != null) {
				return iconCache.getImageFor(new DecoratorOverlayIcon(image,
					(ImageDescriptor[]) overlays.toArray(new ImageDescriptor[overlays.size()]), locations));
			}
		} else {
			addResourcesToBeDecorated(new IResource[] { resource });
		}
		return image;
	}

	
	/*
	 * @see IDecorationNotifier#next()
	 */
	public synchronized IResource next() {
		try {
			if (shutdown) return null;
			
			if (decoratorNeedsUpdating.isEmpty()) {
				wait();
			}
			// We were awakened.
			if (shutdown) {
				// The decorator was awakened by the plug-in as it was shutting down.
				return null;
			}
			IResource resource = (IResource) decoratorNeedsUpdating.remove(0);

			//System.out.println("++ Next: " + resource.getFullPath() + " remaining in cache: " + cache.size());

			return resource;
		} catch (InterruptedException e) {
		}
		return null;
	}

	/*
	 * @see IDecorationNotifier#decorated(IResource[], CVSDecoration[])
	 */
	public synchronized void decorated(IResource[] resources, CVSDecoration[] decorations) {
		if(!shutdown) {
			List decorated = new ArrayList();

			for (int i = 0; i < resources.length; i++) {
				IResource resource= resources[i];
				if(resource.exists()) {
					cache.put(resource, decorations[i]);
					decorated.add(resource);
				}
			}
			postLabelEvent(new LabelProviderChangedEvent(this, decorated.toArray()));
		}
	}

	/*
	 * @see IDecorationNotifier#remaining()
	 */
	public int remaining() {
		return decoratorNeedsUpdating.size();
	}
	/*
	 * Handle resource changes and project description changes
	 * 
	 * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
	 */
	public void resourceChanged(IResourceChangeEvent event) {
		try {
			event.getDelta().accept(new IResourceDeltaVisitor() {
				public boolean visit(IResourceDelta delta) throws CoreException {
					IResource resource = delta.getResource();
					
					if(resource.getType()==IResource.ROOT) {
						// continue with the delta
						return true;
					}
					
					if (resource.getType() == IResource.PROJECT) {
						// deconfigure if appropriate (see CVSDecorator#projectDeconfigured(IProject))
						// do this even if there is a provider (this is required since old projects may still have a cvs nature)
						if (deconfiguredProjects.contains(resource)) {
							deconfiguredProjects.remove(resource);
							refresh((IProject)resource);
						}
						// If there is no CVS provider, don't continue
						if (RepositoryProvider.getProvider((IProject)resource, CVSProviderPlugin.getTypeId()) == null) {
							return false;
						}
					}
					
					if (delta.getKind() == IResourceDelta.REMOVED) {
						// remove the cached decoration for any removed resource
						cache.remove(resource);
					}
					
					return true;
				}
			});	
		} catch (CoreException e) {
			CVSProviderPlugin.log(e.getStatus());
		}
	}
	/*
	 * @see IResourceStateChangeListener#resourceStateChanged(IResource[])
	 */
	public void resourceStateChanged(IResource[] changedResources) {
		// add depth first so that update thread processes parents first.
		//System.out.println(">> State Change Event");
		List resources = new ArrayList();
		List noProviderResources = new ArrayList();
		for (int i = 0; i < changedResources.length; i++) {
			// ignore subtrees that aren't associated with a provider, this can happen on import
			// of a new project to CVS.
			IResource resource = changedResources[i];
			if (getCVSProviderFor(resource) == null) {
				// post a changed event but forget any cached information about this resource
				noProviderResources.add(resource);
			}
			resources.addAll(computeParents(resource));
		}
		
		addResourcesToBeDecorated((IResource[]) resources.toArray(new IResource[resources.size()]));
		
		// post label events for resources that cannot or should not be decorated by CVS
		if(!noProviderResources.isEmpty()) {
			List resourcesToUpdate = new ArrayList();
			for (Iterator it = resources.iterator(); it.hasNext();) {
				IResource element = (IResource) it.next();
				resourcesToUpdate.add(element);
			}
			postLabelEvent(new LabelProviderChangedEvent(this, resourcesToUpdate.toArray()));
		}
	}

	private void clearCache() {
		cache.clear();
	}
	
	public void refresh(IProject project) {
		final List resources = new ArrayList();
		try {
			project.accept(new IResourceVisitor() {
				public boolean visit(IResource resource) {
					resources.add(resource);
					return true;
				}
			});
			resourceStateChanged((IResource[]) resources.toArray(new IResource[resources.size()]));	
		} catch (CoreException e) {
		}
	}

	private List computeParents(IResource resource) {
		IResource current = resource;
		List resources = new ArrayList();
		if(CVSUIPlugin.getPlugin().getPreferenceStore().getBoolean(ICVSUIConstants.PREF_CALCULATE_DIRTY)) {
			while (current.getType() != IResource.ROOT) {
				resources.add(current);
				current = current.getParent();
			}
		} else {
			resources.add(current);			
		}
		return resources;
	}
	
	private synchronized void addResourcesToBeDecorated(IResource[] resources) {
		if (resources.length > 0) {
			for (int i = 0; i < resources.length; i++) {
				IResource resource = resources[i];
				if(!decoratorNeedsUpdating.contains(resource)) {
					decoratorNeedsUpdating.add(resource);
				}
			}
			notify();
		}
	}

	/**
	 * Returns the resource for the given input object, or
	 * null if there is no resource associated with it.
	 * 
	 * @param object  the object to find the resource for
	 * @return the resource for the given object, or null
	 */
	private IResource getResource(Object object) {
		if (object instanceof IResource) {
			return (IResource)object;
		}
		if (object instanceof IAdaptable) {
			return (IResource)((IAdaptable)object).getAdapter(IResource.class);
		}
		return null;
	}

	/**
	 * Post the label event to the UI thread
	 * 
	 * @param events  the events to post
	 */
	private void postLabelEvent(final LabelProviderChangedEvent event) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				fireLabelProviderChanged(event);
			}
		});
	} 

	
	private void shutdown() {
		shutdown = true;
		// Wake the thread up if it is asleep.
		synchronized (this) {
			notifyAll();
		}
		try {
			// Wait for the decorator thread to finish before returning.
			decoratorUpdateThread.join();
		} catch (InterruptedException e) {
		}
	}
		
	/*
	 * @see IBaseLabelProvider#dispose()
	 */
	public void dispose() {
		super.dispose();
		
		// terminate decoration thread
		shutdown();
		
		// unregister change listeners
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		CVSProviderPlugin.removeResourceStateChangeListener(this);
		
		// dispose of images created as overlays
		decoratorNeedsUpdating.clear();
		clearCache();
		iconCache.disposeAll();
	}
	/**
	 * @see IResourceStateChangeListener#projectConfigured(IProject)
	 */
	public void projectConfigured(IProject project) {
		refresh(project);
	}

	/**
	 * @see IResourceStateChangeListener#projectDeconfigured(IProject)
	 */
	public void projectDeconfigured(IProject project) {
		// Unfortunately, the nature is still associated with the project at this point.
		// Therefore, we will remember that the project has been deconfigured and we will 
		// refresh the decorators in the resource delta listener
		deconfiguredProjects.add(project);
	}
	
	public static boolean isDirty(final ICVSResource cvsFile) {
		try {
			final boolean[] isDirty = new boolean[] {false};
			cvsFile.getParent().run(new ICVSRunnable() {
				public void run(IProgressMonitor monitor) throws CVSException {
					// file is dirty or file has been merged by an update
					if(!cvsFile.isIgnored()) {
						isDirty[0] = cvsFile.isModified();
					}
				}
			}, ICVSFolder.READ_ONLY, null);
			return isDirty[0];
		} catch (CVSException e) {
			//if we get an error report it to the log but assume dirty
			CVSUIPlugin.log(e.getStatus());
			return true;
		}
	}

	public static boolean isDirty(IResource resource) {
		
		// No need to decorate non-existant resources
		if (!resource.exists()) return false;
		
		return isDirty(CVSWorkspaceRoot.getCVSResourceFor(resource));

	}
	/**
	 * This method is used to indicate whether a particular resource is a member of
	 * a project that is in the process of being deconfigured. Such resources should not
	 * be decorated.
	 */
	/* package */ boolean isMemberDeconfiguredProject(IResource resource) {
		if (deconfiguredProjects.isEmpty()) return false;
		return deconfiguredProjects.contains(resource.getProject());
	}
	
	/**
	 * @see org.eclipse.team.internal.ccvs.core.IResourceStateChangeListener#resourceSyncInfoChanged(org.eclipse.core.resources.IResource[])
	 */
	public void resourceSyncInfoChanged(IResource[] changedResources) {
		resourceStateChanged(changedResources);
	}
	/**
	 * @see org.eclipse.team.internal.ccvs.core.IResourceStateChangeListener#resourceModificationStateChanged(org.eclipse.core.resources.IResource[])
	 */
	public void resourceModified(IResource[] changedResources) {
		resourceStateChanged(changedResources);
	}

}

Back to the top