protected String getCurrencyAmountPattern() { StringBuffer strBuf = new StringBuffer("#,##0"); int precision = currencyData.getDefaultFractionDigits(); if (precision > 0) { strBuf.append('.'); for (int i = 0; i < precision; i++) { strBuf.append('0'); } } return strBuf.toString(); }
/** * Constructs a CurrencyWidget object with specified currency. * * @param currencyCode International currency code (ISO 4217). */ public CurrencyWidget(String currencyCode) { currencyData = CurrencyList.get().lookup(currencyCode); assert (currencyData != null); currencySymbol = new Label(currencyData.getPortableCurrencySymbol()); formatter = NumberFormat.getFormat(getCurrencyAmountPattern()); initWidget(horizontalPanel); horizontalPanel.setSpacing(2); if (isLeadingSymbol()) { horizontalPanel.add(currencySymbol); horizontalPanel.add(amountBox); amountBox.setTextAlignment(TextBoxBase.ALIGN_LEFT); } else { horizontalPanel.add(amountBox); horizontalPanel.add(currencySymbol); amountBox.setTextAlignment(TextBoxBase.ALIGN_RIGHT); } amountBox.setText(formatter.format(0)); amountBox.addFocusListener( new FocusListener() { public void onFocus(Widget sender) { if (!valueInitiated) { amountBox.setText(""); valueInitiated = true; } } public void onLostFocus(Widget sender) { amountBox.setText(reformatContent()); } }); amountBox.addKeyboardListener( new KeyboardListenerAdapter() { public void onKeyDown(Widget sender, char keyCode, int modifiers) { if (keyCode == (char) KEY_ENTER) { amountBox.setText(reformatContent()); } } public void onKeyPress(Widget sender, char keyCode, int modifiers) { if (acceptableCharset.indexOf(keyCode) == -1) { amountBox.cancelKey(); } if (amountBox.getText().length() >= 16) { amountBox.cancelKey(); } } }); }