Search code examples
javacomboboxvaadin7

Show property item name instead of id


I have layout with multiple components ( 4 textFields and 2 comboboxes). This is how defined:

private ManufacturerRepository manRepo;
InteriorDoor interiorDoor;

Manufacturer manufacturer;

ComponentHelper componentHelper;

@PropertyId("width")
private TextField widthField;

@PropertyId("height")
private TextField heightField;

@PropertyId("thickness")
private TextField thicknessField;

@PropertyId("modelName")
private TextField modelName;

@PropertyId("hasGlass")
private ComboBox glassCombo;

@PropertyId("manufacturerId")
private ComboBox manCombo;

private BeanItemContainer<Manufacturer> beanItemContainer;

beanItemContainer is used to fetch data from database:

beanItemContainer = new BeanItemContainer<>(Manufacturer.class, manRepo.findAll());
manCombo.setContainerDataSource(beanItemContainer);

don't mind about glassCombo since it has constant data and binding works there well.

This is how i bind fields:

// binding data to fields
    BeanFieldGroup binder = new BeanFieldGroup<>(InteriorDoor.class);
    binder.setItemDataSource(interiorDoor);
    binder.setBuffered(false);
    binder.bindMemberFields(this);

So far so good, but when it happens to wiring fields, it comes to big problem with manCombo- upon selecting item from combobox, i receive this

Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Could not convert value to Long at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:778) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:755) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.setValue(AbstractField.java:539) ~[vaadin-server-7.7.5.jar:7.7.5] ... 49 common frames omitted Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type .domain.Manufacturer to model type class java.lang.Long. No converter is set and the types are not compatible. at com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:182) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:775) ~[vaadin-server-7.7.5.jar:7.7.5] ... 51 common frames omitted

Please help

P.S I tried to cast as (((Manufacturer).getValue).getId() but it has no effect

UPDATE 1

@Marco C solution doesn't work. Now on invoking layout i receive this:

java.lang.NullPointerException: null at java.lang.Class.isAssignableFrom(Native Method) ~[na:1.8.0_121] at com.vaadin.data.util.converter.ConverterUtil.canConverterPossiblyHandle(ConverterUtil.java:243) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:649) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.FieldGroup.bind(FieldGroup.java:272) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.BeanFieldGroup.bind(BeanFieldGroup.java:155) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.FieldGroup.buildAndBindMemberFields(FieldGroup.java:1011) ~[vaadin-server-7.7.5.jar:7.7.5] at com.vaadin.data.fieldgroup.FieldGroup.bindMemberFields(FieldGroup.java:856) ~[vaadin-server-7.7.5.jar:7.7.5] at com.reborn.doorshop.ui.components.InteriorEditor.editEntity(InteriorEditor.java:156) ~[classes/:na] at com.reborn.doorshop.ui.views.InteriorView.lambda$initComponents$61446b05$1(InteriorView.java:53) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

UPDATE2:

manCombo.setConverter(new Converter<Object, Long>() {
        @Override
        public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale)
                throws ConversionException {
            if (value == null) {
                return null;
            }
            return ((Manufacturer)value).getId();
        }

        @Override
        public Object convertToPresentation(Long value, Class<?> targetType, Locale locale)
                throws ConversionException {
            if (value == null) {
                return null;
            }
            return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
                    .findFirst().orElse(null);
        }

        @Override
        public Class<Long> getModelType() {
            return null;
        }

        @Override
        public Class<Object> getPresentationType() {
            return null;
        }
    });

Solution

  • You can implement a Converter Manufacturer -> Long and set it on the combobox

    manCombo.setConverter(new Converter<Object, Long>() {
            @Override
            public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale) throws ConversionException {
                if (value == null) {
                    return null;
                }
                return ((Manifacturer)value).getId();
            }
    
            @Override
            public Object convertToPresentation(Long value, Class<?> targetType, Locale locale) throws ConversionException {
                if (value == null) {
                    return null;
                }
                return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
                    .findFirst().orElse(null);
            }
            ...
    });