Search code examples
gwtuibinder

GWT calling UiConstructor with array argument from uibinder


I have the following UiConstructor which takes one array argument, an ImageResource array

@UiField ToggleButton leftToggleButton;

public @UiConstructor ToggleButtonGroup(ImageResource[] faceImages) {
    initWidget(uiBinder.createAndBindUi(this));

    leftToggleButton.getUpFace().setImage(new Image(faceImages[0]));
    leftToggleButton.getDownFace().setImage(new Image(faceImages[1]));
}

I have no idea about the ui.xml notation for calling such constructor, because it is an array

If i reduce the type to only ImageResource, i can successfully use the following in my ui.xml file

<ui:image field="SelectInverseImage" src="bundle/SelectInv.png" />
...
<mywidget:ToggleButtonGroup faceImages="{SelectInverseImage}"/>

However, when i try

<ui:image field="SelectImage" src="bundle/Select.png" />
<ui:image field="SelectInverseImage" src="bundle/SelectInv.png" />
<mywidget:ToggleButtonGroup faceImages="{SelectInverseImage}{SelectImage}"/>

when i use the array type ImageResource[], i end up with the following error

[ERROR] [roza] Cannot parse value: "{SelectInverseImage}{SelectImage}" as type ImageResource[]

Any tips?

PS: I am not interested in the part of setting the upFace and downFace of a button, this is only here for demonstration.. The main topic is defining ui.xml notation for setting constructor's array-type attributes


Solution

  • ui:with is your friend.

    Have a method

    ImageResource[] imgRsrcs() {
      return array here ...
    }
    

    use ui:with to bind a imgRsrcs method to the template.

    Then use imgRsrcs as the argument, presuming that imgRsrcs() is found inside the class zzz.Main

    <ui:with field='main' type='zzz.Main'/>
    
    <mywidget:ToggleButtonGroup faceImages="{main.imgRsrcs}"/>
    

    Read about ui:with at https://developers.google.com/web-toolkit/doc/latest/DevGuideUiBinder.