Search in sources :

Example 1 with OSGiResourceLocator

use of org.eclipse.e4.ui.css.core.util.impl.resources.OSGiResourceLocator in project eclipse.platform.ui by eclipse-platform.

the class PartRenderingEngine method initializeStyling.

public static void initializeStyling(Display display, IEclipseContext appContext) {
    String cssTheme = (String) appContext.get(E4Application.THEME_ID);
    String cssURI = (String) appContext.get(IWorkbench.CSS_URI_ARG);
    IScopeContext[] contexts = new IScopeContext[] { DefaultScope.INSTANCE, InstanceScope.INSTANCE };
    boolean enableThemePreference = Platform.getPreferencesService().getBoolean("org.eclipse.e4.ui.workbench.renderers.swt", ENABLED_THEME_KEY, true, contexts);
    if ("none".equals(cssTheme) || (!enableThemePreference)) {
        appContext.set(IStylingEngine.class, new IStylingEngine() {

            @Override
            public void setClassname(Object widget, String classname) {
            }

            @Override
            public void setId(Object widget, String id) {
            }

            @Override
            public void style(Object widget) {
            }

            @Override
            public CSSStyleDeclaration getStyle(Object widget) {
                return null;
            }

            @Override
            public void setClassnameAndId(Object widget, String classname, String id) {
            }
        });
    } else if (cssTheme != null) {
        final IThemeEngine themeEngine = createThemeEngine(display, appContext);
        String cssResourcesURI = (String) appContext.get(IWorkbench.CSS_RESOURCE_URI_ARG);
        // Create the OSGi resource locator
        if (cssResourcesURI != null) {
            // TODO: Should this be set through an extension as well?
            themeEngine.registerResourceLocator(new OSGiResourceLocator(cssResourcesURI));
        }
        appContext.set(IStylingEngine.class, new IStylingEngine() {

            @Override
            public void setClassname(Object widget, String classname) {
                WidgetElement.setCSSClass((Widget) widget, classname);
                themeEngine.applyStyles(widget, true);
            }

            @Override
            public void setId(Object widget, String id) {
                WidgetElement.setID((Widget) widget, id);
                themeEngine.applyStyles(widget, true);
            }

            @Override
            public void style(Object widget) {
                themeEngine.applyStyles(widget, true);
            }

            @Override
            public CSSStyleDeclaration getStyle(Object widget) {
                return themeEngine.getStyle(widget);
            }

            @Override
            public void setClassnameAndId(Object widget, String classname, String id) {
                WidgetElement.setCSSClass((Widget) widget, classname);
                WidgetElement.setID((Widget) widget, id);
                themeEngine.applyStyles(widget, true);
            }
        });
        setCSSTheme(display, themeEngine, cssTheme);
    } else if (cssURI != null) {
        String cssResourcesURI = (String) appContext.get(IWorkbench.CSS_RESOURCE_URI_ARG);
        final CSSSWTEngineImpl cssEngine = new CSSSWTEngineImpl(display, true);
        WidgetElement.setEngine(display, cssEngine);
        if (cssResourcesURI != null) {
            cssEngine.getResourcesLocatorManager().registerResourceLocator(new OSGiResourceLocator(cssResourcesURI));
        }
        // FIXME: is this needed?
        // $NON-NLS-1$
        display.setData("org.eclipse.e4.ui.css.context", appContext);
        appContext.set(IStylingEngine.class, new IStylingEngine() {

            @Override
            public void setClassname(Object widget, String classname) {
                WidgetElement.setCSSClass((Widget) widget, classname);
                cssEngine.applyStyles(widget, true);
            }

            @Override
            public void setId(Object widget, String id) {
                WidgetElement.setID((Widget) widget, id);
                cssEngine.applyStyles(widget, true);
            }

            @Override
            public void style(Object widget) {
                cssEngine.applyStyles(widget, true);
            }

            @Override
            public CSSStyleDeclaration getStyle(Object widget) {
                Element e = cssEngine.getCSSElementContext(widget).getElement();
                if (e == null) {
                    return null;
                }
                return cssEngine.getViewCSS().getComputedStyle(e, null);
            }

            @Override
            public void setClassnameAndId(Object widget, String classname, String id) {
                WidgetElement.setCSSClass((Widget) widget, classname);
                WidgetElement.setID((Widget) widget, id);
                cssEngine.applyStyles(widget, true);
            }
        });
        URL url;
        try {
            url = FileLocator.resolve(new URL(cssURI));
            try (InputStream stream = url.openStream()) {
                cssEngine.parseStyleSheet(stream);
            }
        } catch (IOException e) {
            Activator.log(LogService.LOG_ERROR, e.getMessage(), e);
        }
        Shell[] shells = display.getShells();
        for (Shell s : shells) {
            try {
                s.setRedraw(false);
                s.reskin(SWT.ALL);
                cssEngine.applyStyles(s, true);
            } catch (Exception e) {
                Activator.log(LogService.LOG_ERROR, e.getMessage(), e);
            } finally {
                s.setRedraw(true);
            }
        }
    }
    CSSRenderingUtils cssUtils = ContextInjectionFactory.make(CSSRenderingUtils.class, appContext);
    appContext.set(CSSRenderingUtils.class, cssUtils);
}
Also used : InputStream(java.io.InputStream) MApplicationElement(org.eclipse.e4.ui.model.application.MApplicationElement) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) WidgetElement(org.eclipse.e4.ui.css.swt.dom.WidgetElement) Element(org.w3c.dom.Element) Widget(org.eclipse.swt.widgets.Widget) OSGiResourceLocator(org.eclipse.e4.ui.css.core.util.impl.resources.OSGiResourceLocator) IOException(java.io.IOException) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) URL(java.net.URL) IOException(java.io.IOException) InjectionException(org.eclipse.e4.core.di.InjectionException) InvalidRegistryObjectException(org.eclipse.core.runtime.InvalidRegistryObjectException) CSSSWTEngineImpl(org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl) IScopeContext(org.eclipse.core.runtime.preferences.IScopeContext) Shell(org.eclipse.swt.widgets.Shell) IThemeEngine(org.eclipse.e4.ui.css.swt.theme.IThemeEngine) IStylingEngine(org.eclipse.e4.ui.services.IStylingEngine) TestableObject(org.eclipse.ui.testing.TestableObject) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 InvalidRegistryObjectException (org.eclipse.core.runtime.InvalidRegistryObjectException)1 IScopeContext (org.eclipse.core.runtime.preferences.IScopeContext)1 InjectionException (org.eclipse.e4.core.di.InjectionException)1 OSGiResourceLocator (org.eclipse.e4.ui.css.core.util.impl.resources.OSGiResourceLocator)1 WidgetElement (org.eclipse.e4.ui.css.swt.dom.WidgetElement)1 CSSSWTEngineImpl (org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl)1 IThemeEngine (org.eclipse.e4.ui.css.swt.theme.IThemeEngine)1 MApplicationElement (org.eclipse.e4.ui.model.application.MApplicationElement)1 MUIElement (org.eclipse.e4.ui.model.application.ui.MUIElement)1 IStylingEngine (org.eclipse.e4.ui.services.IStylingEngine)1 EObject (org.eclipse.emf.ecore.EObject)1 Shell (org.eclipse.swt.widgets.Shell)1 Widget (org.eclipse.swt.widgets.Widget)1 TestableObject (org.eclipse.ui.testing.TestableObject)1 Element (org.w3c.dom.Element)1 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)1