Tip: Styling Label Providers

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

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

mail1

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

[code lang=”java”]
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);
}
}
[/code]

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

[code lang=”java”]
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);
}
}
[/code]

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

mail21

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 Comments
  • Posted at 11:29 pm, March 10, 2009

    Nifty. Thanks for the Tip. 🙂

  • marc
    Posted at 12:23 am, March 11, 2009

    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!

  • Posted at 2:51 am, March 11, 2009

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

  • philk
    Posted at 9:19 am, March 12, 2009

    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?

  • Posted at 3:03 pm, June 23, 2009

    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.

  • Posted at 3:05 pm, June 23, 2009

    Previous message got scrambled, I meant: weird string like

    hithere

    .
    Anyway, post is useful 🙂

  • philk
    Posted at 9:13 pm, July 3, 2009

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

  • Catalin Gerea
    Posted at 10:29 pm, February 13, 2010

    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.

  • Catalin Gerea
    Posted at 12:20 am, February 27, 2010

    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().

  • sebastian
    Posted at 11:49 am, May 5, 2010

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

  • imran
    Posted at 10:58 am, November 3, 2010

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

  • JollyAmruta
    Posted at 7:45 am, February 2, 2012

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