Skip to main content
summaryrefslogtreecommitdiffstats
blob: d44eb752f9422f5b5cc56d81cbd2031a568ee1e4 (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
package org.eclipse.team.internal.ccvs.ui;

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

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

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
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.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.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.ccvs.core.CVSTeamProvider;
import org.eclipse.team.core.IResourceStateChangeListener;
import org.eclipse.team.core.ITeamProvider;
import org.eclipse.team.core.TeamPlugin;
import org.eclipse.team.internal.ccvs.core.CVSProvider;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.util.Assert;
import org.eclipse.team.internal.ccvs.core.util.ResourceDeltaVisitor;

/**
 * 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, IResourceStateChangeListener, IDecorationNotifier {

	private static CVSDecorator theDecorator = null;
	
	// 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 Hashtable imageCache = new Hashtable();
	
	private ChangeListener changeListener;
	
	private class ChangeListener extends ResourceDeltaVisitor {
		List changedResources = new ArrayList();
		protected void handleAdded(IResource[] resources) {
		}
		protected void handleRemoved(IResource[] resources) {
		}
		protected void handleChanged(IResource[] resources) {
			changedResources.addAll(Arrays.asList(resources));
		}
		protected void finished() {
			resourceStateChanged((IResource[])changedResources.toArray(new IResource[changedResources.size()]));
		}
		protected int getEventMask() {
			return IResourceChangeEvent.PRE_AUTO_BUILD;
		}
	}
	
	public CVSDecorator() {
		// The decorator is a singleton, there should never be more than one instance.
		// temporary until the UI component properly calls dispose when the workbench shutsdown
		// UI Bug 9633
		Assert.isTrue(theDecorator==null);
		theDecorator = this;
		
		decoratorUpdateThread = new Thread(new CVSDecorationRunnable(this), "CVS"); //$NON-NLS-1$
		decoratorUpdateThread.start();
		TeamPlugin.getManager().addResourceStateChangeListener(this);
		changeListener = new ChangeListener();
		changeListener.register();
	}

	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();
			return overlays == null ? image : getCachedImage(image, overlays);
		} else {
			addResourcesToBeDecorated(new IResource[] { resource });
			return image;
		}
	}

	/**
	 * Get the composite image for the given image and overlays. Return one from the cache if
	 * it exists. If not, create it, cache it, and return it.
	 */
	private Image getCachedImage(Image image, List overlays) {
		Hashtable overlayTable = (Hashtable)imageCache.get(image);
		if (overlayTable == null) {
			overlayTable = new Hashtable();
			imageCache.put(image, overlayTable);
		}
		Image cachedImage = (Image)overlayTable.get(overlays);
		if (cachedImage == null) {
			cachedImage = new OverlayIcon(image.getImageData(), new ImageDescriptor[][] {(ImageDescriptor[])overlays.toArray(new ImageDescriptor[overlays.size()])}, new Point(16, 16)).createImage();
			overlayTable.put(overlays, cachedImage);
		}
		return cachedImage;
	}
	
	/*
	 * @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#notify(IResource, CVSDecoration)
	 */
	public synchronized void decorated(IResource resource, CVSDecoration decoration) {
		// ignore resources that aren't in the workbench anymore.
		if(resource.exists() && !shutdown) {
			cache.put(resource, decoration);
			postLabelEvents(new LabelProviderChangedEvent[] { new LabelProviderChangedEvent(this, resource)});
		}
	}

	/*
	 * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
	 */

	/*
	 * @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 events = new ArrayList();
			for (Iterator it = resources.iterator(); it.hasNext();) {
				IResource element = (IResource) it.next();
				events.add(new LabelProviderChangedEvent(this, element));
			}
			postLabelEvents((LabelProviderChangedEvent[]) events.toArray(new LabelProviderChangedEvent[events.size()]));
		}
	}

	public static void refresh() {
		try {
			IResource[] resources = ResourcesPlugin.getWorkspace().getRoot().members();
			for (int i = 0; i < resources.length; i++) {
				if (getCVSProviderFor(resources[i]) != null) {
					refresh(resources[i]);
				}
			}
		} catch (CoreException e) {
		}
	}

	public static void refresh(IResource resource) {
		final List resources = new ArrayList();
		try {
			resource.accept(new IResourceVisitor() {
				public boolean visit(IResource resource) {
					resources.add(resource);
					return true;
				}
			});
			TeamPlugin.getManager().broadcastResourceStateChanges((IResource[]) resources.toArray(new IResource[resources.size()]));	
		} catch (CoreException e) {
		}
	}

	private List computeParents(IResource resource) {
		IResource current = resource;
		List resources = new ArrayList();
		while (current.getType() != IResource.ROOT) {
			resources.add(current);
			current = current.getParent();
		}
		return resources;
	}
	
	private synchronized void addResourcesToBeDecorated(IResource[] resources) {
		if (resources.length > 0) {
			for (int i = 0; i < resources.length; i++) {
				IResource resource = resources[i];
				//System.out.println("\t to update: " + resource.getFullPath());
				if(!decoratorNeedsUpdating.contains(resource)) {
					//System.out.println("\t adding: " + resource.getFullPath());
					decoratorNeedsUpdating.add(resource);
				}
			}
			notify();
		}
	}

	/** 
	 * 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) {
		ITeamProvider p = TeamPlugin.getManager().getProvider(resource);
		if (p == null || !(p instanceof CVSTeamProvider)) {
			return null;
		}
		return (CVSTeamProvider) p;
	}
	
	/**
	 * 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 events to the UI thread
	 * 
	 * @param events  the events to post
	 */
	private void postLabelEvents(final LabelProviderChangedEvent[] events) {
		// now post the change events to the UI thread
		if (events.length > 0) {
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					for (int i = 0; i < events.length; i++) {
						fireLabelProviderChanged(events[i]);
					}
				}
			});
		}
	} 
	
	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) {
		}
	}

	public static void shutdownAll() {
		if(theDecorator!=null) {
			theDecorator.dispose();
		}
	}
	
	public static String getFileTypeString(String name, String keyword) {
		StringBuffer buffer = new StringBuffer();
		boolean isBinary = false;
		if(keyword!=null) {
			if (keyword.equals("-kb")) { //$NON-NLS-1$
				isBinary = true;
			}
		} else {
			isBinary = !CVSProvider.isText(name);
		}
		
		if(isBinary) {
			buffer.append(Policy.bind("CVSFilePropertiesPage.binary")); 
		} else {
			buffer.append(Policy.bind("CVSFilePropertiesPage.text"));
			if(keyword!=null && !keyword.equals("-ko") && !"".equals(keyword)) { //$NON-NLS-1$ //$NON-NLS-2$
				buffer.append(" " + keyword); //$NON-NLS-1$
			}
		}		
		return buffer.toString();
	}
	
	/*
	 * @see IBaseLabelProvider#dispose()
	 */
	public void dispose() {
		super.dispose();
		
		// terminate decoration thread
		shutdown();
		
		// unregister change listeners
		changeListener.register();
		TeamPlugin.getManager().removeResourceStateChangeListener(this);
		
		// dispose of images created as overlays
		decoratorNeedsUpdating.clear();
		cache.clear();
		Iterator it = imageCache.values().iterator();
		while (it.hasNext()) {
			Hashtable overlayTable = (Hashtable)it.next();
			Iterator it2 = overlayTable.values().iterator();
			while (it2.hasNext()) {
				((Image)it2.next()).dispose();
			}
		}
		imageCache = new Hashtable();
		theDecorator = null;
	}
}

Back to the top