All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nz.co.senanque.vaadin.converter.StringToChoiceBase Maven / Gradle / Ivy

Go to download

Integrates Madura Objects with Vaadin applications including support for dynamic metadata changes (available choices, read/write status of fields etc).

There is a newer version: 3.4.1
Show newest version
package nz.co.senanque.vaadin.converter;

import java.util.Locale;

import nz.co.senanque.vaadin.MaduraPropertyWrapper;
import nz.co.senanque.validationengine.choicelists.ChoiceBase;

import com.vaadin.data.util.converter.Converter;

/**
 * Allows Vaadin to convert between a ChoiceBase and a String.
 * @author Roger Parkinson
 *
 */
public class StringToChoiceBase implements Converter {
	
	private final MaduraPropertyWrapper m_property;

	public StringToChoiceBase(MaduraPropertyWrapper property) {
		m_property = property;
	}

	@Override
	public Object convertToModel(Object value,
			Class targetType, Locale locale)
			throws com.vaadin.data.util.converter.Converter.ConversionException {
		if (value == null) {
			return null;
		}
		if (targetType.equals(String.class)) {
			return value.toString();
		}
			
		if (value instanceof ChoiceBase) {
			return value.toString();
		}
		if (value instanceof String ) {
			return getChoiceBase((String)value);
		}
		return null;
	}

	@Override
	public Object convertToPresentation(Object value,
			Class targetType, Locale locale)
			throws com.vaadin.data.util.converter.Converter.ConversionException {
		if (value == null) {
			return null;
		}
		if (targetType.equals(String.class)) {
			return value.toString();
		}
		if (value instanceof ChoiceBase) {
			return value.toString();
		}
		if (value instanceof String) {
			return getChoiceBase((String)value);
		}
		return null;
	}
	
	private ChoiceBase getChoiceBase(String key) {
		for (ChoiceBase v: m_property.getAvailableValues())
        {
        	if (v.toString().equals(key)) {
        		return v;
        	}
        }
		return null;
	}

	@Override
	public Class getModelType() {
		return Object.class;
	}

	@Override
	public Class getPresentationType() {
		return Object.class;
	}


}