Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

LocalDate presentation in ComboBox

Leonardo Marinho
5 years ago Apr 20, 2016 5:16pm
Olli Tietäväinen
5 years ago Apr 21, 2016 8:51am

Hi,

you can set the captions of individual items by comboBox.setItemCaption(itemId, caption), which might be enough for your needs. If you need the same conversion in many places, one possible solution would be to create a new ConverterFactory for the session, which would look like something like the following:

public class StringToLocalDateConverter implements Converter<String, LocalDate> {

        @Override
        public LocalDate convertToModel(String value, Class<? extends LocalDate> targetType, Locale locale)
                throws com.vaadin.data.util.converter.Converter.ConversionException {
            if(value == null) {
                return null;
            } else{
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                return LocalDate.parse((String) value, formatter);
            }
        }

        @Override
        public String convertToPresentation(LocalDate value, Class<? extends String> targetType, Locale locale)
                throws com.vaadin.data.util.converter.Converter.ConversionException {
               if (value == null){
                    return null;
                }
                
                else{
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                    return ((LocalDate) value).format(formatter);
                }
        }

        @Override
        public Class<LocalDate> getModelType() {
            return LocalDate.class;
        }

        @Override
        public Class<String> getPresentationType() {
            return String.class;
        }
        
    }
    
    public class MyConverterFactory extends DefaultConverterFactory {
        @Override
        protected <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> findConverter(
                Class<PRESENTATION> presentationType, Class<MODEL> modelType) {
            // Your specific localDate converter
            if (presentationType == String.class && modelType == LocalDate.class) {
                return (Converter<PRESENTATION, MODEL>) new StringToLocalDateConverter();
            }
            // Fall back to default in other cases
            return super.findConverter(presentationType, modelType);
        }
    }

    private void createComboBox(VerticalLayout layout) {
        VaadinSession.getCurrent().setConverterFactory(new MyConverterFactory());
        LocalDate ld = LocalDate.now();
        LocalDate ld2 = ld.plusDays(3);
        List<LocalDate> list = new ArrayList<>();
        list.add(ld);
        list.add(ld2);
        BeanItemContainer<LocalDate> bic = new BeanItemContainer<>(LocalDate.class, list);
        ComboBox cb = new ComboBox();
        cb.setContainerDataSource(bic);
        layout.addComponent(cb);
    }
Leonardo Marinho
5 years ago Apr 25, 2016 12:27pm
Matti Tahvonen
5 years ago Apr 27, 2016 2:44pm

I'd suggest to throw in Viritin add-on and using it's TypedSelect component. It just makes things just so much easier:

https://github.com/viritin/viritin/blob/master/src/test/java/org/vaadin/viritin/it/examples/LocalDateSelector.java

cheers,
matti