english version
Indice dei tutorials: http://gheryd.blogspot.com/2011/06/javascript-gwt-tutorials.html
E possibile configurare nel modulo GWT il paese per la corretta formattazione del numero.
Per i formati con virgola ho addottato il valore
Comunque i formati possono essere essere usati formati "custom" con il metodo "setNumberFormat" a cui occorre passare un oggetto NumberFormat. Per disabilitare la formattazione usare il metodo disableFormatting.
Esempio:
CurrencyTextBox ctb = new CurrencyTextBox();
ctb.setNumber(1000);
RootPanel.get().add(ctb);
Indice dei tutorials: http://gheryd.blogspot.com/2011/06/javascript-gwt-tutorials.html
E possibile configurare nel modulo GWT il paese per la corretta formattazione del numero.
<?xml version="1.0" encoding="UTF-8"?> <module> <extend-property name="locale" values="it_IT" /> <set-property-fallback name="locale" value="it_IT" /> </module>
Per i formati con virgola ho addottato il valore
public static final NumberFormat nfFloat = NumberFormat.getFormat("#,##0.00");mentre per i valori interi:
public static final NumberFormat nfInt = NumberFormat.getFormat("#,##0");
Comunque i formati possono essere essere usati formati "custom" con il metodo "setNumberFormat" a cui occorre passare un oggetto NumberFormat. Per disabilitare la formattazione usare il metodo disableFormatting.
import com.google.gwt.event.dom.client.BlurEvent; import com.google.gwt.event.dom.client.BlurHandler; import com.google.gwt.event.dom.client.FocusEvent; import com.google.gwt.event.dom.client.FocusHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyPressHandler; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.TextBox; public class CurrencyTextBox extends TextBox { public static final NumberFormat nfFloat = NumberFormat.getFormat("#,##0.00"); public static final NumberFormat nfInt = NumberFormat.getFormat("#,##0"); public static interface CurrencyTextBoxHandler { public void onChange(CurrencyTextBox currencyTextBox); } private int decimals = 2; private int integers = 10; private NumberFormat nf; private float cifra = 0f; private char inputSeparator = '.'; private char currencySumbol = '€'; private boolean enableNegative = false; private boolean enableDecimal; private CurrencyTextBoxHandler handler = null; private String suffix = ""; public CurrencyTextBox() { this(true, true); } public CurrencyTextBox(boolean showCurrencySimbol, boolean enableDecimal) { super.setTextAlignment(TextBox.ALIGN_RIGHT); if(!showCurrencySimbol) { currencySumbol = ' '; } this.enableDecimal = enableDecimal; if(enableDecimal) { nf = nfFloat; }else { nf = nfInt; } addKeyPressHandler(keyPressHandler); addBlurHandler(new BlurHandler() { @Override public void onBlur(BlurEvent event) { if(CurrencyTextBox.this.isReadOnly()) { return; } parse(); showFormatted(); if(handler!=null) { handler.onChange(CurrencyTextBox.this); } } }); addFocusHandler(new FocusHandler() { @Override public void onFocus(FocusEvent event) { if(CurrencyTextBox.this.isReadOnly()) { return; } if(CurrencyTextBox.this.enableDecimal) { CurrencyTextBox.super.setText( (cifra==0f?"":cifra)+"" ); }else { int n = (int)cifra; CurrencyTextBox.super.setText( (n==0?"":n)+"" ); } } }); showFormatted(); } @Override public void setValue(String value) { setText(value); } @Override public String getValue() { return getText(); } @Override public void setText(String text) { float n = 0f; try { n = Float.parseFloat( text ); }catch(Exception e){} setNumber(n); } @Override public String getText() { if(enableDecimal) { Float n = getNumber(); return "" + (n==null ? "" : n); }else { Integer n = getNumberAsInteger(); return "" + (n==null ? "" : n); } } public void setNumberFormat(NumberFormat nf) { this.nf = nf; showFormatted(); } public void disableFormatting() { this.nf = null; } public void setEnableNegative(boolean enableNegative) { this.enableNegative = enableNegative; } public void setCurrencyTextBoxHandler(CurrencyTextBoxHandler handler) { this.handler = handler; } public void setDecimals(int decimals) { this.decimals = decimals; } public void setIntegers(int integers) { this.integers = integers; } public void setCurrencySymbol(char symbol) { this.currencySumbol = symbol; showFormatted(); } public float getNumber() { return cifra; } public int getNumberAsInteger() { return (int)cifra; } public void setNumber(Integer cifra) { if (cifra!=null){ setNumber(cifra.floatValue()); }else { setNumber(0f); } } public void setNumber(Float cifra) { if (cifra!=null){ if(enableDecimal) { this.cifra = cifra; }else { this.cifra = cifra.intValue(); } }else { this.cifra = 0f; } showFormatted(); } private KeyPressHandler keyPressHandler = new KeyPressHandler(){ @Override public void onKeyPress(KeyPressEvent event) { char c = event.getCharCode(); if(c==KeyCodes.KEY_ENTER || c==KeyCodes.KEY_TAB) { CurrencyTextBox.this.setFocus(false); return; } String text = CurrencyTextBox.super.getText(); int curPos = CurrencyTextBox.this.getCursorPos(); if(c==KeyCodes.KEY_BACKSPACE || c==KeyCodes.KEY_DELETE || c==KeyCodes.KEY_RIGHT || c==KeyCodes.KEY_LEFT) { return; }else if( c==KeyCodes.KEY_UP ) { parse(); cifra++; CurrencyTextBox.super.setText(cifra+""); }else if( c==KeyCodes.KEY_DOWN ) { parse(); cifra--; CurrencyTextBox.super.setText(cifra+""); }else if(curPos == 0 && enableNegative && c == '-' && !text.startsWith("c")) { }else if(c==inputSeparator) { if(enableDecimal) { int index = CurrencyTextBox.super.getText().indexOf(inputSeparator); if(index>-1 && text.length()>=index) { CurrencyTextBox.this.cancelKey(); } }else { CurrencyTextBox.this.cancelKey(); } }else if( !Character.isDigit(c) ) { CurrencyTextBox.this.cancelKey(); } int posSep = -1; if(enableDecimal) posSep = text.indexOf(inputSeparator); if(Character.isDigit(c) && posSep>-1) { // c'è il punto if( curPos>posSep && (text.length()-posSep)>decimals ) { CurrencyTextBox.this.cancelKey(); }else if(curPos<=posSep && posSep>integers ) { CurrencyTextBox.this.cancelKey(); } }else if(posSep==-1 && text.length()>=integers) { // non c'è il punto CurrencyTextBox.this.cancelKey(); } } }; private void parse() { cifra = 0f; try { cifra= Float.parseFloat(CurrencyTextBox.super.getText()); }catch(Exception e) { cifra = 0f; } } private void showFormatted() { CurrencyTextBox.super.setText( (currencySumbol!=' '?currencySumbol:"")+" "+ getFormatted() + suffix ); } private String getFormatted() { if(nf!=null) { return nf.format(cifra); }else { if(enableDecimal) { return cifra+""; }else { return getNumberAsInteger()+""; } } } public void setSuffix(String suffix) { this.suffix = suffix; showFormatted(); } }
CurrencyTextBox ctb = new CurrencyTextBox();
ctb.setNumber(1000);
RootPanel.get().add(ctb);