001: /* BaseCalendar.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:
030: /**
031: * The Base implementation of Calendar.
032: * This component should be declared nested under {@link org.zkoss.jsf.zul.Page}.
033: * @author Dennis.Chen
034: *
035: */
036: abstract public class BaseCalendar extends BranchInput {
037:
038: /**
039: * Override method, add a {@link DateFormatConverter} as the default converter if no converter is assigned.
040: */
041: protected void afterZULComponentComposed(Component zulcomp) {
042:
043: //add default converter before super process attribute.
044: Converter converter = this .getConverter();
045: if (converter == null) {
046: converter = new DateFormatConverter();
047: this .setConverter(converter);
048: }
049:
050: super .afterZULComponentComposed(zulcomp);
051: }
052:
053: /**
054: * this implementation return java.lang.Date which is parsed from value;
055: * @see ValueHolderSupport
056: */
057: public Object transferValueForAttribute(String value) {
058: if (value == null)
059: return null;
060: return getConverter().getAsObject(getFacesContext(), this ,
061: value);
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: }
|