001: /* BaseDecimalbox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Aug 8, 2007 5:48:27 PM 2007, Created by Dennis.Chen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 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.jsf.zul.impl;
020:
021: import java.math.BigDecimal;
022: import java.text.DecimalFormat;
023: import java.util.regex.Pattern;
024:
025: import javax.faces.component.UIComponent;
026: import javax.faces.context.FacesContext;
027: import javax.faces.convert.Converter;
028: import javax.faces.convert.ConverterException;
029:
030: import org.zkoss.zk.ui.Component;
031: import org.zkoss.zul.Decimalbox;
032:
033: /**
034: * The Base implementation of Decimalbox. This component should be declared
035: * nested under {@link org.zkoss.jsf.zul.Page}.
036: *
037: * @author Dennis.Chen
038: *
039: */
040: abstract public class BaseDecimalbox extends BranchInput {
041:
042: /**
043: * Override method, add a {@link DecimalFormatConverter} as the default
044: * converter if no converter is assigned.
045: */
046: protected void afterZULComponentComposed(Component zulcomp) {
047:
048: // add default converter before super process attribute.
049: Converter converter = this .getConverter();
050: if (converter == null) {
051: String format = ((Decimalbox) zulcomp).getFormat();
052: converter = new DecimalFormatConverter(format);
053: this .setConverter(converter);
054: }
055:
056: super .afterZULComponentComposed(zulcomp);
057: }
058:
059: /**
060: * Override Method, Return ZUL Component attribute name which can handler
061: * the submitting of input. Always return "text"
062: *
063: * @see ClientInputSupport
064: */
065: public String getMappedAttributeName() {
066: return "text";
067: }
068:
069: /**
070: * Converter String to BigDecmial, or BigDecmial to String by a
071: * {@link java.text.DecimalFormat}.
072: *
073: * @author Dennis.Chen
074: *
075: */
076: static public class DecimalFormatConverter extends FormateHolder
077: implements Converter {
078:
079: private DecimalFormat _formater;
080: static private Pattern pattern = Pattern
081: .compile("\\-*[0-9,\\.]*");
082:
083: public DecimalFormatConverter() {
084: formatChanged();
085: }
086:
087: public DecimalFormatConverter(String format) {
088: super (format);
089: formatChanged();
090: }
091:
092: protected void formatChanged() {
093: if (_formater == null) {
094: _formater = new DecimalFormat();
095: }
096: if (_format == null) {
097: _formater.applyPattern("#0.##########");
098: } else {
099: _formater.applyPattern(_format);
100: }
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
107: * javax.faces.component.UIComponent, java.lang.String)
108: */
109: public Object getAsObject(FacesContext context,
110: UIComponent component, String value) {
111: try {
112: if (value == null || "".equals(value.trim()))
113: return null;
114:
115: if (!pattern.matcher(value).matches()) {
116: throw new ConverterException("Format Error");
117: }
118: BigDecimal bd = new BigDecimal(value);
119: return bd;
120: } catch (Exception e) {
121: throw new ConverterException(e.getMessage(), e);
122: }
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
129: * javax.faces.component.UIComponent, java.lang.Object)
130: */
131: public String getAsString(FacesContext context,
132: UIComponent component, Object value) {
133: if (value == null)
134: return null;
135: try {
136: return _formater.format(value);
137: } catch (Exception e) {
138: throw new ConverterException(e.getMessage(), e);
139: }
140: }
141: }
142:
143: }
|