KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CVSDecoration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.Preferences;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.IDecoration;
22 import org.eclipse.swt.graphics.*;
23 import org.eclipse.team.core.diff.IThreeWayDiff;
24 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
25 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager;
26 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryRoot;
27 import org.eclipse.team.internal.ui.TeamUIPlugin;
28 import org.eclipse.team.ui.ISharedImages;
29 import org.eclipse.team.ui.synchronize.TeamStateDescription;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.themes.ITheme;
32
33 /**
34  * A decoration describes the annotations to a user interface element. The
35  * annotations can apply to text (e.g. prefix, suffix, color, font) and to an
36  * image (e.g. overlays).
37  * <p>
38  * This class is derived from an internal workbench class
39  * <code>IDecoration</code> and is often used in conjunction with the label
40  * decoration APIs. As such a client can convert between them using helpers
41  * defined in this class.
42  * </p>
43  * TODO:
44  * profile
45  * add colors and fonts to preferences instead of being hard coded
46  * what to do with CVSDecorationConfiguration class?
47  * preference page externalizations
48  * preference page preview should update when theme changes
49  *
50  * @since 3.1
51  */

52 public class CVSDecoration {
53
54     public static final int MODEL = 1000;
55     
56     // Decorations
57
private String JavaDoc prefix;
58     private String JavaDoc suffix;
59     private ImageDescriptor overlay;
60     private Color bkgColor;
61     private Color fgColor;
62     private Font font;
63     
64     // Properties
65
private int resourceType = IResource.FILE;
66     private boolean watchEditEnabled = false;
67     private boolean isDirty = false;
68     private boolean isIgnored = false;
69     private boolean isAdded = false;
70     private boolean isNewResource = false;
71     private boolean hasRemote = false;
72     private boolean readOnly = false;
73     private boolean needsMerge = false;
74     private boolean virtualFolder = false;
75     private String JavaDoc tag;
76     private String JavaDoc revision;
77     private String JavaDoc repository;
78     private ICVSRepositoryLocation location;
79     private String JavaDoc keywordSubstitution;
80     private int stateFlags;
81
82     // Text formatters
83
private String JavaDoc fileFormatter;
84     private String JavaDoc folderFormatter;
85     private String JavaDoc projectFormatter;
86     
87     // Images cached for better performance
88
private static ImageDescriptor dirty;
89     private static ImageDescriptor checkedIn;
90     private static ImageDescriptor noRemoteDir;
91     private static ImageDescriptor added;
92     private static ImageDescriptor merged;
93     private static ImageDescriptor newResource;
94     private static ImageDescriptor edited;
95     
96     // List of preferences used to configure the decorations that
97
// are applied.
98
private Preferences preferences;
99     
100     /*
101      * Define a cached image descriptor which only creates the image data once
102      */

103     public static class CachedImageDescriptor extends ImageDescriptor {
104
105         ImageDescriptor descriptor;
106         ImageData data;
107
108         public CachedImageDescriptor(ImageDescriptor descriptor) {
109             Assert.isNotNull(descriptor);
110             this.descriptor = descriptor;
111         }
112
113         public ImageData getImageData() {
114             if (data == null) {
115                 data = descriptor.getImageData();
116             }
117             return data;
118         }
119     }
120
121     static {
122         dirty = new CachedImageDescriptor(TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_DIRTY_OVR));
123         checkedIn = new CachedImageDescriptor(TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_CHECKEDIN_OVR));
124         added = new CachedImageDescriptor(TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_CHECKEDIN_OVR));
125         merged = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_MERGED));
126         newResource = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_QUESTIONABLE));
127         edited = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_EDITED));
128         noRemoteDir = new CachedImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_NO_REMOTEDIR));
129     }
130
131     /**
132      * Default constructor uses the plug-in's preferences to determine text decoration
133      * formatters and preferences.
134      */

135     public CVSDecoration() {
136         // TODO: for efficiency don't look up a pref until its needed
137
IPreferenceStore store = getStore();
138         Preferences prefs = new Preferences();
139         
140         prefs.setValue(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION));
141         prefs.setValue(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION));
142         prefs.setValue(ICVSUIConstants.PREF_SHOW_HASREMOTE_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_HASREMOTE_DECORATION));
143         prefs.setValue(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION, store.getBoolean(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION));
144         prefs.setValue(ICVSUIConstants.PREF_CALCULATE_DIRTY, store.getBoolean(ICVSUIConstants.PREF_CALCULATE_DIRTY));
145         prefs.setValue(ICVSUIConstants.PREF_DIRTY_FLAG, store.getString(ICVSUIConstants.PREF_DIRTY_FLAG));
146         prefs.setValue(ICVSUIConstants.PREF_ADDED_FLAG, store.getString(ICVSUIConstants.PREF_ADDED_FLAG));
147         prefs.setValue(ICVSUIConstants.PREF_USE_FONT_DECORATORS, store.getString(ICVSUIConstants.PREF_USE_FONT_DECORATORS));
148         
149         initialize(prefs, store.getString(ICVSUIConstants.PREF_FILETEXT_DECORATION), store.getString(ICVSUIConstants.PREF_FOLDERTEXT_DECORATION), store.getString(ICVSUIConstants.PREF_PROJECTTEXT_DECORATION));
150     }
151
152     public CVSDecoration(Preferences preferences, String JavaDoc fileFormater, String JavaDoc folderFormatter, String JavaDoc projectFormatter) {
153         initialize(preferences, fileFormater, folderFormatter, projectFormatter);
154     }
155
156     private IPreferenceStore getStore() {
157         return CVSUIPlugin.getPlugin().getPreferenceStore();
158     }
159
160     private void initialize(Preferences preferences, String JavaDoc fileFormater, String JavaDoc folderFormatter, String JavaDoc projectFormatter) {
161         this.preferences = preferences;
162         this.fileFormatter = updateOldDirtyFlag(fileFormater);
163         this.folderFormatter = updateOldDirtyFlag(folderFormatter);
164         this.projectFormatter = updateOldDirtyFlag(projectFormatter);
165     }
166
167     //see bug 110022
168
public static String JavaDoc updateOldDirtyFlag(String JavaDoc param){
169         return param.replaceAll(CVSDecoratorConfiguration.OLD_DIRTY_FLAG,
170                 CVSDecoratorConfiguration.NEW_DIRTY_FLAG);
171     }
172
173     public void addPrefix(String JavaDoc prefix) {
174         this.prefix = prefix;
175     }
176
177     public void addSuffix(String JavaDoc suffix) {
178         this.suffix = suffix;
179     }
180
181     public void setForegroundColor(Color fgColor) {
182         this.fgColor = fgColor;
183     }
184
185     public void setBackgroundColor(Color bkgColor) {
186         this.bkgColor = bkgColor;
187     }
188
189     public void setFont(Font font) {
190         this.font = font;
191     }
192
193     public Color getBackgroundColor() {
194         return bkgColor;
195     }
196
197     public Color getForegroundColor() {
198         return fgColor;
199     }
200
201     public Font getFont() {
202         return font;
203     }
204
205     public ImageDescriptor getOverlay() {
206         return overlay;
207     }
208
209     public String JavaDoc getPrefix() {
210         return prefix;
211     }
212
213     public String JavaDoc getSuffix() {
214         return suffix;
215     }
216     
217     public void setResourceType(int type) {
218         this.resourceType = type;
219     }
220
221     public void apply(IDecoration decoration) {
222         compute();
223         // apply changes
224
String JavaDoc suffix = getSuffix();
225         if(suffix != null)
226             decoration.addSuffix(suffix);
227         String JavaDoc prefix = getPrefix();
228         if(prefix != null)
229             decoration.addPrefix(prefix);
230         ImageDescriptor overlay = getOverlay();
231         if(overlay != null)
232             decoration.addOverlay(overlay);
233         Color bc = getBackgroundColor();
234         if(bc != null)
235             decoration.setBackgroundColor(bc);
236         Color fc = getForegroundColor();
237         if(fc != null)
238             decoration.setForegroundColor(fc);
239         Font f = getFont();
240         if(f != null)
241             decoration.setFont(f);
242     }
243
244     public void compute() {
245         computeText();
246         overlay = computeImage();
247         computeColorsAndFonts();
248     }
249
250     private void computeText() {
251         if (isIgnored())
252             return;
253         Map JavaDoc bindings = new HashMap JavaDoc();
254         if (isDirty()) {
255             bindings.put(CVSDecoratorConfiguration.NEW_DIRTY_FLAG, preferences.getString(ICVSUIConstants.PREF_DIRTY_FLAG));
256         }
257         if (isAdded()) {
258             bindings.put(CVSDecoratorConfiguration.ADDED_FLAG, preferences.getString(ICVSUIConstants.PREF_ADDED_FLAG));
259         } else if(isHasRemote()){
260             bindings.put(CVSDecoratorConfiguration.FILE_REVISION, getRevision());
261             bindings.put(CVSDecoratorConfiguration.RESOURCE_TAG, getTag());
262         }
263         bindings.put(CVSDecoratorConfiguration.FILE_KEYWORD, getKeywordSubstitution());
264         if ((resourceType == IResource.FOLDER || resourceType == IResource.PROJECT) && location != null) {
265             bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_HOST, location.getHost());
266             bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_METHOD, location.getMethod().getName());
267             bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_USER, location.getUsername());
268             bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_ROOT, location.getRootDirectory());
269             bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_REPOSITORY, repository);
270
271             RepositoryManager repositoryManager = CVSUIPlugin.getPlugin().getRepositoryManager();
272             RepositoryRoot root = repositoryManager.getRepositoryRootFor(location);
273             CVSUIPlugin.getPlugin().getRepositoryManager();
274             String JavaDoc label = root.getName();
275             if (label == null) {
276               label = location.getLocation(true);
277             }
278             bindings.put(CVSDecoratorConfiguration.REMOTELOCATION_LABEL, label);
279         }
280         CVSDecoratorConfiguration.decorate(this, getTextFormatter(), bindings);
281     }
282
283     private ImageDescriptor computeImage() {
284         // show newResource icon
285
if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION) && isNewResource()) {
286             return newResource;
287         }
288         // show dirty icon
289
if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION) && isDirty()) {
290             return dirty;
291         }
292         // show added
293
if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION) && isAdded()) {
294             return added;
295         }
296         // show watch edit
297
if (isWatchEditEnabled() && resourceType == IResource.FILE && !isReadOnly() && isHasRemote()) {
298             return edited;
299         }
300         if (needsMerge)
301             return merged;
302         // show checked in
303
if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_HASREMOTE_DECORATION) && isHasRemote()) {
304             if ((resourceType == IResource.FOLDER || resourceType == IResource.PROJECT) && isVirtualFolder()) {
305                 return noRemoteDir;
306             }
307             return checkedIn;
308         }
309         
310         //nothing matched
311
return null;
312     }
313     
314     private void computeColorsAndFonts() {
315         if (!preferences.getBoolean(ICVSUIConstants.PREF_USE_FONT_DECORATORS))
316             return;
317             
318         ITheme current = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme();
319         if(isIgnored()) {
320             setBackgroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.IGNORED_BACKGROUND_COLOR));
321             setForegroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.IGNORED_FOREGROUND_COLOR));
322             setFont(current.getFontRegistry().get(CVSDecoratorConfiguration.IGNORED_FONT));
323         } else if(isDirty()) {
324             setBackgroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.OUTGOING_CHANGE_BACKGROUND_COLOR));
325             setForegroundColor(current.getColorRegistry().get(CVSDecoratorConfiguration.OUTGOING_CHANGE_FOREGROUND_COLOR));
326             setFont(current.getFontRegistry().get(CVSDecoratorConfiguration.OUTGOING_CHANGE_FONT));
327         }
328     }
329
330     private String JavaDoc getTextFormatter() {
331         switch (resourceType) {
332             case IResource.FILE :
333                 return fileFormatter;
334             case IResource.FOLDER :
335                 return folderFormatter;
336             case IResource.PROJECT :
337                 return projectFormatter;
338             case MODEL :
339                 return folderFormatter;
340         }
341         return "no format specified"; //$NON-NLS-1$
342
}
343
344     public boolean isAdded() {
345         return isAdded;
346     }
347
348     public void setAdded(boolean isAdded) {
349         this.isAdded = isAdded;
350     }
351
352     public boolean isDirty() {
353         return isDirty;
354     }
355
356     public void setDirty(boolean isDirty) {
357         this.isDirty = isDirty;
358     }
359
360     public boolean isIgnored() {
361         return isIgnored;
362     }
363
364     public void setIgnored(boolean isIgnored) {
365         this.isIgnored = isIgnored;
366     }
367
368     public String JavaDoc getTag() {
369         return tag;
370     }
371
372     public void setTag(String JavaDoc tag) {
373         this.tag = tag;
374     }
375
376     public boolean isWatchEditEnabled() {
377         return watchEditEnabled;
378     }
379
380     public void setWatchEditEnabled(boolean watchEditEnabled) {
381         this.watchEditEnabled = watchEditEnabled;
382     }
383
384     public boolean isNewResource() {
385         return isNewResource;
386     }
387
388     public void setNewResource(boolean isNewResource) {
389         this.isNewResource = isNewResource;
390     }
391
392     public void setLocation(ICVSRepositoryLocation location) {
393         this.location = location;
394     }
395
396     public String JavaDoc getRevision() {
397         return revision;
398     }
399
400     public void setRevision(String JavaDoc revision) {
401         this.revision = revision;
402     }
403
404     public String JavaDoc getKeywordSubstitution() {
405         return keywordSubstitution;
406     }
407
408     public void setKeywordSubstitution(String JavaDoc keywordSubstitution) {
409         this.keywordSubstitution = keywordSubstitution;
410     }
411
412     public void setNeedsMerge(boolean needsMerge) {
413         this.needsMerge = needsMerge;
414     }
415
416     public boolean isHasRemote() {
417         return hasRemote;
418     }
419
420     public void setHasRemote(boolean hasRemote) {
421         this.hasRemote = hasRemote;
422     }
423
424     public void setRepository(String JavaDoc repository) {
425         this.repository = repository;
426     }
427
428     public boolean isReadOnly() {
429         return readOnly;
430     }
431
432     public void setReadOnly(boolean readOnly) {
433         this.readOnly = readOnly;
434     }
435
436     public boolean isVirtualFolder() {
437         return virtualFolder;
438     }
439
440     public void setVirtualFolder(boolean virtualFolder) {
441         this.virtualFolder = virtualFolder;
442     }
443
444     public void setStateFlags(int stateFlags) {
445         this.stateFlags = stateFlags;
446         if ((stateFlags & IThreeWayDiff.OUTGOING) != 0) {
447             setDirty(true);
448         }
449     }
450     
451     public TeamStateDescription asTeamStateDescription(String JavaDoc[] properties) {
452         TeamStateDescription desc = new CVSTeamStateDescription(stateFlags);
453         Object JavaDoc o = computeImage();
454         if (o != null && isRequestedProperty(properties, CVSTeamStateDescription.PROP_RESOURCE_STATE)) {
455             desc.setProperty(CVSTeamStateDescription.PROP_RESOURCE_STATE, o);
456         }
457         if (tag != null && isRequestedProperty(properties, CVSTeamStateDescription.PROP_TAG)) {
458             desc.setProperty(CVSTeamStateDescription.PROP_TAG, tag);
459         }
460         return desc;
461     }
462
463     private boolean isRequestedProperty(String JavaDoc[] properties, String JavaDoc property) {
464         if (properties == null)
465             return true;
466         for (int i = 0; i < properties.length; i++) {
467             String JavaDoc string = properties[i];
468             if (string.equals(property))
469                 return true;
470         }
471         return false;
472     }
473 }
474
Popular Tags