SWT JFace TableViewer selection problem in Windows 7
I am using the SWT JFace TableViewer component to display a list of data.  I have implemented the IColorProvider to provide a custom foreground and background colour.  I provide black foreground text with a light background colour in most cases.  However, in some cases I use a dark background colour with white foreground text.  This causes a problem in Windows 7 in that the selection colour is a pale blue.  

What is happening on Windows 7 is when a dark-coloured item in my table (row-select) is selected the background colour is the pale blue selection colour.  However, the foreground colour remains white and you cannot see it (Refer to fig 1 below).

<a href="http://freshwebdev.co.za/uploads/2011/06/TableViewer1.jpg">
Figure 1: JFace SWT TableViewer - Row Selected in Windows 7</a>

1. Firstly, am I doing something wrong here?

2. Secondly, I have attempted the following to fix this (any ideas about the HOT event painting?):


		table.addListener(SWT.EraseItem, new Listener() {
			public void handleEvent(Event event) {
				System.out.println(event);
				boolean selected = (event.detail & SWT.SELECTED) == 0;

				event.detail &= ~SWT.HOT;
				TableItem item = (TableItem) event.item;
				int clientWidth = table.getClientArea().width;
				GC gc = event.gc;
				Color oldForeground = gc.getForeground();
				Color oldBackground = gc.getBackground();
				if (selected) {
					gc.setForeground(ColourHelper.WHITE);
					gc.setBackground(ColourHelper.WHITE);
					gc.fillRectangle(0, event.y, clientWidth, event.height);
				} else {
					gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));					
					gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_BACKGROUND));					
					gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
				}

				gc.setForeground(oldForeground);
				gc.setBackground(oldBackground);
				event.detail &= ~SWT.SELECTED;
			}
		});


When I re-run my test application I get the following.  The selected row is fixed now but there is still a problem with the table-row that is "hovered" over as seen in figure 2.  I have tried to capture the SWT.HOT event within the EraseItem handler, but it doesn't seem to do anything.

<a href="http://freshwebdev.co.za/uploads/2011/06/TableViewer2.jpg">
Figure 2: JFace SWT TableViewer - Added EraseItem Event Handler</a>

I did have some luck adding the PaintItem event handler (as shown below), but I will have to place the same logic regarding cell-alignment in this code for it to render properly.  In addition, my paint event fires and paints over the correctly rendering SELECTED Windows 7 Colour fix.

		table.addListener(SWT.PaintItem, new Listener() {
			public void handleEvent(Event event) {

				boolean hot = (event.detail & SWT.HOT) == 0;
				if (hot) System.out.println("HOT!");
				if ((event.detail & SWT.HOT & ~SWT.SELECTED) == 0)
					return;
				event.detail &= ~SWT.HOT;
				final int TEXT_MARGIN = 3;
				GC gc = event.gc;

				gc.setForeground(ColourHelper.BLACK);
				gc.setBackground(ColourHelper.BLACK);

				TableItem item = (TableItem) event.item;
				item.setBackground(ColourHelper.BLACK);
				item.setForeground(ColourHelper.BLACK);
				String text = item.getText(event.index);
				int yOffset = 0;
				if (event.index == 1) {
					Point size = event.gc.textExtent(text);
					yOffset = Math.max(0, (event.height - size.y) / 2);
				}
				event.gc.drawText(text, event.x + TEXT_MARGIN, event.y + yOffset, true);
			}
		});


In summary, perhaps I am doing something wrong with the IColourProvider and it's that simple?  Alternatively, there is a bug within the Table or TableViewer component.  I have upgraded to the latest by downloading the RCP 3.6.1, but I have the same symptoms.  

Some help would be much appreciated :-)
SWT JFace TableViewer selection problem in Windows 7
===

I am using the SWT JFace TableViewer component to display a list of data.  I have implemented the IColorProvider to provide a custom foreground and background colour.  I provide black foreground text with a light background colour in most cases.  However, in some cases I use a dark background colour with white foreground text.  This causes a problem in Windows 7 in that the selection colour is a pale blue.  

What is happening on Windows 7 is when a dark-coloured item in my table (row-select) is selected the background colour is the pale blue selection colour.  However, the foreground colour remains white and you cannot see it (Refer to fig 1 below).

<a href="http://freshwebdev.co.za/uploads/2011/06/TableViewer1.jpg">
Figure 1: JFace SWT TableViewer - Row Selected in Windows 7</a>

1. Firstly, am I doing something wrong here?

2. Secondly, I have attempted the following to fix this (any ideas about the HOT event painting?):


		table.addListener(SWT.EraseItem, new Listener() {
			public void handleEvent(Event event) {
				System.out.println(event);
				boolean selected = (event.detail & SWT.SELECTED) == 0;

				event.detail &= ~SWT.HOT;
				TableItem item = (TableItem) event.item;
				int clientWidth = table.getClientArea().width;
				GC gc = event.gc;
				Color oldForeground = gc.getForeground();
				Color oldBackground = gc.getBackground();
				if (selected) {
					gc.setForeground(ColourHelper.WHITE);
					gc.setBackground(ColourHelper.WHITE);
					gc.fillRectangle(0, event.y, clientWidth, event.height);
				} else {
					gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));					
					gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_BACKGROUND));					
					gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
				}

				gc.setForeground(oldForeground);
				gc.setBackground(oldBackground);
				event.detail &= ~SWT.SELECTED;
			}
		});


When I re-run my test application I get the following.  The selected row is fixed now but there is still a problem with the table-row that is "hovered" over as seen in figure 2.  I have tried to capture the SWT.HOT event within the EraseItem handler, but it doesn't seem to do anything.

<a href="http://freshwebdev.co.za/uploads/2011/06/TableViewer2.jpg">
Figure 2: JFace SWT TableViewer - Added EraseItem Event Handler</a>

I did have some luck adding the PaintItem event handler (as shown below), but I will have to place the same logic regarding cell-alignment in this code for it to render properly.  In addition, my paint event fires and paints over the correctly rendering SELECTED Windows 7 Colour fix.

		table.addListener(SWT.PaintItem, new Listener() {
			public void handleEvent(Event event) {

				boolean hot = (event.detail & SWT.HOT) == 0;
				if (hot) System.out.println("HOT!");
				if ((event.detail & SWT.HOT & ~SWT.SELECTED) == 0)
					return;
				event.detail &= ~SWT.HOT;
				final int TEXT_MARGIN = 3;
				GC gc = event.gc;

				gc.setForeground(ColourHelper.BLACK);
				gc.setBackground(ColourHelper.BLACK);

				TableItem item = (TableItem) event.item;
				item.setBackground(ColourHelper.BLACK);
				item.setForeground(ColourHelper.BLACK);
				String text = item.getText(event.index);
				int yOffset = 0;
				if (event.index == 1) {
					Point size = event.gc.textExtent(text);
					yOffset = Math.max(0, (event.height - size.y) / 2);
				}
				event.gc.drawText(text, event.x + TEXT_MARGIN, event.y + yOffset, true);
			}
		});


In summary, perhaps I am doing something wrong with the IColourProvider and it's that simple?  Alternatively, there is a bug within the Table or TableViewer component.  I have upgraded to the latest by downloading the RCP 3.6.1, but I have the same symptoms.  

Some help would be much appreciated :-)