001: /* BaseIntbox.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.Intbox;
031:
032: /**
033: * The Base implementation of Intbox. 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 BaseIntbox extends BranchInput {
040:
041: /**
042: * Override method, add a {@link IntgerFormatConverter} 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 = ((Intbox) zulcomp).getFormat();
051: converter = new IntgerFormatConverter(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 Integer, or Integer to String by a
070: * {@link java.text.DecimalFormat}.
071: *
072: * @author Dennis.Chen
073: *
074: */
075: static public class IntgerFormatConverter extends FormateHolder
076: implements Converter {
077:
078: private DecimalFormat _formater;
079: static private Pattern pattern = Pattern.compile("\\-*[0-9,]*");
080:
081: public IntgerFormatConverter() {
082: formatChanged();
083: }
084:
085: public IntgerFormatConverter(String format) {
086: super (format);
087: formatChanged();
088: }
089:
090: protected void formatChanged() {
091: if (_formater == null) {
092: _formater = new DecimalFormat();
093: }
094: if (_format == null) {
095: _formater.applyPattern("#0");
096: } else {
097: _formater.applyPattern(_format);
098: }
099: }
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
105: * javax.faces.component.UIComponent, java.lang.String)
106: */
107: public Object getAsObject(FacesContext context,
108: UIComponent component, String value) {
109: try {
110: if (value == null || "".equals(value.trim()))
111: return null;
112:
113: if (!pattern.matcher(value).matches()) {
114: throw new ConverterException("Format Error");
115: }
116: return new Integer(_formater.parse(value).intValue());
117: } catch (Exception e) {
118: throw new ConverterException(e.getMessage(), e);
119: }
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
126: * javax.faces.component.UIComponent, java.lang.Object)
127: */
128: public String getAsString(FacesContext context,
129: UIComponent component, Object value) {
130: if (value == null)
131: return null;
132: try {
133: return _formater.format(value);
134: } catch (Exception e) {
135: throw new ConverterException(e.getMessage(), e);
136: }
137: }
138: }
139:
140: }
|