001: /* Datebox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Jul 5, 2007 11:57:36 AM, Created by henrichen
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:
020: package org.zkoss.mil;
021:
022: import java.util.Date;
023: import java.util.TimeZone;
024:
025: import org.zkoss.lang.Objects;
026: import org.zkoss.util.TimeZones;
027: import org.zkoss.xml.HTMLs;
028: import org.zkoss.zk.ui.WrongValueException;
029: import org.zkoss.zk.ui.ext.client.Inputable;
030:
031: /**
032: * Datebox for date, time, and date time entry.
033: * @author henrichen
034: *
035: */
036: public class Datebox extends Item {
037: private static final long serialVersionUID = 200707051515L;
038: private static final int DATE = 1;
039: private static final int TIME = 2;
040: private static final int DATE_TIME = 3;
041:
042: private Date _value = new Date();
043: private TimeZone _tzone;
044:
045: public Datebox() {
046: }
047:
048: public Datebox(Date value) throws WrongValueException {
049: setValue(value);
050: }
051:
052: public void setValue(Date value) {
053: if (!Objects.equals(_value, value)) {
054: _value = value;
055: smartUpdate("dt", "" + (value.getTime() + getTzoneOffset()));
056: }
057: }
058:
059: public Date getValue() {
060: return _value;
061: }
062:
063: /** Returns the time zone that this date box belongs to, or null if
064: * the default time zone is used.
065: * <p>The default time zone is determined by {@link TimeZones#getCurrent}.
066: */
067: public TimeZone getTimeZone() {
068: return _tzone;
069: }
070:
071: /** Sets the time zone that this date box belongs to, or null if
072: * the default time zone is used.
073: * <p>The default time zone is determined by {@link TimeZones#getCurrent}.
074: */
075: public void setTimeZone(TimeZone tzone) {
076: _tzone = tzone;
077: }
078:
079: protected int getTzoneOffset() {
080: final TimeZone tz = _tzone != null ? _tzone : TimeZones
081: .getCurrent();
082: return tz.getRawOffset();
083: }
084:
085: protected int getInputMode() {
086: String mold = getMold();
087: if ("default".equals(mold)) {
088: return DATE;
089: } else if ("time".equals(mold)) {
090: return TIME;
091: } else if ("date_time".equals(mold)) {
092: return DATE_TIME;
093: }
094:
095: //default to DATE mode
096: return DATE;
097: }
098:
099: //-- super --//
100: public String getOuterAttrs() {
101: final StringBuffer sb = new StringBuffer(64).append(super
102: .getOuterAttrs());
103: appendAsapAttr(sb, "onChange");
104: return sb.toString();
105: }
106:
107: public String getInnerAttrs() {
108: final StringBuffer sb = new StringBuffer(64).append(super
109: .getInnerAttrs());
110: if (_value != null) {
111: HTMLs.appendAttribute(sb, "dt", ""
112: + (_value.getTime() + getTzoneOffset())); //text
113: }
114: HTMLs.appendAttribute(sb, "md", getInputMode()); //InputMode
115: final TimeZone tz = _tzone != null ? _tzone : TimeZones
116: .getCurrent();
117: HTMLs.appendAttribute(sb, "tz", tz.getID()); //TimeZone
118: return sb.toString();
119: }
120:
121: //-- ComponentCtrl --//
122: protected Object newExtraCtrl() {
123: return new ExtraCtrl();
124: }
125:
126: /** A utility class to implement {@link #getExtraCtrl}.
127: * It is used only by component developers.
128: */
129: protected class ExtraCtrl implements Inputable {
130: //-- Inputable --//
131: public void setTextByClient(String value)
132: throws WrongValueException {
133: _value = new Date(Long.parseLong(value) - getTzoneOffset());
134: }
135: }
136: }
|