Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e8caf800bb627f1b24509b8fab934087d2ef2c4 (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
/*******************************************************************************
 * 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.internal.ccvs.ui;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.swt.graphics.*;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.util.Assert;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.themes.ITheme;

/**
 * A decoration describes the annotations to a user interface element. The
 * annotations can apply to text (e.g. prefix, suffix, color, font) and to an
 * image (e.g. overlays).
 * <p>
 * This class is derived from an internal workbench class
 * <code>IDecoration</code> and is often used in conjunction with the label
 * decoration APIs. As such a client can convert between them using helpers
 * defined in this class.
 * </p>
 * TODO: 
 * profile
 * add colors and fonts to preferences instead of being hard coded
 * what to do with CVSDecorationConfiguration class?
 * preference page externalizations
 * preference page preview should update when theme changes
 * 
 * @since 3.1
 */
public class CVSDecoration {

	// Decorations
	private String prefix;
	private String suffix;
	private ImageDescriptor overlay;
	private Color bkgColor;
	private Color fgColor;
	private Font font;
	
	// Properties
	private int resourceType = IResource.FILE;
	private boolean watchEditEnabled = false;
	private boolean isDirty = false;
	private boolean isIgnored = false;
	private boolean isAdded = false;
	private boolean isNewResource = false;
	private boolean hasRemote = false;
	private boolean readOnly = false;
	private boolean needsMerge = false;
	private boolean virtualFolder = false;
	private String tag;
	private String revision;
	private String repository;
	private ICVSRepositoryLocation location;
	private String keywordSubstitution;

	// Text formatters
	private String fileFormatter;
	private String folderFormatter;
	private String projectFormatter;
	private String dirtyTextIndicator;
	private String addedTextIndicator;
	private String resourceName;
	
	//	Images cached for better performance
	private static ImageDescriptor dirty;
	private static ImageDescriptor checkedIn;
	private static ImageDescriptor noRemoteDir;
	private static ImageDescriptor added;
	private static ImageDescriptor merged;
	private static ImageDescriptor newResource;
	private static ImageDescriptor edited;
	
	// List of preferences used to configure the decorations that
	// are applied.
	private Preferences preferences;

	/*
	 * Define a cached image descriptor which only creates the image data once
	 */
	public static class CachedImageDescriptor extends ImageDescriptor {

		ImageDescriptor descriptor;
		ImageData data;

		public CachedImageDescriptor(ImageDescriptor descriptor) {
			Assert.isNotNull(descriptor);
			this.descriptor = descriptor;
		}

		public ImageData getImageData() {
			if (data == null) {
				data = descriptor.getImageData();
			}
			return data;
		}
	}

	static {
		dirty = new CachedImageDescriptor(TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_DIRTY_OVR));
		checkedIn = new CachedImageDescriptor(TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_CHECKEDIN_OVR));
		added = new CachedImageDescriptor(TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_CHECKEDIN_OVR));
		merged = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_MERGED));
		newResource = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_QUESTIONABLE));
		edited = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_EDITED));
		noRemoteDir = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_NO_REMOTEDIR));
	}

	/**
	 * Default constructor uses the plug-in's preferences to determine text decoration
	 * formatters and preferences.
	 */
	public CVSDecoration(String resourceName) {
		// 	TODO: for efficiency don't look up a pref until its needed
		IPreferenceStore store = getStore();
		Preferences prefs = new Preferences();
		
		prefs.setValue(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION));
		prefs.setValue(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION));
		prefs.setValue(ICVSUIConstants.PREF_SHOW_HASREMOTE_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_HASREMOTE_DECORATION));
		prefs.setValue(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION));
		prefs.setValue(ICVSUIConstants.PREF_CALCULATE_DIRTY, store.getBoolean(ICVSUIConstants.PREF_CALCULATE_DIRTY));
		prefs.setValue(ICVSUIConstants.PREF_DIRTY_FLAG, store.getString(ICVSUIConstants.PREF_DIRTY_FLAG));
		prefs.setValue(ICVSUIConstants.PREF_ADDED_FLAG, store.getString(ICVSUIConstants.PREF_ADDED_FLAG));
		
		initialize(resourceName, prefs, store.getString(ICVSUIConstants.PREF_FILETEXT_DECORATION), store.getString(ICVSUIConstants.PREF_FOLDERTEXT_DECORATION), store.getString(ICVSUIConstants.PREF_PROJECTTEXT_DECORATION));
	}

	public CVSDecoration(String resourceName, Preferences preferences, String fileFormater, String folderFormatter, String projectFormatter) {
		initialize(resourceName, preferences, fileFormater, folderFormatter, projectFormatter);
	}

	private IPreferenceStore getStore() {
		return CVSUIPlugin.getPlugin().getPreferenceStore();
	}

	private void initialize(String resourceName, Preferences preferences, String fileFormater, String folderFormatter, String projectFormatter) {
		this.resourceName = resourceName;
		this.preferences = preferences;
		this.fileFormatter = fileFormater;
		this.folderFormatter = folderFormatter;
		this.projectFormatter = projectFormatter;
	}

	public void addPrefix(String prefix) {
		this.prefix = prefix;
	}

	public void addSuffix(String suffix) {
		this.suffix = suffix;
	}

	public void addOverlay(ImageDescriptor overlay) {
		this.overlay = overlay;
	}

	public void setForegroundColor(Color fgColor) {
		this.fgColor = fgColor;
	}

	public void setBackgroundColor(Color bkgColor) {
		this.bkgColor = bkgColor;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public Color getBackgroundColor() {
		return bkgColor;
	}

	public Color getForegroundColor() {
		return fgColor;
	}

	public Font getFont() {
		return font;
	}

	public ImageDescriptor getOverlay() {
		return overlay;
	}

	public String getPrefix() {
		return prefix;
	}

	public String getSuffix() {
		return suffix;
	}
	
	public void setResourceType(int type) {
		this.resourceType = type;
	}

	public void apply(IDecoration decoration) {
		compute();
		// apply changes
		String suffix = getSuffix();
		if(suffix != null)
			decoration.addSuffix(suffix);
		String prefix = getPrefix();
		if(prefix != null)
			decoration.addPrefix(prefix);
		ImageDescriptor overlay = getOverlay();
		if(overlay != null)
			decoration.addOverlay(getOverlay());
		Color bc = getBackgroundColor();
		if(bc != null)
			decoration.setBackgroundColor(bc);
		Color fc = getForegroundColor();
		if(fc != null)
			decoration.setForegroundColor(fc);
		Font f = getFont();
		if(f != null)
			decoration.setFont(f);
	}

	public void compute() {
		computeText();
		overlay = computeImage();
		computeColorsAndFonts();
	}

	private void computeText() {
		Map bindings = new HashMap();
		IPreferenceStore store = getStore();
		if (isDirty()) {
			bindings.put(CVSDecoratorConfiguration.DIRTY_FLAG, preferences.getString(ICVSUIConstants.PREF_DIRTY_FLAG));
		}
		if (isAdded()) {
			bindings.put(CVSDecoratorConfiguration.ADDED_FLAG, preferences.getString(ICVSUIConstants.PREF_ADDED_FLAG));
		} else if(isHasRemote()){
			bindings.put(CVSDecoratorConfiguration.FILE_REVISION, getRevision());
			bindings.put(CVSDecoratorConfiguration.RESOURCE_TAG, getTag());
		}	
		bindings.put(CVSDecoratorConfiguration.RESOURCE_NAME, getResourceName());
		bindings.put(CVSDecoratorConfiguration.FILE_KEYWORD, getKeywordSubstitution());
		if (resourceType != IResource.FILE && location != null) {
			bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_HOST, location.getHost());
			bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_METHOD, location.getMethod().getName());
			bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_USER, location.getUsername());
			bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_ROOT, location.getRootDirectory());
			bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_REPOSITORY, repository);
		}
		CVSDecoratorConfiguration.decorate(this, getTextFormatter(), bindings);
	}

	private ImageDescriptor computeImage() {
		// show newResource icon
		if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION) && isNewResource()) {
			return newResource;
		}
		// show dirty icon
		if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION) && isDirty()) {
			return dirty;
		}
		// show added
		if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION) && isAdded()) {
			return added;
		}
		// show watch edit
		if (isWatchEditEnabled() && resourceType == IResource.FILE && !isReadOnly() && isHasRemote()) {
			return edited;
		}
		// show checked in
		if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_HASREMOTE_DECORATION) && isHasRemote()) {
			if (resourceType != IResource.FILE && isVirtualFolder()) {
				return noRemoteDir;
			}
			return checkedIn;
		}
		//nothing matched
		return null;
	}	
	
	private void computeColorsAndFonts() {
		ITheme current = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme();
		if(isIgnored()) {
			setBackgroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.IGNORED_BACKGROUND_COLOR));
			setForegroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.IGNORED_FOREGROUND_COLOR));
			setFont(current.getFontRegistry().get(CVSDecoratorConfiguration.IGNORED_FONT));
		} else if(isDirty()) {
			setBackgroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.OUTGOING_CHANGE_BACKGROUND_COLOR));
			setForegroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.OUTGOING_CHANGE_FOREGROUND_COLOR));
			setFont(current.getFontRegistry().get(CVSDecoratorConfiguration.OUTGOING_CHANGE_FONT));
		}
	}

	private String getResourceName() {
		return resourceName;
	}

	private String getTextFormatter() {
		switch (resourceType) {
			case IResource.FILE :
				return fileFormatter;
			case IResource.FOLDER :
				return folderFormatter;
			case IResource.PROJECT :
				return projectFormatter;
		}
		return "no format specified"; //$NON-NLS-1$
	}

	public boolean isAdded() {
		return isAdded;
	}

	public void setAdded(boolean isAdded) {
		this.isAdded = isAdded;
	}

	public boolean isDirty() {
		return isDirty;
	}

	public void setDirty(boolean isDirty) {
		this.isDirty = isDirty;
	}

	public boolean isIgnored() {
		return isIgnored;
	}

	public void setIgnored(boolean isIgnored) {
		this.isIgnored = isIgnored;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public boolean isWatchEditEnabled() {
		return watchEditEnabled;
	}

	public void setWatchEditEnabled(boolean watchEditEnabled) {
		this.watchEditEnabled = watchEditEnabled;
	}

	public boolean isNewResource() {
		return isNewResource;
	}

	public void setNewResource(boolean isNewResource) {
		this.isNewResource = isNewResource;
	}

	public ICVSRepositoryLocation getLocation() {
		return location;
	}

	public void setLocation(ICVSRepositoryLocation location) {
		this.location = location;
	}

	public String getRevision() {
		return revision;
	}

	public void setRevision(String revision) {
		this.revision = revision;
	}

	public String getKeywordSubstitution() {
		return keywordSubstitution;
	}

	public void setKeywordSubstitution(String keywordSubstitution) {
		this.keywordSubstitution = keywordSubstitution;
	}

	public void setNeedsMerge(boolean needsMerge) {
		this.needsMerge = needsMerge;
	}

	public boolean isHasRemote() {
		return hasRemote;
	}

	public void setHasRemote(boolean hasRemote) {
		this.hasRemote = hasRemote;
	}

	public void setRepository(String repository) {
		this.repository = repository;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public boolean isVirtualFolder() {
		return virtualFolder;
	}

	public void setVirtualFolder(boolean virtualFolder) {
		this.virtualFolder = virtualFolder;
	}
}

Back to the top