Using <ui:msg> inside of a @UiChild

268 views
Skip to first unread message

Tiago Rinck Caveden

unread,
Jan 2, 2012, 5:58:51 AM1/2/12
to google-we...@googlegroups.com
Hello all,

I have a widget is sort of a custom implementation of ValueBoxEditorDecorator.
This widget has a @UiChild method which receives a String for the error message:

@UiChild(limit=1, tagname="errorMessage")
public void setErrorMessage(String errorMessage) {
   this.errorMessage = errorMessage;
}

So, in my ui.xml file, I'd like to use a <ui:msg> tag to set the error message, so that it could eventually be translated.
Obviously, it is not working.

But I can't find a simple tutorial explaining how to make your custom widget accept the result of <ui:msg>. I've inspected the code of classes like Label or Button which frequently have <ui:msg> tags inside, but I can't figure out how they do it. Is it the wrap(Element) static method? I don't want to wrap anything, actually errorMessage isn't the only child of my widget...

Does anyone has any tip on how could I implement this?

Thanks,
--
Tiago Rinck Caveden

Thomas Broyer

unread,
Jan 2, 2012, 6:35:24 AM1/2/12
to google-we...@googlegroups.com
Label implements HasText, so it can contain text that is then passed to setText (as if you used a text="" attribute on the element).
Button implements HasHTML, so it can contain HTML that is then passed to setHTML.
Both cases are handled by specialized ElementParsers (HasTextParser and HasHTMLParser) that make use of "interpreters" (TextInterpreter and HtmlInterpreter respectively) that in turn defer to some other interpreters (TextPlaceholderInterpreter and UiTextInterpreter for TextInterpreter, HtmlInterpreter adds UiSafeHtmlInterpreter and various interpreters working on attribute values –to resolve {} references–)
@UiChild are handled by the UiChildParser.

Now, back to your question: you cannot use @UiChild with a String, it must be either a DOM element (a subclass of com.google.gwt.dom.client.Element; the type of the declared attribute is matched to the type corresponding to the element used in the ui.xml by looking at the @TagName annotations on the Element subclasses) or an "imported class" (i.e. you used an xmlns:XXX="urn:import:the.package" and then <XXX:TheClass>).
If you want to set a text property from a ui.xml, you have to use an attribute (in your case: errorMessage="the message"); and that means you cannot use <ui:msg>. You can however use the <ui:attribute> to localize the value (it's only documented for "HTML attributes" <http://code.google.com/webtoolkit/doc/latest/DevGuideUiBinderI18n.html#HTML_attributes_that_need_translation> but looking at the code it should also work for widgets), or of course a {} reference to an external Constants or Messages method.
Reply all
Reply to author
Forward
0 new messages