001: /* Decimalbox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Jun 28 13:40:20 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import java.math.BigDecimal;
022:
023: import org.zkoss.math.BigDecimals;
024: import org.zkoss.zk.ui.WrongValueException;
025:
026: import org.zkoss.zul.mesg.MZul;
027: import org.zkoss.zul.impl.NumberInputElement;
028:
029: /**
030: * An edit box for holding BigDecimal.
031: *
032: * @author tomyeh
033: */
034: public class Decimalbox extends NumberInputElement {
035: /** Used with {@link #setScale} to denote that the scale is decided by
036: * what user has entered.
037: */
038: public static final int AUTO = -1000000000;
039: private int _scale = AUTO;
040:
041: public Decimalbox() {
042: setCols(11);
043: }
044:
045: public Decimalbox(BigDecimal value) throws WrongValueException {
046: this ();
047: setValue(value);
048: }
049:
050: /** Returns the value (in BigDecimal), might be null unless
051: * a constraint stops it.
052: * @exception WrongValueException if user entered a wrong value
053: */
054: public BigDecimal getValue() throws WrongValueException {
055: return (BigDecimal) getTargetValue();
056: }
057:
058: /** Returns the value in double. If null, zero is returned.
059: */
060: public double doubleValue() throws WrongValueException {
061: final BigDecimal val = getValue();
062: return val != null ? val.doubleValue() : 0.0;
063: }
064:
065: /** Returns the value in integer. If null, zero is returned.
066: */
067: public int intValue() throws WrongValueException {
068: final BigDecimal val = getValue();
069: return val != null ? val.intValue() : 0;
070: }
071:
072: /** Returns the value in long. If null, zero is returned.
073: */
074: public long longValue() throws WrongValueException {
075: final BigDecimal val = getValue();
076: return val != null ? val.longValue() : 0;
077: }
078:
079: /** Returns the value in short. If null, zero is returned.
080: */
081: public short shortValue() throws WrongValueException {
082: final BigDecimal val = getValue();
083: return val != null ? val.shortValue() : 0;
084: }
085:
086: /** Sets the value (in BigDecimal).
087: * @exception WrongValueException if value is wrong
088: */
089: public void setValue(BigDecimal value) throws WrongValueException {
090: validate(value);
091: setRawValue(value);
092: }
093:
094: /** Returns the scale for the decimal number storing in this component,
095: * or {@link #AUTO} if the scale is decided automatically (based on
096: * what user has entered).
097: *
098: * <p>Default: {@link #AUTO}.
099: */
100: public int getScale() {
101: return _scale;
102: }
103:
104: /** Returns the scale for the decimal number storing in this component,
105: * or {@link #AUTO} if the scale is decided automatically (based on
106: * what user has entered).
107: *
108: * <p>Default: {@link #AUTO}.
109: */
110: public void setScale(int scale) {
111: _scale = scale;
112: }
113:
114: //-- super --//
115: protected Object coerceFromString(String value)
116: throws WrongValueException {
117: final Object[] vals = toNumberOnly(value);
118: final String val = (String) vals[0];
119: if (val == null || val.length() == 0)
120: return null;
121:
122: try {
123: BigDecimal v = new BigDecimal(val);
124: if (_scale != AUTO)
125: v = v.setScale(_scale, getRoundingMode());
126:
127: int divscale = vals[1] != null ? ((Integer) vals[1])
128: .intValue() : 0;
129: if (divscale > 0) {
130: final BigDecimal ten = new BigDecimal(10);
131: do {
132: v = v.divide(ten, _scale == AUTO ? v.scale() + 1
133: : _scale, getRoundingMode());
134: } while (--divscale > 0);
135: }
136: return v;
137: } catch (NumberFormatException ex) {
138: throw showCustomError(new WrongValueException(this ,
139: MZul.NUMBER_REQUIRED, value));
140: }
141: }
142:
143: protected String coerceToString(Object value) {
144: return formatNumber(value, "0.##########");
145: }
146: }
|