ViewerSupport and ITableColorProvider or ITableFontProvider

345 views Asked by At

I'm writing an RCP-Application and trying to use databinding to link the controls of the GUI with the model. This includes for example binding data to a table.

As far as I understood, org.eclipse.jface.databinding.viewers.ViewerSupport is the recommended method to bind a model to a table viewer. This will however only allow me to put the data as text into the table. I'd also like to change the foreground and background color aswell as the font of some cells, depending on other observables. I'd also be happy if I could somehow get a ITableFontProvider or ITableColorProvider into what ViewerSupport.bind(...) produces.

So far I've not found a nice way to do that. I could copy the contents of ViewerSupport.bind() and override the LabelProvider with my own class. This seems a bit messy.

I could also after calling ViewerSupport.bind retrieve the LabelProvider and replace it with a delegating LabelProvider which also implements ITableFontProvider and ITableColorProvider. This leaves me creating a lot of methods that do nothing but delegate things to another object. Not very elegant aswell.

All of that doesn't seem so nice. Any idea how to do it in an elegant way? Am I overlooking some factory class to do that?

1

There are 1 answers

0
greg-449 On BEST ANSWER

ViewerSupport just provides simplified methods based on the various data binding content and label providers. It is perfectly acceptable to use these content and label providers directly when ViewerSupport does not provide what you want.

For example, ViewerSupport.bind(StructuredViewer viewer, IObservableList input, IValueProperty[] labelProperties) is just:

ObservableListContentProvider contentProvider = new ObservableListContentProvider();
if (viewer.getInput() != null)
    viewer.setInput(null);
viewer.setContentProvider(contentProvider);
viewer.setLabelProvider(new ObservableMapLabelProvider(Properties
        .observeEach(contentProvider.getKnownElements(),
                    labelProperties)));
if (input != null)
    viewer.setInput(input);

So you could just use this code but with a sub-class of ObservableMapLabelProvider with your font and color providers.