001package com.github.gwtbootstrap.client.ui; 002 003import java.math.BigDecimal; 004import java.text.ParseException; 005 006import com.github.gwtbootstrap.client.ui.base.ValueBoxBase; 007import com.google.gwt.dom.client.Document; 008import com.google.gwt.text.shared.AbstractRenderer; 009import com.google.gwt.text.shared.Parser; 010import com.google.gwt.text.shared.Renderer; 011 012/** 013 * A ValueBox that uses {@link BigDecimalParser} and {@link BigDecimalRenderer}. for Bootstrap 014 * 015 * @since 2.2.1.0 016 * @author Nick Lim 017 */ 018public class BigDecimalBox extends ValueBoxBase<BigDecimal> { 019 /** 020 * Create an empty widget. 021 */ 022 public BigDecimalBox() { 023 super(Document.get().createTextInputElement(), BigDecimalRenderer.instance(), 024 BigDecimalParser.instance()); 025 } 026 027 static class BigDecimalParser implements Parser<BigDecimal> { 028 029 private static BigDecimalParser INSTANCE; 030 031 /** 032 * @return the instance of the no-op renderer 033 */ 034 public static Parser<BigDecimal> instance() { 035 if (INSTANCE == null) { 036 INSTANCE = new BigDecimalParser(); 037 } 038 return INSTANCE; 039 } 040 041 protected BigDecimalParser() {} 042 043 public BigDecimal parse(CharSequence object) throws ParseException { 044 if (object == null || "".equals(object.toString())) { 045 return null; 046 } 047 048 try { 049 return new BigDecimal(object.toString()); 050 } catch (NumberFormatException e) { 051 throw new ParseException(e.getMessage(), 0); 052 } 053 } 054 } 055 056 static class BigDecimalRenderer extends AbstractRenderer<BigDecimal> { 057 private static BigDecimalRenderer INSTANCE; 058 059 /** 060 * @return the instance 061 */ 062 public static Renderer<BigDecimal> instance() { 063 if (INSTANCE == null) { 064 INSTANCE = new BigDecimalRenderer(); 065 } 066 return INSTANCE; 067 } 068 069 protected BigDecimalRenderer() {} 070 071 public String render(BigDecimal object) { 072 if (object == null) { 073 return ""; 074 } 075 076 return object.toString(); 077 } 078 } 079}