001: /* Timebox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Jul 9, 2007 10:03:38 AM , 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.zul;
020:
021: import java.text.DateFormat;
022: import java.text.ParseException;
023: import java.text.SimpleDateFormat;
024: import java.util.Date;
025: import java.util.TimeZone;
026:
027: import org.zkoss.lang.Objects;
028: import org.zkoss.util.Locales;
029: import org.zkoss.util.TimeZones;
030:
031: import org.zkoss.xml.HTMLs;
032: import org.zkoss.zk.ui.WrongValueException;
033: import org.zkoss.zul.impl.InputElement;
034: import org.zkoss.zul.mesg.MZul;
035:
036: /**
037: * An edit box for holding a time (a java.util.Date Object , but only Hour & Minute are used.
038: *
039: * <p>Default {@link #getSclass}: datebox.
040: *
041: * <p>timebox doens't support customized format. It support HH:mm formate, where HH is hour of day and mm is minute of hour.
042: *
043: * <p>timebox supports below key events.
044: * <lu>
045: * <li>0-9 : set the time digit depend on the position on the inner text box.
046: * <li>up : increase time digit depend on the position on the inner text box.
047: * <li>down : decrease time digit depend on the position on the inner text box.
048: * <li>delete : clear the time to empty (null)
049: * </lu>
050: * @author Dennis Chen
051: * @since 3.0.0
052: */
053: public class Timebox extends InputElement {
054: private static final String DEFAULT_IMAGE = "~./zul/img/updnbtn.gif";
055: private String _img;
056: private TimeZone _tzone;
057: private boolean _btnVisible = true;
058:
059: public Timebox() {
060: setCols(5);
061: setMaxlength(5);
062: }
063:
064: public Timebox(Date date) throws WrongValueException {
065: this ();
066: setValue(date);
067: }
068:
069: /** Returns the value (in Date), might be null unless
070: * a constraint stops it. And, only Hour and Mintue field is effective.
071: * @exception WrongValueException if user entered a wrong value
072: */
073: public Date getValue() throws WrongValueException {
074: return (Date) getTargetValue();
075: }
076:
077: /** Sets the value (in Date).
078: * If value is null, then an empty will be sent(render) to client.
079: * If else, only the Hour and Mintue field will be sent(render) to client.
080: *
081: * @exception WrongValueException if value is wrong
082: */
083: public void setValue(Date value) throws WrongValueException {
084: validate(value);
085: setRawValue(value);
086: }
087:
088: /** Returns whether the button (on the right of the textbox) is visible.
089: * <p>Default: true.
090: */
091: public boolean isButtonVisible() {
092: return _btnVisible;
093: }
094:
095: /** Sets whether the button (on the right of the textbox) is visible.
096: */
097: public void setButtonVisible(boolean visible) {
098: if (_btnVisible != visible) {
099: _btnVisible = visible;
100: smartUpdate("z.btnVisi", visible);
101: }
102: }
103:
104: /** Returns the URI of the button image.
105: */
106: public String getImage() {
107: return _img != null ? _img : DEFAULT_IMAGE;
108: }
109:
110: /** Sets the URI of the button image.
111: *
112: * @param img the URI of the button image. If null or empty, the default
113: * URI is used.
114: */
115: public void setImage(String img) {
116: if (img != null
117: && (img.length() == 0 || DEFAULT_IMAGE.equals(img)))
118: img = null;
119: if (!Objects.equals(_img, img)) {
120: _img = img;
121: invalidate();
122: }
123: }
124:
125: public String getInnerAttrs() {
126: final String attrs = super .getInnerAttrs();
127: final String style = getInnerStyle();
128: return style.length() > 0 ? attrs + " style=\"" + style + '"'
129: : attrs;
130: }
131:
132: private String getInnerStyle() {
133: final StringBuffer sb = new StringBuffer(32).append(HTMLs
134: .getTextRelevantStyle(getRealStyle()));
135: HTMLs.appendStyle(sb, "width", getWidth());
136: HTMLs.appendStyle(sb, "height", getHeight());
137: return sb.toString();
138: }
139:
140: /** Returns the time zone that this time box belongs to, or null if
141: * the default time zone is used.
142: * <p>The default time zone is determined by {@link TimeZones#getCurrent}.
143: */
144: public TimeZone getTimeZone() {
145: return _tzone;
146: }
147:
148: /** Sets the time zone that this time box belongs to, or null if
149: * the default time zone is used.
150: * <p>The default time zone is determined by {@link TimeZones#getCurrent}.
151: */
152: public void setTimeZone(TimeZone tzone) {
153: _tzone = tzone;
154: }
155:
156: protected Object coerceFromString(String value)
157: throws WrongValueException {
158: //null or empty string,
159: if (value == null || value.length() == 0) {
160: return null;
161: }
162:
163: final DateFormat df = getDateFormat();
164: final Date date;
165: try {
166: date = df.parse(value);
167: } catch (ParseException ex) {
168: throw showCustomError(new WrongValueException(this ,
169: MZul.DATE_REQUIRED, new Object[] { value, "HH:mm" }));
170: }
171: return date;
172: }
173:
174: protected String coerceToString(Object value) {
175: if (value == null)
176: return "";
177: final DateFormat df = getDateFormat();
178: return value != null ? df.format((Date) value) : "";
179: }
180:
181: /** Returns the date format of the time only,
182: *
183: * <p>Default: it uses SimpleDateFormat to format the date.
184: */
185: protected DateFormat getDateFormat() {
186: String fmt = "HH:mm";
187: final DateFormat df = new SimpleDateFormat(fmt, Locales
188: .getCurrent());
189: final TimeZone tz = _tzone != null ? _tzone : TimeZones
190: .getCurrent();
191: df.setTimeZone(tz);
192: return df;
193: }
194: }
|