001: /* BaseSlider.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.jsf.zul.impl.BaseDatebox.DateFormatConverter;
030: import org.zkoss.zk.ui.Component;
031:
032: /**
033: * The Base implementation of Slider. 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 BaseSlider extends BranchInput {
040:
041: /**
042: * Override method, add a {@link DateFormatConverter} 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: converter = new IntgerFormatConverter();
051: this .setConverter(converter);
052: }
053:
054: super .afterZULComponentComposed(zulcomp);
055: }
056:
057: /**
058: * Override Method, Return ZUL Component attribute name which can handler
059: * the submitting of input. Always return "curpos"
060: *
061: * @see ClientInputSupport
062: */
063: public String getMappedAttributeName() {
064: return "curpos";
065: }
066:
067: /**
068: * Converter String to Integer, or Integer to String by a
069: * {@link java.text.DecimalFormat}.
070: *
071: * @author Dennis.Chen
072: *
073: */
074: static public class IntgerFormatConverter extends FormateHolder
075: implements Converter {
076:
077: private DecimalFormat _formater;
078: static private Pattern pattern = Pattern.compile("\\-*[0-9]*");
079:
080: public IntgerFormatConverter() {
081: formatChanged();
082: }
083:
084: public IntgerFormatConverter(String format) {
085: super (format);
086: formatChanged();
087: }
088:
089: protected void formatChanged() {
090: if (_formater == null) {
091: _formater = new DecimalFormat();
092: }
093: if (_format == null) {
094: _formater.applyPattern("#0");
095: } else {
096: _formater.applyPattern(_format);
097: }
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
104: * javax.faces.component.UIComponent, java.lang.String)
105: */
106: public Object getAsObject(FacesContext context,
107: UIComponent component, String value) {
108: try {
109: if (value == null || "".equals(value.trim()))
110: return null;
111:
112: if (!pattern.matcher(value).matches()) {
113: throw new ConverterException("Format Error");
114: }
115: return new Integer(_formater.parse(value).intValue());
116: } catch (Exception e) {
117: throw new ConverterException(e.getMessage(), e);
118: }
119: }
120:
121: /*
122: * (non-Javadoc)
123: *
124: * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
125: * javax.faces.component.UIComponent, java.lang.Object)
126: */
127: public String getAsString(FacesContext context,
128: UIComponent component, Object value) {
129: if (value == null)
130: return null;
131: try {
132: return _formater.format(value);
133: } catch (Exception e) {
134: throw new ConverterException(e.getMessage(), e);
135: }
136: }
137: }
138:
139: }
|