Hi,
The code itself:
The sample use:
HTH
MichalG
The code itself:
Code:
package org.yournamehere.client;
import com.google.gwt.i18n.client.NumberFormat;
import java.util.HashMap;
/**
* While it is fairly easy to display numeric and currency formats in SmartGWT
* (SimpleType has setShort/NormalDisplayFormatter()),
* I haven't been able to provide full locale format support including editing.
* Although FormItem class has setEditorValueFormatter() and setEditorValueParser() methods,
* and SimpleType class has setEditorType(FormItem editorType) method,
* that way goes nowhere because simpleType.setEditorType(editorType) fires exception - something is broken or not yet done here.
*
* Desperately seeking a workaround I finally wrote the following code
* putting together info I got from SmartGWT and SmartClient forums
* and other sources.
* This helper creates and registers custom numeric SimpleType
* based on GWT i18n NumericFormat.
* Such a SimpleType could be then used just as build-in types.
*
* @author michalg
*/
public final class NumericTypeFactory {
private static HashMap<String, NumberFormat> formatMap = new HashMap();
private NumericTypeFactory(){}
public static void registerNumericSimpleType(String name, NumberFormat format) {
formatMap.put(name, format);
createNumericSimpleType(name);
}
private static native void createNumericSimpleType(String name) /*-{
$wnd.isc.ClassFactory.defineClass(name, "TextItem");
$wnd.isc.ClassFactory.getClass(name).addProperties( {
mapDisplayToValue : function(value) {
retVal = @org.yournamehere.client.NumericTypeFactory::parseNumericValue(Ljava/lang/String;Ljava/lang/String;)(name,value);
if (isNaN(retVal))
return value;
return retVal;
},
mapValueToDisplay : function(value) {
if (value == null)
return "";
else if (isNaN(value))
return value;
else {
return @org.yournamehere.client.NumericTypeFactory::formatNumericValue(Ljava/lang/String;D)(name,value);
}
}
});
$wnd.isc.SimpleType.create({name:name,
inheritsFrom:"float",
editorType:name,
normalDisplayFormatter:function(internalValue){
return this.shortDisplayFormatter(internalValue);
},
shortDisplayFormatter:function(value){
if (value == null)
return "";
else if (isNaN(value))
return value;
else {
return @org.yournamehere.client.NumericTypeFactory::formatNumericValue(Ljava/lang/String;D)(name,value);
}
}
});
}-*/;
private static String formatNumericValue(String name, double value) {
return formatMap.get(name).format(value);
}
private static double parseNumericValue(String name, String value) {
try {
//System.out.println("parsing \"" + value + "\"");
double result = formatMap.get(name).parse(value);
//System.out.println("result: " + result);
return result;
} catch (Exception e) {
return Double.NaN;
}
}
}
Code:
package org.yournamehere.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.viewer.DetailViewer;
public class MainEntryPoint implements EntryPoint {
public MainEntryPoint() {
NumericTypeFactory.registerNumericSimpleType("localDecimal", NumberFormat.getDecimalFormat());
NumericTypeFactory.registerNumericSimpleType("localCurrency", NumberFormat.getCurrencyFormat());
NumericTypeFactory.registerNumericSimpleType("USCurrency", NumberFormat.getCurrencyFormat("USD"));
NumericTypeFactory.registerNumericSimpleType("EUCurrency", NumberFormat.getCurrencyFormat("EUR"));
}
public void onModuleLoad() {
RootPanel.get().add(getCanvas());
}
public Canvas getCanvas() {
VLayout layout = new VLayout();
DataSource ds = new DataSource();
ds.setClientOnly(true);
DataSourceField numberField = new DataSourceField();
numberField.setName("localNumber");
numberField.setAttribute("type", "localDecimal");
ds.addField(numberField);
DataSourceField currencyField = new DataSourceField();
currencyField.setName("localMoney");
currencyField.setAttribute("type", "localCurrency");
ds.addField(currencyField);
DataSourceField usdField = new DataSourceField();
usdField.setName("USD");
usdField.setAttribute("type", "USCurrency");
ds.addField(usdField);
DataSourceField eurField = new DataSourceField();
eurField.setName("EUR");
eurField.setAttribute("type", "EUCurrency");
ds.addField(eurField);
final DynamicForm form = new DynamicForm();
form.setDataSource(ds);
final DetailViewer view = new DetailViewer();
view.setDataSource(ds);
Button button = new Button("validate");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.validate();
view.setData(form.getRecordList());
}
});
layout.addMember(form);
layout.addMember(button);
layout.addMember(view);
return layout;
}
}
MichalG
Comment