Access GWT UiBinder CSS Constants

Using GWT 2.1, I am trying to create a CSS file that contains many constants and common styles. I would like to use the ui: style tag to include it in the UiBinder template:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' xmlns:g='urn:import:com.google.gwt.user.client.ui' <ui:style field="css" src="constants.css" /> </ui:UiBinder> 

I can easily use styles for elements:

 <g:FlowPanel styleName="{css.panel}">...</g:FlowPanel> 

But an attempt to use constants in another style block fails:

 <ui:Style> .templateSpecificStyle { background-color: {css.royalBlue}; padding: 1em; } </ui:Style> 

Oddly enough, I am not getting a compilation error. An intricate CSS class is created; however, the contents are empty. Is there a way to access these CSS constants in another style block? Is it possible to use the old ResourceBundle / CssResource template?

+4
source share
2 answers

After reading fooobar.com/questions/646639 / ... again, I see that the constants work if you add a style matching the pattern inside the style block:

 <ui:Style src="constants.css"> .templateSpecificStyle { background-color: royalBlue; padding: 1em; } </ui:Style> 

This is perfect for my needs.

+7
source

It may be in your interest to define these constants in some class, and then use the runtime substitution to include this constant in every CSS resource that you are going to use.

CSSConstants.java

 package com.foo.client; public final class CSSConstants { public static final String ROYAL_BLUE = "#4169E1"; } 

Style Block in UiBinder Template

 <ui:style> @eval royalBlue com.foo.client.ROYAL_BLUE .templateSpecificStyle { background-color: royalBlue } </ui:style> 

Note that even the name of the method is โ€œrun-time substitutionโ€, the GWT compiler will replace royalBlue string literal because royalBlue can be evaluated at compile time.

For more interesting things you can use in CSS resources, see http://code.google.com/webtoolkit/doc/latest/DevGuideClientBundle.html#CssResource

+4
source

All Articles