001: /* Longbox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Jun 28 13:39:37 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.util.Locale;
022:
023: import org.zkoss.zk.ui.WrongValueException;
024:
025: import org.zkoss.zul.mesg.MZul;
026: import org.zkoss.zul.impl.NumberInputElement;
027:
028: /**
029: * An edit box for holding an integer.
030: *
031: * @author tomyeh
032: */
033: public class Longbox extends NumberInputElement {
034: public Longbox() {
035: setCols(11);
036: }
037:
038: public Longbox(long value) throws WrongValueException {
039: this ();
040: setValue(new Long(value));
041: }
042:
043: public Longbox(int value) throws WrongValueException {
044: this ();
045: setValue(new Long(value));
046: }
047:
048: /** Returns the value (in Long), might be null unless
049: * a constraint stops it.
050: * @exception WrongValueException if user entered a wrong value
051: */
052: public Long getValue() throws WrongValueException {
053: return (Long) getTargetValue();
054: }
055:
056: /** Returns the value in long. If null, zero is returned.
057: */
058: public long longValue() throws WrongValueException {
059: final Object val = getTargetValue();
060: return val != null ? ((Long) val).longValue() : 0;
061: }
062:
063: /** Returns the value in int. If null, zero is returned.
064: */
065: public long intValue() throws WrongValueException {
066: final Object val = getTargetValue();
067: return val != null ? ((Long) val).intValue() : 0;
068: }
069:
070: /** Sets the value (in Long).
071: * @exception WrongValueException if value is wrong
072: */
073: public void setValue(Long value) throws WrongValueException {
074: validate(value);
075: setRawValue(value);
076: }
077:
078: //-- super --//
079: protected Object coerceFromString(String value)
080: throws WrongValueException {
081: final Object[] vals = toNumberOnly(value);
082: final String val = (String) vals[0];
083: if (val == null || val.length() == 0)
084: return null;
085:
086: try {
087: long v = Long.parseLong(val);
088: int divscale = vals[1] != null ? ((Integer) vals[1])
089: .intValue() : 0;
090: while (v != 0 && --divscale >= 0)
091: v /= 10;
092: return new Long(v);
093: } catch (NumberFormatException ex) {
094: throw showCustomError(new WrongValueException(this ,
095: MZul.NUMBER_REQUIRED, value));
096: }
097: }
098:
099: protected String coerceToString(Object value) {
100: return value != null && getFormat() == null ? value.toString()
101: : formatNumber(value, null);
102: }
103: }
|