import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.Locale;
import java.util.logging.Logger;
import com.vaadin.data.Buffered;
*/
private boolean isListeningToPropertyEvents = false;
+ /**
+ * The locale used when setting the value.
+ */
+ private Locale valueLocale = null;
+
/* Component basics */
/*
}
// There is no buffered value so use whatever the data model provides
- return convertFromDataSource(getDataSourceValue());
+ return convertFromModel(getDataSourceValue());
}
/*
throw new Property.ReadOnlyException();
}
try {
- T doubleConvertedFieldValue = convertFromDataSource(convertToModel(newFieldValue));
+ T doubleConvertedFieldValue = convertFromModel(convertToModel(newFieldValue));
if (!equals(newFieldValue, doubleConvertedFieldValue)) {
newFieldValue = doubleConvertedFieldValue;
repaintIsNotNeeded = false;
}
}
- private static boolean equals(Object value1, Object value2) {
+ static boolean equals(Object value1, Object value2) {
if (value1 == null) {
return value2 == null;
}
// Gets the value from source
try {
if (dataSource != null) {
- T fieldValue = convertFromDataSource(getDataSourceValue());
+ T fieldValue = convertFromModel(getDataSourceValue());
setInternalValue(fieldValue);
}
setModified(false);
* if there is no converter and the type is not compatible with
* the data source type.
*/
- private T convertFromDataSource(Object newValue) {
+ private T convertFromModel(Object newValue) {
+ return convertFromModel(newValue, getLocale());
+ }
+
+ /**
+ * Convert the given value from the data source type to the UI type.
+ *
+ * @param newValue
+ * The data source value to convert.
+ * @return The converted value that is compatible with the UI type or the
+ * original value if its type is compatible and no converter is set.
+ * @throws Converter.ConversionException
+ * if there is no converter and the type is not compatible with
+ * the data source type.
+ */
+ private T convertFromModel(Object newValue, Locale locale) {
return ConverterUtil.convertFromModel(newValue, getType(),
- getConverter(), getLocale());
+ getConverter(), locale);
}
/**
*/
private Object convertToModel(T fieldValue)
throws Converter.ConversionException {
+ return convertToModel(fieldValue, getLocale());
+ }
+
+ /**
+ * Convert the given value from the UI type to the data source type.
+ *
+ * @param fieldValue
+ * The value to convert. Typically returned by
+ * {@link #getFieldValue()}
+ * @param locale
+ * The locale to use for the conversion
+ * @return The converted value that is compatible with the data source type.
+ * @throws Converter.ConversionException
+ * if there is no converter and the type is not compatible with
+ * the data source type.
+ */
+ private Object convertToModel(T fieldValue, Locale locale)
+ throws Converter.ConversionException {
Class<?> modelType = null;
Property pd = getPropertyDataSource();
if (pd != null) {
}
try {
return ConverterUtil.convertToModel(fieldValue,
- (Class<Object>) modelType, getConverter(), getLocale());
+ (Class<Object>) modelType, getConverter(), locale);
} catch (ConversionException e) {
throw new ConversionException(getConversionError(modelType), e);
}
* The value to set. Must be the same type as the data source.
*/
public void setConvertedValue(Object value) {
- setValue(convertFromDataSource(value));
+ setValue(convertFromModel(value));
}
/* Validation */
}
private void readValueFromProperty(Property.ValueChangeEvent event) {
- setInternalValue(convertFromDataSource(event.getProperty().getValue()));
+ setInternalValue(convertFromModel(event.getProperty().getValue()));
}
/**
*/
protected void setInternalValue(T newValue) {
value = newValue;
+ valueLocale = getLocale();
if (validators != null && !validators.isEmpty()) {
markAsDirty();
}
public void attach() {
super.attach();
+ localeMightHaveChanged();
if (!isListeningToPropertyEvents) {
addPropertyListeners();
if (!isModified() && !isBuffered()) {
}
}
+ @Override
+ public void setLocale(Locale locale) {
+ super.setLocale(locale);
+ localeMightHaveChanged();
+ }
+
+ private void localeMightHaveChanged() {
+ if (!equals(valueLocale, getLocale())) {
+ Object modelValue = convertToModel(getValue(), valueLocale);
+ setValue(convertFromModel(modelValue));
+ }
+ }
+
@Override
public void detach() {
super.detach();
try {
// Discards buffer by overwriting from datasource
- newFieldValue = convertFromDataSource(getDataSourceValue());
+ newFieldValue = convertFromModel(getDataSourceValue());
// If successful, remove set the buffering state to be ok
if (getCurrentBufferedSourceException() != null) {
package com.vaadin.ui;
import java.lang.reflect.Method;
+import java.util.Locale;
import java.util.logging.Logger;
import com.vaadin.data.Property;
}
/**
- * Returns the current value of the data source. Assumes there is a data
- * source.
+ * Returns the current value of the data source converted using the current
+ * locale.
*
* @return
*/
*/
@Override
public void valueChange(Property.ValueChangeEvent event) {
+ updateValueFromDataSource();
+ }
+
+ private void updateValueFromDataSource() {
// Update the internal value from the data source
- getState().text = getValue();
+ String newConvertedValue = getDataSourceValue();
+ if (!AbstractField.equals(newConvertedValue, getState().text)) {
+ getState().text = newConvertedValue;
+ fireValueChange();
+ }
+ }
- fireValueChange();
+ @Override
+ public void attach() {
+ super.attach();
+ localeMightHaveChanged();
+ }
+
+ @Override
+ public void setLocale(Locale locale) {
+ super.setLocale(locale);
+ localeMightHaveChanged();
+ }
+
+ private void localeMightHaveChanged() {
+ if (getPropertyDataSource() != null) {
+ updateValueFromDataSource();
+ }
}
private String getComparableValue() {