use GwtCreateResource to provide text programatically

153 views Asked by At

I would like my uiBinder to use a ClientBundle which will provide some runtime customized labels. Kind of a TextResource but not from a text file !
I tried with GwtCreateResource but from the DevGuide it seems like it's not possible. Am I right ? (create() and name() are the only methods available) What I would like to achieve is something like this:

client bundle:

public interface MyWidgetResources extends ClientBundle {
    GwtCreateResource<WidgetLabels> labels();

    @Source("lol.css")
    CssResource style();
}

labels class:

public final class MyWidgetLabels {
    public String title() {
        return load("mywidget-title");
    }

    public String banner() {
        return load("mywidget-banner");
    }

    private String load(String key) {
        // load from external..
    }
}

uiBinder:

<ui:with type="com.package.MyWidgetResources" field="res"/>

<gwt:SimplePanel>
    <gwt:Label text="{res.labels.title}"></gwt:Label>
    <gwt:Label text="{res.labels.banner}"></gwt:Label>
</gwt:SimplePanel>

My code looks like this already but res.label.title does not work because GwtCreateResource can only serve as class instantiator (res.labels.create().title()).

Is there a solution for me ? Maybe with a custom ResourceGenerator ?

1

There are 1 answers

2
Colin Alworth On

As long as MyWidgetLabels can be created by GWT.create, you can put anything you want into that type, and you can make it behave however you'd like. You will need the create reference in your uibinder as you suggested at the end of the post to actually build the object, so your lines will look about like this:

<gwt:Label text="{res.labels.create.title}"></gwt:Label>

Each . separated piece (except the first, which is a ui:field/@UiField) is a no-arg method to be called - you declared labels() in MyWidgetResources, create() already existed in GwtCreateResource, and you created title() in your own MyWidgetLabels type.

Since that first piece is a ui:field/@UiField, you could have another that references res.labels.create as something like labels so that later you could instead say:

<gwt:Label text="{labels.title}"></gwt:Label>

Finally, yes, you could build your own ResourceGenerator which would enable you to do whatever you wanted to emit the type in question, as long as you extended the ResourcePrototype type and had a getName() method.