选择和悬停覆盖SWT表组件中的单元格背景颜色 [英] Selection and hover overrides Cell background color in an SWT Table component

查看:176
本文介绍了选择和悬停覆盖SWT表组件中的单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SWT(和Eclipse RCP)来渲染表格。我的问题是,如果我改变一个单元格的背景(事实上是一个ViewerCell),我可以看到它有新的颜色。

I'm using SWT (and Eclipse RCP) to render a table. My problem is that if I change the background of a cell (a ViewerCell in fact) I can see that it has the new color.

我的问题是,如果我在表格中选择一行,或者如果我将鼠标悬停在包含我的单元格的行上,则选择/悬停背景会覆盖我的单元格颜色。我怎样才能覆盖它?

My problem is that if I select a row in my Table or if I hover over the row containing my cell in question then the selection/hover background overrides my cell color. How can I override this?

推荐答案

使用StyledCellLabelProvider解决了问题。如果你想看一些代码,请告诉我。
修改:
我们使用它来显示验证错误,因此请忽略此处的验证内容:

Problem solved with StyledCellLabelProvider. Tell me if you want to see some code. We use it do display validation errors so ignore the validation stuff here:

public class ValidationCellLabelProvider extends StyledCellLabelProvider {

    private static final int DELAY = 200;
    private static final int SHIFT_X = 5;
    private static final int SHIFT_Y = 5;
    private static final int DISPLAY = 5000;
    private CellLabelProvider provider;
    private String propertyName;
    private final StyleRange[] styleRanges = new StyleRange[1];

    /**
     * Default constructor.
     * @param provider provider
     * @param propertyName propertyName
     */
    public ValidationCellLabelProvider(CellLabelProvider provider, String propertyName) {
        super(StyledCellLabelProvider.COLORS_ON_SELECTION);
        this.provider = provider;
        this.propertyName = propertyName;
        this.setOwnerDrawEnabled(true);
    }

    @Override
    public void initialize(ColumnViewer viewer, ViewerColumn column) {
        super.initialize(viewer, column);
        final StyleRange styleRange = new StyleRange();
        styleRange.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
        styleRange.background = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
        styleRanges[0] = styleRange;
    }

    @Override
    public void update(ViewerCell cell) {
        provider.update(cell);
        if (cell.getStyleRanges() == null) {
            cell.setStyleRanges(styleRanges);
        }
        if (cell.getElement() instanceof IValidable) {
            IValidable model = (IValidable) cell.getElement();
            if (!ControllerRegistry.getCurrentViolations().getViolations(model.getModelId(), propertyName).isEmpty()) {
                if (cell.getText().isEmpty()) {
                    cell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
                    cell.setImage(FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).getImage());
                } else {
                    if (styleRanges[0].length < cell.getText().length()) {
                        styleRanges[0].length = cell.getText().length();
                    }
                }
            } else {
                if (cell.getImage() != null) {
                    cell.setImage(null);
                }
                cell.setStyleRanges(null);
            }
        }
        super.update(cell);
    }

    //mine
    @Override
    protected void paint(Event event, Object element) {
        if (element instanceof IValidable) {
            IValidable model = (IValidable) element;
            if (!ControllerRegistry.getCurrentViolations().getViolations(model.getModelId(), propertyName).isEmpty()) {
                int width = 1000;
                int x = event.x;
                int y = event.y;

                int height = event.height - 1;
                GC gc = event.gc;

                Color oldBackground = gc.getBackground();

                gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));

                gc.fillRectangle(x, y, width, height);

                gc.setBackground(oldBackground);
            }
        }
        super.paint(event, element);
    }

    //-----

    @Override
    public String getToolTipText(Object element) {
        String ret = null;
        if (element instanceof IValidable) {
            List<ConstraintViolation> constraintViolations = ControllerRegistry.getCurrentViolations().getViolations(
                    ((IValidable) element).getModelId(), propertyName);
            if (!constraintViolations.isEmpty()) {
                ret = ValidationHelper.getMessage(constraintViolations);
            }
        }
        if (ret != null) {
            ret = ret.length() > 0 ? ret.toString() : null;
        }
        return ret;
    }

    @Override
    public int getToolTipDisplayDelayTime(Object object) {
        return DELAY;
    }

    @Override
    public Point getToolTipShift(Object object) {
        return new Point(SHIFT_X, SHIFT_Y);
    }

    @Override
    public int getToolTipTimeDisplayed(Object object) {
        return DISPLAY;
    }

}

这篇关于选择和悬停覆盖SWT表组件中的单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆