Tip: Styling Label Providers

Were you ever curious how to get those pretty colored labels in your JFace viewers you see all over Eclipse? For example, the blue counter label in the search results view:

search 300x132 Tip: Styling Label Providers

Well, I’ll tell you how! Let’s start with famous RCP Mail example:

mail1 300x200 Tip: Styling Label Providers

Let’s look at the current label provider in the RCP Mail example:

class ViewLabelProvider extends LabelProvider {

     public String getText(Object obj) {
          return obj.toString();
     }

     public Image getImage(Object obj) {
          String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
          if (obj instanceof TreeParent)
               imageKey = ISharedImages.IMG_OBJ_FOLDER;
          return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
     }
}

Pretty basic right? Well, to get styled labels all we need to do is extend StyledCellLabelProvider (available since Eclipse 3.4):

class ViewLabelProvider extends StyledCellLabelProvider {
     public void update(ViewerCell cell) {
          Object obj = cell.getElement();
          StyledString styledString = new StyledString(obj.toString());

          if(obj instanceof TreeParent) {
               TreeParent parent = (TreeParent) obj;
               styledString.append(" (" +
                    parent.getChildren().length +
                    ")", StyledString.COUNTER_STYLER);
          }

         cell.setText(styledString.toString());
         cell.setStyleRanges(styledString.getStyleRanges());
         cell.setImage(getImage(obj));
         super.update(cell);
     }

     public Image getImage(Object obj) {
          String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
          if (obj instanceof TreeParent)
               imageKey = ISharedImages.IMG_OBJ_FOLDER;
          return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
     }
}

If you do that, the RCP Mail example will look like this now:

mail21 300x196 Tip: Styling Label Providers

This just scratches the surface of what is possible with owner draw, styled strings and styled label providers.

Hope this helps and happy styling!

13 Responses to “Tip: Styling Label Providers”

  1. Moritz Post says:

    Nifty. Thanks for the Tip. :)

  2. marc says:

    Thanks for the tip Chris. I know this is outside the scope of the article, but it raises the question for me: How does one structure plugins to target multiple versions of eclipse? I can see easily implementing this in a few places in one of my plugins, but I still need to support 3.3 for a while. Tips, articles, etc?

    thanks!

  3. Ben Vitale says:

    DelegatingStyledCellLabelProvider is also useful, if you are retrofitting existing label providers.

  4. philk says:

    If I am not using the JFace Viewers is it still possible to style my own Table using a StyledString? How would that be done?

  5. Phil, you should be able to just use SWT owner draw manually and do it yourself. However, it’s a lot of code which StyledString does already for you. Check out this article on owner draw.

  6. Chrix says:

    Very helpful. Thanks! I was using my own pseudo html syntax on top of SWT, feeding jface label providers with weird strings like hithere. I can get rid of it with Eclipse 3.4.

  7. Chrix says:

    Previous message got scrambled, I meant: weird string like

    hithere

    .
    Anyway, post is useful :-)

  8. philk says:

    is it possible to have multi-line labels with different heights each?

  9. Catalin Gerea says:

    1. How can I change the color of the appended text? I have tried to use the following code:

    Styler style = StyledString.createColorRegistryStyler(“RED”, “BLUE”);
    if (parent.getChildren().size() > 0) {
    styledString.append(” (” + parent.getChildren().size() + “)”, style);
    }
    but my appended text wasn’t red on blue.

  10. Catalin Gerea says:

    Figured out how to change the color of the appended text: foreground or background color.

    Example for foreground color only:
    — define a string
    private static final String FOREGROUND_COLOR_STYLER = “custom_foreground_color”;
    — add the string to the color registry with the corresponding RGB color
    ColorRegistry colorRegistry = JFaceResources.getColorRegistry();
    colorRegistry.put(FOREGROUND_COLOR_STYLER, new RGB(255, 0, 0));
    — create the styler
    Styler style = StyledString.createColorRegistryStyler(FOREGROUND_COLOR_STYLER, null);

    If I needed to change only the foreground color I could have overwritten the value of JFacePreferences.COUNTER_COLOR in the JFacePreferences.getPreferenceStore().

  11. sebastian says:

    Really great, thanks a lot. I really searched a long time for this solution

  12. imran says:

    Hi
    Thanx for sharing code snipet, i have a question that can i highlight some children objects of my tree view ?

  13. JollyAmruta says:

    This really works!!!
    You need to remove the old getText() method and put update method in it. !!!!

13 responses so far

Written by . Published in Categories: Planet Eclipse

Author:
Published:
Mar 10th, 2009