001: /* Doublebox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Sat Oct 14 12:59:39 2006, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2006 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 org.zkoss.zk.ui.WrongValueException;
022:
023: import org.zkoss.zul.mesg.MZul;
024: import org.zkoss.zul.impl.NumberInputElement;
025:
026: /**
027: * An edit box for holding an float point value (double).
028: *
029: * @author henrichen
030: */
031: public class Doublebox extends NumberInputElement {
032: public Doublebox() {
033: setCols(11);
034: }
035:
036: public Doublebox(double value) throws WrongValueException {
037: this ();
038: setValue(value);
039: }
040:
041: public Doublebox(Double value) throws WrongValueException {
042: this ();
043: setValue(value);
044: }
045:
046: /** Returns the value (in Double), might be null unless
047: * a constraint stops it.
048: * @exception WrongValueException if user entered a wrong value
049: */
050: public Double getValue() throws WrongValueException {
051: return (Double) getTargetValue();
052: }
053:
054: /** Returns the value in double. If null, zero is returned.
055: */
056: public double doubleValue() throws WrongValueException {
057: final Object val = getTargetValue();
058: return val != null ? ((Double) val).doubleValue() : 0.0;
059: }
060:
061: /** Returns the value in integer. If null, zero is returned.
062: */
063: public int intValue() throws WrongValueException {
064: final Object val = getTargetValue();
065: return val != null ? ((Double) val).intValue() : 0;
066: }
067:
068: /** Returns the value in long. If null, zero is returned.
069: */
070: public long longValue() throws WrongValueException {
071: final Object val = getTargetValue();
072: return val != null ? ((Double) val).longValue() : 0;
073: }
074:
075: /** Returns the value in short. If null, zero is returned.
076: */
077: public short shortValue() throws WrongValueException {
078: final Object val = getTargetValue();
079: return val != null ? ((Double) val).shortValue() : 0;
080: }
081:
082: /** Sets the value (in Double).
083: * @exception WrongValueException if value is wrong
084: */
085: public void setValue(Double value) throws WrongValueException {
086: validate(value);
087: setRawValue(value);
088: }
089:
090: /** Sets the value (in double)
091: * @exception WrongValueException if value is wrong
092: */
093: public void setValue(double value) throws WrongValueException {
094: setValue(new Double(value));
095: }
096:
097: //-- super --//
098: protected Object coerceFromString(String value)
099: throws WrongValueException {
100: final Object[] vals = toNumberOnly(value);
101: final String val = (String) vals[0];
102: if (val == null || val.length() == 0)
103: return null;
104:
105: try {
106: double v = Double.parseDouble(val);
107: int divscale = vals[1] != null ? ((Integer) vals[1])
108: .intValue() : 0;
109: if (divscale > 0)
110: v /= Math.pow(10, divscale);
111: return new Double(v);
112: } catch (NumberFormatException ex) {
113: throw showCustomError(new WrongValueException(this ,
114: MZul.NUMBER_REQUIRED, value));
115: }
116: }
117:
118: protected String coerceToString(Object value) {
119: return formatNumber(value, "0.##########");
120: }
121: }
|