0

I have a tableviewer and I created a contextual menu and now on right click on the row I have the option: Color the selected row. The menu command is linked to a class inside my plugin project. I want to select the row then right-click and click on contextual menu option: Color the selected row and then this command to color all the text contained in every cell of the row, in red for example.

  public class ShowSelected extends AbstractHandler {

  @SuppressWarnings("unchecked")
  public Object execute(ExecutionEvent event) throws ExecutionException {




        //here should be my piece of code



    return null;
  }
}

How to select the row and color the text in every cell of that row ? I have 5 cells for every row.

John Smith
  • 777
  • 2
  • 14
  • 37

1 Answers1

1

Select a row with:

TableViewer viewer = .... get your viewer

RowData rowData = .... get the model row data that you want to select

viewer.setSelection(new StructuredSelection(rowData));

To color rows make your label provider implement IColorProvider (in addition to anything else it implements). You will now have to implement:

@Override
public Color getForeground(Object element)
{
  // TODO return foreground color or null
}

@Override
public Color getBackground(Object element)
{
  // TODO return background color or null
}

The element parameter is the model row data for which the color is required.

You can get the table viewer to request the updated colors from the label provider using:

viewer.update(rowData);

A note on Color - any Color objects that you create must be disposed when they are no longer needed. You should minimize the number of Color objects created.

If you are using ColumnLabelProvider then that already implements IColorProvider so you just need to override getForeground / getBackground. So for example:

@Override
public Color getForeground(Object element)
{
  RowData rowData = (RowData)element;

  // TODO if rowData should have a color return it, otherwise return null
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • My label provider implements a getText method, the color should be changed when an option from the context menu is selected....I can't use my label provider because this will actually color the row on selection right ? – John Smith Sep 09 '14 at 14:17
  • You can use you label provider. The colors for the row are requested separately from the text. You will have to put code in to the getForeground/getBackground to decide which color applies to the row. – greg-449 Sep 09 '14 at 14:52
  • col.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { Row p = (Row) element; return p.getLine(); } }); this is how my labelprovider looks like....I have to change the color in another piece of code, when the menu option is pressed, I can't understand how to activate the piece of code for color in the menu method.... – John Smith Sep 10 '14 at 07:26
  • Calling `viewer.update(rowData)` in the menu method will make the viewer call the getText, getForeground and getBackground methods for that row. – greg-449 Sep 10 '14 at 07:36
  • I can't use viewer.update(rowData) in menu method , this is how I declared rowData: ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection(); IStructuredSelection strucSelection = (IStructuredSelection) selection; Row rowData = (Row) strucSelection.getFirstElement(); – John Smith Sep 10 '14 at 07:52
  • I can't return the color also....I have some problems with the imports.... – John Smith Sep 10 '14 at 08:02
  • SWT Color is `org.eclipse.swt.graphics.Color` – greg-449 Sep 10 '14 at 08:37
  • That is not how SWT Color works, read up on SWT colors. – greg-449 Sep 10 '14 at 09:12
  • Ok, I read and now getForeground works, but colors all the elements when the view is started(I'm creating a view with a tableviewer inside), I need to call getForeground only at a press of an option in the menu – John Smith Sep 10 '14 at 09:26
  • You have to test the `element` parameter to see if it matches the row you want to color – greg-449 Sep 10 '14 at 09:32