1 16 package com.blandware.atleap.webapp.taglib.core.html; 17 18 19 import com.blandware.atleap.common.util.DateUtil; 20 import com.blandware.atleap.webapp.util.core.ApplicationResources; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.struts.Globals; 24 import org.apache.struts.taglib.TagUtils; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.SimpleTagSupport ; 30 import java.io.IOException ; 31 import java.io.StringWriter ; 32 import java.util.Locale ; 33 34 123 public class CalendarTag extends SimpleTagSupport { 124 125 protected transient final Log log = LogFactory.getLog(CalendarTag.class); 126 127 132 protected String fieldName; 133 134 139 protected String anchorName = null; 140 141 149 protected String datePattern = null; 150 151 155 protected String startDate = null; 156 157 162 protected String style = "window"; 163 164 170 protected String divStyleId = null; 171 172 176 protected String cssPrefix = "calendar-"; 177 178 182 protected Boolean showYearNavigation = Boolean.FALSE; 183 184 188 protected Boolean showYearNavigationInput = Boolean.FALSE; 189 190 194 protected Boolean showNavigationDropdowns = Boolean.FALSE; 195 196 201 protected String monthNamesKey = null; 202 203 208 protected String dayHeadersKey = null; 209 210 215 protected String weekStartDayKey = null; 216 217 222 protected String todayTextKey = null; 223 224 protected static final String NUMBER_KEY = "com.blandware.atleap.taglib.CALENDAR_TAG_NUMBER"; 225 protected static final String DIV_STYLE_ID_KEY = "com.blandware.atleap.taglib.CALENDAR_TAG_DIV_STYLE_ID"; 226 227 237 public String getFieldName() { 238 return fieldName; 239 } 240 241 247 public void setFieldName(String fieldName) { 248 this.fieldName = fieldName; 249 } 250 251 261 public String getAnchorName() { 262 return anchorName; 263 } 264 265 271 public void setAnchorName(String anchorName) { 272 this.anchorName = anchorName; 273 } 274 275 285 public String getDatePattern() { 286 return datePattern; 287 } 288 289 295 public void setDatePattern(String datePattern) { 296 this.datePattern = datePattern; 297 } 298 299 309 public String getStartDate() { 310 return startDate; 311 } 312 313 319 public void setStartDate(String startDate) { 320 this.startDate = startDate; 321 } 322 323 333 public String getStyle() { 334 return style; 335 } 336 337 343 public void setStyle(String style) { 344 this.style = style; 345 } 346 347 357 public String getDivStyleId() { 358 return divStyleId; 359 } 360 361 367 public void setDivStyleId(String divStyleId) { 368 this.divStyleId = divStyleId; 369 } 370 371 381 public String getCssPrefix() { 382 return cssPrefix; 383 } 384 385 391 public void setCssPrefix(String cssPrefix) { 392 this.cssPrefix = cssPrefix; 393 } 394 395 405 public Boolean getShowYearNavigation() { 406 return showYearNavigation; 407 } 408 409 415 public void setShowYearNavigation(Boolean showYearNavigation) { 416 this.showYearNavigation = showYearNavigation; 417 } 418 419 429 public Boolean getShowYearNavigationInput() { 430 return showYearNavigationInput; 431 } 432 433 439 public void setShowYearNavigationInput(Boolean showYearNavigationInput) { 440 this.showYearNavigationInput = showYearNavigationInput; 441 } 442 443 453 public Boolean getShowNavigationDropdowns() { 454 return showNavigationDropdowns; 455 } 456 457 463 public void setShowNavigationDropdowns(Boolean showNavigationDropdowns) { 464 this.showNavigationDropdowns = showNavigationDropdowns; 465 } 466 467 477 public String getMonthNamesKey() { 478 return monthNamesKey; 479 } 480 481 487 public void setMonthNamesKey(String monthNamesKey) { 488 this.monthNamesKey = monthNamesKey; 489 } 490 491 501 public String getDayHeadersKey() { 502 return dayHeadersKey; 503 } 504 505 511 public void setDayHeadersKey(String dayHeadersKey) { 512 this.dayHeadersKey = dayHeadersKey; 513 } 514 515 525 public String getWeekStartDayKey() { 526 return weekStartDayKey; 527 } 528 529 535 public void setWeekStartDayKey(String weekStartDayKey) { 536 this.weekStartDayKey = weekStartDayKey; 537 } 538 539 549 public String getTodayTextKey() { 550 return todayTextKey; 551 } 552 553 559 public void setTodayTextKey(String todayTextKey) { 560 this.todayTextKey = todayTextKey; 561 } 562 563 569 public void doTag() throws JspException , IOException { 570 571 PageContext pageContext = (PageContext ) getJspContext(); 572 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 573 574 if ( datePattern == null ) { 575 datePattern = DateUtil.getDatePattern((Locale ) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE)); 576 } 577 578 Integer tagNumber = (Integer ) pageContext.getAttribute(NUMBER_KEY); 579 580 if ( tagNumber == null ) { 581 tagNumber = new Integer (0); 582 } else { 583 tagNumber = new Integer (tagNumber.intValue() + 1); 584 } 585 586 if ( log.isDebugEnabled() ) { 587 log.debug("This tag number will be " + tagNumber); 588 } 589 590 pageContext.setAttribute(NUMBER_KEY, tagNumber); 591 592 String var = "jsCalendar" + tagNumber; 593 594 if ( log.isDebugEnabled() ) { 595 log.debug("Anchor name: " + anchorName); 596 } 597 598 if ( anchorName == null ) { 599 anchorName = "jsCalendarAnchor" + tagNumber; 600 } 601 602 if ( "div".equalsIgnoreCase(style) ) { 603 if ( divStyleId == null ) { 604 divStyleId = "jsCalendarDiv"; 605 } 606 } else { 607 divStyleId = null; 608 } 609 610 if ( log.isDebugEnabled() ) { 611 log.debug("Style: " + style + "\tDIV style ID: " + divStyleId); 612 } 613 614 String script = "<script language=\"javascript\">\n"; 615 if ( divStyleId != null ) { 616 script += "var " + var + " = new CalendarPopup(\'" + divStyleId + "\');\n"; 617 } else { 618 script += "var " + var + " = new CalendarPopup();\n"; 619 } 620 if ( cssPrefix != null ) { 621 script += var + ".setCssPrefix(\'" + cssPrefix + "\');\n"; 622 } 623 if ( showYearNavigation.booleanValue() ) { 624 script += var + ".showYearNavigation();\n"; 625 } 626 627 if ( showYearNavigationInput.booleanValue() ) { 628 script += var + ".showYearNavigationInput();\n"; 629 } 630 631 if ( showNavigationDropdowns.booleanValue() ) { 632 script += var + ".showNavigationDropDowns();\n"; 633 } 634 635 if ( monthNamesKey == null ) { 636 monthNamesKey = "core.calendar.monthNames"; 637 } 638 639 if ( dayHeadersKey == null ) { 640 dayHeadersKey = "core.calendar.dayHeaders"; 641 } 642 643 if ( weekStartDayKey == null ) { 644 weekStartDayKey = "core.calendar.weekStartDay"; 645 } 646 if ( todayTextKey == null ) { 647 todayTextKey = "core.calendar.todayText"; 648 } 649 650 TagUtils tagUtils = TagUtils.getInstance(); 651 ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext()); 652 String monthNames = applicationResources.getMessage(request, monthNamesKey); 653 if ( monthNames == null ) { 654 if ( log.isWarnEnabled() ) { 655 log.warn("No message for key '" + monthNamesKey + "' was found "); 656 } 657 } else { 658 script += var + ".setMonthNames(" + monthNames + ");\n"; 659 } 660 661 String dayHeaders = applicationResources.getMessage(request, dayHeadersKey); 662 if ( dayHeaders == null ) { 663 if ( log.isWarnEnabled() ) { 664 log.warn("No message for key '" + dayHeadersKey + "' was found "); 665 } 666 } else { 667 script += var + ".setDayHeaders(" + dayHeaders + ");\n"; 668 } 669 670 String weekStartDay = applicationResources.getMessage(request, weekStartDayKey); 671 if ( weekStartDay == null ) { 672 if ( log.isWarnEnabled() ) { 673 log.warn("No message for key '" + weekStartDayKey + "' was found "); 674 } 675 } else { 676 script += var + ".setWeekStartDay(" + weekStartDay + ");\n"; 677 } 678 679 String todayText = applicationResources.getMessage(request, todayTextKey); 680 if ( todayText == null ) { 681 if ( log.isWarnEnabled() ) { 682 log.warn("No message for key " + todayTextKey + " was found "); 683 } 684 } else { 685 script += var + ".setTodayText(\'" + todayText + "\');\n"; 686 } 687 688 script += "</script>\n"; 689 690 StringWriter sw = new StringWriter (); 691 StringBuffer sb = sw.getBuffer(); 692 sb.append(script).append("<a HREF=\"#\" id=\"") 693 .append(anchorName).append("\" name=\"") 694 .append(anchorName).append("\" onclick=\"") 695 .append(var).append(".select(").append(fieldName) 696 .append(", \'").append(anchorName).append("\', \'") 697 .append(datePattern).append("\'") 698 .append(startDate != null ? ", \'" + startDate + "\'" : ");") 699 .append(" return false;\">"); 700 701 getJspBody().invoke(sw); 702 sb.append("</a>\n"); 703 704 if ( style.equalsIgnoreCase("div") && pageContext.getAttribute(DIV_STYLE_ID_KEY) == null ) { 705 sb.append("<div id=\"").append(divStyleId).append("\" style=\"visibility: hidden; position: absolute; background-color: white; layer-background-color: white;\"></div>"); 706 pageContext.setAttribute(DIV_STYLE_ID_KEY, divStyleId); 707 } 708 709 tagUtils.write(pageContext, sw.toString()); 710 } 711 712 } 713 | Popular Tags |