Converters and subtypes

Greetings to all,

I have the following scenario:

  1. My data is made of a set of beans that implement the same interface.
  2. I have also a converter that can manage the INTERFACE and returns a standard implementation when converting.

Unfortunately, due to ConverterUtil.canConverterHandle(...) , and more specifically, due to the following code:


if (!modelType.isAssignableFrom(converter.getModelType())) {
            return false;
        }

my converter does not work, as it is removed from the editor: in fact, if the above method returns false, the AbstractField.setPropertyDataSource(…) sets the converter to the one embedded in the Property.

I may be mistaking, but, based on the fact that the requirement should be that any of the beans that the converter manages should be compliant with the model type of the converter, and NOT the opposite, I would expect that the above code should be changed as the following:


if (!converter.getModelType().isAssignableFrom(modelType)) {
            return false;
        }

This way, the converter could convert far more object types, provided that they implement the same interface.

Here it is my code example:


package org.moonlight.components;

import java.io.Serializable;
import java.util.Locale;

import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class Tab9Component extends CustomComponent {
	@Override
	public void attach() {
		super.attach();
		
		final VerticalLayout v = new VerticalLayout();
		
		final ObjectProperty<IMyFoo> op =
				new ObjectProperty<IMyFoo>(new MyFoo("InitialValue"));
		final TextField tf = new TextField();
		tf.setConverter(new MyConverter());
		tf.setPropertyDataSource(op);
		
		v.addComponent(tf);
		
		setCompositionRoot(v);
	}
	
	private static class MyConverter implements Converter<String, IMyFoo> {

		@Override
		public IMyFoo convertToModel(String value, Locale locale)
				throws ConversionException {
			return value == null ? null : new MyFoo(value);
		}

		@Override
		public String convertToPresentation(IMyFoo value, Locale locale)
				throws ConversionException {
			return value == null ? null : value.getValue();
		}

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

		@Override
		public Class<String> getPresentationType() {
			return String.class;
		}
		
	}
	
	private static interface IMyFoo extends Serializable {
		public String getValue();
	}
	
	private static class MyFoo implements IMyFoo {
		private final String value;

		public MyFoo(String value) {
			super();
			this.value = value;
		}

		public String getValue() {
			return value;
		}
	}
	
}

I am using Vaadin 7 beta 1.

Many thanks for your support.

Kind regards.

Mirko

Did you found any solution for this issue. I thing the bug is still there

Vadin 7.2 and setPropertyDataSource(…) still removes user defined Converter. Workaround is to set a Converter after setting data source

tf.setPropertyDataSource(op); tf.setConverter(new MyConverter()); The right solution is to allow the application programmer to override canConverterHandle(…). Unfortunatelly this must be done by Vaadin team, the call is hard coded in AbstractField.