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;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.core.Function;
013: import com.gwtext.client.util.JavaScriptObjectHelper;
014: import com.gwtext.client.widgets.event.DatePickerListener;
015:
016: import java.util.Date;
017:
018: /**
019: * Simple date picker class.
020: */
021: public class DatePicker extends Component {
022:
023: private static JavaScriptObject configPrototype;
024: private Date initDate;
025:
026: static {
027: init();
028: }
029:
030: private static native void init()/*-{
031: var c = new $wnd.Ext.DatePicker();
032: @com.gwtext.client.widgets.DatePicker::configPrototype = c.initialConfig;
033: }-*/;
034:
035: protected JavaScriptObject getConfigPrototype() {
036: return configPrototype;
037: }
038:
039: public String getXType() {
040: return "datepicker";
041: }
042:
043: /**
044: * Create a new DatePicker.
045: */
046: public DatePicker() {
047: }
048:
049: public DatePicker(JavaScriptObject jsObj) {
050: super (jsObj);
051: }
052:
053: protected native JavaScriptObject create(JavaScriptObject config) /*-{
054: return new $wnd.Ext.DatePicker(config);
055: }-*/;
056:
057: private static DatePicker instance(JavaScriptObject jsObj) {
058: return new DatePicker(jsObj);
059: }
060:
061: /**
062: * Get the value of the date picker.
063: *
064: * @return null if no value selected
065: */
066: public Date getValue() {
067: if (!isRendered()) {
068: return initDate;
069: } else {
070: long time = getValueMillis(getOrCreateJsObj());
071: return time == -1 ? null : new Date(time);
072: }
073: }
074:
075: private native long getValueMillis(JavaScriptObject dp)/*-{
076: var date = dp.getValue();
077: return (date == '' || date == null) ? -1 : date.getTime();
078: }-*/;
079:
080: /**
081: * Set the value of the date picker.
082: *
083: * @param date the date
084: */
085: public void setValue(final Date date) {
086: if (!isRendered()) {
087: initDate = date;
088: addListener("render", new Function() {
089: public void execute() {
090: setValue(date);
091: }
092: });
093: }
094: setValueMillis(getOrCreateJsObj(), date.getTime());
095: }
096:
097: private native void setValueMillis(JavaScriptObject dp, long time) /*-{
098: var date = new $wnd.Date();
099: date.setTime(time);
100: dp.setValue(date);
101: }-*/;
102:
103: /**
104: * Add a Date Picker Listener.
105: *
106: * @param listener the listener
107: */
108: public native void addListener(DatePickerListener listener) /*-{
109: this.@com.gwtext.client.widgets.Component::addListener(Lcom/gwtext/client/widgets/event/ComponentListener;)(listener);
110: var componentJ = this;
111:
112: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('select',
113: function(datePicker, date) {
114: var d = @com.gwtext.client.util.DateUtil::create(J)(date.getTime());
115: listener.@com.gwtext.client.widgets.event.DatePickerListener::onSelect(Lcom/gwtext/client/widgets/DatePicker;Ljava/util/Date;)(componentJ, d);
116: }
117: );
118: }-*/;
119:
120: // --- config properties ---
121:
122: //TODO this property is noed being used in Ext 2.0 final
123: //http://extjs.com/forum/showthread.php?t=23365
124: /**
125: * True to constrain the date picker to the viewport (defaults to true).
126: *
127: * @param constrainToViewport true to constrain to view port
128: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
129: */
130: public void setConstrainToViewport(boolean constrainToViewport)
131: throws IllegalStateException {
132: setAttribute("constrainToViewport", constrainToViewport, true);
133: }
134:
135: /**
136: * @return true if constrin to viewport
137: */
138: public boolean getConstrainToViewport() {
139: return JavaScriptObjectHelper.getAttributeAsBoolean(config,
140: "constrainToViewport");
141: }
142:
143: /**
144: * Regular expression used to disable a pattern of dates.
145: *
146: * @param disabledDatesRE regular expression to disable dates
147: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
148: */
149: public void setDisabledDatesRE(String disabledDatesRE)
150: throws IllegalStateException {
151: check();
152: doSetDisabledDatesRE(config, disabledDatesRE);
153: }
154:
155: private native void doSetDisabledDatesRE(JavaScriptObject config,
156: String disabledDatesRE) /*-{
157: config['disabledDatesRE'] = new RegExp(disabledDatesRE);
158: }-*/;
159:
160: /**
161: * The tooltip text to display when the date falls on a disabled date (defaults to "").
162: *
163: * @param disabledDatesText disabled dates text
164: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
165: */
166: public void setDisabledDatesText(String disabledDatesText)
167: throws IllegalStateException {
168: setAttribute("disabledDatesText", disabledDatesText, true);
169: }
170:
171: /**
172: * The tooltip text to display when the date falls on a disabled date (defaults to "").
173: *
174: * @return the disabled dates text
175: */
176: public String getDisabledDatesText() {
177: return JavaScriptObjectHelper.getAttribute(config,
178: "disabledDatesText");
179: }
180:
181: /**
182: * An array of days to disable, 0-based. For example, [0, 6] disables Sunday and Saturday.
183: *
184: * @param disabledDays dates to disable
185: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
186: */
187: public void setDisabledDays(String[] disabledDays)
188: throws IllegalStateException {
189: setAttribute("disabledDays", JavaScriptObjectHelper
190: .convertToJavaScriptArray(disabledDays), true);
191: }
192:
193: /**
194: * An array of days to disable, 0-based. For example, [0, 6] disables Sunday and Saturday.
195: *
196: * @return the disabled days
197: */
198: public String[] getDisabledDays() {
199: return JavaScriptObjectHelper.getAttributeAsStringArray(config,
200: "disabledDays");
201: }
202:
203: /**
204: * The tooltip to display when the date falls on a disabled day (defaults to "")
205: *
206: * @param disabledDaysText disabled days text
207: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
208: */
209: public void setDisabledDaysText(String disabledDaysText)
210: throws IllegalStateException {
211: setAttribute("disabledDaysText", disabledDaysText, true);
212: }
213:
214: /**
215: * The tooltip to display when the date falls on a disabled day (defaults to "").
216: *
217: * @return the disabled days text
218: */
219: public String getDisabledDaysText() {
220: return JavaScriptObjectHelper.getAttribute(config,
221: "disabledDaysText");
222: }
223:
224: /**
225: * The default date format string which can be overriden for localization support. The format must be a valid pattern (defaults to 'm/d/y').
226: *
227: * @param format the date format
228: * @see com.gwtext.client.util.DateUtil
229: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
230: */
231: public void setFormat(String format) throws IllegalStateException {
232: setAttribute("format", format, true);
233: }
234:
235: /**
236: * The default date format string which can be overriden for localization support. The format must be a valid pattern (defaults to 'm/d/y').
237: *
238: * @return the date format
239: */
240: public String getFormat() {
241: return JavaScriptObjectHelper.getAttribute(config, "format");
242: }
243:
244: /**
245: * Maximum allowable date.
246: *
247: * @param maxDate the max date
248: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
249: */
250: public void setMaxDate(Date maxDate) throws IllegalStateException {
251: setAttribute("maxDate", maxDate, true);
252: }
253:
254: /**
255: * Maximum allowable date.
256: *
257: * @return the max date
258: */
259: public Date getMaxDate() {
260: return JavaScriptObjectHelper.getAttributeAsDate(config,
261: "maxDate");
262: }
263:
264: /**
265: * The error text to display if the maxDate validation fails (defaults to "This date is after the maximum date").
266: *
267: * @param maxText the max error text
268: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
269: */
270: public void setMaxText(String maxText) throws IllegalStateException {
271: setAttribute("maxText", maxText, true);
272: }
273:
274: /**
275: * The error text to display if the maxDate validation fails (defaults to "This date is after the maximum date").
276: *
277: * @return the error text to display if the maxDate validation fails
278: */
279: public String getMaxText() {
280: return JavaScriptObjectHelper.getAttribute(config, "maxText");
281: }
282:
283: /**
284: * Minimum allowable date.
285: *
286: * @param minDate the min date
287: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
288: */
289: public void setMinDate(Date minDate) throws IllegalStateException {
290: setAttribute("minDate", minDate, true);
291: }
292:
293: /**
294: * Minimum allowable date.
295: *
296: * @return the min date
297: */
298: public Date getMinDate() {
299: return JavaScriptObjectHelper.getAttributeAsDate(config,
300: "minDate");
301: }
302:
303: /**
304: * The error text to display if the minDate validation fails (defaults to "This date is before the minimum date").
305: *
306: * @param minText the min error text
307: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
308: */
309: public void setMinText(String minText) throws IllegalStateException {
310: setAttribute("minText", minText, true);
311: }
312:
313: /**
314: * The error text to display if the minDate validation fails (defaults to "This date is before the minimum date").
315: *
316: * @return the error text to display if the minDate validation fails
317: */
318: public String getMinText() {
319: return JavaScriptObjectHelper.getAttribute(config, "minText");
320: }
321:
322: /**
323: * An array of textual month names which can be overriden for localization support.
324: *
325: * @param monthNames the month names
326: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
327: */
328: public void setMonthNames(String[] monthNames)
329: throws IllegalStateException {
330: setAttribute("monthNames", JavaScriptObjectHelper
331: .arrayConvert(monthNames), true);
332: }
333:
334: /**
335: * An array of textual month names which can be overriden for localization support.
336: *
337: * @return the month names
338: */
339: public String[] getMonthNames() {
340: return JavaScriptObjectHelper.getAttributeAsStringArray(config,
341: "monthNames");
342: }
343:
344: /**
345: * The header month selector tooltip (defaults to 'Choose a month (Control+Up/Down to move years)').
346: *
347: * @param monthYearText the header month tooltip text
348: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
349: */
350: public void setMonthYearText(String monthYearText)
351: throws IllegalStateException {
352: setAttribute("monthYearText", monthYearText, true);
353: }
354:
355: /**
356: * The header month selector tooltip (defaults to 'Choose a month (Control+Up/Down to move years)').
357: *
358: * @return the header month selector tooltip text
359: */
360: public String getMonthYearText() {
361: return JavaScriptObjectHelper.getAttribute(config,
362: "monthYearText");
363: }
364:
365: /**
366: * The next month navigation button tooltip (defaults to 'Next Month (Control+Right)').
367: *
368: * @param nextText the next button tooltip text
369: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
370: */
371: public void setNextText(String nextText)
372: throws IllegalStateException {
373: setAttribute("nextText", nextText, true);
374: }
375:
376: /**
377: * The next month navigation button tooltip (defaults to 'Next Month (Control+Right)').
378: *
379: * @return the next month navigation button tooltip
380: */
381: public String getNextText() {
382: return JavaScriptObjectHelper.getAttribute(config, "nextText");
383: }
384:
385: /**
386: * The text to display on the ok button.
387: *
388: * @param okText the OK text
389: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
390: */
391: public void setOkText(String okText) throws IllegalStateException {
392: setAttribute("okText", okText, true);
393: }
394:
395: /**
396: * The text to display on the ok button.
397: *
398: * @return the text for the ok button
399: */
400: public String getOkText() {
401: return JavaScriptObjectHelper.getAttribute(config, "okText");
402: }
403:
404: /**
405: * The previous month navigation button tooltip (defaults to 'Previous Month (Control+Left)').
406: *
407: * @param prevText the previous month tooltip text
408: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
409: */
410: public void setPrevText(String prevText)
411: throws IllegalStateException {
412: setAttribute("prevText", prevText, true);
413: }
414:
415: /**
416: * The previous month navigation button tooltip (defaults to 'Previous Month (Control+Left)').
417: *
418: * @return the previous month navigation button tooltip
419: */
420: public String getPrevText() {
421: return JavaScriptObjectHelper.getAttribute(config, "prevText");
422: }
423:
424: /**
425: * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday).
426: *
427: * @param startDay the start day
428: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
429: */
430: public void setStartDay(int startDay) throws IllegalStateException {
431: setAttribute("startDay", startDay, true);
432: }
433:
434: /**
435: * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday).
436: *
437: * @return the day index at which the week should begin, 0-based
438: */
439: public int getStartDay() {
440: return JavaScriptObjectHelper.getAttributeAsInt(config,
441: "startDay");
442: }
443:
444: /**
445: * The text to display on the button that selects the current date (defaults to "Today").
446: *
447: * @param todayText the today text
448: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
449: */
450: public void setTodayText(String todayText)
451: throws IllegalStateException {
452: setAttribute("todayText", todayText, true);
453: }
454:
455: /**
456: * The text to display on the button that selects the current date (defaults to "Today").
457: *
458: * @return the text to display on the button that selects the current date
459: */
460: public String getTodayText() {
461: return getAttribute("todayText");
462: }
463:
464: /**
465: * The tooltip to display for the button that selects the current date (defaults to "{current date} (Spacebar)").
466: *
467: * @param todayTip the tooltip for current date
468: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
469: */
470: public void setTodayTip(String todayTip)
471: throws IllegalStateException {
472: //todo can handle internally
473: setAttribute("todayTip", todayTip, true);
474: }
475:
476: /**
477: * The tooltip to display for the button that selects the current date (defaults to "{current date} (Spacebar)").
478: *
479: * @return the tooltip to display for the button that selects the current date
480: */
481: public String getTodayTip() {
482: return getAttribute("todayTip");
483: }
484: }
|