You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Eclipse CSS机制为自定义控件设置默认样式?

通过CSS为Eclipse自定义控件设置默认样式

我已经掌握Eclipse CSS的element、elementProvider和propertyHandler相关知识,现在需要为自定义控件设置默认样式——当没有CSS配置该控件的draw-color属性时,控件的drawColor默认值为蓝色,且不想通过Java构造函数硬编码实现,希望完全通过CSS机制完成。

现有代码如下:

自定义控件代码

public class Custom extends Canvas {

    private Color drawColor ;
    
    public Custom( Composite parent ) {
        super( parent, SWT.DOUBLE_BUFFERED ) ;
        super.addListener( SWT.Paint, this :: handlePaint ) ;
    }
    
    private void handlePaint( Event event ) {
        GC gc = event.gc ;
        gc.setAdvanced( true ) ;
        gc.setAntialias( SWT.ON ) ;
        
        if ( drawColor == null ) { return ;}
        gc.setBackground( drawColor ) ;
        // draw something
    }
    
    public void setDrawColor(Color drawColor) {
        this.drawColor = drawColor;
    }
    
}

对应的PropertyHandler代码

public class CustomPropertyHandler implements ICSSPropertyHandler {
    
    private static final String ATTR_DRAW_COLOR = "draw-color" ;

    @Override
    public boolean applyCSSProperty(Object element, String property, CSSValue value, String pseudo, CSSEngine engine)
            throws Exception {
        Custom custom = (Custom) ((CompositeElement) element).getNativeWidget() ;
        switch ( property ) {
            case ATTR_DRAW_COLOR:
                Color color = (Color) engine.convert( value, Color.class, custom.getDisplay() ) ;
                custom.setDrawColor( color ) ;
                break;
            default:
                break;
        }
        return true;
    }

}

当前状态:当CSS中配置Custom { draw-color: red; }时,控件会正确显示红色,但无CSS配置时drawColor为null,控件不绘制颜色。


解决方案

方案1:通过插件默认样式表定义全局默认规则

在插件中添加默认CSS文件(如default.css),写入默认样式:

Custom {
    draw-color: blue;
}

然后在plugin.xml中配置让Eclipse加载该默认样式表(关联到你的主题):

<extension
      point="org.eclipse.e4.ui.css.swt.theme">
   <theme
         basestylesheeturi="platform:/plugin/你的插件ID/default.css"
         id="你的插件ID.default.theme"
         label="默认主题">
   </theme>
</extension>

此方案下,用户未自定义配置时会加载默认样式,自定义配置会覆盖默认值。

方案2:通过ElementProvider设置默认CSS属性

创建自定义ElementProvider,在控件对应的CSS元素初始化时注入默认属性值:

public class CustomElementProvider implements IElementProvider {
    @Override
    public Element createElement(Object element, CSSEngine engine) {
        CompositeElement compositeElement = new CompositeElement(element, engine);
        if (element instanceof Custom custom) {
            Display display = custom.getDisplay();
            Color defaultBlue = display.getSystemColor(SWT.COLOR_BLUE);
            try {
                // 将默认颜色转换为CSS引擎可识别的CSSValue
                CSSValue defaultValue = engine.convert(defaultBlue, Color.class);
                compositeElement.setProperty("draw-color", defaultValue);
            } catch (Exception e) {
                // 异常处理,比如打日志
                e.printStackTrace();
            }
        }
        return compositeElement;
    }
}

然后在plugin.xml中注册该ElementProvider:

<extension
      point="org.eclipse.e4.ui.css.core.elementProvider">
   <elementProvider
         class="你的包路径.CustomElementProvider"
         priority="10">
      <widget
            class="你的包路径.Custom">
      </widget>
   </elementProvider>
</extension>

此方案会在CSS引擎处理控件时,自动为未配置draw-color的控件注入默认值,触发PropertyHandler设置蓝色。


内容的提问来源于stack exchange,提问作者Bourbon_7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 21:25:11