com.vaadin.data.Converter Java Examples

The following examples show how to use com.vaadin.data.Converter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormFactoryImpl.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the display property converters.
 *
 * @param                     <T> the generic type
 * @param displayProperties   the display properties
 * @param formContent         the form content
 * @param binder              the binder
 * @param propertyDescriptors the property descriptors
 */
private static <T extends Serializable> void createDisplayPropertyConverters(final List<String> displayProperties,
		final ComponentContainer formContent, final Binder<T> binder, final PropertyDescriptor[] propertyDescriptors) {
	for (final String property : displayProperties) {
		final Class<?> typeOfProperty = getTypeOfProperty(propertyDescriptors, property);

		if (typeOfProperty != null) {
			final AbstractField<?> field = new TextField();
			field.setReadOnly(true);
			field.setCaption(property);
			field.setWidth(ContentSize.FULL_SIZE);
			formContent.addComponent(field);
			final Converter converter = getConverterForType(typeOfProperty);

			if (converter != null) {
				binder.forField(field).withConverter(converter).bind(property);
			} else {
				binder.forField(field).bind(property);
			} 
		}
	}
}
 
Example #2
Source File: FormFactoryImpl.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the converter for type.
 *
 * @param typeOfProperty
 *            the type of property
 * @return the converter for type
 */
private static Converter getConverterForType(final Class<?> typeOfProperty) {
	Converter converter;
	
	if (Date.class.equals(typeOfProperty)) {
		converter = new StringToDateConverter();
	} else if (Integer.class.equals(typeOfProperty) || "int".equalsIgnoreCase(typeOfProperty.getName())) {
		converter = new StringToIntegerConverter("Input value should be an integer");
	} else if (Long.class.equals(typeOfProperty) || "long".equalsIgnoreCase(typeOfProperty.getName())) {
		converter = new StringToLongConverter("Input value should be an long");
	} else if (BigInteger.class.equals(typeOfProperty)) {
		converter = new StringToBigIntegerConverter("Input value should be an biginteger");
	} else if (BigDecimal.class.equals(typeOfProperty)) {
		converter = new StringToBigDecimalConverter("Input value should be an bigdecimal");
	} else if (Boolean.class.equals(typeOfProperty) || "boolean".equalsIgnoreCase(typeOfProperty.getName())) {
		converter = new StringToBooleanConverter("Input value should be an boolean");
	} else if (typeOfProperty.isEnum()) {
		converter = new StringToEnumConverter();
	} else {
		converter = null;
	}
	
	return converter;
}
 
Example #3
Source File: FieldFactory.java    From vaadin-grid-util with MIT License 6 votes vote down vote up
public static <T> TextField genNumberField(Binder<T> binder, String propertyId, Converter converter, String inputPrompt) {
    final TextField field = new TextField();
    field.setWidth("100%");
    field.addStyleName(STYLENAME_GRIDCELLFILTER);
    field.addStyleName(ValoTheme.TEXTFIELD_TINY);
    field.addValueChangeListener(e -> {
        if (binder.isValid()) {
            field.setComponentError(null);
        }
    });
    binder.forField(field)
            .withNullRepresentation("")
            // .withValidator(text -> text != null && text.length() > 0, "invalid")
            .withConverter(converter)
            .bind(propertyId);
    field.setPlaceholder(inputPrompt);
    return field;
}
 
Example #4
Source File: NumberUtil.java    From vaadin-grid-util with MIT License 5 votes vote down vote up
public static <T extends Number & Comparable<? super T>> Converter getConverter(final Class<T> type, final String converterErrorMessage) {
    if (Integer.class.equals(type)) {
        return new StringToIntegerConverter(converterErrorMessage);
    } else if (Double.class.equals(type)) {
        return new StringToDoubleConverter(converterErrorMessage);
    } else if (Float.class.equals(type)) {
        return new StringToFloatConverter(converterErrorMessage);
    } else if (BigInteger.class.equals(type)) {
        return new StringToBigIntegerConverter(converterErrorMessage);
    } else if (BigDecimal.class.equals(type)) {
        return new StringToBigDecimalConverter(converterErrorMessage);
    } else {
        return new StringToLongConverter(converterErrorMessage);
    }
}
 
Example #5
Source File: BuildCriterionComponent.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
private Object getConvertedValue(HasValue<?> component) {
    if (component instanceof Converter) {
        Converter converter = (Converter) component;
        Result result = converter.convertToModel(component.getValue(), null);
        try {
            return result.getOrThrow(SerializableFunction.identity());
        } catch (Throwable throwable) {
            throw new MyCollabException(throwable);
        }
    }
    return component.getValue();
}