Eclipse: table column image transparent or not?

As soon as I start drawing my own images in an Eclipse table cell, highlighting that the table cell results in a strange selection color. See for yourself:

alt text

While the transparency is actually maintained, as in the first column, the blue backlight is not as blue as it should. Is this my mistake or an Eclipse error?

Here's the code snippet:

public class TransparentOrNot {

  public static void main(String[] args) {
    Display firstDisplay = new Display();
    Shell firstShell = new Shell(firstDisplay);
    firstShell.setText("Transparent-or-not!");
    firstShell.setSize(300, 200);
    firstShell.setLayout(new FillLayout());

    TableViewer viewer = new TableViewer(firstShell, SWT.MULTI);
    viewer.getTable().setLinesVisible(true);
    viewer.getTable().setHeaderVisible(true);

    TableViewerColumn tableViewerColumn = new TableViewerColumn(viewer, SWT.CENTER);
    tableViewerColumn.getColumn().setText("First");
    tableViewerColumn.getColumn().setWidth(150);
    tableViewerColumn.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public Image getImage(Object element) {
        return ImageDescriptor.createFromFile(TransparentOrNot.class, "/red.png").createImage();
      }

      @Override
      public String getText(Object element) {
        return null;
      }
    });
    tableViewerColumn = new TableViewerColumn(viewer, SWT.CENTER);
    tableViewerColumn.getColumn().setText("Second");
    tableViewerColumn.getColumn().setWidth(150);
    tableViewerColumn.setLabelProvider(new CenterImageLabelProvider());
    viewer.setContentProvider(ArrayContentProvider.getInstance());

    viewer.setInput(new String[][]{{"a", "b"}, {"c", "d"}});

    firstShell.open();
    while (!firstShell.isDisposed()) {
      if (!firstDisplay.readAndDispatch()) {
        firstDisplay.sleep();
      }
    }
    firstDisplay.dispose();
  }

  static class CenterImageLabelProvider extends OwnerDrawLabelProvider {

    @Override
    protected void measure(Event event, Object element) {
      // no-op
    }

    @Override
    protected void paint(Event event, Object element) {

      Image image = ImageDescriptor.createFromFile(TransparentOrNot.class, "/green.png").createImage();

      Widget item = event.item;
      Rectangle bounds = ((TableItem) item).getBounds(event.index);

      Rectangle imgBounds = image.getBounds();
      bounds.width /= 2;
      bounds.width -= imgBounds.width / 2;
      bounds.height /= 2;
      bounds.height -= imgBounds.height / 2;

      int x = bounds.width > 0 ? bounds.x + bounds.width : bounds.x;
      int y = bounds.height > 0 ? bounds.y + bounds.height : bounds.y;

      event.gc.drawImage(image, x, y);
    }
  }
}
+3
source share
1 answer

Overcoming erase () and not calling super.erase ().

+2
source

Source: https://habr.com/ru/post/1726483/


All Articles