GWT - What's the shortest way of simply sharing strings and number constants between Java code and UiBinder files?
Asked Answered
W

1

5

Can someone post an example of the shortest way of sharing a (preferably static final) string or number constant between Java code and UiBinder XML, where I can use the constant either in an attribute:

<g:TextArea width="...px"/>

or in a value:

<g:Label>...</g:Label>

I can't seem to find an example of this, only text from a file, which I don't want.

Wasp answered 8/2, 2013 at 17:49 Comment(1)
Ui binder mechanism is not designed to work like that. You define there a markup and in the widget code you can set additional attributes to your dynamic components.Pointdevice
O
10

static fields (and enum constants) can be used with a simple <ui:import>:

<ui:import field="com.example.Pojo.CONSTANT" />

or

<ui:import field="com.example.Pojo.*" />

and then simply:

<g:Label text="{CONSTANT}" />

or

<g:Label><ui:text from="{CONSTANT}"/></g:Label>

See https://code.google.com/p/google-web-toolkit/source/browse/trunk/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.ui.xml#87 for an example.

Octet answered 9/2, 2013 at 11:51 Comment(5)
Thanks! It currently only works with <ui:import.... Is there a plan to merge that with ui:with?Wasp
Thomas, is it efficient even if I have a big class which contains the constants? Does it only import the static final constants and nothing else (hopefully)? Is it limited only to primitives and Strings?Wasp
Re. ui:import, ah, oops, my bad! I'll update the answer. Otherwise, the generator actually creates a field in the generated class, one per static field in the imported class, initialized with the fully-qualified name of the constant; so it shouldn't really matter whether you use the wildcard or import each needed field.Octet
Thomas, if I use *, does it import non-final static fields too, and does it ignore any non-String, non-primitive static members of that class?Wasp
A quick look at the code tells me it doesn't check whether the field is final or not, and the field's type doesn't matter. Try it for yourself, and look at the generated code (hint: pass -gen someFolder to the Compiler or DevMode)Octet

© 2022 - 2024 — McMap. All rights reserved.