Search code examples
javagwtenumsuibinder

Is it possible to reference enums in a GWT UiBinder context


I have an enumeration that looks like the following

public enum MyEnum {
    A,
    B;
}

And then I have a UiBinder file with a custom component that has a setter and getter expecting the enum above. (I've stripped the extra code for

<ui:UiBinder ....>
    <g:HTMLPanel>
        ....
        <myNamespace:myComponent myAttribute="" />
        ....
    </g:HTMLPanel>
</ui:UiBinder>

Can I reference my enum and put that value into myAttribute in any way? What I want to accomplish is something like this

<ui:UiBinder ....>
    <ui:with field="myEnumField" type="com.example.MyEnum" />
    <g:HTMLPanel>
        ....
        <myNamespace:myComponent myAttribute="{myEnumField.A}" />
        ....
    </g:HTMLPanel>
</ui:UiBinder>

However it would seem that I cannot do this with ui:with. Can I do this in any way at all?


Solution

  • It is possible. You should be able to pass the enum to the attribute directly like,

    <ui:UiBinder ....>
        <g:HTMLPanel>
            ....
            <myNamespace:myComponent myAttribute="A" />
            ....
        </g:HTMLPanel>
    </ui:UiBinder>
    

    The MyComponent widget should have a uiConstructor that accepts MyEnum type.

    @UiConstructor
    public MyComponent(MyEnum myAttribute){
    }