001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets.form;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.google.gwt.user.client.Element;
013: import com.gwtext.client.util.JavaScriptObjectHelper;
014:
015: import java.util.Date;
016:
017: /**
018: * Provides a date input field with {@link com.gwtext.client.widgets.DatePicker} dropdown and automatic date validation.
019: */
020: public class DateField extends TextField {
021:
022: /**
023: * Construct a new DateField.
024: */
025: public DateField() {
026: }
027:
028: /**
029: * Construct a new DateField.
030: *
031: * @param fieldLabel the field label
032: * @param name the field name
033: * @param width the field width
034: */
035: public DateField(String fieldLabel, String name, int width) {
036: super (fieldLabel, name, width);
037: }
038:
039: /**
040: * Construct a new DateField.
041: *
042: * @param label the field label value
043: * @param format the date format value
044: */
045: public DateField(String label, String format) {
046: setFieldLabel(label);
047: setFormat(format);
048: }
049:
050: public DateField(JavaScriptObject jsObj) {
051: super (jsObj);
052: }
053:
054: protected native JavaScriptObject create(JavaScriptObject jsObj) /*-{
055: return new $wnd.Ext.form.DateField(jsObj);
056: }-*/;
057:
058: /**
059: * Returns the current date value of the date field.
060: *
061: * @return the date value
062: */
063: public Date getValue() {
064: long date = getTime(getOrCreateJsObj());
065: return date == -1 ? null : new Date(date);
066: }
067:
068: protected native Element getElement(JavaScriptObject jsObj) /*-{
069: //for trigger fields, we want the text area as well as the trigger button to be treated as the element
070: //unit
071: var extEl = jsObj.wrap;
072: if(extEl == null || extEl === undefined) {
073: return null;
074: }
075: var el = extEl.dom;
076: if(el == null || el === undefined) {
077: return null;
078: //forms buttons are detached when initially added
079: } else {
080: //There's an inconsistency in Ext where most elements have the property 'el' set to Ext's Element
081: //with the exception of Menu->Item, Menu->Separator, Menu->TextItem, Toolbar.Item and subclasses
082: //(Toolbar.Separator, Toolbar.Spacer, Toolbar.TextItem) where the 'el' property is set to
083: //the DOM element itself. Therefore retruning 'el' if 'el' is not Ext's Element. See details in issue 39.
084: return el.dom || el ;
085: }
086: }-*/;
087:
088: /**
089: * Sets the value of the date field.
090: *
091: * @param date the date value
092: */
093: public void setValue(Date date) {
094: long time = date.getTime();
095: setTime(getOrCreateJsObj(), time);
096: }
097:
098: private native void setTime(JavaScriptObject df, long time)/*-{
099: df.setValue(new $wnd.Date(time));
100: }-*/;
101:
102: private native long getTime(JavaScriptObject df)/*-{
103: //ext 1.1rc1 returns empty string.
104: var val = df.getValue();
105: return (val == '' || val == null || val === undefined)? -1 : df.getValue().getTime();
106: }-*/;
107:
108: //-- config properties ---
109:
110: public String getXType() {
111: return "datefield";
112: }
113:
114: public void setAltFormats(String altFormats) {
115: setAttribute("altFormats", altFormats, true, true);
116: }
117:
118: public void setDisabledDates(String[] disabledDates) {
119: setAttribute("disabledDates", disabledDates, true, true);
120: }
121:
122: public void setDisabledDatesText(String disabledDatesText) {
123: setAttribute("disabledDatesText", disabledDatesText, true, true);
124: }
125:
126: public void setDisabledDays(int[] disabledDays) {
127: setAttribute("disabledDays", disabledDays, true, true);
128: }
129:
130: public void setDisabledDaysText(String disabledDaysText) {
131: setAttribute("disabledDaysText", disabledDaysText, true, true);
132: }
133:
134: public void setFormat(String format) {
135: setAttribute("format", format, true, true);
136: }
137:
138: public void setInvalidText(String invalidText) {
139: setAttribute("invalidText", invalidText, true, true);
140: }
141:
142: public void setMinValue(String minValue)
143: throws IllegalArgumentException {
144: setAttribute("minValue", minValue, true);
145: }
146:
147: public void setMinValue(Date minValue)
148: throws IllegalArgumentException {
149: setAttribute("minValue", minValue, true);
150: }
151:
152: public void setMinText(String minText) {
153: setAttribute("minText", minText, true, true);
154: }
155:
156: public void setMaxValue(String maxValue)
157: throws IllegalArgumentException {
158: setAttribute("maxValue", maxValue, true);
159: }
160:
161: public void setMaxValue(Date maxValue)
162: throws IllegalArgumentException {
163: setAttribute("maxValue", maxValue, true);
164: }
165:
166: public void setMaxText(String maxText) {
167: setAttribute("maxText", maxText, true, true);
168: }
169: }
|