Search code examples
javagwtuibinder

Single CSS stylesheet across many UiBinders in different packages


I have a GWT 2.4 application which has many UiBinders within different client packages, set out the same as below:

  • com.xxx

    • project.gwt.xml
  • com.xxx.client

    • entrypoint.java
    • stylesheet.css
  • com.xxx.client.package1

    • Page1.java
    • Page1.ui.xml
    • stylesheet.css
  • com.xxx.client.package2

    • Page2.java
    • Page2.ui.xml
    • stylesheet.css
  • com.xxx.client.package3

    • Page3.java
    • Page3.ui.xml
  • war

    • index.html
    • global_stylesheet.css

In my ui.xml files I am having to reference the stylesheet.css file which is within the same package as the UiBinder. I've been trying to reference the global_stylesheet.css file but am having no luck. Ideally I would like to be able to still use the {style.xxx} method of applying a style.


Solution

  • You can use a central ClientBundle with a CssResource for @Source("global_stylesheet.css"), and reference it from your UiBinder templates:

    <ui:with type="mypackage.client.MyClientBundle" field="myClientBundle"/>
    

    So you can use it like:

    <g:Label addStyleNames="{myClientBundle.myCssResource.example}"/>
    

    But you would have to put global_stylesheet.css somewhere in your classpath - otherwise it cannot possibly be compiled.

    Also, don't forget to call ensureInjected() on your CssResource.