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