11/*******************************************************************************
2- * Copyright (c) 2008, 2019 Angelo Zerr and others.
2+ * Copyright (c) 2008, 2026 Angelo Zerr and others.
33 *
44 * This program and the accompanying materials
55 * are made available under the terms of the Eclipse Public License 2.0
1616
1717import org .eclipse .core .runtime .ILog ;
1818import org .eclipse .core .runtime .RegistryFactory ;
19+ import org .eclipse .e4 .ui .css .core .dom .CSSStylableElement ;
20+ import org .eclipse .e4 .ui .css .core .engine .CSSElementContext ;
21+ import org .eclipse .e4 .ui .css .core .impl .engine .CSSEngineImpl ;
1922import org .eclipse .e4 .ui .css .core .impl .engine .RegistryCSSElementProvider ;
2023import org .eclipse .e4 .ui .css .core .impl .engine .RegistryCSSPropertyHandlerProvider ;
24+ import org .eclipse .e4 .ui .css .core .resources .IResourcesRegistry ;
25+ import org .eclipse .e4 .ui .css .swt .dom .WidgetElement ;
26+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTColorConverterImpl ;
27+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTCursorConverterImpl ;
28+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTFontConverterImpl ;
29+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTFontDataConverterImpl ;
30+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTGradientConverterImpl ;
31+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTImageConverterImpl ;
32+ import org .eclipse .e4 .ui .css .swt .properties .converters .CSSValueSWTRGBConverterImpl ;
33+ import org .eclipse .e4 .ui .css .swt .resources .SWTResourceRegistryKeyFactory ;
34+ import org .eclipse .e4 .ui .css .swt .resources .SWTResourcesRegistry ;
2135import org .eclipse .swt .SWT ;
2236import org .eclipse .swt .events .DisposeListener ;
2337import org .eclipse .swt .widgets .Display ;
2438import org .eclipse .swt .widgets .Shell ;
2539import org .eclipse .swt .widgets .Widget ;
40+ import org .w3c .dom .Element ;
2641
2742/**
28- * CSS SWT Engine implementation which configure CSSEngineImpl to apply styles
29- * to SWT widgets with static handler strategy.
43+ * CSS SWT Engine. Configures {@link CSSEngineImpl} with the SWT-specific
44+ * value converters and the registry-driven element + property handler
45+ * providers, and applies styles to SWT widgets.
3046 */
31- public class CSSSWTEngineImpl extends AbstractCSSSWTEngineImpl {
47+ public class CSSSWTEngineImpl extends CSSEngineImpl {
3248
33- private DisposeListener disposeListener ;
49+ protected Display display ;
50+
51+ private final DisposeListener disposeListener = e -> handleWidgetDisposed (e .widget );
3452
3553 public CSSSWTEngineImpl (Display display ) {
36- super (display );
37- init ();
54+ this (display , false );
3855 }
3956
4057 public CSSSWTEngineImpl (Display display , boolean lazyApplyingStyles ) {
41- super (display , lazyApplyingStyles );
42- init ();
43- }
58+ this .display = display ;
4459
45- private void init () {
46- disposeListener = e -> handleWidgetDisposed (e .widget );
60+ registerCSSValueConverter (CSSValueSWTRGBConverterImpl .INSTANCE );
61+ registerCSSValueConverter (CSSValueSWTColorConverterImpl .INSTANCE );
62+ registerCSSValueConverter (CSSValueSWTGradientConverterImpl .INSTANCE );
63+ registerCSSValueConverter (CSSValueSWTCursorConverterImpl .INSTANCE );
64+ registerCSSValueConverter (CSSValueSWTFontConverterImpl .INSTANCE );
65+ registerCSSValueConverter (CSSValueSWTFontDataConverterImpl .INSTANCE );
66+ registerCSSValueConverter (CSSValueSWTImageConverterImpl .INSTANCE );
67+
68+ if (lazyApplyingStyles ) {
69+ new CSSSWTApplyStylesListener (display , this );
70+ }
71+
72+ setElementProvider (new RegistryCSSElementProvider (RegistryFactory .getRegistry ()));
73+ propertyHandlerProviders .add (new RegistryCSSPropertyHandlerProvider (RegistryFactory .getRegistry ()));
74+
75+ setResourceRegistryKeyFactory (new SWTResourceRegistryKeyFactory ());
4776 }
4877
4978 @ Override
@@ -54,13 +83,58 @@ protected void hookNativeWidget(Object widget) {
5483 }
5584
5685 @ Override
57- protected void initializeCSSPropertyHandlers () {
58- propertyHandlerProviders .add (new RegistryCSSPropertyHandlerProvider (RegistryFactory .getRegistry ()));
86+ public IResourcesRegistry getResourcesRegistry () {
87+ IResourcesRegistry resourcesRegistry = super .getResourcesRegistry ();
88+ if (resourcesRegistry == null ) {
89+ super .setResourcesRegistry (new SWTResourcesRegistry (display ));
90+ }
91+ return super .getResourcesRegistry ();
5992 }
6093
6194 @ Override
62- protected void initializeCSSElementProvider () {
63- setElementProvider (new RegistryCSSElementProvider (RegistryFactory .getRegistry ()));
95+ public Element getElement (Object element ) {
96+ if (element instanceof CSSStylableElement
97+ && ((CSSStylableElement ) element ).getNativeWidget () instanceof Widget ) {
98+ return (CSSStylableElement ) element ;
99+ } else if (element instanceof Widget ) {
100+ if (isStylable ((Widget ) element )) {
101+ return super .getElement (element );
102+ }
103+ } else {
104+ // FIXME: we need to pass through the ThemeElementDefinitions;
105+ // perhaps they should be handled by a separate engine
106+ return super .getElement (element );
107+ }
108+ return null ;
109+ }
110+
111+ /**
112+ * Return true if the given widget can be styled.
113+ */
114+ protected boolean isStylable (Widget widget ) {
115+ return !widget .isDisposed ()
116+ && !Boolean .TRUE .equals (widget .getData ("org.eclipse.e4.ui.css.disabled" )); //$NON-NLS-1$
117+ }
118+
119+ @ Override
120+ public void reset () {
121+ for (CSSElementContext elementContext : getElementsContext ().values ()) {
122+ Element element = elementContext .getElement ();
123+ if (element instanceof WidgetElement
124+ && isApplicableToReset ((WidgetElement ) element )) {
125+ ((WidgetElement ) element ).reset ();
126+ }
127+ }
128+
129+ getResourcesRegistry ().dispose ();
130+ super .reset ();
131+ }
132+
133+ private boolean isApplicableToReset (WidgetElement element ) {
134+ if (element .getNativeWidget () instanceof Widget ) {
135+ return !((Widget ) element .getNativeWidget ()).isDisposed ();
136+ }
137+ return false ;
64138 }
65139
66140 @ Override
@@ -78,5 +152,4 @@ public void reapply() {
78152 }
79153 }
80154 }
81-
82155}
0 commit comments