My favorites | Sign in
Google
                
Search
for
Updated Dec 16 (5 days ago) by bobv@google.com
ClientBundle  
Compile-time resource optimizations

Documentation for the GWT 2.0 release of ClientBundle can be found on the GWT Developer's Guide website.

Introduction

The resources in a deployed GWT application can be roughly categorized into resources to never cache .nocache.js, to cache forever .cache.html, and everything else myapp.css. The ClientBundle interface moves entries in the everything-else category into the cache-forever category.

Goals

Non-Goals

Examples

To use the ClientBundle, add an inherits tag to your gwt.xml file:

<inherits name="com.google.gwt.resources.Resources" />

Interfaces:

public interface MyResources extends ClientBundle {
  public static final MyResources INSTANCE =  GWT.create(MyResources.class);

  @Source("my.css")
  public CssResource css();

  @Source("config.xml")
  public TextResource initialConfiguration();

  @Source("manual.pdf")
  public DataResource ownersManual();
}

You can then say:

  Window.alert(MyResources.INSTANCE.css().getText());
  new Frame(MyResources.INSTANCE.ownersManual().getURL());

I18N

ClientBundle is compatible with GWT's I18N module.

Suppose you defined a resource:

@Source("default.txt")
public TextResource defaultText();

For each possible value of the locale deferred-binding property, the ClientBundle generator will look for variations of the specified filename in a manner similar to that of Java's ResourceBundle.

Suppose the locale were set to fr_FR. The generator would look for files in the following order:

  1. default_fr_FR.txt
  2. default_fr.txt
  3. default.txt

This will work equally well with all resource types, which can allow you to provide localized versions of other resources, say ownersManual_en.pdf versus ownersManual_fr.pdf.

Pluggable Resource Generation

Each subtype of ResourcePrototype must define a @ResourceGenerator annotation whose value is a concrete Java class that extends ResourceGenerator. The instance of the ResourceGenerator is responsible for accumulation (or bundling) of incoming resource data as well as a small degree of code generation to assemble the concrete implementation of the ClientBundle class. Implementors of ResourceGenerator subclasses can expect that only one ResourceGenerator will be created for a given type of resource within an ClientBundle interface.

The methods on a ResourceGenerator are called in the following order

  1. init to provide the ResourceGenerator with a ResourceContext
  2. prepare is called for each JMethod the ResourceGenerator is expected to handle
  3. createFields allows the ResourceGenerator to add code at the class level
  4. createAssignment is called for each JMethod. The generated code should be suitable for use as the right-hand side of an assignment expression.
  5. finish is called after all assignments should have been written.

ResourceGenerators are expected to make use of the ResourceGeneratorUtil class.

Potential pitfalls

Levers and knobs

Resource types

Migrating from ImmutableResourceBundle

Plan

Changes


Comment by Eljapi, Sep 08, 2009

When i try to inherit com.google.gwt.resources.Resources, i get:

ERROR? Unable to find 'com/google/gwt/resources/Resources.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?

So i guess im missing a jar of some sort. Where is the Resources module jar located? and where can i download it?

Comment by brandonjlutz, Sep 30, 2009

That's because you're not using GWT 2. The Resources module is part of GWT 2 and is located as expected at com/google/gwt/resources.

Comment by chandu2708, Oct 05, 2009

But the download area dosent have a link to GWT2 its pointing to GWT1.7

Comment by hong.zhu.hzhu, Oct 19, 2009

I am having the same problem. any resolution to this?

Thank you

Comment by rj...@google.com, Oct 19, 2009

GWT 2 isn't done yet. There is a preview release in the downloads area, 2.0-ms1, but you'll note that it's marked deprecated to as a warning that it is not intended for production use, and that its API will certainly shift. Or you can build from svn trunk, keeping the same disclaimers in mind.

Comment by hong.zhu.hzhu, Oct 19, 2009

Thank you! One follow-up question: Can I only use the ClientBundle with GWT1.7.1?

Comment by rj...@google.com, Oct 19, 2009

No. You can use the ClientBundle's predecessor, the incubator's ImmutableResourceBundle?, with pre-2.0 builds. ClientBundle is part of GWT 2.0.

http://code.google.com/p/google-web-toolkit-incubator/

Comment by nenchev.mariyan, Nov 19, 2009

Hi, is there any example how to use ClientBundle for images that need to be repeated (repreat-x for example) as background. At the moment i do it with css like this: .bg-repeat {

background-image: url("client/customButtonMiddleImage.gif"); background-repeat: repeat-x;
}

But with this i am not using ClientBundle and i have many images that must be used this way, so my UI performance will decrease. Please give me example how to do it? This ImageOption? repeat seems to stretch the image not repeat it, but i can't do it even with it.

Comment by shahidzaman, Nov 20, 2009

I am trying to use the ClientBundle. I have got a inherit for Resources in my gwt.xml. I have a public folder at the same level as my gwt.xml and which contains an images folder. Finally I have an interface extending the ClientBundle as follows:

public interface AppBundle extends ClientBundle {
	
	public static final AppBundle INSTANCE = GWT.create(AppBundle.class);

	@Source("com/tech/keswick/web/public/images/organisnew.gif")
	public ImageResource add_org();

but it is showing error Resource file organisnew.gif is missing (expected at com/tech/keswick/web/public/images/organisnew.gif)

I used this with ms1 and it did not use to complain.

Comment by nenchev.mariyan, Nov 21, 2009

May be you should place the resources in some package not in the public folder.

Comment by a.revolution.ultra.blue, Dec 19 (2 days ago)

@nenchev You want to add the following annotation to your x/y repeating images @ImageOptions?(repeatStyle=RepeatStyle?.Horizontal) This will generate up to three bundled images, one for vertical repeat, one for horizontal repeat, and the last for static sizing. Of course, unless you disable inlining, this will only be used for non-data URI IE compiles.

@shahidzaman, Nenchev is correct. Source files for bundles must be in client-accessible packages. If you want to use the bundle, but you also want to copy the images to /war for deployment purposes {redundant and I don't like it, but that doesn't mean you don't have a good reason to do so}, you can use a second .gwt.xml file which sets the client source path to public, so files in that folder will be source files, AND copied to war.

I have stuff like `<source path="shell"/> <inherits name="x.y"/> <source path="client"/>` in most of my modules. x.y is where I put RPC classes to shorten the Strings sent over the wire. You could do the same, just change shell to public, and in the middle, you have to inherit something, preferably something that sets source path="client" at the top of the module, or else you might get weird errors. com.google.gwt.user.User is a good one.

Comment by kondalar9, Yesterday (31 hours ago)

Not able to re-size the image with ClientBundle. Is there any way to achieve this?


Sign in to add a comment