栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号