Java 选择和悬停覆盖SWT表格组件中的单元格背景色

Java 选择和悬停覆盖SWT表格组件中的单元格背景色,java,swt,eclipse-rcp,Java,Swt,Eclipse Rcp,我正在使用SWT(和EclipseRCP)呈现一个表。我的问题是,如果我改变一个单元格(实际上是一个ViewerCell)的背景,我可以看到它有新的颜色 我的问题是,如果我在表格中选择一行或将鼠标悬停在包含相关单元格的行上,则选择/悬停背景会覆盖单元格颜色。如何覆盖此选项?我看到的唯一选项是使用所有者的DrawLabelProvider并自己绘制整个单元格 有一种方法可以防止表格绘制其选择背景,但字体颜色仍将更改为其选择颜色,因此,根据您的操作系统,在选择行时,您可能会在白色背景上显示白色文本。

我正在使用SWT(和EclipseRCP)呈现一个表。我的问题是,如果我改变一个单元格(实际上是一个ViewerCell)的背景,我可以看到它有新的颜色


我的问题是,如果我在表格中选择一行或将鼠标悬停在包含相关单元格的行上,则选择/悬停背景会覆盖单元格颜色。如何覆盖此选项?

我看到的唯一选项是使用
所有者的DrawLabelProvider
并自己绘制整个单元格


有一种方法可以防止表格绘制其选择背景,但字体颜色仍将更改为其选择颜色,因此,根据您的操作系统,在选择行时,您可能会在白色背景上显示白色文本。

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

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;
    }

}
公共类验证CellLabelProvider扩展了StyledCellLabelProvider{
专用静态最终整数延迟=200;
专用静态最终整数移位_X=5;
专用静态最终整数移位_Y=5;
专用静态最终整数显示=5000;
私人手机提供商;
私有字符串propertyName;
私有最终样式范围[]样式范围=新样式范围[1];
/**
*默认构造函数。
*@param提供程序
*@param propertyName propertyName
*/
公共验证CellLabelProvider(CellLabelProvider提供程序,字符串属性名称){
super(StyledCellLabelProvider.COLORS\u ON\u SELECTION);
this.provider=提供者;
this.propertyName=propertyName;
此.setOwnerDrawEnabled(true);
}
@凌驾
公共void初始化(ColumnViewer、ViewerColumn列){
super.initialize(查看器、列);
final StyleRange StyleRange=新StyleRange();
styleRange.foreground=Display.getCurrent().getSystemColor(SWT.COLOR\u WHITE);
styleRange.background=Display.getCurrent().getSystemColor(SWT.COLOR\u RED);
styleRanges[0]=styleRange;
}
@凌驾
公共无效更新(ViewerCell单元格){
提供程序更新(单元);
if(cell.getStyleRanges()==null){
cell.setStyleRanges(样式范围);
}
if(cell.getElement()instanceof IValidable){
IValidable模型=(IValidable)cell.getElement();
如果(!ControllerRegistry.GetCurrentVillections().GetVillections(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());
}否则{
if(styleRanges[0].length0?ret.toString():空;
}
返回ret;
}
@凌驾
public int getToolTipDisplayDelayTime(对象){
返回延迟;
}
@凌驾
公共点getToolTipShift(对象){
返回新点(SHIFT_X,SHIFT_Y);
}
@凌驾
public int getToolTipTimeDisplayed(对象){
返回显示;
}
}

我似乎解决了StyledCellLabelProvider的问题。如果我完成了,我会提供解决方案。