001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.component;
042:
043: import java.io.IOException;
044: import javax.faces.component.UIComponent;
045: import javax.faces.context.FacesContext;
046: import javax.faces.el.MethodBinding;
047: import javax.faces.el.ValueBinding;
048:
049: /**
050: * <p>Use the <code>ui:calendar</code> when the user needs to select a
051: * date. The calendar component displays a text field that expects a
052: * date as input, together with an icon that when clicked displays a
053: * small calendar. The user can either type directly into the
054: * textfield or select a date from the calendar display.
055: * </p>
056: *
057: * <h3>HTML Elements and Layout</h3>
058: * <p>
059: * The component renders several elements: an optional
060: * <code><label></code>, an <code><input type="text"></code>
061: * and an <code><img></code> element for the icon. They are laid
062: * out inside a HTML <code><table></code>. </p> <p> The pop-up
063: * calendar is a complex component also laid out using a HTML
064: * <code><table></code>. It has child components corresponding to
065: * <code><ui:dropDown></code> and
066: * <code><ui:iconHyperlink></code> (please see these for details)
067: * and anchors <code><a></code> to represent the dates and the
068: * "close" button. </p>
069: *
070: * <h3>Configuring the <code>ui:calendar</code> tag </h3>
071: *
072: * <p>Use the <code>selectedDate</code> attribute to associate the
073: * component with a model object that represents the current value, by
074: * setting the attribute's value to an EL expression that corresponds to
075: * a property of a backing bean.</p>
076: *
077: * <p>By default, the component accepts dates between the current date
078: * and four years out. The years shown in the popup calendar reflect
079: * this range. If a date outside of the range is entered into the
080: * textfield, the component indicates a validation error. To specify
081: * a different range of date, use the <code>minDate</code> and
082: * <code>maxDate</code> attributes.
083: *
084: * <p>To optionally specify a label for the component, use the
085: * <code>label</code> attribute, or specify a label facet. </p>
086: *
087: * <h3>Facets</h3>
088: *
089: * <ul>
090: * <li><code>label</code>: use this facet to specify a custom
091: * component for the label.</li>
092: * <li><code>readOnly</code>: use this facet to specify a custom
093: * component for displaying the value of this component when it is marked as readonly. The default is a <code>ui:staticText</code>. </li>
094: * </ul>
095: *
096: *
097: * <h3>Client-side JavaScript functions</h3>
098: *
099: * <p>In all the functions below, <code><id></code> should be
100: * the generated id of the TextField component. </p>
101: *
102: * <table cellpadding="2" cellspacing="2" border="1"
103: * style="text-align: left; width: 100%;">
104: * <tbody>
105: * <tr>
106: * <td style="vertical-align">
107: * <code>[JSOBJECT_NAME]_setDisabled(<id>, <disabled>)</code>
108: * </td>
109: * <td style="vertical-align: top">
110: * Enable/disable the field. Set <code><disabled></code>
111: * to true to disable the component, or false to enable it.
112: * </td>
113: * </tr>
114: * <tr>
115: * <td style="vertical-align: top">
116: * <code>component_setVisible(<id>)</code>
117: * </td>
118: * <td style="vertical-align: top">Hide or show this component.
119: * </td>
120: * </tr>
121: * </tbody>
122: * </table>
123: *
124: * <h3>Examples</h3>
125: *
126: * <h4>Example 1: Basic Popup Calendar</h4>
127: *
128: * <p>The component gets the options from a managed bean called
129: * <code>CalendarBean</code>. The value of the component
130: * <code>selectedDate</code> is bound to a property of the managed
131: * bean. A label for the component as a whole (<code>label</code>) is
132: * shown next to the component.
133: * </p>
134: *
135: *
136: * <p>
137: * This example shows how to create a simple calendar.
138: * </p>
139: * <p>
140: * <pre><ui:calendar id="startDate"
141: * selectedDate="#{CalendarBean.startDate}"
142: * label="Start Date: " /></pre>
143: * </p>
144: *
145: * <p>Code for the managed bean:<p>
146: *
147: * <h4>CalendarBean.java</h4>
148: *
149: * <code>import java.io.Serializable;<br>
150: * import java.util.Date;<br>
151: * import java.util.Calendar;<br>
152: * import javax.faces.event.ValueChangeEvent;<br>
153: * <br>
154: * <br>
155: * public class CalendarBean {<br>
156: * <br>
157: * public CalendarBean() {<br>
158: * } <br>
159: * <br>
160: * private Date startDate = null;<br>
161: * <br>
162: * public Date getStartDate() {<br>
163: * <br>
164: *
165: * return this.startDate;<br>
166: * }<br>
167: * <br>
168: * <br>
169: * public void setStartDate(Date startDate)
170: * {<br>
171: * <br>
172: *
173: * this.startDate = startDate;<br>
174: * }<br>
175: * }</code><br>
176: *
177: *
178: * The <code>selectAll</code> attribute indicates that the
179: * <code>Add All</code> and <code>Remove All</code> buttons should be
180: * shown. A label for the component as a whole (<code>label</code>) is shown
181: * next to the component (<code>labelOnTop</code> is false). Labels have
182: * been specified for the list of available items and for the list of
183: * selected items. The <code>sorted</code> attribute indicates that the options on
184: * the list will be shown in alphabetical order.</p>
185: *
186: * <h4>Example 2: DateFormat Pattern and Range of Dates configured</h4>
187: *
188: * <p>The component gets the options from a managed bean called
189: * <code>TravelBean</code>. The value of the component
190: * <code>selectedDate</code> is bound to a property
191: * <code>travelDate</code>of the managed
192: * bean. A label for the component as a whole (<code>label</code>) is
193: * shown next to the component; the label is retrieved from a message
194: * bundle.
195: * </p>
196: *
197: * <p>The component has been configured to use a pattern for date
198: * representation consisting of four digits for the year, two for the
199: * month, and two for the day, separated by dashes. This pattern, set
200: * using the <code>dateFormatPattern</code> attribute will be used
201: * regardless of locale. With this date format pattern, the default
202: * help string will be "YYYY-MM-DD", which is suitable for English,
203: * but not for other locales where other words are used, so a
204: * different message is retrieved for each locale
205: * (<code>dateFormatPattern</code>). </p>
206: *
207: * <p>
208: * The component is also configured to restrict the range of dates that
209: * are valid, so that the first valid date is the day after the day
210: * the component is viewed, and the last valid date is six months
211: * from that date.
212: * <p>
213: *
214: * <pre>
215: * <ui:calendar id="travelDate"
216: * selectedDate="#{TravelBean.travelDate}"
217: * label="#{msgs.travelDate}"
218: * dateFormatPattern="yyyy-MM-dd"
219: * dateFormatPatternHelp="#{msgs.dateFormatPattern}"
220: * minDate="#{TravelBean.tomorrowsDate}"
221: * maxDate="#{TravelBean.sixMonthsFromNow}" />
222: * </pre>
223: * <p>Auto-generated component class.
224: * Do <strong>NOT</strong> modify; all changes
225: * <strong>will</strong> be lost!</p>
226: */
227:
228: public abstract class CalendarBase extends
229: com.sun.rave.web.ui.component.Field {
230:
231: /**
232: * <p>Construct a new <code>CalendarBase</code>.</p>
233: */
234: public CalendarBase() {
235: super ();
236: setRendererType("com.sun.rave.web.ui.Calendar");
237: }
238:
239: /**
240: * <p>Return the identifier of the component family to which this
241: * component belongs. This identifier, in conjunction with the value
242: * of the <code>rendererType</code> property, may be used to select
243: * the appropriate {@link Renderer} for this component instance.</p>
244: */
245: public String getFamily() {
246: return "com.sun.rave.web.ui.Calendar";
247: }
248:
249: /**
250: * <p>Return the <code>ValueBinding</code> stored for the
251: * specified name (if any), respecting any property aliases.</p>
252: *
253: * @param name Name of value binding to retrieve
254: */
255: public ValueBinding getValueBinding(String name) {
256: if (name.equals("selectedDate")) {
257: return super .getValueBinding("value");
258: }
259: return super .getValueBinding(name);
260: }
261:
262: /**
263: * <p>Set the <code>ValueBinding</code> stored for the
264: * specified name (if any), respecting any property
265: * aliases.</p>
266: *
267: * @param name Name of value binding to set
268: * @param binding ValueBinding to set, or null to remove
269: */
270: public void setValueBinding(String name, ValueBinding binding) {
271: if (name.equals("selectedDate")) {
272: super .setValueBinding("value", binding);
273: return;
274: }
275: super .setValueBinding(name, binding);
276: }
277:
278: // dateFormatPattern
279: private String dateFormatPattern = null;
280:
281: /**
282: * <p>The date format pattern to use (i.e. yyyy-MM-dd). The
283: * component uses an instance of
284: * <code>java.text.SimpleDateFormat</code> and you may specify
285: * a pattern to be used by this component, with the following
286: * restriction: the format pattern must include <code>yyyy</code> (not
287: * <code>yy</code>), <code>MM</code>, and <code>dd</code>; and no
288: * other parts of time may be displayed. If a pattern is not
289: * specified, a locale-specific default is used.</p>
290: * <p>
291: * If you change the date format pattern, you may also need to
292: * change the <code>dateFormatPatternHelp</code> attribute. See the
293: * documentation for that attribute.
294: * </p>
295: */
296: public String getDateFormatPattern() {
297: if (this .dateFormatPattern != null) {
298: return this .dateFormatPattern;
299: }
300: ValueBinding _vb = getValueBinding("dateFormatPattern");
301: if (_vb != null) {
302: return (String) _vb.getValue(getFacesContext());
303: }
304: return null;
305: }
306:
307: /**
308: * <p>The date format pattern to use (i.e. yyyy-MM-dd). The
309: * component uses an instance of
310: * <code>java.text.SimpleDateFormat</code> and you may specify
311: * a pattern to be used by this component, with the following
312: * restriction: the format pattern must include <code>yyyy</code> (not
313: * <code>yy</code>), <code>MM</code>, and <code>dd</code>; and no
314: * other parts of time may be displayed. If a pattern is not
315: * specified, a locale-specific default is used.</p>
316: * <p>
317: * If you change the date format pattern, you may also need to
318: * change the <code>dateFormatPatternHelp</code> attribute. See the
319: * documentation for that attribute.
320: * </p>
321: * @see #getDateFormatPattern()
322: */
323: public void setDateFormatPattern(String dateFormatPattern) {
324: this .dateFormatPattern = dateFormatPattern;
325: }
326:
327: // dateFormatPatternHelp
328: private String dateFormatPatternHelp = null;
329:
330: /**
331: * <p>A message below the textfield for the date, indicating the
332: * string format to use when entering a date as text into the
333: * textfield.</p>
334: *
335: * <p>If the <code>dateFormatPattern</code> attribute has not been
336: * set, there is no need to set this attribute, as an
337: * appropriate locale-specific help string will be shown.</p>
338: *
339: * <p>However, if the default <code>dateFormatPattern</code> has
340: * been overridden, then you may need to override this attribute
341: * also. The default behavior of the component is to show the
342: * pattern but capitalize it, so for example, if the value of
343: * <code>dateFormatPattern</code> is <code>yyyy-MM-dd</code>,
344: * then the default help text will be <code>YYYY-MM-DD</code>.
345: * This is likely to be inadequate for languages other than
346: * English, in which you may use this attribute to provide
347: * descriptions that are appropriate for each locale.</p>
348: */
349: public String getDateFormatPatternHelp() {
350: if (this .dateFormatPatternHelp != null) {
351: return this .dateFormatPatternHelp;
352: }
353: ValueBinding _vb = getValueBinding("dateFormatPatternHelp");
354: if (_vb != null) {
355: return (String) _vb.getValue(getFacesContext());
356: }
357: return null;
358: }
359:
360: /**
361: * <p>A message below the textfield for the date, indicating the
362: * string format to use when entering a date as text into the
363: * textfield.</p>
364: *
365: * <p>If the <code>dateFormatPattern</code> attribute has not been
366: * set, there is no need to set this attribute, as an
367: * appropriate locale-specific help string will be shown.</p>
368: *
369: * <p>However, if the default <code>dateFormatPattern</code> has
370: * been overridden, then you may need to override this attribute
371: * also. The default behavior of the component is to show the
372: * pattern but capitalize it, so for example, if the value of
373: * <code>dateFormatPattern</code> is <code>yyyy-MM-dd</code>,
374: * then the default help text will be <code>YYYY-MM-DD</code>.
375: * This is likely to be inadequate for languages other than
376: * English, in which you may use this attribute to provide
377: * descriptions that are appropriate for each locale.</p>
378: * @see #getDateFormatPatternHelp()
379: */
380: public void setDateFormatPatternHelp(String dateFormatPatternHelp) {
381: this .dateFormatPatternHelp = dateFormatPatternHelp;
382: }
383:
384: // maxDate
385: private java.util.Date maxDate = null;
386:
387: /**
388: * <p>A <code>java.util.Date</code> object representing the last
389: * selectable day. The default value is four years after the
390: * <code>minDate</code> (which is evaluated first).</p>
391: * <p>The value of this attribute is reflected in the years that
392: * are available for selection in the month display. In future
393: * releases of this component, web application users will also not
394: * be able to view months after this date, or select days that
395: * follow this date. At present such dates can be selected, but
396: * will not be validated when the form is submitted.</p>
397: */
398: public java.util.Date getMaxDate() {
399: if (this .maxDate != null) {
400: return this .maxDate;
401: }
402: ValueBinding _vb = getValueBinding("maxDate");
403: if (_vb != null) {
404: return (java.util.Date) _vb.getValue(getFacesContext());
405: }
406: return null;
407: }
408:
409: /**
410: * <p>A <code>java.util.Date</code> object representing the last
411: * selectable day. The default value is four years after the
412: * <code>minDate</code> (which is evaluated first).</p>
413: * <p>The value of this attribute is reflected in the years that
414: * are available for selection in the month display. In future
415: * releases of this component, web application users will also not
416: * be able to view months after this date, or select days that
417: * follow this date. At present such dates can be selected, but
418: * will not be validated when the form is submitted.</p>
419: * @see #getMaxDate()
420: */
421: public void setMaxDate(java.util.Date maxDate) {
422: this .maxDate = maxDate;
423: }
424:
425: // minDate
426: private java.util.Date minDate = null;
427:
428: /**
429: * <p>A <code>java.util.Date</code> object representing the first
430: * selectable day. The default value is today's date.</p>
431: * <p>The value of this attribute is reflected in the years that
432: * are available for selection in the month display. In future
433: * releases of this component, web application users will also not
434: * be able to view months before this date, or select days that
435: * precede this date. At present such dates can be selected, but
436: * will not be validated when the form is submitted.</p>
437: */
438: public java.util.Date getMinDate() {
439: if (this .minDate != null) {
440: return this .minDate;
441: }
442: ValueBinding _vb = getValueBinding("minDate");
443: if (_vb != null) {
444: return (java.util.Date) _vb.getValue(getFacesContext());
445: }
446: return null;
447: }
448:
449: /**
450: * <p>A <code>java.util.Date</code> object representing the first
451: * selectable day. The default value is today's date.</p>
452: * <p>The value of this attribute is reflected in the years that
453: * are available for selection in the month display. In future
454: * releases of this component, web application users will also not
455: * be able to view months before this date, or select days that
456: * precede this date. At present such dates can be selected, but
457: * will not be validated when the form is submitted.</p>
458: * @see #getMinDate()
459: */
460: public void setMinDate(java.util.Date minDate) {
461: this .minDate = minDate;
462: }
463:
464: // selectedDate
465: /**
466: * <p>A <code>java.util.Date</code> object representing the currently
467: * selected calendar date.</p>
468: */
469: public java.util.Date getSelectedDate() {
470: return (java.util.Date) getValue();
471: }
472:
473: /**
474: * <p>A <code>java.util.Date</code> object representing the currently
475: * selected calendar date.</p>
476: * @see #getSelectedDate()
477: */
478: public void setSelectedDate(java.util.Date selectedDate) {
479: setValue((Object) selectedDate);
480: }
481:
482: // timeZone
483: private java.util.TimeZone timeZone = null;
484:
485: /**
486: * <p>The <code>java.util.TimeZone</code> used with this
487: * component. Unless set, the default TimeZone for the locale in
488: * <code>javax.faces.component.UIViewRoot</code> is used.</p>
489: */
490: public java.util.TimeZone getTimeZone() {
491: if (this .timeZone != null) {
492: return this .timeZone;
493: }
494: ValueBinding _vb = getValueBinding("timeZone");
495: if (_vb != null) {
496: return (java.util.TimeZone) _vb.getValue(getFacesContext());
497: }
498: return null;
499: }
500:
501: /**
502: * <p>The <code>java.util.TimeZone</code> used with this
503: * component. Unless set, the default TimeZone for the locale in
504: * <code>javax.faces.component.UIViewRoot</code> is used.</p>
505: * @see #getTimeZone()
506: */
507: public void setTimeZone(java.util.TimeZone timeZone) {
508: this .timeZone = timeZone;
509: }
510:
511: /**
512: * <p>Restore the state of this component.</p>
513: */
514: public void restoreState(FacesContext _context, Object _state) {
515: Object _values[] = (Object[]) _state;
516: super .restoreState(_context, _values[0]);
517: this .dateFormatPattern = (String) _values[1];
518: this .dateFormatPatternHelp = (String) _values[2];
519: this .maxDate = (java.util.Date) _values[3];
520: this .minDate = (java.util.Date) _values[4];
521: this .timeZone = (java.util.TimeZone) _values[5];
522: }
523:
524: /**
525: * <p>Save the state of this component.</p>
526: */
527: public Object saveState(FacesContext _context) {
528: Object _values[] = new Object[6];
529: _values[0] = super .saveState(_context);
530: _values[1] = this .dateFormatPattern;
531: _values[2] = this .dateFormatPatternHelp;
532: _values[3] = this .maxDate;
533: _values[4] = this .minDate;
534: _values[5] = this.timeZone;
535: return _values;
536: }
537:
538: }
|