1 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 ; 38 import java.text.DateFormat ; 39 import java.text.DateFormatSymbols ; 40 import java.text.ParseException ; 41 import java.text.SimpleDateFormat ; 42 import java.util.Calendar ; 43 import java.util.Date ; 44 import java.util.Locale ; 45 import java.util.List ; 46 47 119 public class HtmlCalendarRenderer 120 extends HtmlRenderer 121 { 122 123 125 public void encodeEnd(FacesContext facesContext, UIComponent component) 126 throws IOException 127 { 128 RendererUtils.checkParamValidity(facesContext, component, HtmlInputCalendar.class); 129 130 HtmlInputCalendar inputCalendar = (HtmlInputCalendar) component; 131 132 Locale currentLocale = facesContext.getViewRoot().getLocale(); 133 134 135 Date value; 136 137 try 138 { 139 value = RendererUtils.getDateValue(inputCalendar); 140 } 141 catch (IllegalArgumentException illegalArgumentException) 142 { 143 value = null; 144 } 145 146 147 Calendar timeKeeper = Calendar.getInstance(currentLocale); 148 timeKeeper.setTime(value!=null?value:new Date ()); 149 150 DateFormatSymbols symbols = new DateFormatSymbols (currentLocale); 151 152 String [] weekdays = mapWeekdays(symbols); 153 String [] months = mapMonths(symbols); 154 155 if(inputCalendar.isRenderAsPopup()) 156 { 157 if(inputCalendar.isAddResources()) 158 addScriptAndCSSResources(facesContext); 159 160 String dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext, 161 inputCalendar.getPopupDateFormat()); 162 163 Application application = facesContext.getApplication(); 164 165 HtmlInputText inputText = null; 166 167 List 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 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 289 static public void addScriptAndCSSResources(FacesContext facesContext) throws IOException { 290 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 getLocalizedLanguageScript(DateFormatSymbols symbols, 309 String [] months, int firstDayOfWeek, HtmlInputCalendar inputCalendar) 310 { 311 int realFirstDayOfWeek = firstDayOfWeek-1; 312 313 String [] 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 ("Week may only start with sunday or monday."); 325 326 StringBuffer script = new StringBuffer (); 327 defineStringArray(script, "jscalendarMonthName", months); 328 defineStringArray(script, "jscalendarDayName", weekDays); 329 setIntegerVariable(script, "jscalendarStartAt",realFirstDayOfWeek); 330 331 if( inputCalendar != null ){ 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 script, String 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 script, String name, String 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 script, String arrayName, String [] 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 getScriptBtnText(String clientId, String dateFormat, String popupButtonString) 388 { 389 390 StringBuffer script = new StringBuffer (); 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 timeKeeper, 409 int currentDay, String [] weekdays, 410 String [] months) 411 throws IOException 412 { 413 Calendar 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 (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 shiftMonth(FacesContext facesContext, 428 Calendar timeKeeper, int currentDay, int shift) 429 { 430 Calendar 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 copyCalendar(FacesContext facesContext, Calendar timeKeeper) 442 { 443 Calendar 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 [] weekdays, FacesContext facesContext, ResponseWriter writer, UIInput inputComponent) 454 throws IOException 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 timeKeeper, int currentDay, int weekStartsAtDayIndex, 467 int weekDayOfFirstDayOfMonth, int lastDayInMonth, String [] weekdays) 468 throws IOException 469 { 470 Calendar 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 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 content, 536 Date valueForLink, String styleClass) 537 throws IOException 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 content, 555 UIInput component, 556 FacesContext facesContext, 557 Date valueForLink) 558 throws IOException 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 [] mapWeekdays(DateFormatSymbols symbols) 622 { 623 String [] weekdays = new String [7]; 624 625 String [] 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 [] mapWeekdaysStartingWithSunday(DateFormatSymbols symbols) 639 { 640 String [] weekdays = new String [7]; 641 642 String [] 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 [] mapMonths(DateFormatSymbols symbols) 656 { 657 String [] months = new String [12]; 658 659 String [] 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 getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object 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 )) 697 { 698 throw new IllegalArgumentException ("Submitted value of type String expected"); 699 } 700 701 return converter.getAsObject(facesContext, uiComponent, (String ) submittedValue); 702 } 703 704 public static class CalendarDateTimeConverter implements Converter 705 { 706 707 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) 708 { 709 if(s==null || s.trim().length()==0) 710 return null; 711 712 DateFormat dateFormat = null; 713 714 if(uiComponent instanceof HtmlInputCalendar && ((HtmlInputCalendar) uiComponent).isRenderAsPopup()) 715 { 716 String popupDateFormat = ((HtmlInputCalendar) uiComponent).getPopupDateFormat(); 717 718 dateFormat = new SimpleDateFormat (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 e) 730 { 731 ConverterException ex = new ConverterException(e); 732 throw ex; 733 } 734 } 735 736 public static String createJSPopupFormat(FacesContext facesContext, String popupDateFormat) 737 { 738 739 if(popupDateFormat == null) 740 { 741 SimpleDateFormat defaultDateFormat = createStandardDateFormat(facesContext); 742 popupDateFormat = defaultDateFormat.toPattern(); 743 } 744 745 StringBuffer jsPopupDateFormat = new StringBuffer (); 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 getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) 768 { 769 Date date = (Date ) o; 770 771 if(date==null) 772 return null; 773 774 DateFormat dateFormat = null; 775 776 if(uiComponent instanceof HtmlInputCalendar && ((HtmlInputCalendar) uiComponent).isRenderAsPopup()) 777 { 778 String popupDateFormat = ((HtmlInputCalendar) uiComponent).getPopupDateFormat(); 779 780 dateFormat = new SimpleDateFormat (createJSPopupFormat(facesContext, popupDateFormat)); 781 } 782 else 783 { 784 dateFormat = createStandardDateFormat(facesContext); 785 } 786 787 return dateFormat.format(date); 788 } 789 790 private static SimpleDateFormat createStandardDateFormat(FacesContext facesContext) 791 { 792 DateFormat dateFormat; 793 dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, 794 facesContext.getViewRoot().getLocale()); 795 796 if(dateFormat instanceof SimpleDateFormat ) 797 return (SimpleDateFormat ) dateFormat; 798 else 799 return new SimpleDateFormat ("dd.MM.yyyy"); 800 } 801 802 } 803 } 804 | Popular Tags |