01: /* Intbox.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Jul 5, 2007 5:02:21 PM, Created by henrichen
10: }}IS_NOTE
11:
12: Copyright (C) 2007 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:
20: package org.zkoss.mil;
21:
22: import org.zkoss.mil.impl.InputElement;
23: import org.zkoss.mil.mesg.MMil;
24: import org.zkoss.zk.ui.WrongValueException;
25:
26: /**
27: * A integer input box.
28: * @author henrichen
29: *
30: */
31: public class Intbox extends InputElement {
32: private static final long serialVersionUID = 200707051728L;
33:
34: public Intbox() {
35: setMaxlength(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: setRawValue(value);
63: }
64:
65: //super
66: protected Object coerceFromString(String value)
67: throws WrongValueException {
68: try {
69: int v = Integer.parseInt(value);
70: return new Integer(v);
71: } catch (NumberFormatException ex) {
72: throw new WrongValueException(this , MMil.INTEGER_REQUIRED,
73: value);
74: }
75: }
76:
77: protected String coerceToString(Object value) {
78: return value != null ? value.toString() : "";
79: }
80:
81: protected int getInternalType() {
82: return NUMERIC;
83: }
84:
85: }
|