Jump to: navigation, search

Difference between revisions of "FAQ What is a label decorator?"

 
m
Line 1: Line 1:
A <tt>DecoratingLabelProvider</tt> can be used when an element&#146;s
+
A <tt>DecoratingLabelProvider</tt> can be used when an element&#146;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, <tt>CompositeImageDescriptor</tt>, 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.
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, <tt>CompositeImageDescriptor</tt>, 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&#146;s Package Explorer uses
+
a decorating label provider to decorate resources with a decorator to
+
indicate either a warning or an error.
+
 
+
  
 +
Figure 8.3 shows how the JDT&#146;s Package Explorer uses  a decorating label provider to decorate resources with a decorator to  indicate either a warning or an error.
  
 
&nbsp;&nbsp;&nbsp;&nbsp;<img src="../images/dlp.png" width=500>
 
&nbsp;&nbsp;&nbsp;&nbsp;<img src="../images/dlp.png" width=500>
  
 
+
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 8.3'''&nbsp;&nbsp; Spider diagram of the parts of a decorating label provider
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 8.3'''&nbsp;&nbsp;
+
Spider diagram of the parts of a decorating label provider
+
 
+
 
+
 
+
 
+
 
+
 
+
  
 
Lightweight label decorators were introduced in Eclipse 2.1.  These decorators
 
Lightweight label decorators were introduced in Eclipse 2.1.  These decorators
Line 48: Line 25:
 
   }
 
   }
 
</pre>
 
</pre>
 
 
 
  
 
A decorating label provider is installed on a viewer in the same way
 
A decorating label provider is installed on a viewer in the same way
Line 57: Line 31:
 
   ILabelProvider lp = ... the basic label provider
 
   ILabelProvider lp = ... the basic label provider
 
   ILabelDecorator decorator = ... the decoration
 
   ILabelDecorator decorator = ... the decoration
   viewer.setLabelProvider(
+
   viewer.setLabelProvider(new DecoratingLabelProvider(lp, decorator));
      new DecoratingLabelProvider(lp, decorator));
+
 
</pre>
 
</pre>
 
 
 
 
  
 
== See Also: ==
 
== See Also: ==
 +
*[[FAQ What is a viewer?]]
 +
*[[FAQ What are content and label providers?]]
 +
*[[FAQ How do I create a label decorator declaratively?]]
 +
*[[FAQ How do I add label decorations to my viewer?]]
  
 
+
{{Template:FAQ_Tagline}}
[[FAQ_What_is_a_viewer%3F]]
+
 
+
 
+
[[FAQ_What_are_content_and_label_providers%3F]]
+
 
+
 
+
[[FAQ_How_do_I_create_a_label_decorator_declaratively%3F]]
+
 
+
 
+
[[FAQ_How_do_I_add_label_decorations_to_my_viewer%3F]]
+
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Revision as of 00:12, 16 June 2006

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 decoration
   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.