title: SWT JFace TableViewer selection in Windows 7
body: <p>I am using the SWT JFace <code>TableViewer</code> component to display a list of data.  I have implemented the <code>IColorProvider</code> 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.  </p>
  
  <p>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).</p>
  
  <p><a href="http://freshwebdev.co.za/uploads/2011/06/TableViewer1.jpg" rel="nofollow">
  Figure 1: JFace SWT TableViewer - Row Selected in Windows 7</a></p>
  
  <ol>
  <li><p>Firstly, am I doing something wrong here?</p></li>
  <li><p>Secondly, I have attempted the following to fix this (any ideas about the HOT event painting?):</p>
  
  <pre><code>table.addListener(SWT.EraseItem, new Listener() {
      public void handleEvent(Event event) {
          System.out.println(event);
          boolean selected = (event.detail &amp; SWT.SELECTED) == 0;
  
          event.detail &amp;= ~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 &amp;= ~SWT.SELECTED;
      }
  });
  </code></pre></li>
  </ol>
  
  <p>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.</p>
  
  <p><a href="http://freshwebdev.co.za/uploads/2011/06/TableViewer2.jpg" rel="nofollow">
  Figure 2: JFace SWT TableViewer - Added EraseItem Event Handler</a></p>
  
  <p>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.</p>
  
  <pre><code>table.addListener(SWT.PaintItem, new Listener() {
      public void handleEvent(Event event) {
  
          boolean hot = (event.detail &amp; SWT.HOT) == 0;
          if (hot) System.out.println("HOT!");
          if ((event.detail &amp; SWT.HOT &amp; ~SWT.SELECTED) == 0)
              return;
          event.detail &amp;= ~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);
      }
  });
  </code></pre>
  
  <p>In summary, perhaps I am doing something wrong with the <code>IColourProvider</code> and it's that simple? Alternatively, there is a bug within the <code>Table</code> or <code>TableViewer</code> component. I have upgraded to the latest by downloading the RCP 3.6.1, but I have the same symptoms.  </p>
  
  <p>Some help would be much appreciated :-)</p>