1 33 34 package com.icesoft.faces.component.selectinputdate; 35 36 import com.icesoft.faces.component.CSS_DEFAULT; 37 import com.icesoft.faces.component.ext.HtmlInputText; 38 import com.icesoft.faces.component.ext.taglib.Util; 39 import com.icesoft.faces.context.DOMResponseWriter; 40 import com.icesoft.faces.util.CoreUtils; 41 42 import org.krysalis.jcharts.properties.LegendProperties; 43 44 import javax.faces.context.FacesContext; 45 import javax.faces.el.ValueBinding; 46 import java.io.IOException ; 47 import java.text.DateFormat ; 48 import java.text.SimpleDateFormat ; 49 import java.util.ArrayList ; 50 import java.util.Calendar ; 51 import java.util.Date ; 52 import java.util.HashMap ; 53 import java.util.List ; 54 import java.util.Map ; 55 56 68 public class SelectInputDate 69 extends HtmlInputText { 70 73 public static final String COMPONENT_TYPE = 74 "com.icesoft.faces.SelectInputDate"; 75 78 public static final String COMPONENT_FAMILY = "javax.faces.Input"; 79 82 private static final String DEFAULT_RENDERER_TYPE = 83 "com.icesoft.faces.Calendar"; 84 87 public static final String DEFAULT_POPUP_DATE_FORMAT = "MM/dd/yyyy"; 88 89 private String style = null; 91 92 private String styleClass = null; 93 94 97 private Boolean _renderAsPopup = null; 98 101 private String _imageDir = null; 102 105 private String _moveNextImage = null; 106 109 private String _movePreviousImage = null; 110 113 private String _openPopupImage = null; 114 117 private String _closePopupImage = null; 118 119 123 private String _popupDateFormat = null; 124 125 128 private List showPopup = new ArrayList (); 129 132 private boolean navEvent = false; 133 136 private Date navDate = null; 137 138 144 private final String DEFAULT_IMAGEDIR = "/xmlhttp/css/xp/css-images/"; 145 148 private final String DEFAULT_MOVENEXT = "cal_arrow_right.gif"; 149 152 private final String DEFAULT_MOVEPREV = "cal_arrow_left.gif"; 153 156 private final String DEFAULT_OPENPOPUP = "cal_button.gif"; 157 160 private final String DEFAULT_CLOSEPOPUP = "cal_off.gif"; 161 164 private DateFormat myDateFormat = 165 new SimpleDateFormat (DEFAULT_POPUP_DATE_FORMAT); 166 167 170 public SelectInputDate() { 171 setRendererType(DEFAULT_RENDERER_TYPE); 172 } 173 174 public void encodeBegin(FacesContext context) throws IOException { 175 super.encodeBegin(context); 176 buildHeighLightMap(); 177 } 178 179 185 186 public String getStyle() { 187 if (this.style != null) { 188 return this.style; 189 } 190 ValueBinding _vb = getValueBinding("style"); 191 192 193 if (_vb != null) { 194 return (String ) _vb.getValue(getFacesContext()); 195 } 196 return null; 197 } 198 199 206 public void setStyle(String style) { 207 this.style = style; 208 } 209 210 211 217 public String formatDate(Date date) { 218 if (date != null) { 219 return myDateFormat.format(date); 220 } else { 221 return ""; 222 } 223 } 224 225 231 public void setNavEvent(boolean navEvent) { 232 this.navEvent = navEvent; 233 } 234 235 240 public boolean isNavEvent() { 241 return this.navEvent; 242 } 243 244 251 public void setNavDate(Date navDate) { 252 this.navDate = navDate; 253 } 254 255 260 public Date getNavDate() { 261 return this.navDate; 262 } 263 264 271 public void setShowPopup(boolean showPopup) { 272 if (showPopup) { 273 this.showPopup.add(getClientId(FacesContext.getCurrentInstance())); 274 } else { 275 this.showPopup 276 .remove(getClientId(FacesContext.getCurrentInstance())); 277 } 278 } 279 280 286 public boolean isShowPopup() { 287 if (showPopup 288 .contains(getClientId(FacesContext.getCurrentInstance()))) { 289 return true; 290 } else { 291 return false; 292 } 293 } 294 295 298 public String getFamily() { 299 return COMPONENT_FAMILY; 300 } 301 302 303 311 public String getMonthYearRowClass() { 312 return Util.getQualifiedStyleClass(this, 313 CSS_DEFAULT.DEFAULT_YEARMONTHHEADER_CLASS, 314 isDisabled()); 315 } 316 317 318 325 public String getWeekRowClass() { 326 return Util.getQualifiedStyleClass(this, 327 CSS_DEFAULT.DEFAULT_WEEKHEADER_CLASS, 328 isDisabled()); 329 } 330 331 334 public String getCalendarInputClass() { 335 return Util.getQualifiedStyleClass(this, 336 CSS_DEFAULT.DEFAULT_CALENDARINPUT_CLASS, 337 isDisabled()); 338 } 339 340 347 public String getDayCellClass() { 348 return Util.getQualifiedStyleClass(this, 349 CSS_DEFAULT.DEFAULT_DAYCELL_CLASS, 350 isDisabled()); 351 } 352 353 356 public void setStyleClass(String styleClass) { 357 this.styleClass = styleClass; 358 } 359 360 365 public String getStyleClass() { 366 return Util.getQualifiedStyleClass(this, 367 styleClass, 368 CSS_DEFAULT.DEFAULT_CALENDAR, 369 "styleClass", isDisabled()); 370 } 371 372 377 public String getCurrentDayCellClass() { 378 return Util.getQualifiedStyleClass(this, 379 CSS_DEFAULT.DEFAULT_CURRENTDAYCELL_CLASS, 380 isDisabled()); 381 } 382 383 384 387 public boolean isRenderAsPopup() { 388 if (_renderAsPopup != null) { 389 return _renderAsPopup.booleanValue(); 390 } 391 ValueBinding vb = getValueBinding("renderAsPopup"); 392 Boolean v = 393 vb != null ? (Boolean ) vb.getValue(getFacesContext()) : null; 394 return v != null ? v.booleanValue() : false; 395 } 396 397 400 public void setRenderAsPopup(boolean b) { 401 _renderAsPopup = new Boolean (b); 402 } 403 404 410 public void setImageDir(String imageDir) { 411 _imageDir = imageDir; 412 } 413 414 418 public String getImageDir() { 419 if (_imageDir != null) { 420 return _imageDir; 421 } 422 423 ValueBinding vb = getValueBinding("imageDir"); 424 if (vb != null) { 425 return (String ) vb.getValue(getFacesContext()); 426 } else { 427 return DEFAULT_IMAGEDIR; 428 } 429 } 430 431 434 public String getMoveNextImage() { 435 return this.DEFAULT_MOVENEXT; 436 } 437 438 443 public String getMovePreviousImage() { 444 return this.DEFAULT_MOVEPREV; 445 } 446 447 452 public String getOpenPopupImage() { 453 return this.DEFAULT_OPENPOPUP; 454 } 455 456 461 public String getClosePopupImage() { 462 return this.DEFAULT_CLOSEPOPUP; 463 } 464 465 471 public void setPopupDateFormat(String popupDateFormat) { 472 _popupDateFormat = popupDateFormat; 473 } 474 475 480 public String getPopupDateFormat() { 481 if (_popupDateFormat != null) { 482 myDateFormat = new SimpleDateFormat (_popupDateFormat); 483 return _popupDateFormat; 484 } 485 ValueBinding vb = getValueBinding("popupDateFormat"); 486 if (vb != null ) { 487 myDateFormat = new SimpleDateFormat ((String ) vb.getValue(getFacesContext())); 488 return (String ) vb.getValue(getFacesContext()); 489 } else { 490 return DEFAULT_POPUP_DATE_FORMAT; 491 } 492 } 493 494 497 public Object saveState(FacesContext context) { 498 Object values[] = new Object [10]; 499 values[0] = super.saveState(context); 500 values[1] = _renderAsPopup; 501 values[2] = _popupDateFormat; 502 values[3] = _imageDir; 503 values[4] = _moveNextImage; 504 values[5] = _movePreviousImage; 505 values[6] = _openPopupImage; 506 values[7] = _closePopupImage; 507 values[8] = new Boolean (navEvent); 508 values[9] = navDate; 509 return ((Object ) (values)); 510 } 511 512 515 public void restoreState(FacesContext context, Object state) { 516 Object values[] = (Object []) state; 517 super.restoreState(context, values[0]); 518 _renderAsPopup = (Boolean ) values[1]; 519 _popupDateFormat = (String ) values[2]; 520 _imageDir = (String ) values[3]; 521 _moveNextImage = (String ) values[4]; 522 _movePreviousImage = (String ) values[5]; 523 _openPopupImage = (String ) values[6]; 524 _closePopupImage = (String ) values[7]; 525 navEvent = ((Boolean ) values[8]).booleanValue(); 526 navDate = (Date ) values[9]; 527 } 528 529 private Map linkMap = new HashMap (); 530 531 534 public Map getLinkMap() { 535 return linkMap; 536 } 537 538 541 public void setLinkMap(Map linkMap) { 542 this.linkMap = linkMap; 543 } 544 545 private String selectedDayLink; 546 547 550 public String getSelectedDayLink() { 551 return selectedDayLink; 552 } 553 554 557 public void setSelectedDayLink(String selectedDayLink) { 558 this.selectedDayLink = selectedDayLink; 559 } 560 561 566 public Object getValue() { 567 if (super.getValue() == null) { 568 if (DOMResponseWriter.isStreamWriting()) { 569 return new Date (); 570 } 571 return null; 572 } else { 573 return super.getValue(); 574 } 575 } 576 577 private String highlightClass; 578 583 public void setHighlightClass(String highlightClass) { 584 this.highlightClass = highlightClass; 585 } 586 587 593 public String getHighlightClass() { 594 if (highlightClass != null) { 595 return highlightClass; 596 } 597 ValueBinding vb = getValueBinding("highlightClass"); 598 return vb != null ? (String ) vb.getValue(getFacesContext()) : ""; 599 } 600 601 private String highlightUnit; 602 607 public void setHighlightUnit(String highlightUnit) { 608 this.highlightUnit = highlightUnit; 609 } 610 611 617 public String getHighlightUnit() { 618 if (highlightUnit != null) { 619 return highlightUnit; 620 } 621 ValueBinding vb = getValueBinding("highlightUnit"); 622 return vb != null ? (String ) vb.getValue(getFacesContext()) : ""; 623 } 624 625 private String highlightValue; 626 631 public void setHighlightValue(String highlightValue) { 632 this.highlightValue = highlightValue; 633 } 634 635 641 public String getHighlightValue() { 642 if (highlightValue != null) { 643 return highlightValue; 644 } 645 ValueBinding vb = getValueBinding("highlightValue"); 646 return vb != null ? (String ) vb.getValue(getFacesContext()) : ""; 647 } 648 649 private Map hightlightRules = new HashMap (); 650 private Map unitMap = new UnitMap(); 651 private void buildHeighLightMap() { 652 validateHighlight(); 653 resetHighlightClasses(Calendar.YEAR); 654 } 655 656 private boolean validateHighlight() { 657 hightlightRules.clear(); 658 String highlightClassArray[] = getHighlightClass().split(":"); 659 String highlightUnitArray[] = getHighlightUnit().split(":"); 660 String highlightValueArray[] = getHighlightValue().split(":"); 661 if ((highlightClassArray.length < 1 ) || 662 highlightClassArray[0].equals("") || 663 highlightUnitArray[0].equals("") || 664 highlightValueArray[0].equals("")) { 665 return false; 666 } 667 if (!(highlightClassArray.length == highlightUnitArray.length) || 668 !(highlightUnitArray.length == highlightValueArray.length)) { 669 System.out.println("\n[SelectInputDate] The following attributes does not have corresponding values:" + 670 "\n-highlightClass \n-highlightUnit \n-highlightValue \n" + 671 "Note: When highlighting required, all above attributes " + 672 "need to be used together and should have corresponding values.\n" + 673 "Each entity can be separated using the : colon, e.g. \n" + 674 "highlightClass=\"weekend: newyear\" \n" + 675 "highlightUnit=\"DAY_OF_WEEK: DAY_OF_YEAR\" \n"+ 676 "highlightValue=\"1, 7: 1\" " 677 ); 678 return false; 679 } 680 681 for(int i = 0; i < highlightUnitArray.length; i++) { 682 try { 683 int option = Integer.parseInt(highlightUnitArray[i].trim()); 684 if (option <1 || option > 8) { 685 System.out.println("[SelectInputDate:highlightUnit] \""+ highlightUnitArray[i].trim() +"\" " + 686 "s not a valid unit value. Valid values are between 1 to 8"); 687 return false; 688 } 689 } catch (NumberFormatException exception) { 690 if (unitMap.containsKey(highlightUnitArray[i].trim())) { 691 highlightUnitArray[i] = String.valueOf(unitMap.get( 692 highlightUnitArray[i].trim())); 693 } else { 694 System.out.println("[SelectInputDate:highlightUnit] \""+ highlightUnitArray[i] +"\" is " + 695 "not a valid unit value, String representation " + 696 "of unit must match with java.util.Calendar contants (e.g.)" + 697 "\nYEAR, MONTH, WEEK_OF_YEAR, WEEK_OF_MONTH, DATE, DAY_OF_YEAR, " + 698 "DAY_OF_WEEK and DAY_OF_WEEK_IN_MONTH"); 699 return false; 700 } 701 } 702 String [] value = highlightValueArray[i].replaceAll(" ", "").trim().split(","); 703 for (int j=0; j <value.length; j++ ) { 704 hightlightRules.put(highlightUnitArray[i].trim() + "$"+ value[j] , highlightClassArray[i]); 705 } 706 } 707 708 709 return true; 710 } 711 712 Map getHightlightRules() { 713 return hightlightRules; 714 } 715 716 void setHightlightRules(Map hightlightRules) { 717 this.hightlightRules = hightlightRules; 718 } 719 720 private String highlightYearClass = ""; 721 private String highlightMonthClass =""; 722 private String highlightWeekClass =""; 723 private String highlightDayClass =""; 724 725 String getHighlightDayCellClass() { 726 return highlightYearClass + 727 highlightMonthClass + 728 highlightWeekClass + 729 highlightDayClass; 730 } 731 732 String getHighlightMonthClass() { 733 return highlightMonthClass; 734 } 735 736 void setHighlightMonthClass(String highlightMonthClass) { 737 this.highlightMonthClass = highlightMonthClass; 738 } 739 740 String getHighlightYearClass() { 741 return highlightYearClass; 742 } 743 744 void setHighlightYearClass(String highlightYearClass) { 745 this.highlightYearClass = highlightYearClass; 746 } 747 748 String getHighlightWeekClass() { 749 return highlightWeekClass; 750 } 751 752 void addHighlightWeekClass(String highlightWeekClass) { 753 if (this.highlightWeekClass.indexOf(highlightWeekClass) == -1) { 754 this.highlightWeekClass += (highlightWeekClass + " "); 755 } 756 } 757 758 void addHighlightDayClass(String highlightDayClass) { 759 if (this.highlightDayClass.indexOf(highlightDayClass) == -1) { 760 this.highlightDayClass += (highlightDayClass + " "); 761 } 762 } 763 764 void resetHighlightClasses(int level) { 765 if (level <= Calendar.MONTH) { 766 this.highlightMonthClass = ""; 767 this.highlightYearClass = ""; 768 this.highlightDayClass = ""; 769 } 770 this.highlightDayClass = ""; 771 this.highlightWeekClass = ""; 772 } 773 } 774 775 class UnitMap extends HashMap { 776 public UnitMap() { 777 this.put("YEAR", new Integer (Calendar.YEAR)); 778 this.put("MONTH", new Integer (Calendar.MONTH)); 779 this.put("WEEK_OF_YEAR", new Integer (Calendar.WEEK_OF_YEAR)); 780 this.put("WEEK_OF_MONTH", new Integer (Calendar.WEEK_OF_MONTH)); 781 this.put("DATE", new Integer (Calendar.DATE)); 782 this.put("DAY_OF_YEAR", new Integer (Calendar.DAY_OF_YEAR)); 783 this.put("DAY_OF_WEEK", new Integer (Calendar.DAY_OF_WEEK)); 784 this.put("DAY_OF_WEEK_IN_MONTH", new Integer (Calendar.DAY_OF_WEEK_IN_MONTH)); 785 } 786 787 public int getConstant(String key) { 788 if (!super.containsKey(key)) { 789 return 0; 790 } 791 return Integer.parseInt(super.get(key).toString()); 792 } 793 } 794 795 | Popular Tags |