Hi!
I have a ListGrid which allows edits. The type of the cell I trying to edit is Float. I needed the custom decimal configuration, so I set the Portugal locale and then used NumberFormat for parsing and formatting. But strange thing is happen when I trying to enter an invalid value in the cell.
Cell is initialized by “0”. I’m trying to type “d” instead of it.
Here is the logs of what happens next:
<<<<<<<<<<< parser
value: d
<<<<<<<<<<<<<< formatter
value: d
type: class java.lang.String
<<<<<<<<<<< parser
value: d
<<<<<<<<<<<<<< formatter
value: d
type: class java.lang.String
<<<<<<<<<<<<<< formatter
value: 0
type: class java.lang.String
<<<<<<<<<<< parser
value: 0
<<<<<<<<<<< parser
value: 0
So, formatter and parser are called multiple times and in the end the value which comes for formatting and parsing is the “old” one, but not just entered. In result of this the old value overrides the newly entered in the cell.
Expected behaviour is that user can see what he entered in the cell.
validator:
package pt.olisipo.servicemanagement.gwt.client.widgets.formatters;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
public class DecimalValidator extends CustomValidator {
@Override
protected boolean condition(Object value) {
log("<<<<<<<<<<<< validator");
log("value: " + value);
if (value == null) {
return true;
}
log("type: " + value.getClass());
if (!(value instanceof Number)) {
NumberFormat nf = NumberFormat.getDecimalFormat();
try {
nf.parse(value.toString());
return true;
} catch (NumberFormatException e) {
try {
Double.parseDouble(value.toString());
return true;
} catch (NumberFormatException e1) {
return false;
}
}
} else {
return true;
}
}
public static native void log(String s) /*-{if(console&&console.log) console.log(s);}-*/;
}
formatter:
package pt.olisipo.servicemanagement.gwt.client.widgets.formatters;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemValueFormatter;
import com.smartgwt.client.widgets.form.fields.FormItem;
public class DecimalValueFormatter implements FormItemValueFormatter {
private boolean firstLaunch = true;
public void setFirstLaunch(boolean firstLaunch) {
this.firstLaunch = firstLaunch;
}
@Override
public String formatValue(Object value, Record record, DynamicForm dynamicForm, FormItem formItem) {
log("<<<<<<<<<<<<<< formatter");
log("value: " + value);
if (value == null) return null;
log("type: " + value.getClass());
try {
String s = null;
if (value instanceof String) {
if (firstLaunch) {
NumberFormat.setForcedLatinDigits(true);
Double doubleValue = Double.parseDouble((String) value);
NumberFormat.setForcedLatinDigits(false);
s = NumberFormat.getDecimalFormat().format(doubleValue);
} else {
s = (String) value;
}
} else if (value instanceof Number) {
s = NumberFormat.getDecimalFormat().format(((Number) value).doubleValue());
}
return s;
} catch (Exception e) {
return value.toString();
}
}
public static native void log(String s) /*-{if(console&&console.log) console.log(s);}-*/;
}
parser:
package pt.olisipo.servicemanagement.gwt.client.widgets.formatters;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemValueParser;
import com.smartgwt.client.widgets.form.fields.FormItem;
public class DecimalValueParser implements FormItemValueParser {
@Override
public Object parseValue(String s, DynamicForm dynamicForm, FormItem formItem) {
log("<<<<<<<<<<< parser");
log("value: " + s);
try {
NumberFormat nf = NumberFormat.getDecimalFormat();
return nf.parse(s);
} catch (NumberFormatException e) {
return s;
}
}
public static native void log(String s) /*-{if(console&&console.log) console.log(s);}-*/;
}
Using in the application:
ListGridField valueField = new ListGridField("value", "Value");
valueField.setType(ListGridFieldType.FLOAT);
valueField.setCanEdit(true);
valueField.setCellFormatter(new DecimalCellFormatter());
valueFieldFormatter = new DecimalValueFormatter();
final TextItem textItem = new TextItem();
textItem.setEditorValueFormatter(valueFieldFormatter);
textItem.setEditorValueParser(parser);
textItem.setValidators(decimalValidator);
textItem.setValidateOnChange(true);
textItem.addChangedHandler(new ChangedHandler() {
@Override
public void onChanged(ChangedEvent changedEvent) {
valueFieldFormatter.setFirstLaunch(false);
}
});
valueField.setEditorType(textItem);
SmartGWT 2.4
all browsers
