Search code examples
gwtuibinder

Dynamic String in UiBinder (not for i18n)


I'm trying to make a Widget that generalizes a bootstrap style from twitter for collapsible items.

I got to make it work hardcoding it, but I find some dificulties abstracting it.

The widget looks like:

<div class="accordion" id="accordion1">
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapseOne">
        ... text to show collapsed ...
      </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse">
      <div class="accordion-inner">
        ... anything to show expanded ...
      </div>
    </div>
  </div>
</div>

The thing is, this uses a javascript which depends on ids of some div tags. The generalized widget would need this to be randomized or depending on some seed passed in the constructor.

It would also be nice to have access to those generated Strings from Java part, as it would be a fancy way to set the text showing in the widget.

My first approach was to use something like <ui:with type="com.a.b.c.IdGenerator" field="idGenerator"></ui:with> but it seems to instanciate dynamically the class IdGenerator so no access to the Strings is obtained in Java part.

Is there any fancy way to dinamically generate those Strings having access to them from the Java part?


Solution

  • You simply need a @UiField IdGenerator idGenerator on the Java side to have the instance created by the <ui:with> injected in it (or you can @UiField(provided = true) it).