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

org.jsimpledb.vaadin.SimpleFieldConverter Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.vaadin;

import com.google.common.base.Preconditions;
import com.vaadin.data.util.converter.Converter;

import java.util.Locale;

import org.jsimpledb.core.FieldType;

/**
 * Vaadin {@link Converter} for {@link FieldType}s to/from {@link String}.
 * Trims whitespace before converting from {@link String}.
 *
 * @param  field type
 */
@SuppressWarnings("serial")
public class SimpleFieldConverter implements Converter {

    private final FieldType fieldType;

    public SimpleFieldConverter(FieldType fieldType) {
        Preconditions.checkArgument(fieldType != null, "null fieldType");
        this.fieldType = fieldType;
    }

    public Class getPresentationType() {
        return String.class;
    }

    @SuppressWarnings("unchecked")
    public Class getModelType() {
        return (Class)this.fieldType.getTypeToken().getRawType();
    }

    @Override
    public String convertToPresentation(T value, Class targetType, Locale locale) {
        if (value == null)
            return null;
        return this.fieldType.toString(value);
    }

    @Override
    public T convertToModel(String value, Class targetType, Locale locale) {
        if (value == null)
            return null;
        try {
            return this.fieldType.fromString(value.trim());
        } catch (IllegalArgumentException e) {
            throw new Converter.ConversionException("invalid value of type `" + this.fieldType + "': " + e.getMessage());
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy