1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 52 public class CVSDecoration { 53 54 public static final int MODEL = 1000; 55 56 private String prefix; 58 private String suffix; 59 private ImageDescriptor overlay; 60 private Color bkgColor; 61 private Color fgColor; 62 private Font font; 63 64 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 tag; 76 private String revision; 77 private String repository; 78 private ICVSRepositoryLocation location; 79 private String keywordSubstitution; 80 private int stateFlags; 81 82 private String fileFormatter; 84 private String folderFormatter; 85 private String projectFormatter; 86 87 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 private Preferences preferences; 99 100 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 135 public CVSDecoration() { 136 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 fileFormater, String folderFormatter, String 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 fileFormater, String folderFormatter, String projectFormatter) { 161 this.preferences = preferences; 162 this.fileFormatter = updateOldDirtyFlag(fileFormater); 163 this.folderFormatter = updateOldDirtyFlag(folderFormatter); 164 this.projectFormatter = updateOldDirtyFlag(projectFormatter); 165 } 166 167 public static String updateOldDirtyFlag(String param){ 169 return param.replaceAll(CVSDecoratorConfiguration.OLD_DIRTY_FLAG, 170 CVSDecoratorConfiguration.NEW_DIRTY_FLAG); 171 } 172 173 public void addPrefix(String prefix) { 174 this.prefix = prefix; 175 } 176 177 public void addSuffix(String 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 getPrefix() { 210 return prefix; 211 } 212 213 public String 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 String suffix = getSuffix(); 225 if(suffix != null) 226 decoration.addSuffix(suffix); 227 String 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 bindings = new HashMap (); 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 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 if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_NEWRESOURCE_DECORATION) && isNewResource()) { 286 return newResource; 287 } 288 if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_DIRTY_DECORATION) && isDirty()) { 290 return dirty; 291 } 292 if (preferences.getBoolean(ICVSUIConstants.PREF_SHOW_ADDED_DECORATION) && isAdded()) { 294 return added; 295 } 296 if (isWatchEditEnabled() && resourceType == IResource.FILE && !isReadOnly() && isHasRemote()) { 298 return edited; 299 } 300 if (needsMerge) 301 return merged; 302 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 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 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"; } 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 getTag() { 369 return tag; 370 } 371 372 public void setTag(String 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 getRevision() { 397 return revision; 398 } 399 400 public void setRevision(String revision) { 401 this.revision = revision; 402 } 403 404 public String getKeywordSubstitution() { 405 return keywordSubstitution; 406 } 407 408 public void setKeywordSubstitution(String 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 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 [] properties) { 452 TeamStateDescription desc = new CVSTeamStateDescription(stateFlags); 453 Object 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 [] properties, String property) { 464 if (properties == null) 465 return true; 466 for (int i = 0; i < properties.length; i++) { 467 String string = properties[i]; 468 if (string.equals(property)) 469 return true; 470 } 471 return false; 472 } 473 } 474 | Popular Tags |