My favorites | Sign in
Google
                
Search
for
Updated May 21, 2009 by bobv@google.com
Labels: Type-Library
StyleInjector  
Combines ImmutableResourceBundle with CSS styleshees

StyleInjector and CssResource have been promoted from the incubator into mainline GWT development. The incubator code has been deprecated. Current documentation is located in the main GWT project.

Goal

Details

In your gwt.xml file:

<inherits name="com.google.gwt.libideas.ImmutableResources" />
<inherits name="com.google.gwt.libideas.StyleInjector" />

Define your ImmutableResourceBundle:

public interface Resources extends ImmutableResourceBundle {
  public static final Resources INSTANCE =
      GWT.create(Resources.class);

  @Resource("myBackground.png")
  @ImageOptions(repeatStyle = RepeatStyle.Horizontal)
  public ImageResource backgroundFunction();

  @Resource("myCss.css")
  public CssResource css();
}

The CSS contents (see CssResource for more information):

@sprite .some .selector {
  gwt-image: 'backgroundFunction';
}

In your onModuleLoad:

StyleInjector.injectStylesheet(Resources.INSTANCE.css().getText());

You now have your stylesheet applied to the document while taking advantage of strongly-named or inlined resource URLs. Because the standard I18N-style of resource naming is applied to the ImageResource instance, it is possible to provide localized CSS background images without the need for multiple, per-locale stylesheets. It is now possible to have myBackground_fr.png and myBackground_en.png substituted based on the locale deferred binding property.

It is possible to update the contents of a previously-injected stylesheet using the setContents() method:

StyleElement elt = StyleInjector.injectStylesheet("CSS contents");
StyleInjector.setContents(elt, "New CSS contents");

Comment by jdwyah, Feb 01, 2008

fyi: The module should be <inherits name="com.google.gwt.libideas.StyleInjector" />

Comment by chris.f.jones, Jun 12, 2008

How would one add a background image to css from an ImageBundle? using StyleInjector?

Comment by pcourt, Jul 04, 2008

Not sure of the correct place to post this, but the compiler throws out warnings when you use -moz-??? tags in your CSS.

Comment by mprasetya, Dec 17, 2008

Try to escape the first dash with backspace (\-moz...).


Sign in to add a comment