|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 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.jface.viewers; |
| 12 |
|
| 13 |
import org.eclipse.core.runtime.Assert; |
| 14 |
import org.eclipse.jface.resource.JFaceResources; |
| 15 |
import org.eclipse.jface.resource.LocalResourceManager; |
| 16 |
import org.eclipse.jface.resource.ResourceManager; |
| 17 |
import org.eclipse.jface.viewers.StyledString.Style; |
| 18 |
import org.eclipse.swt.graphics.Color; |
| 19 |
import org.eclipse.swt.graphics.Font; |
| 20 |
import org.eclipse.swt.graphics.Image; |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* A {@link DecoratingStyledCellLabelProvider} is a {@link StyledCellLabelProvider} |
| 25 |
* that uses a nested {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider} |
| 26 |
* to compute styled text label and image. |
| 27 |
* Optionally, it can also take a {@link ILabelDecorator} to decorate the label. |
| 28 |
* |
| 29 |
* <p>Use this label provider to add styled ranges to existing label providers: |
| 30 |
* Existing label providers can be enhanced by implementing |
| 31 |
* {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider}, and are then able of being |
| 32 |
* used in viewer with styled labels.</p> |
| 33 |
* |
| 34 |
* <p>The {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider} can |
| 35 |
* optionally implement {@link IColorProvider} and {@link IFontProvider} to provide |
| 36 |
* foreground and background color and a default font. |
| 37 |
* </p> |
| 38 |
* |
| 39 |
* <p>The {@link ILabelDecorator} can |
| 40 |
* optionally implement {@link IColorDecorator} and {@link IFontDecorator} to provide |
| 41 |
* foreground and background color and font decoration. |
| 42 |
* </p> |
| 43 |
* |
| 44 |
* @since 3.4 |
| 45 |
*/ |
| 46 |
public class DecoratingStyledCellLabelProvider extends StyledCellLabelProvider { |
| 47 |
|
| 48 |
/** |
| 49 |
* Interface marking a label provider that provides styled text labels and images. |
| 50 |
* <p>The {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider} can |
| 51 |
* optionally implement {@link IColorProvider} and {@link IFontProvider} to provide |
| 52 |
* foreground and background color and a default font. |
| 53 |
* </p> |
| 54 |
*/ |
| 55 |
public static interface IStyledLabelProvider extends IBaseLabelProvider { |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the styled text label for the given element |
| 59 |
* |
| 60 |
* @param element the element to evaluate the styled string for |
| 61 |
* |
| 62 |
* @return the styled string. |
| 63 |
*/ |
| 64 |
public StyledString getStyledText(Object element); |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns the image for the label of the given element. The image |
| 68 |
* is owned by the label provider and must not be disposed directly. |
| 69 |
* Instead, dispose the label provider when no longer needed. |
| 70 |
* |
| 71 |
* @param element the element for which to provide the label image |
| 72 |
* @return the image used to label the element, or <code>null</code> |
| 73 |
* if there is no image for the given object |
| 74 |
*/ |
| 75 |
public Image getImage(Object element); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
private IStyledLabelProvider styledLabelProvider; |
| 80 |
|
| 81 |
private ILabelDecorator decorator; |
| 82 |
private IDecorationContext decorationContext; |
| 83 |
private ILabelProviderListener labelProviderListener; |
| 84 |
|
| 85 |
/** |
| 86 |
* Creates a {@link DecoratingStyledCellLabelProvider} that delegates the requests for |
| 87 |
* styled labels and for images to a {@link IStyledLabelProvider}. |
| 88 |
* |
| 89 |
* @param labelProvider the styled label provider |
| 90 |
* @param decorator a label decorator or <code>null</code> to not decorate the label |
| 91 |
* @param decorationContext a decoration context or <code>null</code> if the no decorator |
| 92 |
* is configured or the default decorator should be used |
| 93 |
*/ |
| 94 |
public DecoratingStyledCellLabelProvider(IStyledLabelProvider labelProvider, ILabelDecorator decorator, IDecorationContext decorationContext) { |
| 95 |
if (labelProvider == null) |
| 96 |
throw new IllegalArgumentException("Label provider must not be null"); //$NON-NLS-1$ |
| 97 |
|
| 98 |
this.styledLabelProvider= labelProvider; |
| 99 |
this.decorator= decorator; |
| 100 |
this.decorationContext= decorationContext; |
| 101 |
if (decorator != null && decorationContext == null) { |
| 102 |
decorationContext= createDefaultDecorationContext(); |
| 103 |
} |
| 104 |
labelProviderListener= new ILabelProviderListener() { |
| 105 |
public void labelProviderChanged(LabelProviderChangedEvent event) { |
| 106 |
fireLabelProviderChanged(event); |
| 107 |
} |
| 108 |
}; |
| 109 |
labelProvider.addListener(labelProviderListener); |
| 110 |
if (decorator != null) |
| 111 |
decorator.addListener(labelProviderListener); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Creates a {@link DecoratingStyledCellLabelProvider} that delegates the requests for |
| 116 |
* styled labels and for images to a {@link IStyledLabelProvider}. |
| 117 |
* |
| 118 |
* @param labelProvider the styled label provider |
| 119 |
*/ |
| 120 |
public DecoratingStyledCellLabelProvider(IStyledLabelProvider labelProvider) { |
| 121 |
this(labelProvider, null, null); |
| 122 |
} |
| 123 |
|
| 124 |
private IDecorationContext createDefaultDecorationContext() { |
| 125 |
DecorationContext newContext = new DecorationContext(); |
| 126 |
newContext.putProperty(DecorationContext.RESOURCE_MANAGER_KEY, new LocalResourceManager(JFaceResources.getResources())); |
| 127 |
return newContext; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns the decoration context associated with this label provider. It |
| 132 |
* will be passed to the decorator if the decorator is an instance of |
| 133 |
* {@link LabelDecorator}. |
| 134 |
* |
| 135 |
* @return the decoration context associated with this label provider |
| 136 |
*/ |
| 137 |
public IDecorationContext getDecorationContext() { |
| 138 |
return decorationContext; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Set the decoration context that will be based to the decorator for this |
| 143 |
* label provider if that decorator implements {@link LabelDecorator}. |
| 144 |
* |
| 145 |
* If this decorationContext has a {@link ResourceManager} stored for the |
| 146 |
* {@link DecorationContext#RESOURCE_MANAGER_KEY} property it will be |
| 147 |
* disposed when the label provider is disposed. |
| 148 |
* |
| 149 |
* @param decorationContext |
| 150 |
* the decoration context. |
| 151 |
*/ |
| 152 |
public void setDecorationContext(IDecorationContext decorationContext) { |
| 153 |
Assert.isNotNull(decorationContext); |
| 154 |
this.decorationContext = decorationContext; |
| 155 |
} |
| 156 |
|
| 157 |
private boolean waitForPendingDecoration(ViewerCell cell) { |
| 158 |
if (decorator == null) |
| 159 |
return false; |
| 160 |
|
| 161 |
Object element= cell.getElement(); |
| 162 |
String oldText= cell.getText(); |
| 163 |
|
| 164 |
boolean isDecorationPending= false; |
| 165 |
if (decorator instanceof LabelDecorator) { |
| 166 |
isDecorationPending= !((LabelDecorator) decorator).prepareDecoration(element, oldText, getDecorationContext()); |
| 167 |
} else if (decorator instanceof IDelayedLabelDecorator) { |
| 168 |
isDecorationPending= !((IDelayedLabelDecorator) decorator).prepareDecoration(element, oldText); |
| 169 |
} |
| 170 |
if (isDecorationPending && oldText.length() == 0) { |
| 171 |
// item is empty: is shown for the first time: don't wait |
| 172 |
return false; |
| 173 |
} |
| 174 |
return isDecorationPending; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/* (non-Javadoc) |
| 179 |
* @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell) |
| 180 |
*/ |
| 181 |
public void update(ViewerCell cell) { |
| 182 |
if (waitForPendingDecoration(cell)) { |
| 183 |
return; // wait until the decoration is ready |
| 184 |
} |
| 185 |
|
| 186 |
Object element= cell.getElement(); |
| 187 |
|
| 188 |
StyledString styledString= getStyledText(element); |
| 189 |
cell.setText(styledString.getString()); |
| 190 |
|
| 191 |
if (isOwnerDrawEnabled()) { |
| 192 |
cell.setStyleRanges(styledString.getStyleRanges()); |
| 193 |
} else { |
| 194 |
cell.setStyleRanges(null); |
| 195 |
} |
| 196 |
|
| 197 |
cell.setImage(getImage(element)); |
| 198 |
cell.setFont(getFont(element)); |
| 199 |
cell.setForeground(getForeground(element)); |
| 200 |
cell.setBackground(getBackground(element)); |
| 201 |
|
| 202 |
super.update(cell); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Provides a foreground color for the given element. |
| 207 |
* |
| 208 |
* @param element the element |
| 209 |
* @return the foreground color for the element, or <code>null</code> |
| 210 |
* to use the default foreground color |
| 211 |
*/ |
| 212 |
public Color getForeground(Object element) { |
| 213 |
if (decorator instanceof IColorDecorator) { |
| 214 |
Color foreground= ((IColorDecorator) decorator).decorateForeground(element); |
| 215 |
if (foreground != null) |
| 216 |
return foreground; |
| 217 |
} |
| 218 |
if (styledLabelProvider instanceof IColorProvider) { |
| 219 |
return ((IColorProvider) styledLabelProvider).getForeground(element); |
| 220 |
} |
| 221 |
return null; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Provides a background color for the given element. |
| 226 |
* |
| 227 |
* @param element the element |
| 228 |
* @return the background color for the element, or <code>null</code> |
| 229 |
* to use the default background color |
| 230 |
*/ |
| 231 |
public Color getBackground(Object element) { |
| 232 |
if (decorator instanceof IColorDecorator) { |
| 233 |
Color color= ((IColorDecorator) decorator).decorateBackground(element); |
| 234 |
if (color != null) |
| 235 |
return color; |
| 236 |
} |
| 237 |
if (styledLabelProvider instanceof IColorProvider) { |
| 238 |
return ((IColorProvider) styledLabelProvider).getBackground(element); |
| 239 |
} |
| 240 |
return null; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Provides a font for the given element. |
| 245 |
* |
| 246 |
* @param element the element |
| 247 |
* @return the font for the element, or <code>null</code> |
| 248 |
* to use the default font |
| 249 |
*/ |
| 250 |
public Font getFont(Object element) { |
| 251 |
if (decorator instanceof IFontDecorator) { |
| 252 |
Font font= ((IFontDecorator) decorator).decorateFont(element); |
| 253 |
if (font != null) |
| 254 |
return font; |
| 255 |
} |
| 256 |
if (styledLabelProvider instanceof IFontProvider) { |
| 257 |
return ((IFontProvider) styledLabelProvider).getFont(element); |
| 258 |
} |
| 259 |
return null; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the image for the label of the given element. The image |
| 265 |
* is owned by the label provider and must not be disposed directly. |
| 266 |
* Instead, dispose the label provider when no longer needed. |
| 267 |
* |
| 268 |
* @param element the element for which to provide the label image |
| 269 |
* @return the image used to label the element, or <code>null</code> |
| 270 |
* if there is no image for the given object |
| 271 |
*/ |
| 272 |
public Image getImage(Object element) { |
| 273 |
Image image= styledLabelProvider.getImage(element); |
| 274 |
if (decorator == null) { |
| 275 |
return image; |
| 276 |
} |
| 277 |
|
| 278 |
Image decorated= null; |
| 279 |
if (decorator instanceof LabelDecorator) { |
| 280 |
decorated= ((LabelDecorator) decorator).decorateImage(image, element, getDecorationContext()); |
| 281 |
} else { |
| 282 |
decorated= decorator.decorateImage(image, element); |
| 283 |
} |
| 284 |
if (decorated != null) |
| 285 |
return decorated; |
| 286 |
|
| 287 |
return image; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Returns the styled text for the label of the given element. |
| 292 |
* |
| 293 |
* @param element the element for which to provide the styled label text |
| 294 |
* @return the styled text string used to label the element |
| 295 |
*/ |
| 296 |
public StyledString getStyledText(Object element) { |
| 297 |
StyledString string= styledLabelProvider.getStyledText(element); |
| 298 |
if (decorator == null) { |
| 299 |
return string; |
| 300 |
} |
| 301 |
|
| 302 |
String label= string.getString(); |
| 303 |
String decorated; |
| 304 |
if (decorator instanceof LabelDecorator) { |
| 305 |
decorated= ((LabelDecorator) decorator).decorateText(label, element, getDecorationContext()); |
| 306 |
} else { |
| 307 |
decorated= decorator.decorateText(label, element); |
| 308 |
} |
| 309 |
if (decorated == null) |
| 310 |
return string; |
| 311 |
|
| 312 |
int originalStart= decorated.indexOf(label); |
| 313 |
if (originalStart == -1) { |
| 314 |
return new StyledString(decorated); // the decorator did something wild |
| 315 |
} |
| 316 |
|
| 317 |
if (decorated.length() == label.length()) |
| 318 |
return string; |
| 319 |
|
| 320 |
Style style= getDecorationStyle(element); |
| 321 |
if (originalStart > 0) { |
| 322 |
StyledString newString= new StyledString(decorated.substring(0, originalStart), style); |
| 323 |
newString.append(string); |
| 324 |
string= newString; |
| 325 |
} |
| 326 |
if (decorated.length() > originalStart + label.length()) { // decorator appended something |
| 327 |
return string.append(decorated.substring(originalStart + label.length()), style); |
| 328 |
} |
| 329 |
return string; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Sets the {@link StyledString.Style} to be used for string decorations. By default |
| 334 |
* the {@link StyledString#DECORATIONS_STYLE decoration style}. Clients can override. |
| 335 |
* |
| 336 |
* Note that it is the client's responsibility to react on color changes of the decoration color by |
| 337 |
* refreshing the view |
| 338 |
* |
| 339 |
* @param element the element that has been decorated |
| 340 |
* |
| 341 |
* @return return the decoration style |
| 342 |
*/ |
| 343 |
protected Style getDecorationStyle(Object element) { |
| 344 |
return StyledString.DECORATIONS_STYLE; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Returns the styled string provider. |
| 349 |
* |
| 350 |
* @return the wrapped label provider |
| 351 |
*/ |
| 352 |
public IStyledLabelProvider getStyledStringProvider() { |
| 353 |
return styledLabelProvider; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Sets the styled label provider. Removes all known listeners from the old |
| 358 |
* provider, and adds all known listeners to the new provider. The old |
| 359 |
* provider is not disposed. Fires a label provider changed event |
| 360 |
* indicating that all labels should be updated. Has no effect if the given |
| 361 |
* decorator is identical to the current one. |
| 362 |
* |
| 363 |
* @param newLabelProvider the new styled label provider |
| 364 |
*/ |
| 365 |
public void setStyledLabelProvider(IStyledLabelProvider newLabelProvider) { |
| 366 |
if (newLabelProvider == null) |
| 367 |
throw new IllegalArgumentException(); |
| 368 |
|
| 369 |
IStyledLabelProvider oldProvider= this.styledLabelProvider; |
| 370 |
if (newLabelProvider != oldProvider) { |
| 371 |
oldProvider.removeListener(labelProviderListener); |
| 372 |
this.styledLabelProvider= newLabelProvider; |
| 373 |
newLabelProvider.addListener(labelProviderListener); |
| 374 |
|
| 375 |
} |
| 376 |
fireLabelProviderChanged(new LabelProviderChangedEvent(this)); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Returns the decorator or <code>null</code> if no decorator is installed |
| 381 |
* |
| 382 |
* @return the decorator or <code>null</code> if no decorator is installed |
| 383 |
*/ |
| 384 |
public ILabelDecorator getLabelDecorator() { |
| 385 |
return decorator; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Sets the label decorator. Removes all known listeners from the old |
| 390 |
* decorator, and adds all known listeners to the new decorator. The old |
| 391 |
* decorator is not disposed. Fires a label provider changed event |
| 392 |
* indicating that all labels should be updated. Has no effect if the given |
| 393 |
* decorator is identical to the current one. |
| 394 |
* |
| 395 |
* @param newDecorator |
| 396 |
* the label decorator, or <code>null</code> if no decorations |
| 397 |
* are to be applied |
| 398 |
*/ |
| 399 |
public void setLabelDecorator(ILabelDecorator newDecorator) { |
| 400 |
ILabelDecorator oldDecorator= this.decorator; |
| 401 |
if (oldDecorator != newDecorator) { |
| 402 |
if (oldDecorator != null) |
| 403 |
oldDecorator.removeListener(labelProviderListener); |
| 404 |
this.decorator= newDecorator; |
| 405 |
if (newDecorator != null) { |
| 406 |
newDecorator.addListener(labelProviderListener); |
| 407 |
} |
| 408 |
} |
| 409 |
fireLabelProviderChanged(new LabelProviderChangedEvent(this)); |
| 410 |
} |
| 411 |
|
| 412 |
/* (non-Javadoc) |
| 413 |
* @see org.eclipse.jface.viewers.BaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) |
| 414 |
*/ |
| 415 |
public void addListener(ILabelProviderListener listener) { |
| 416 |
super.addListener(listener); |
| 417 |
styledLabelProvider.addListener(listener); |
| 418 |
if (decorator != null) { |
| 419 |
decorator.addListener(listener); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/* (non-Javadoc) |
| 424 |
* @see org.eclipse.jface.viewers.BaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) |
| 425 |
*/ |
| 426 |
public void removeListener(ILabelProviderListener listener) { |
| 427 |
super.removeListener(listener); |
| 428 |
styledLabelProvider.removeListener(listener); |
| 429 |
if (decorator != null) { |
| 430 |
decorator.removeListener(listener); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/* (non-Javadoc) |
| 435 |
* @see org.eclipse.jface.viewers.BaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String) |
| 436 |
*/ |
| 437 |
public boolean isLabelProperty(Object element, String property) { |
| 438 |
if (styledLabelProvider.isLabelProperty(element, property)) { |
| 439 |
return true; |
| 440 |
} |
| 441 |
return decorator != null && decorator.isLabelProperty(element, property); |
| 442 |
} |
| 443 |
|
| 444 |
/* (non-Javadoc) |
| 445 |
* @see org.eclipse.jface.viewers.StyledCellLabelProvider#dispose() |
| 446 |
*/ |
| 447 |
public void dispose() { |
| 448 |
super.dispose(); |
| 449 |
if (decorationContext != null) { |
| 450 |
Object manager = decorationContext.getProperty(DecorationContext.RESOURCE_MANAGER_KEY); |
| 451 |
if (manager instanceof ResourceManager) |
| 452 |
((ResourceManager) manager).dispose(); |
| 453 |
} |
| 454 |
styledLabelProvider.removeListener(labelProviderListener); |
| 455 |
styledLabelProvider.dispose(); |
| 456 |
if (decorator != null) { |
| 457 |
decorator.removeListener(labelProviderListener); |
| 458 |
decorator.dispose(); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
} |