How to handle combobox captions of type Enum

Another title could be “how to externalize combobox captions of type Enum”, but I’m not focusing on externalising strings (i18n) but trying to find the correct method for showing the item captions.

Let’s go with an example.
I’ve the following Enum:

public enum EnumContactAction {
MAIL ("MAIL"),
PHONE ("PHONE"),
TWITTER ("TWITTER"),

BLANK ("")
;
private String _value;

private EnumContactAction(String value){
this._value = value;
}

public String getValue() {
return this._value;
}

public static String getCaption( EnumContactAction input ) {

switch (input){
case MAIL:
return "Email address";
case PHONE:
return "Telephone number";
case TWITTER:
return "Twitter account";
default:
return "";
}
}
}

As you can see it has a static method getCaption for getting the correct captions, there I should put the externalization stuff using i18n common tools.

I’ve added the Enum into a “new Vaadin 8 Project”.

The changed init method of the MyUi class is:

@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();

// new Vaadin 8 combobox definition
ComboBox<EnumContactAction> cb = new ComboBox<EnumContactAction>();

// just setting the combobox size using some java 8 trick, you can disregards it
cb.setWidth( EnumSet.allOf(EnumContactAction.class).stream()
.map(EnumContactAction::getCaption)
.max(Comparator.comparingInt(String::length))
.get()
.length()+"em");


// get all values from enumerator and put them in combobox
cb.setItems(EnumSet.allOf(EnumContactAction.class));

// combobox item must be selected (BLANK value handle the lack of real value)
cb.setEmptySelectionAllowed(false);

// preset to the blan value in ordr to avoid the 'null' value
cb.setValue(EnumContactAction.BLANK);

// here is where the captions in the combobox are handled
cb.setItemCaptionGenerator(EnumContactAction::getCaption);

Button button = new Button("Click Me");
button.addClickListener(e -> {
layout.addComponent(new Label("Choosen value: '" + EnumContactAction.getCaption(cb.getValue())
+ "', and this is the internal enum value: '" + cb.getValue() + "'"));
});

layout.addComponents(cb, button);

setContent(layout);

This is how I’m using a combobox linked to a enumerator.
It’s working, but am i doing right? Can it be improved?

Can any of the Vaadin/java guru presenti in the forum answer?


Here
is the complete sample.

Currently I’m having trouble rending that enum into a grid, but this is another issue, I’ll write it in another thread.

Hi

Let’s start by saying that I’m not a Vaadin Guru (40% success on last vaadin certification).

I use java bundle to translate enum in to labels. Bundle is configured and loaded by spring with

org.springframework.context.support.ResourceBundleMessageSource

Then I write a method somewhere, better if static, to translate a key in to label with given locale, something like:

Translator.translate(String key, Locale loc) I use Enum name to build labels this way:

public static String getCaption( EnumContactAction input, Locale loc ) { return Translator.translate("my.prefix."+input.name(), loc); } I think you can make getCaption not static.

I hope this can help you.

Thanks Luca (Grazie),
the focus of my post is on Enum and ComboBox, not text externalization (bundle and so on).