Customizing ComboBox using Custom Field add-on

Hi all,

I’m trying to customize ComboBox using CustomField add-on in the following manner:

ComboBox is tied to BeanContainer and when form containing this ComboBox is commited, value of the ComboBox in question should be a bean from BeanContainer instead of ID of that bean.

By looking at the examples in SVN repo and using Vaadin 6, I customized combo using FIeldWrapper as shown in following:


public class ComboBoxWrapper<T> extends FieldWrapper<T> {

	// name of the method that resolves
	private static final String			ID_GETTER_NAME	= "getId";
	// main layout for this custom component
	protected FormLayout				mainLayout;
	// class instance of the container property type
	protected Class<T>					propertyType;
	// container for combo box
	protected BeanContainer<Long, T>	container;

	public ComboBoxWrapper(Class<T> propertyType) {
		super( new ComboBox(), propertyType );
		this.propertyType = propertyType;
		this.setCompositionRoot( getWrappedField() );
		init();
	}

	public ComboBoxWrapper(Class<T> propertyType, ComponentContainer layout) {
		super( new ComboBox(), propertyType );
		this.propertyType = propertyType;
		this.setCompositionRoot( layout );
		layout.addComponent( getWrappedField() );
		init();
	}

	/**
	 * Initializes layout and sets container for combo box
	 */
	protected void init() {
		/*
		 * Set combo container
		 */
		getComboInstance().setContainerDataSource( getContainer() );
	}

	/**
	 * Converts selected value to the underlying property type
	 */
	@Override
	protected T parse(Object formattedValue) throws ConversionException {
		if (formattedValue != null) {
			ComboBox comboBox = (ComboBox) getWrappedField();
			
			BeanItem<T> beanItem = (BeanItem<T>) comboBox.getItem( formattedValue );
			if (beanItem != null) {
				return beanItem.getBean();
			}
		}
		return null;
	}

	/**
	 * Converts underlying property value to be presented in UI
	 */
	@Override
	protected Object format(T value) {
		if (value != null) {
			try {
				return resolveIdValue( value );
			} catch (Exception e) {
				throw new RuntimeException( "Error resolving property value", e );
			}
		}
		return null;
	}

	/**
	 * Resolves id value from underlying property object insance
	 * 
	 * @param value
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	protected Object resolveIdValue(T value) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Method method = this.propertyType.getDeclaredMethod( ID_GETTER_NAME );
		method.setAccessible( true );
		Object result = method.invoke( value );
		return result;
	}

When this component is used in a form when creating new entities, it works well. Combos have items in them and when selected everything is saved to db.
Problem occurs when form is used to edit entities. When entity from db is bound to the form, ComboBox does not have selected value.
Methods parse and format get invoked with no exceptions.

Is this a known bug or am I misusing this component. Is there any other way for achieving this?
The goal is to get combo to bind objects from container instead of their IDs when used in form.

Thanks in advance.