-1

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;
        }
    });
Malakai
  • 3,011
  • 9
  • 35
  • 49

1 Answers1

1

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);
        }
        ...
});
Marco C
  • 706
  • 4
  • 6
  • It's weird that it doesn't work. I tried it and was fine. From the stacktrace seems like **getModelType** in converter returns null. Can you post your converter code? – Marco C Feb 04 '17 at 19:30
  • It looks exactly like you posted. I tried multiple options. Are you sure that there needs to be only two overriden methods instead of four? – Malakai Feb 04 '17 at 19:33
  • sorry, edited my answer. All methods must be implemented, I left them out on the sample code – Marco C Feb 04 '17 at 19:34
  • ` @Override public Class getModelType() { return Long.class; } @Override public Class getPresentationType() { return Object.class; } ` – Marco C Feb 04 '17 at 19:38
  • Yes! finally it worked! I owe you a beer! Is it possible to decouple that so i would easily mention it as `manCombo.setConverter(new myConverter())`? Atm it's impossible to do that because of beanintemcontainer.. – Malakai Feb 04 '17 at 19:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134867/discussion-between-marco-c-and-reborn). – Marco C Feb 04 '17 at 19:45