1 11 package org.eclipse.jface.viewers; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.ListenerList; 15 import org.eclipse.swt.graphics.Color; 16 import org.eclipse.swt.graphics.Font; 17 import org.eclipse.swt.graphics.Image; 18 19 25 public class DecoratingLabelProvider extends LabelProvider implements 26 ILabelProvider, IViewerLabelProvider, IColorProvider, IFontProvider, ITreePathLabelProvider { 27 28 private ILabelProvider provider; 29 30 private ILabelDecorator decorator; 31 32 private ListenerList listeners = new ListenerList(); 34 35 private IDecorationContext decorationContext = DecorationContext.DEFAULT_CONTEXT; 36 37 44 public DecoratingLabelProvider(ILabelProvider provider, 45 ILabelDecorator decorator) { 46 Assert.isNotNull(provider); 47 this.provider = provider; 48 this.decorator = decorator; 49 } 50 51 57 public void addListener(ILabelProviderListener listener) { 58 super.addListener(listener); 59 provider.addListener(listener); 60 if (decorator != null) { 61 decorator.addListener(listener); 62 } 63 listeners.add(listener); 64 } 65 66 70 public void dispose() { 71 provider.dispose(); 72 if (decorator != null) { 73 decorator.dispose(); 74 } 75 } 76 77 84 public Image getImage(Object element) { 85 Image image = provider.getImage(element); 86 if (decorator != null) { 87 if (decorator instanceof LabelDecorator) { 88 LabelDecorator ld2 = (LabelDecorator) decorator; 89 Image decorated = ld2.decorateImage(image, element, getDecorationContext()); 90 if (decorated != null) { 91 return decorated; 92 } 93 } else { 94 Image decorated = decorator.decorateImage(image, element); 95 if (decorated != null) { 96 return decorated; 97 } 98 } 99 } 100 return image; 101 } 102 103 108 public ILabelDecorator getLabelDecorator() { 109 return decorator; 110 } 111 112 117 public ILabelProvider getLabelProvider() { 118 return provider; 119 } 120 121 128 public String getText(Object element) { 129 String text = provider.getText(element); 130 if (decorator != null) { 131 if (decorator instanceof LabelDecorator) { 132 LabelDecorator ld2 = (LabelDecorator) decorator; 133 String decorated = ld2.decorateText(text, element, getDecorationContext()); 134 if (decorated != null) { 135 return decorated; 136 } 137 } else { 138 String decorated = decorator.decorateText(text, element); 139 if (decorated != null) { 140 return decorated; 141 } 142 } 143 } 144 return text; 145 } 146 147 153 public boolean isLabelProperty(Object element, String property) { 154 if (provider.isLabelProperty(element, property)) { 155 return true; 156 } 157 if (decorator != null && decorator.isLabelProperty(element, property)) { 158 return true; 159 } 160 return false; 161 } 162 163 169 public void removeListener(ILabelProviderListener listener) { 170 super.removeListener(listener); 171 provider.removeListener(listener); 172 if (decorator != null) { 173 decorator.removeListener(listener); 174 } 175 listeners.remove(listener); 176 } 177 178 187 public void setLabelDecorator(ILabelDecorator decorator) { 188 ILabelDecorator oldDecorator = this.decorator; 189 if (oldDecorator != decorator) { 190 Object [] listenerList = this.listeners.getListeners(); 191 if (oldDecorator != null) { 192 for (int i = 0; i < listenerList.length; ++i) { 193 oldDecorator 194 .removeListener((ILabelProviderListener) listenerList[i]); 195 } 196 } 197 this.decorator = decorator; 198 if (decorator != null) { 199 for (int i = 0; i < listenerList.length; ++i) { 200 decorator 201 .addListener((ILabelProviderListener) listenerList[i]); 202 } 203 } 204 fireLabelProviderChanged(new LabelProviderChangedEvent(this)); 205 } 206 } 207 208 209 213 public void updateLabel(ViewerLabel settings, Object element) { 214 215 ILabelDecorator currentDecorator = getLabelDecorator(); 216 String oldText = settings.getText(); 217 boolean decorationReady = true; 218 if (currentDecorator instanceof IDelayedLabelDecorator) { 219 IDelayedLabelDecorator delayedDecorator = (IDelayedLabelDecorator) currentDecorator; 220 if (!delayedDecorator.prepareDecoration(element, oldText)) { 221 decorationReady = false; 223 } 224 } 225 227 if (decorationReady || oldText == null 228 || settings.getText().length() == 0) { 229 settings.setText(getText(element)); 230 } 231 232 Image oldImage = settings.getImage(); 233 if (decorationReady || oldImage == null) { 234 settings.setImage(getImage(element)); 235 } 236 237 if(decorationReady) { 238 updateForDecorationReady(settings,element); 239 } 240 241 } 242 243 249 protected void updateForDecorationReady(ViewerLabel settings, Object element) { 250 251 if(decorator instanceof IColorDecorator){ 252 IColorDecorator colorDecorator = (IColorDecorator) decorator; 253 settings.setBackground(colorDecorator.decorateBackground(element)); 254 settings.setForeground(colorDecorator.decorateForeground(element)); 255 } 256 257 if(decorator instanceof IFontDecorator) { 258 settings.setFont(((IFontDecorator) decorator).decorateFont(element)); 259 } 260 261 } 262 263 266 public Color getBackground(Object element) { 267 if(provider instanceof IColorProvider) { 268 return ((IColorProvider) provider).getBackground(element); 269 } 270 return null; 271 } 272 273 276 public Font getFont(Object element) { 277 if(provider instanceof IFontProvider) { 278 return ((IFontProvider) provider).getFont(element); 279 } 280 return null; 281 } 282 283 286 public Color getForeground(Object element) { 287 if(provider instanceof IColorProvider) { 288 return ((IColorProvider) provider).getForeground(element); 289 } 290 return null; 291 } 292 293 301 public IDecorationContext getDecorationContext() { 302 return decorationContext; 303 } 304 305 312 public void setDecorationContext(IDecorationContext decorationContext) { 313 Assert.isNotNull(decorationContext); 314 this.decorationContext = decorationContext; 315 } 316 317 320 public void updateLabel(ViewerLabel settings, TreePath elementPath) { 321 ILabelDecorator currentDecorator = getLabelDecorator(); 322 String oldText = settings.getText(); 323 Object element = elementPath.getLastSegment(); 324 boolean decorationReady = true; 325 if (currentDecorator instanceof LabelDecorator) { 326 LabelDecorator labelDecorator = (LabelDecorator) currentDecorator; 327 if (!labelDecorator.prepareDecoration(element, oldText, getDecorationContext())) { 328 decorationReady = false; 330 } 331 } else if (currentDecorator instanceof IDelayedLabelDecorator) { 332 IDelayedLabelDecorator delayedDecorator = (IDelayedLabelDecorator) currentDecorator; 333 if (!delayedDecorator.prepareDecoration(element, oldText)) { 334 decorationReady = false; 336 } 337 } 338 settings.setHasPendingDecorations(!decorationReady); 339 341 if (provider instanceof ITreePathLabelProvider) { 342 ITreePathLabelProvider pprov = (ITreePathLabelProvider) provider; 343 if (decorationReady || oldText == null 344 || settings.getText().length() == 0) { 345 pprov.updateLabel(settings, elementPath); 346 decorateSettings(settings, elementPath); 347 } 348 } else { 349 if (decorationReady || oldText == null 350 || settings.getText().length() == 0) { 351 settings.setText(getText(element)); 352 } 353 354 Image oldImage = settings.getImage(); 355 if (decorationReady || oldImage == null) { 356 settings.setImage(getImage(element)); 357 } 358 359 if(decorationReady) { 360 updateForDecorationReady(settings,element); 361 } 362 } 363 364 } 365 366 371 private void decorateSettings(ViewerLabel settings, TreePath elementPath) { 372 Object element = elementPath.getLastSegment(); 373 if (decorator != null) { 374 if (decorator instanceof LabelDecorator) { 375 LabelDecorator labelDecorator = (LabelDecorator) decorator; 376 String text = labelDecorator.decorateText(settings.getText(), element, getDecorationContext()); 377 if (text != null && text.length() > 0) 378 settings.setText(text); 379 Image image = labelDecorator.decorateImage(settings.getImage(), element, getDecorationContext()); 380 if (image != null) 381 settings.setImage(image); 382 383 } else { 384 String text = decorator.decorateText(settings.getText(), element); 385 if (text != null && text.length() > 0) 386 settings.setText(text); 387 Image image = decorator.decorateImage(settings.getImage(), element); 388 if (image != null) 389 settings.setImage(image); 390 } 391 if(decorator instanceof IColorDecorator){ 392 IColorDecorator colorDecorator = (IColorDecorator) decorator; 393 Color background = colorDecorator.decorateBackground(element); 394 if (background != null) 395 settings.setBackground(background); 396 Color foreground = colorDecorator.decorateForeground(element); 397 if (foreground != null) 398 settings.setForeground(foreground); 399 } 400 401 if(decorator instanceof IFontDecorator) { 402 Font font = ((IFontDecorator) decorator).decorateFont(element); 403 if (font != null) 404 settings.setFont(font); 405 } 406 } 407 } 408 } 409 | Popular Tags |