Create a Decorator class say MyTreeDecorator.
Then set this Decorator as the Label Provider for the Viewer.

public class MyTreeDecorator extends StyledCellLabelProvider {

private MyTreeLabelProvider myTreeLabelProvider;

private static StyleRange styleRange;
private ImageDescriptor overlayImg = null;
private OverlayImageDescriptor overlayIntentImageDescriptor;
private Point fSize;
private ImageDescriptor baseImg = CompositeResourcesUIActivator
.getDefault()
.getImageDescriptor(“icons/other/baseImg.png”); //$NON-NLS-1$

private ImageDescriptor overlayWarningImg = ImageDescriptor
.createFromImage(icons/other/warningImage.bmpE);
private ImageDescriptor overlayErrorImg = CompositeResourcesUIActivator
.getDefault().getImageDescriptor(“icons/other/errorImage.bmp”); //$NON-NLS-1$

public MyTreeDecorator(
MyTreeLabelProvider myTreeLabelProvider) {
super();
this.myTreeLabelProvider = myTreeLabelProvider;
overlayMyImageDescriptor = new OverlayMyDescriptor(baseImg);
}

/**
* @param ViewerCell
*/
public void update(ViewerCell cell) {

Object selection = cell.getElement();

// based upon the selection get the baseImg
baseImg = myTreeLabelProvider.getImage(selection);
// determine the overlayImg
OverlayImageDescriptor overlayImageDescriptor = new OverlayImageDescriptor(
baseImg);
cell.setImage(overlayImageDescriptor.createImage());

// now decorate text
StyleRange styleRange = new StyleRange();
styleRange.start = selection.getName().length();
styleRange.length = myTreeLabelProvider.getText(selection).length()
– selection.getName().length();

styleRange.fontStyle = SWT.BOLD;
styleRange.foreground = foregroundColor;
cell.setStyleRanges(new StyleRange[] { styleRange });

cell.setText(myTreeLabelProvider.getText(selection));
}

}

Advertisements