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