How can I decorate column images and text in an SWT TableViewer?

807 views Asked by At

I have an SWT table that is wrapped with a JFace TableViewer.

My requirements are:

  1. Decorate the column image with the decorators as they are defined in the workbench decorator manager
  2. Decorate the text of the columns with different colors

I was able to #1 by extending DecoratingLabelProvider and implementing ITableLabelProvider. I passed it my original TableLabelProvider and the workbench decorator manager, and I got icons with decorators.

Then I started to work on #2. I asked this question here and was told that IStyleLabelProvider (what I was trying to use for the colored text) was incompatible with ITableLabelProvider. So I switched to using a ColumnLabelProvider that implements IStyledLabelProvider.

However, now I am stuck. These 2 requirements seem to be mutually exclusive. I cannot extend both ColumnLabelProvider and DecoratingLabelProvider. When I tried to simply pass in the workbench decorator manager to the ColumnLabelProvider like this, but it did not decorate the image at all. Did I pass it in wrong, or will that only work in a DecoratingLabelProvider? What else can I try?

    public Image getColumnImage(final Object element, final int columnIndex) {
    if (columnIndex == MY_COLUMN_INDEX) {
            final MyObject myObj = (MyObject) element;
            final Image image = myObj .getImage();
            Image newImage = null;
            if(this.decorator != null) {
                newImage = this.decorator.decorateImage(image, myObj );
            }
            return newImage == null ? image : newImage;
    }
    return null;
}
1

There are 1 answers

2
greg-449 On BEST ANSWER

You can use DecoratingStyledCellLabelProvider which takes an IStyledLabelProvider and an ILabelDecorator as parameters:

new DecoratingStyledCellLabelProvider(styledLabelProvider,
                    PlatformUI.getWorkbench().getDecoratorManager()
                            .getLabelDecorator(), null);