001: /* Calendar.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Apr 24 17:12:27 2006, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017: package org.zkoss.zul;
018:
019: import java.util.Date;
020: import java.util.TimeZone;
021: import java.text.DateFormat;
022: import java.text.SimpleDateFormat;
023: import java.text.ParseException;
024:
025: import org.zkoss.lang.Objects;
026: import org.zkoss.util.Dates;
027: import org.zkoss.util.Locales;
028: import org.zkoss.util.TimeZones;
029: import org.zkoss.xml.HTMLs;
030:
031: import org.zkoss.zk.ui.ext.client.InputableX;
032: import org.zkoss.zk.ui.WrongValueException;
033: import org.zkoss.zk.ui.event.Events;
034:
035: import org.zkoss.zul.impl.XulElement;
036:
037: /**
038: * A calendar.
039: *
040: * <p>Default {@link #getSclass}: calendar.
041: *
042: * @author tomyeh
043: */
044: public class Calendar extends XulElement {
045: private TimeZone _tzone;
046: private Date _value;
047: /** The name. */
048: private String _name;
049: private boolean _compact;
050:
051: /** Contructs a calendar whose value is default to today.
052: */
053: public Calendar() {
054: this (null);
055: }
056:
057: public Calendar(Date value) {
058: setSclass("calendar");
059: _value = value != null ? value : Dates.today();
060: _compact = "zh".equals(Locales.getCurrent().getLanguage());
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: /** Returns the value that is assigned to this component, never null.
080: */
081: public Date getValue() {
082: return _value;
083: }
084:
085: /** Assigns a value to this component.
086: * @param value the date to assign. If null, today is assumed.
087: */
088: public void setValue(Date value) {
089: if (value == null)
090: value = Dates.today();
091: if (!value.equals(_value)) {
092: _value = value;
093: smartUpdate("z.value", getDateFormat().format(_value));
094: }
095: }
096:
097: private final DateFormat getDateFormat() {
098: final DateFormat df = new SimpleDateFormat("yyyy/MM/dd",
099: Locales.getCurrent());
100: final TimeZone tz = _tzone != null ? _tzone : TimeZones
101: .getCurrent();
102: df.setTimeZone(tz);
103: return df;
104: }
105:
106: /** Returns whether to use a compact layout.
107: * <p>Default: true if zh_TW or zh_CN; false otherwise.
108: */
109: public boolean isCompact() {
110: return _compact;
111: }
112:
113: /** Sets whether to use a compact layout.
114: */
115: public void setCompact(boolean compact) {
116: if (_compact != compact) {
117: _compact = compact;
118: invalidate();
119: }
120: }
121:
122: /** Returns the name of this component.
123: * <p>Default: null.
124: * <p>The name is used only to work with "legacy" Web application that
125: * handles user's request by servlets.
126: * It works only with HTTP/HTML-based browsers. It doesn't work
127: * with other kind of clients.
128: * <p>Don't use this method if your application is purely based
129: * on ZK's event-driven model.
130: * @since 3.0.0
131: */
132: public String getName() {
133: return _name;
134: }
135:
136: /** Sets the name of this component.
137: * <p>The name is used only to work with "legacy" Web application that
138: * handles user's request by servlets.
139: * It works only with HTTP/HTML-based browsers. It doesn't work
140: * with other kind of clients.
141: * <p>Don't use this method if your application is purely based
142: * on ZK's event-driven model.
143: *
144: * @param name the name of this component.
145: * @since 3.0.0
146: */
147: public void setName(String name) {
148: if (name != null && name.length() == 0)
149: name = null;
150: if (!Objects.equals(_name, name)) {
151: _name = name;
152: smartUpdate("z.name", _name);
153: }
154: }
155:
156: //-- super --//
157: public String getOuterAttrs() {
158: final StringBuffer sb = new StringBuffer(64).append(super
159: .getOuterAttrs());
160:
161: appendAsapAttr(sb, Events.ON_CHANGE);
162:
163: HTMLs.appendAttribute(sb, "z.name", _name);
164: HTMLs.appendAttribute(sb, "z.value", getDateFormat().format(
165: _value));
166: if (_compact)
167: sb.append(" z.compact=\"true\"");
168: return sb.toString();
169: }
170:
171: //-- ComponentCtrl --//
172: protected Object newExtraCtrl() {
173: return new ExtraCtrl();
174: }
175:
176: /** A utility class to implement {@link #getExtraCtrl}.
177: * It is used only by component developers.
178: */
179: protected class ExtraCtrl extends XulElement.ExtraCtrl implements
180: InputableX {
181: //-- InputableX --//
182: public boolean setTextByClient(String value)
183: throws WrongValueException {
184: try {
185: final Date newval = getDateFormat().parse(value);
186: if (!Objects.equals(_value, newval)) {
187: _value = newval;
188: return true;
189: }
190: return false;
191: } catch (ParseException ex) {
192: throw new InternalError(value);
193: }
194: }
195: }
196: }
|