Java Code Examples for com.vaadin.flow.data.binder.Result#ok()

The following examples show how to use com.vaadin.flow.data.binder.Result#ok() . 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: StringToBooleanConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Boolean> convertToModel(String value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    // Remove leading and trailing white space
    value = value.trim();

    Locale locale = context.getLocale().orElse(null);
    if (getTrueString(locale).equals(value)) {
        return Result.ok(true);
    } else if (getFalseString(locale).equals(value)) {
        return Result.ok(false);
    } else if (value.isEmpty()) {
        return Result.ok(null);
    } else {
        return Result.error(errorMessageProvider.apply(context));
    }
}
 
Example 2
Source File: StringToDateConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Date> convertToModel(String value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    // Remove leading and trailing white space
    value = value.trim();

    ParsePosition parsePosition = new ParsePosition(0);
    Date parsedValue = getFormat(context.getLocale().orElse(null))
            .parse(value, parsePosition);
    if (parsePosition.getIndex() != value.length()) {
        return Result.error("Could not convert '" + value);
    }

    return Result.ok(parsedValue);
}
 
Example 3
Source File: AbstractStringToNumberConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the value to a Number using the given locale and
 * {@link #getFormat(Locale)}.
 *
 * @param value
 *            The value to convert
 * @param context
 *            The value context for conversion
 * @return The converted value
 */
protected Result<Number> convertToNumber(String value,
        ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    // Remove leading and trailing white space
    value = value.trim();

    // Parse and detect errors. If the full string was not used, it is
    // an error.
    ParsePosition parsePosition = new ParsePosition(0);
    Number parsedValue = getFormat(context.getLocale().orElse(null))
            .parse(value, parsePosition);
    if (parsePosition.getIndex() != value.length()) {
        return Result.error(getErrorMessage(context));
    }

    if (parsedValue == null) {
        // Convert "" to the empty value
        return Result.ok(emptyValue);
    }

    return Result.ok(parsedValue);
}
 
Example 4
Source File: StringToUuidConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public Result<UUID> convertToModel(String value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    // Remove leading and trailing white space
    value = value.trim();

    // Parse string as UUID.
    UUID uuid = null;
    try {
        uuid = UUID.fromString(value);
    } catch (java.lang.IllegalArgumentException e) {
        LoggerFactory.getLogger(StringToUuidConverter.class.getName()).warn(
                "Unable to convert String to UUID: " + value, e);
        return Result.error(this.errorMessageProvider.apply(context));
    }
    return Result.ok(uuid); // Return the UUID object, converted from String.
}
 
Example 5
Source File: DoubleToIntegerConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<Integer> convertToModel(Double value, ValueContext context) {
    try {
        if (value == null) {
            return Result.ok(null);
        }
        return Result.ok(value.intValue());
    } catch (Exception ex) {
        return Result.error(errorMessage);
    }
}
 
Example 6
Source File: RadiusUserDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<String> convertToModel(RadiusUserDto value, ValueContext context) {
    if (value == null) {
        return Result.ok("");
    }
    radiusUserDto = value;
    return Result.ok(value.getUsername());
}
 
Example 7
Source File: AttributeDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<String> convertToModel(T value, ValueContext context) {
    if (value == null) {
        return Result.ok("");
    }
    attributeDto = value;
    return Result.ok(value.getName());
}
 
Example 8
Source File: RadiusGroupDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<String> convertToModel(RadiusGroupDto value, ValueContext context) {
    if (value == null) {
        return Result.ok("");
    }
    radiusGroupDto = value;
    return Result.ok(value.getName());
}
 
Example 9
Source File: StringToCharacterConverter.java    From crudui with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Character> convertToModel(String value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    if (value.length() > 1) {
        return Result.error("Could not convert '" + value);
    }

    return Result.ok(value.charAt(0));
}
 
Example 10
Source File: DateToLongConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Long> convertToModel(Date value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    return Result.ok(value.getTime());
}
 
Example 11
Source File: LocalDateToDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Date> convertToModel(LocalDate localDate,
        ValueContext context) {
    if (localDate == null) {
        return Result.ok(null);
    }

    return Result.ok(Date.from(localDate.atStartOfDay(zoneId).toInstant()));
}
 
Example 12
Source File: LocalDateTimeToDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Date> convertToModel(LocalDateTime localDate, ValueContext context) {
    if (localDate == null) {
        return Result.ok(null);
    }

    return Result.ok(Date.from(localDate.atZone(zoneId).toInstant()));
}
 
Example 13
Source File: DateToSqlDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<java.sql.Date> convertToModel(Date value,
        ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    return Result.ok(new java.sql.Date(value.getTime()));
}