Can I use enum values as field values inside UiBinder template?
Asked Answered
L

2

10

Can I use enum values as field values inside UiBinder template ? I'm using GWT 2.4

Something like this

<ui:with field="en" type="com.mine.courierApp.shared.PayerType" />

looks promising, where

public enum PayerType
{
    Sender,
    Recipient
}

but I can't refer to values of the enum by en.Sender.

Is it even possible ?

Lien answered 29/2, 2012 at 2:12 Comment(0)
I
13
<ui:import field='com.mine.courierApp.shared.PayerType.Sender' />

or

<ui:import field='com.mine.courierApp.shared.PayerType.*' />

And then you can use it as payerType='{Sender}'.

But UiBinder should automatically try to translate enum constant names into values, so the following should work without any need for a ui:with:

<my:MyWidget payerType='Sender' />

If the MyWidget widget has a public void setPayerType(PayerType type) method, UiBinder should look for an enum value named Sender (from the *.ui.xml file) in the PayerType enum (from the method's argument type).

Isochronism answered 29/2, 2012 at 11:43 Comment(5)
Unfortunately it fails in runtime saying: ERROR: Returns class com.mine.courierApp.shared.PayerType, can't be used as class java.lang.StringLien
Thomas, can I ask you also take a look at this question? You know a lot about GWT internals.Lien
Sorry, that should be ui:import instead of ui:with. I just checked in the unit-tests for the feature (and you can have a look at the EnumeratedLabel there for an example of automatically translating a string into an enum value.Isochronism
Thanks a lot! There is important note for those who stumble upon this. You have to add .name to your enum so it's converted to String if necessary. I'll modify answer to reflect that.Lien
Watch out if you enable Enum ellison in the compiler.Contraceptive
D
0

If you don't ui:import the enum class like this:

<ui:import field='com.mine.courierApp.shared.PayerType.*' />

then you don't get content-assist, which the whole point of this in the first place.

But then you run into another issue...

Although you can simple type { ctrl-space } to get a popup menu of the enum value, if you are using, say, bootstrap3, there are various enums that each have their own "DEFAULT" value. The ui template editor will complain about that; i.e. if you start making extensive use of this content-assist feature, you will need to ensure the imported enums have unique value names.

Delative answered 21/5, 2014 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.