【问题标题】:Selection and hover overrides Cell background color in an SWT Table component选择和悬停覆盖 SWT 表组件中的单元格背景颜色
【发布时间】:2011-10-25 14:40:39
【问题描述】:

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

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

【问题讨论】:

    标签: java swt eclipse-rcp


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我看到的唯一选择是使用 OwnerDrawLabelProvider 并自己绘制整个单元格。

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

      【讨论】:

      • 看来我用StyledCellLabelProvider解决了这个问题。如果我完成它,我会提供解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多