KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > calendar > HtmlCalendarRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.calendar;
17
18 import org.apache.myfaces.component.html.ext.HtmlInputText;
19 import org.apache.myfaces.component.html.util.AddResource;
20 import org.apache.myfaces.renderkit.JSFAttr;
21 import org.apache.myfaces.renderkit.RendererUtils;
22 import org.apache.myfaces.renderkit.html.HTML;
23 import org.apache.myfaces.renderkit.html.HtmlRenderer;
24 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
25 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
26
27 import javax.faces.application.Application;
28 import javax.faces.component.UIComponent;
29 import javax.faces.component.UIInput;
30 import javax.faces.component.UIParameter;
31 import javax.faces.component.html.HtmlCommandLink;
32 import javax.faces.component.html.HtmlOutputText;
33 import javax.faces.context.FacesContext;
34 import javax.faces.context.ResponseWriter;
35 import javax.faces.convert.Converter;
36 import javax.faces.convert.ConverterException;
37 import java.io.IOException JavaDoc;
38 import java.text.DateFormat JavaDoc;
39 import java.text.DateFormatSymbols JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * $Log: HtmlCalendarRenderer.java,v $
49  * Revision 1.21 2005/04/08 02:02:26 schof
50  * Fixes MYFACES-167 (Thanks to David Heffelfinger for reporting the bug and supplying the patch.)
51  *
52  * Revision 1.20 2005/02/11 14:11:04 mmarinschek
53  * fixed jira 97
54  *
55  * Revision 1.19 2005/02/11 13:24:01 mmarinschek
56  * fix jira 60, showing fix in examples
57  *
58  * Revision 1.18 2005/01/17 10:08:44 mmarinschek
59  * trying to fix problem with added resources to header; css and js was not changeable by the user anymore...
60  *
61  * Revision 1.17 2005/01/09 18:15:12 mmarinschek
62  * small changes - better error handling, label renderer supports more hooks for sub-classes
63  *
64  * Revision 1.16 2004/12/27 04:11:11 mmarinschek
65  * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table
66  *
67  * Revision 1.15 2004/12/10 02:08:54 svieujot
68  * Share code with HtmlInputDate.
69  *
70  * Revision 1.14 2004/12/09 12:18:43 mmarinschek
71  * changes in Calendar-Renderer to check for submitted-value first
72  *
73  * Revision 1.13 2004/12/09 05:13:02 svieujot
74  * Mark potential bugs where we use the backing bean's value, and do not check for submitted value
75  *
76  * Revision 1.12 2004/12/02 22:26:23 svieujot
77  * Simplify the AddResource methods
78  *
79  * Revision 1.11 2004/12/01 20:25:10 svieujot
80  * Make the Extensions filter support css and image resources.
81  * Convert the popup calendar to use this new filter.
82  *
83  * Revision 1.10 2004/11/29 13:57:56 mmarinschek
84  * changes for input-calendar bugs
85  *
86  * Revision 1.9 2004/10/13 11:50:57 matze
87  * renamed packages to org.apache
88  *
89  * Revision 1.8 2004/09/06 08:41:48 tinytoony
90  * changes to calendar - rendering wrong weekday, check output-text behavior
91  *
92  * Revision 1.7 2004/07/28 18:00:47 tinytoony
93  * calendar; revisited again for complete i18
94  *
95  * Revision 1.6 2004/07/28 17:13:54 tinytoony
96  * new calendar popup, revisited, global variables renamed to help with uniqueness
97  *
98  * Revision 1.5 2004/07/27 16:48:02 tinytoony
99  * new calendar popup, revisited
100  *
101  * Revision 1.4 2004/07/27 06:28:32 tinytoony
102  * new calendar component as a popup
103  *
104  * Revision 1.3 2004/07/01 21:53:11 mwessendorf
105  * ASF switch
106  *
107  * Revision 1.2 2004/04/20 14:39:12 royalts
108  * writeLink: added text.setValue(content), removed link.setValue(content)
109  *
110  * Revision 1.1 2004/03/31 11:58:34 manolito
111  * custom component refactoring
112  *
113  * Revision 1.6 2004/03/29 15:06:17 manolito
114  * no longer depends on specific LinkRenderer
115  *
116  * @author Martin Marinschek (latest modification by $Author: schof $)
117  * @version $Revision: 1.21 $ $Date: 2005/04/08 02:02:26 $
118  */

119 public class HtmlCalendarRenderer
120         extends HtmlRenderer
121 {
122
123     //private static Log log = LogFactory.getLog(HtmlCalendarRenderer.class);
124

125     public void encodeEnd(FacesContext facesContext, UIComponent component)
126             throws IOException JavaDoc
127     {
128         RendererUtils.checkParamValidity(facesContext, component, HtmlInputCalendar.class);
129
130         HtmlInputCalendar inputCalendar = (HtmlInputCalendar) component;
131
132         Locale JavaDoc currentLocale = facesContext.getViewRoot().getLocale();
133
134         
135         Date JavaDoc value;
136         
137         try
138         {
139             value = RendererUtils.getDateValue(inputCalendar);
140         }
141         catch (IllegalArgumentException JavaDoc illegalArgumentException)
142         {
143             value = null;
144         }
145        
146
147         Calendar JavaDoc timeKeeper = Calendar.getInstance(currentLocale);
148         timeKeeper.setTime(value!=null?value:new Date JavaDoc());
149
150         DateFormatSymbols JavaDoc symbols = new DateFormatSymbols JavaDoc(currentLocale);
151
152         String JavaDoc[] weekdays = mapWeekdays(symbols);
153         String JavaDoc[] months = mapMonths(symbols);
154
155         if(inputCalendar.isRenderAsPopup())
156         {
157             if(inputCalendar.isAddResources())
158                 addScriptAndCSSResources(facesContext);
159             
160             String JavaDoc dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext,
161                     inputCalendar.getPopupDateFormat());
162
163             Application application = facesContext.getApplication();
164
165             HtmlInputText inputText = null;
166
167             List JavaDoc li = inputCalendar.getChildren();
168
169             for (int i = 0; i < li.size(); i++)
170             {
171                 UIComponent uiComponent = (UIComponent) li.get(i);
172
173                 if(uiComponent instanceof HtmlInputText)
174                 {
175                     inputText = (HtmlInputText) uiComponent;
176                     break;
177                 }
178             }
179
180             if(inputText == null)
181             {
182                 inputText = (HtmlInputText) application.createComponent(HtmlInputText.COMPONENT_TYPE);
183             }
184
185             RendererUtils.copyHtmlInputTextAttributes(inputCalendar, inputText);
186
187             inputText.setTransient(true);
188             
189             if (value == null && inputCalendar.getSubmittedValue() != null)
190             {
191                 inputText.setValue(inputCalendar.getSubmittedValue());
192             }
193             else
194             {
195                 inputText.setValue(getConverter(inputCalendar).getAsString(
196                         facesContext,inputCalendar,value));
197             }
198             inputText.setEnabledOnUserRole(inputCalendar.getEnabledOnUserRole());
199             inputText.setVisibleOnUserRole(inputCalendar.getVisibleOnUserRole());
200
201             inputCalendar.getChildren().add(inputText);
202
203             RendererUtils.renderChild(facesContext, inputText);
204
205             inputCalendar.getChildren().remove(inputText);
206
207             ResponseWriter writer = facesContext.getResponseWriter();
208             
209             writer.startElement(HTML.SCRIPT_ELEM,null);
210             writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
211             writer.write("<!--\n");
212             writer.writeText(getLocalizedLanguageScript(symbols, months,
213                     timeKeeper.getFirstDayOfWeek(),inputCalendar),null);
214             writer.writeText(getScriptBtnText(inputCalendar.getClientId(facesContext),
215                     dateFormat,inputCalendar.getPopupButtonString()),null);
216             writer.write("\n-->");
217             writer.endElement(HTML.SCRIPT_ELEM);
218
219 /* writer.startElement(HTML.INPUT_ELEM,null);
220             writer.writeAttribute(HTML.TYPE_ATTR,HTML.INPUT_TYPE_BUTTON,null);
221             writer.writeAttribute(HTML.ONCLICK_ATTR,"popUpCalendar(this, "+inputText.getClientId(facesContext)+
222                     ", \\\"dd.mm.yyyy\\\")",null);
223             writer.endElement(HTML.INPUT_TYPE_BUTTON);*/

224         }
225         else
226         {
227
228             int lastDayInMonth = timeKeeper.getActualMaximum(Calendar.DAY_OF_MONTH);
229
230             int currentDay = timeKeeper.get(Calendar.DAY_OF_MONTH);
231
232             if (currentDay > lastDayInMonth)
233                 currentDay = lastDayInMonth;
234
235             timeKeeper.set(Calendar.DAY_OF_MONTH, 1);
236
237             int weekDayOfFirstDayOfMonth = mapCalendarDayToCommonDay(timeKeeper.get(Calendar.DAY_OF_WEEK));
238
239             int weekStartsAtDayIndex = mapCalendarDayToCommonDay(timeKeeper.getFirstDayOfWeek());
240
241             ResponseWriter writer = facesContext.getResponseWriter();
242
243             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
244             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
245
246             writer.startElement(HTML.TABLE_ELEM, component);
247             HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.UNIVERSAL_ATTRIBUTES);
248             HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.EVENT_HANDLER_ATTRIBUTES);
249             writer.flush();
250
251             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
252
253             writer.startElement(HTML.TR_ELEM, component);
254
255             if(inputCalendar.getMonthYearRowClass() != null)
256                 writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getMonthYearRowClass(), null);
257
258             writeMonthYearHeader(facesContext, writer, inputCalendar, timeKeeper,
259                     currentDay, weekdays, months);
260
261             writer.endElement(HTML.TR_ELEM);
262
263             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
264
265             writer.startElement(HTML.TR_ELEM, component);
266
267             if(inputCalendar.getWeekRowClass() != null)
268                 writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getWeekRowClass(), null);
269
270             writeWeekDayNameHeader(weekStartsAtDayIndex, weekdays,
271                     facesContext, writer, inputCalendar);
272
273             writer.endElement(HTML.TR_ELEM);
274
275             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
276
277             writeDays(facesContext, writer, inputCalendar, timeKeeper,
278                     currentDay, weekStartsAtDayIndex, weekDayOfFirstDayOfMonth,
279                     lastDayInMonth, weekdays);
280
281             writer.endElement(HTML.TABLE_ELEM);
282         }
283     }
284     
285     /**
286      * Used by the x:inputDate renderer : HTMLDateRenderer
287      * @throws IOException
288      */

289     static public void addScriptAndCSSResources(FacesContext facesContext) throws IOException JavaDoc{
290         // Add the javascript and CSS pages
291
AddResource.addStyleSheet(HtmlCalendarRenderer.class, "WH/theme.css", facesContext);
292         AddResource.addStyleSheet(HtmlCalendarRenderer.class, "DB/theme.css", facesContext);
293         AddResource.addJavaScriptToHeader(HtmlCalendarRenderer.class, "popcalendar.js", facesContext);
294         
295         ResponseWriter writer = facesContext.getResponseWriter();
296         
297         writer.startElement(HTML.SCRIPT_ELEM, null);
298         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
299         writer.write(
300                 "jscalendarSetImageDirectory(\""
301                     +JavascriptUtils.encodeString(
302                             AddResource.getResourceMappedPath(HtmlCalendarRenderer.class, "DB/", facesContext)
303                      )
304                 +"\")");
305         writer.endElement(HTML.SCRIPT_ELEM);
306     }
307
308     public static String JavaDoc getLocalizedLanguageScript(DateFormatSymbols JavaDoc symbols,
309             String JavaDoc[] months, int firstDayOfWeek, HtmlInputCalendar inputCalendar)
310     {
311         int realFirstDayOfWeek = firstDayOfWeek-1/*Java has different starting-point*/;
312
313         String JavaDoc[] weekDays;
314
315         if(realFirstDayOfWeek==0)
316         {
317             weekDays = mapWeekdaysStartingWithSunday(symbols);
318         }
319         else if(realFirstDayOfWeek==1)
320         {
321             weekDays = mapWeekdays(symbols);
322         }
323         else
324             throw new IllegalStateException JavaDoc("Week may only start with sunday or monday.");
325
326         StringBuffer JavaDoc script = new StringBuffer JavaDoc();
327         defineStringArray(script, "jscalendarMonthName", months);
328         defineStringArray(script, "jscalendarDayName", weekDays);
329         setIntegerVariable(script, "jscalendarStartAt",realFirstDayOfWeek);
330
331         if( inputCalendar != null ){ // To allow null parameter for inputDate tag.
332
if(inputCalendar.getPopupGotoString()!=null)
333                 setStringVariable(script, "jscalendarGotoString",inputCalendar.getPopupGotoString());
334             if(inputCalendar.getPopupTodayString()!=null)
335                 setStringVariable(script, "jscalendarTodayString",inputCalendar.getPopupTodayString());
336             if(inputCalendar.getPopupWeekString()!=null)
337                 setStringVariable(script, "jscalendarWeekString",inputCalendar.getPopupWeekString());
338             if(inputCalendar.getPopupScrollLeftMessage()!=null)
339                 setStringVariable(script, "jscalendarScrollLeftMessage",inputCalendar.getPopupScrollLeftMessage());
340             if(inputCalendar.getPopupScrollRightMessage()!=null)
341                 setStringVariable(script, "jscalendarScrollRightMessage",inputCalendar.getPopupScrollRightMessage());
342             if(inputCalendar.getPopupSelectMonthMessage()!=null)
343                 setStringVariable(script, "jscalendarSelectMonthMessage",inputCalendar.getPopupSelectMonthMessage());
344             if(inputCalendar.getPopupSelectYearMessage()!=null)
345                 setStringVariable(script, "jscalendarSelectYearMessage",inputCalendar.getPopupSelectYearMessage());
346             if(inputCalendar.getPopupSelectDateMessage()!=null)
347                 setStringVariable(script, "jscalendarSelectDateMessage",inputCalendar.getPopupSelectDateMessage());
348         }
349
350         return script.toString();
351     }
352
353     private static void setIntegerVariable(StringBuffer JavaDoc script, String JavaDoc name, int value)
354     {
355         script.append(name);
356         script.append(" = ");
357         script.append(value);
358         script.append(";\n");
359     }
360
361     private static void setStringVariable(StringBuffer JavaDoc script, String JavaDoc name, String JavaDoc value)
362     {
363         script.append(name);
364         script.append(" = \"");
365         script.append(value);
366         script.append("\";\n");
367     }
368
369     private static void defineStringArray(StringBuffer JavaDoc script, String JavaDoc arrayName, String JavaDoc[] array)
370     {
371         script.append(arrayName);
372         script.append(" = new Array(");
373
374         for(int i=0;i<array.length;i++)
375         {
376             if(i!=0)
377                 script.append(",");
378
379             script.append("\"");
380             script.append(array[i]);
381             script.append("\"");
382         }
383
384         script.append(");");
385     }
386
387     private String JavaDoc getScriptBtnText(String JavaDoc clientId, String JavaDoc dateFormat, String JavaDoc popupButtonString)
388     {
389
390         StringBuffer JavaDoc script = new StringBuffer JavaDoc();
391         script.append("if (!document.layers) {\n");
392         script.append("document.write(");
393         script.append("\"<input type='button' onclick='jscalendarPopUpCalendar(this,this.form.elements[\\\"");
394         script.append(clientId);
395         script.append("\\\"],\\\"");
396         script.append(dateFormat);
397         script.append("\\\")' value='");
398         if(popupButtonString==null)
399             popupButtonString="...";
400         script.append(popupButtonString);
401         script.append("'/>\"");
402         script.append(");");
403         script.append("\n}");
404
405         return script.toString();
406     }
407
408     private void writeMonthYearHeader(FacesContext facesContext, ResponseWriter writer, UIInput inputComponent, Calendar JavaDoc timeKeeper,
409                                       int currentDay, String JavaDoc[] weekdays,
410                                       String JavaDoc[] months)
411             throws IOException JavaDoc
412     {
413         Calendar JavaDoc cal = shiftMonth(facesContext, timeKeeper, currentDay, -1);
414
415         writeCell(facesContext, writer, inputComponent, "<", cal.getTime(), null);
416
417         writer.startElement(HTML.TD_ELEM, inputComponent);
418         writer.writeAttribute(HTML.COLSPAN_ATTR, new Integer JavaDoc(weekdays.length - 2), null);
419         writer.writeText(months[timeKeeper.get(Calendar.MONTH)] + " " + timeKeeper.get(Calendar.YEAR), null);
420         writer.endElement(HTML.TD_ELEM);
421
422         cal = shiftMonth(facesContext, timeKeeper, currentDay, 1);
423
424         writeCell(facesContext, writer, inputComponent, ">", cal.getTime(), null);
425     }
426
427     private Calendar JavaDoc shiftMonth(FacesContext facesContext,
428                                 Calendar JavaDoc timeKeeper, int currentDay, int shift)
429     {
430         Calendar JavaDoc cal = copyCalendar(facesContext, timeKeeper);
431
432         cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + shift);
433
434         if(currentDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH))
435             currentDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
436
437         cal.set(Calendar.DAY_OF_MONTH, currentDay);
438         return cal;
439     }
440
441     private Calendar JavaDoc copyCalendar(FacesContext facesContext, Calendar JavaDoc timeKeeper)
442     {
443         Calendar JavaDoc cal = Calendar.getInstance(facesContext.getViewRoot().getLocale());
444         cal.set(Calendar.YEAR, timeKeeper.get(Calendar.YEAR));
445         cal.set(Calendar.MONTH, timeKeeper.get(Calendar.MONTH));
446         cal.set(Calendar.HOUR_OF_DAY, timeKeeper.get(Calendar.HOUR_OF_DAY));
447         cal.set(Calendar.MINUTE, timeKeeper.get(Calendar.MINUTE));
448         cal.set(Calendar.SECOND, timeKeeper.get(Calendar.SECOND));
449         cal.set(Calendar.MILLISECOND, timeKeeper.get(Calendar.MILLISECOND));
450         return cal;
451     }
452
453     private void writeWeekDayNameHeader(int weekStartsAtDayIndex, String JavaDoc[] weekdays, FacesContext facesContext, ResponseWriter writer, UIInput inputComponent)
454             throws IOException JavaDoc
455     {
456         for (int i = weekStartsAtDayIndex; i < weekdays.length; i++)
457             writeCell(facesContext,
458                     writer, inputComponent, weekdays[i], null, null);
459
460         for (int i = 0; i < weekStartsAtDayIndex; i++)
461             writeCell(facesContext, writer,
462                     inputComponent, weekdays[i], null, null);
463     }
464
465     private void writeDays(FacesContext facesContext, ResponseWriter writer,
466                            HtmlInputCalendar inputComponent, Calendar JavaDoc timeKeeper, int currentDay, int weekStartsAtDayIndex,
467                            int weekDayOfFirstDayOfMonth, int lastDayInMonth, String JavaDoc[] weekdays)
468             throws IOException JavaDoc
469     {
470         Calendar JavaDoc cal;
471
472         int space = (weekStartsAtDayIndex < weekDayOfFirstDayOfMonth) ? (weekDayOfFirstDayOfMonth - weekStartsAtDayIndex)
473                 : (weekdays.length - weekStartsAtDayIndex + weekDayOfFirstDayOfMonth);
474
475         if (space == weekdays.length)
476             space = 0;
477
478         int columnIndexCounter = 0;
479
480         for (int i = 0; i < space; i++)
481         {
482             if (columnIndexCounter == 0)
483             {
484                 writer.startElement(HTML.TR_ELEM, inputComponent);
485             }
486
487             writeCell(facesContext, writer, inputComponent, "",
488                     null, inputComponent.getDayCellClass());
489             columnIndexCounter++;
490         }
491
492         for (int i = 0; i < lastDayInMonth; i++)
493         {
494             if (columnIndexCounter == 0)
495             {
496                 writer.startElement(HTML.TR_ELEM, inputComponent);
497             }
498
499             cal = copyCalendar(facesContext, timeKeeper);
500             cal.set(Calendar.DAY_OF_MONTH, i + 1);
501
502             String JavaDoc cellStyle = inputComponent.getDayCellClass();
503
504             if((currentDay - 1) == i)
505                 cellStyle = inputComponent.getCurrentDayCellClass();
506
507             writeCell(facesContext, writer,
508                     inputComponent, String.valueOf(i + 1), cal.getTime(),
509                     cellStyle);
510
511             columnIndexCounter++;
512
513             if (columnIndexCounter == weekdays.length)
514             {
515                 writer.endElement(HTML.TR_ELEM);
516                 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
517                 columnIndexCounter = 0;
518             }
519         }
520
521         if (columnIndexCounter != 0)
522         {
523             for (int i = columnIndexCounter; i < weekdays.length; i++)
524             {
525                 writeCell(facesContext, writer,
526                         inputComponent, "", null, inputComponent.getDayCellClass());
527             }
528
529             writer.endElement(HTML.TR_ELEM);
530             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
531         }
532     }
533
534     private void writeCell(FacesContext facesContext,
535                            ResponseWriter writer, UIInput component, String JavaDoc content,
536                            Date JavaDoc valueForLink, String JavaDoc styleClass)
537             throws IOException JavaDoc
538     {
539         writer.startElement(HTML.TD_ELEM, component);
540
541         if (styleClass != null)
542             writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null);
543
544         if (valueForLink == null)
545             writer.writeText(content, JSFAttr.VALUE_ATTR);
546         else
547         {
548             writeLink(content, component, facesContext, valueForLink);
549         }
550
551         writer.endElement(HTML.TD_ELEM);
552     }
553
554     private void writeLink(String JavaDoc content,
555                            UIInput component,
556                            FacesContext facesContext,
557                            Date JavaDoc valueForLink)
558             throws IOException JavaDoc
559     {
560         Converter converter = getConverter(component);
561
562         Application application = facesContext.getApplication();
563         HtmlCommandLink link
564                 = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE);
565         link.setId(component.getId() + "_" + valueForLink.getTime() + "_link");
566         link.setTransient(true);
567         link.setImmediate(component.isImmediate());
568
569         HtmlOutputText text
570                 = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);
571         text.setValue(content);
572         text.setId(component.getId() + "_" + valueForLink.getTime() + "_text");
573         text.setTransient(true);
574
575         UIParameter parameter
576                 = (UIParameter)application.createComponent(UIParameter.COMPONENT_TYPE);
577         parameter.setId(component.getId() + "_" + valueForLink.getTime() + "_param");
578         parameter.setTransient(true);
579         parameter.setName(component.getClientId(facesContext));
580         parameter.setValue(converter.getAsString(facesContext, component, valueForLink));
581
582         component.getChildren().add(link);
583         link.getChildren().add(parameter);
584         link.getChildren().add(text);
585
586         RendererUtils.renderChild(facesContext, link);
587     }
588
589     private Converter getConverter(UIInput component)
590     {
591         Converter converter = component.getConverter();
592
593         if (converter == null)
594         {
595             converter = new CalendarDateTimeConverter();
596         }
597         return converter;
598     }
599
600     private int mapCalendarDayToCommonDay(int day)
601     {
602         switch (day)
603         {
604             case Calendar.TUESDAY:
605                 return 1;
606             case Calendar.WEDNESDAY:
607                 return 2;
608             case Calendar.THURSDAY:
609                 return 3;
610             case Calendar.FRIDAY:
611                 return 4;
612             case Calendar.SATURDAY:
613                 return 5;
614             case Calendar.SUNDAY:
615                 return 6;
616             default:
617                 return 0;
618         }
619     }
620
621     private static String JavaDoc[] mapWeekdays(DateFormatSymbols JavaDoc symbols)
622     {
623         String JavaDoc[] weekdays = new String JavaDoc[7];
624
625         String JavaDoc[] localeWeekdays = symbols.getShortWeekdays();
626
627         weekdays[0] = localeWeekdays[Calendar.MONDAY];
628         weekdays[1] = localeWeekdays[Calendar.TUESDAY];
629         weekdays[2] = localeWeekdays[Calendar.WEDNESDAY];
630         weekdays[3] = localeWeekdays[Calendar.THURSDAY];
631         weekdays[4] = localeWeekdays[Calendar.FRIDAY];
632         weekdays[5] = localeWeekdays[Calendar.SATURDAY];
633         weekdays[6] = localeWeekdays[Calendar.SUNDAY];
634
635         return weekdays;
636     }
637
638     private static String JavaDoc[] mapWeekdaysStartingWithSunday(DateFormatSymbols JavaDoc symbols)
639     {
640         String JavaDoc[] weekdays = new String JavaDoc[7];
641
642         String JavaDoc[] localeWeekdays = symbols.getShortWeekdays();
643
644         weekdays[0] = localeWeekdays[Calendar.SUNDAY];
645         weekdays[1] = localeWeekdays[Calendar.MONDAY];
646         weekdays[2] = localeWeekdays[Calendar.TUESDAY];
647         weekdays[3] = localeWeekdays[Calendar.WEDNESDAY];
648         weekdays[4] = localeWeekdays[Calendar.THURSDAY];
649         weekdays[5] = localeWeekdays[Calendar.FRIDAY];
650         weekdays[6] = localeWeekdays[Calendar.SATURDAY];
651
652         return weekdays;
653     }
654
655     public static String JavaDoc[] mapMonths(DateFormatSymbols JavaDoc symbols)
656     {
657         String JavaDoc[] months = new String JavaDoc[12];
658
659         String JavaDoc[] localeMonths = symbols.getMonths();
660
661         months[0] = localeMonths[Calendar.JANUARY];
662         months[1] = localeMonths[Calendar.FEBRUARY];
663         months[2] = localeMonths[Calendar.MARCH];
664         months[3] = localeMonths[Calendar.APRIL];
665         months[4] = localeMonths[Calendar.MAY];
666         months[5] = localeMonths[Calendar.JUNE];
667         months[6] = localeMonths[Calendar.JULY];
668         months[7] = localeMonths[Calendar.AUGUST];
669         months[8] = localeMonths[Calendar.SEPTEMBER];
670         months[9] = localeMonths[Calendar.OCTOBER];
671         months[10] = localeMonths[Calendar.NOVEMBER];
672         months[11] = localeMonths[Calendar.DECEMBER];
673
674         return months;
675     }
676
677
678     public void decode(FacesContext facesContext, UIComponent component)
679     {
680         RendererUtils.checkParamValidity(facesContext, component, HtmlInputCalendar.class);
681
682         HtmlRendererUtils.decodeUIInput(facesContext, component);
683     }
684
685     public Object JavaDoc getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc submittedValue) throws ConverterException
686     {
687         RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlInputCalendar.class);
688
689         UIInput uiInput = (UIInput) uiComponent;
690
691         Converter converter = uiInput.getConverter();
692
693         if(converter==null)
694             converter = new CalendarDateTimeConverter();
695
696         if (!(submittedValue == null || submittedValue instanceof String JavaDoc))
697         {
698             throw new IllegalArgumentException JavaDoc("Submitted value of type String expected");
699         }
700
701         return converter.getAsObject(facesContext, uiComponent, (String JavaDoc) submittedValue);
702     }
703
704     public static class CalendarDateTimeConverter implements Converter
705     {
706
707         public Object JavaDoc getAsObject(FacesContext facesContext, UIComponent uiComponent, String JavaDoc s)
708         {
709             if(s==null || s.trim().length()==0)
710                 return null;
711
712             DateFormat JavaDoc dateFormat = null;
713
714             if(uiComponent instanceof HtmlInputCalendar && ((HtmlInputCalendar) uiComponent).isRenderAsPopup())
715             {
716                 String JavaDoc popupDateFormat = ((HtmlInputCalendar) uiComponent).getPopupDateFormat();
717
718                 dateFormat = new SimpleDateFormat JavaDoc(createJSPopupFormat(facesContext, popupDateFormat));
719             }
720             else
721             {
722                 dateFormat = createStandardDateFormat(facesContext);
723             }
724
725             try
726             {
727                 return dateFormat.parse(s);
728             }
729             catch (ParseException JavaDoc e)
730             {
731                 ConverterException ex = new ConverterException(e);
732                 throw ex;
733             }
734         }
735
736         public static String JavaDoc createJSPopupFormat(FacesContext facesContext, String JavaDoc popupDateFormat)
737         {
738
739             if(popupDateFormat == null)
740             {
741                 SimpleDateFormat JavaDoc defaultDateFormat = createStandardDateFormat(facesContext);
742                 popupDateFormat = defaultDateFormat.toPattern();
743             }
744
745             StringBuffer JavaDoc jsPopupDateFormat = new StringBuffer JavaDoc();
746
747             for(int i=0;i<popupDateFormat.length();i++)
748             {
749                 char c = popupDateFormat.charAt(i);
750
751                 if(c=='M')
752                     jsPopupDateFormat.append('M');
753                 else if(c=='d')
754                     jsPopupDateFormat.append('d');
755                 else if(c=='y')
756                     jsPopupDateFormat.append('y');
757                 else if(c==' ')
758                     jsPopupDateFormat.append(' ');
759                 else if(c=='.')
760                     jsPopupDateFormat.append('.');
761                 else if(c=='/')
762                     jsPopupDateFormat.append('/');
763             }
764             return jsPopupDateFormat.toString().trim();
765         }
766
767         public String JavaDoc getAsString(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc o)
768         {
769             Date JavaDoc date = (Date JavaDoc) o;
770
771             if(date==null)
772                 return null;
773
774             DateFormat JavaDoc dateFormat = null;
775
776             if(uiComponent instanceof HtmlInputCalendar && ((HtmlInputCalendar) uiComponent).isRenderAsPopup())
777             {
778                 String JavaDoc popupDateFormat = ((HtmlInputCalendar) uiComponent).getPopupDateFormat();
779
780                 dateFormat = new SimpleDateFormat JavaDoc(createJSPopupFormat(facesContext, popupDateFormat));
781             }
782             else
783             {
784                 dateFormat = createStandardDateFormat(facesContext);
785             }
786
787             return dateFormat.format(date);
788         }
789
790         private static SimpleDateFormat JavaDoc createStandardDateFormat(FacesContext facesContext)
791         {
792             DateFormat JavaDoc dateFormat;
793             dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
794                     facesContext.getViewRoot().getLocale());
795
796             if(dateFormat instanceof SimpleDateFormat JavaDoc)
797                 return (SimpleDateFormat JavaDoc) dateFormat;
798             else
799                 return new SimpleDateFormat JavaDoc("dd.MM.yyyy");
800         }
801
802     }
803 }
804
Popular Tags