Reputation: 533
I'm using combo box and I've set container data source which is of AttributeDisplayType
class type.
typeBeanItemContainer = new MyBeanItemContainer<AttributeDisplayType>(AttributeDisplayType.class);
typeComboBox.setContainerDataSource(typeBeanItemContainer);
beanFieldGroup.bind(typeComboBox, propertyName(beanFieldGroup.proxy().getClientAttribute().getAttributeDisplayType()));
But I want to localize values displayed in combo box. I don't want to create wrapper around AttributeDisplayType
and use it to as a container data source.
Is there way by which I can use setItemCaptionPropertyId
and use some utility method to do localization of values displayed in combo box, something like below
typeComboBox.setItemCaptionPropertyId(getLocalizedText(propertyName(Lambda.on(AttributeDisplayType.class).getDisplayTypeName().toLowerCase())));
or
typeComboBox.setItemCaptionPropertyId(getLocalizedText(propertyName(Lambda.on(AttributeDisplayType.class).getDisplayTypeName().toLowerCase())));
public String getLocalizedText(String displayTypeName) {
return resouceBundle.getKey(displayTypeName);
}
I can't add my field or method in existing AttributeDisplayType
as resource bundle is not available for that class.
Is it possible or do I've to create wrapper and use it's method/field
as setItemCaptionPropertyId
?
Upvotes: 0
Views: 482
Reputation: 1934
Add Viritin add-on to your project. It's TypedSelect component (which allows to use ComboBox as the underlaying implementation), contains similar API as in the example by Archana Mundane, but called just CaptionGenerator and better typed. Usage example with it:
// Better typed alternative for Vaadin select
final TypedSelect<Person> typedSelect = new TypedSelect<Person>();
// Uses "NativeSelect" as impl. by default, but all core select types are
// supported. E.g.: withSelectType(ListSelect.class);
// Typed API for setOptions makes its usage easier to figure out where are we
// selecting from. Note that its "fluent" so you can chain it directly into
// constrcutor as well.
typedSelect.setOptions(options);
// for most objects you want to customize the caption text. Again, you
// can use chaind invocation and you'll use lambdas in you java 8 project.
typedSelect.setCaptionGenerator(new CaptionGenerator<Person>() {
@Override
public String getCaption(Person option) {
return option.getFirstName() + " " + option.getLastName();
}
});
Upvotes: 0
Reputation: 533
typeComboBox.setItemCaptionGenerator(new ItemCaptionGenerator() {
@Override
public String getItemCaption(AbstractSelect source, Object itemId) {
AttributeDisplayType type = (AttributeDisplayType) itemId;
return getText(type.getDisplayTypeName().toLowerCase());
}
});
getText is method which does localization, solutions was so simple and not sure why I was thinking in difficult ways. Thanks to my colleague.
Upvotes: 2
Reputation: 4385
The most simple solution would be to use a GeneratedPropertyContainer.
You could even think about creating a general localized container. Maybe similar to this (not a full implementation, but it gives you the idea):
public class LocalizedContainer extends GeneratedPropertyContainer {
public LocalizedContainer(Indexed container) {
super(container);
Collection<?> containerPropertyIds = container.getContainerPropertyIds();
for (final Object containerPropertyId : containerPropertyIds) {
if (containerPropertyId instanceof String) {
String localizedPropertyId = containerPropertyId + ".localized";
addGeneratedProperty(localizedPropertyId, new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId, Object propertyId) {
Object value = item.getItemProperty(containerPropertyId).getValue();
return getLocalizedValue(value);
}
@Override
public Class<String> getType() {
return String.class;
}
});
}
}
}
private String getLocalizedValue(Object value) {
// Do localization here
return String.valueOf(value);
}
}
And then you could use setItemCaptionPropertyId("yourpropertyid.localized")
on the ComboBox
and wrap your container inside the LocalizedContainer
.
Upvotes: 0