开发者

Eclipse: table column image transparent or not?

Once I start drawing my own images in an Eclipse table cell, highlighting that table cell results in a strange highlight color. See for yourself:

Eclipse: table column image transparent or not?

While the transparency is actually preserved like in the first column, the highlight-blue isn't as blue as it should. Is this my fault or Eclipse's fault?

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(tru开发者_JS百科e);
    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);
    }
  }
}


Overriding erase() and not calling super.erase() helped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜