01: /* Intbox.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Tue Jun 28 13:39:37 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.zul;
20:
21: import java.util.Locale;
22:
23: import org.zkoss.zk.ui.WrongValueException;
24:
25: import org.zkoss.zul.mesg.MZul;
26: import org.zkoss.zul.impl.NumberInputElement;
27:
28: /**
29: * An edit box for holding an integer.
30: *
31: * @author tomyeh
32: */
33: public class Intbox extends NumberInputElement {
34: public Intbox() {
35: setCols(11);
36: }
37:
38: public Intbox(int value) throws WrongValueException {
39: this ();
40: setValue(new Integer(value));
41: }
42:
43: /** Returns the value (in Integer), might be null unless
44: * a constraint stops it.
45: * @exception WrongValueException if user entered a wrong value
46: */
47: public Integer getValue() throws WrongValueException {
48: return (Integer) getTargetValue();
49: }
50:
51: /** Returns the value in int. If null, zero is returned.
52: */
53: public int intValue() throws WrongValueException {
54: final Object val = getTargetValue();
55: return val != null ? ((Integer) val).intValue() : 0;
56: }
57:
58: /** Sets the value (in Integer).
59: * @exception WrongValueException if value is wrong
60: */
61: public void setValue(Integer value) throws WrongValueException {
62: validate(value);
63: setRawValue(value);
64: }
65:
66: //-- super --//
67: protected Object coerceFromString(String value)
68: throws WrongValueException {
69: final Object[] vals = toNumberOnly(value);
70: final String val = (String) vals[0];
71: if (val == null || val.length() == 0)
72: return null;
73:
74: try {
75: int v = Integer.parseInt(val);
76: int divscale = vals[1] != null ? ((Integer) vals[1])
77: .intValue() : 0;
78: while (v != 0 && --divscale >= 0)
79: v /= 10;
80: return new Integer(v);
81: } catch (NumberFormatException ex) {
82: throw showCustomError(new WrongValueException(this ,
83: MZul.NUMBER_REQUIRED, value));
84: }
85: }
86:
87: protected String coerceToString(Object value) {
88: return value != null && getFormat() == null ? value.toString()
89: : formatNumber(value, null);
90: }
91: }
|