use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class WebAbstractTable method getFormattedValue.
protected String getFormattedValue(Column column, Object value) {
String cellText;
if (value == null) {
cellText = "";
} else {
if (value instanceof String) {
cellText = (String) value;
} else {
Formatter formatter = column.getFormatter();
if (formatter != null) {
// noinspection unchecked
cellText = formatter.format(value);
} else {
Datatype datatype = Datatypes.get(value.getClass());
if (datatype != null) {
UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
cellText = datatype.format(value, sessionSource.getLocale());
} else {
cellText = value.toString();
}
}
}
}
return cellText;
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class WebAbstractTextField method setValue.
@Override
public void setValue(Object value) {
if (!(value instanceof String)) {
String formattedValue;
Datatype<String> stringDatatype = Datatypes.getNN(String.class);
Datatype datatype = getActualDatatype();
if (datatype != null && stringDatatype != datatype) {
formattedValue = datatype.format(value, locale);
} else {
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
formattedValue = metadataTools.format(value);
}
super.setValue(formattedValue);
} else {
super.setValue(value);
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class AppPropertiesEdit method init.
@Override
public void init(Map<String, Object> params) {
cannotEditValueLabel.setVisible(item.getOverridden());
Datatype datatype = Datatypes.get(item.getDataTypeName());
fieldGroup.addCustomField("currentValue", (datasource, propertyId) -> {
if (item.getOverridden()) {
TextField textField = componentsFactory.createComponent(TextField.class);
textField.setValue(item.getDisplayedCurrentValue());
textField.setEditable(false);
return textField;
}
if (item.getEnumValues() != null) {
return createLookupField(Arrays.asList(item.getEnumValues().split(",")), item.getCurrentValue());
} else {
if (datatype instanceof BooleanDatatype) {
return createLookupField(Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()), item.getCurrentValue());
} else {
if (Boolean.TRUE.equals(item.getSecret())) {
PasswordField passwordField = componentsFactory.createComponent(PasswordField.class);
passwordField.setValue(item.getCurrentValue());
passwordField.addValueChangeListener(e -> {
appPropertyDs.getItem().setCurrentValue(e.getValue() == null ? null : e.getValue().toString());
});
return passwordField;
} else {
TextField textField = componentsFactory.createComponent(TextField.class);
textField.setValue(item.getCurrentValue());
try {
datatype.parse(item.getCurrentValue(), userSessionSource.getLocale());
textField.setDatatype(datatype);
} catch (ParseException e) {
// do not assign datatype then
log.trace("Localized parsing by datatype cannot be used for value {}", item.getCurrentValue());
}
textField.addValueChangeListener(e -> {
appPropertyDs.getItem().setCurrentValue(e.getValue() == null ? null : e.getValue().toString());
});
return textField;
}
}
}
});
final Formatter<String> defaultValueFormatter = (value) -> {
if (datatype instanceof BooleanDatatype) {
return value;
}
try {
Object parsedDefaultValue = datatype.parse(value);
return datatype.format(parsedDefaultValue, userSessionSource.getLocale());
} catch (ParseException e) {
log.trace("Localized parsing by datatype cannot be used for value {}", value, e);
}
return value;
};
displayedDefaultValueField.setFormatter(defaultValueFormatter);
appPropertyDs.setItem(metadata.getTools().copy(item));
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class NumberFormatter method format.
@Override
public String format(Number value) {
if (value == null) {
return null;
}
String pattern = element != null ? element.attributeValue("format") : null;
if (pattern == null) {
Datatype datatype = Datatypes.getNN(value.getClass());
return datatype.format(value, userSessionSource.getLocale());
} else {
if (pattern.startsWith("msg://")) {
pattern = messages.getMainMessage(pattern.substring(6, pattern.length()));
}
FormatStrings formatStrings = Datatypes.getFormatStrings(userSessionSource.getLocale());
if (formatStrings == null)
throw new IllegalStateException("FormatStrings are not defined for " + userSessionSource.getLocale());
DecimalFormat format = new DecimalFormat(pattern, formatStrings.getFormatSymbols());
return format.format(value);
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class DateValidator method validate.
@Override
public void validate(Object value) throws ValidationException {
if (value == null)
return;
boolean result;
if (value instanceof String) {
try {
Datatype datatype = Datatypes.getNN(java.sql.Date.class);
UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
datatype.parse((String) value, sessionSource.getLocale());
result = true;
} catch (ParseException e) {
result = false;
}
} else {
result = value instanceof Date;
}
if (!result) {
String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
throw new ValidationException(String.format(msg, value));
}
}
Aggregations