Jump to: navigation, search

FAQ What is a label decorator?

A DecoratingLabelProvider can be used when an element’s text and image need to be annotated to show different states or properties. This is accomplished by bringing together a standard label provider with an optional decorator. The standard label provider is first asked to generate the text and image for the input element, and then the decorator, if installed, is allowed to augment or replace the original text and image with new values. JFace provides a helper class, CompositeImageDescriptor, to help you combine an image with one or more overlays. Because multiple overlays in a particular position will obscure one another, you must be careful to avoid overlapping decorators when creating a decorating label provider.

Figure 8.3 shows how the JDT’s Package Explorer uses a decorating label provider to decorate resources with a decorator to indicate either a warning or an error.

    <img src="../images/dlp.png" width=500>

    Figure 8.3   Spider diagram of the parts of a decorating label provider

Lightweight label decorators were introduced in Eclipse 2.1. These decorators abstract away the details of performing the image overlay. An implementation of ILightWeightLabelDecorator is provided with an IDecoration object, which it calls in order to add the decorations. The following simple example decorates a viewer containing java.io.File objects to indicate whether they are read-only:

   ImageDescriptor readOnlyOverlay = ...;
   public void decorate(Object element, IDecoration decoration) {
      if (!(element instanceof java.io.File))
         return;
      boolean readOnly = !((java.io.File) element).canWrite();
      if (!readOnly)
         return;
      decoration.addOverlay(readOnlyOverlay);
      decoration.addSuffix(" (read only)");
   }

A decorating label provider is installed on a viewer in the same way as any other label provider:

   ILabelProvider lp = ... the basic label provider
   ILabelDecorator decorator = ... the decorator
   viewer.setLabelProvider(new DecoratingLabelProvider(lp, decorator));

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.