2

I have a TreeViewer used in an eclipse plugin that uses a content provider and a label provider that implements all of (ITableLabelProvider, IFontProvider, IColorProvider).

But I need one of the columns of the table it creates to hold "links" - underlined blue text that when clicked causes some popup to open. I guess what I want to do is cause that single column to hold styled text and not just text, and attach a listener to the items in that column of the tree, but I couldn't figure out how to do it.

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
Oren Sarid
  • 143
  • 1
  • 12
  • You can use `DelegatingStyledCellLabelProvider` as a column label provider similar to http://stackoverflow.com/q/26173834/2670892 – greg-449 Oct 06 '14 at 07:46
  • Thanks for the quick response, Greg. But I didn't understand how to apply the styled text only to a single TreeColumn (the index is known, obviously). Oren – Oren Sarid Oct 06 '14 at 07:58
  • Added a answer with more details – greg-449 Oct 06 '14 at 08:07

1 Answers1

3

Use a separate label provider for each column using TreeViewerColumn:

TreeViewer viewer = new TreeViewer(.....);

TreeViewerColumn col1 = new TreeViewerColumn(viewer, SWT.LEAD);

col1.setLabelProvider(col1 label provider);

... repeat for other columns

For columns that require styling use DelegatingStyledCellLabelProvider as the column label provider as described here

Note: Do not call viewer.setLabelProvider when using column label providers.

Community
  • 1
  • 1
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • I don't have a TableViewer, I have a TreeViewer. Is this still applicable? – Oren Sarid Oct 06 '14 at 08:17
  • Sorry not reading carefully enough. Updated answer to TreeViewerColumn – greg-449 Oct 06 '14 at 08:57
  • Sorry for being a pain... I tried that and eclipse forced me to have the new label provider extend CellLabelProvider in addition to implementing the interface (or I cannot use setLabelProvider). But it doesn't seem to call the getStyledText on the cells, and if I don't use it for all columns I just get their object representation. What am I missing? – Oren Sarid Oct 06 '14 at 09:45
  • 1
    Use `DelegatingStyledCellLabelProvider` for columns that require styling, `ColumnLabelProvider` for those that do not. Both extend `CellLabelProvider`. – greg-449 Oct 06 '14 at 10:08
  • OK, I got this to work - almost. For some reason the StyledText.fontStyle doesn't "catch" - my instructions for bold/italic are ignored. Other than that all is honey sweet. Any idea? – Oren Sarid Oct 06 '14 at 11:28
  • Are you talking about `StyleRange.fontStyle`? This code only supports `TextRange`. You would have to set `TextRange.font` to change the font. – greg-449 Oct 06 '14 at 11:44