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