View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.ole.sys.document.web.renderers;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.JspWriter;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.rice.core.api.config.property.ConfigurationService;
27  
28  /**
29   * Renders a field as a text field with a date picker
30   */
31  public class DateRenderer extends TextRenderer {
32  
33      /**
34       * 
35       * @see org.kuali.ole.sys.document.web.renderers.TextRenderer#clear()
36       */
37      @Override
38      public void clear() {
39          super.clear();
40      }
41  
42      /**
43       * 
44       * @see org.kuali.ole.sys.document.web.renderers.TextRenderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.rice.krad.bo.BusinessObject)
45       */
46      @Override
47      public void render(PageContext pageContext, Tag parentTag) throws JspException {
48          super.render(pageContext, parentTag);
49          
50          JspWriter out = pageContext.getOut();
51          try {
52              out.write(buildDateImage());
53              out.write(buildDateJavascript());
54          }
55          catch (IOException ioe) {
56              throw new JspException("Difficulty rendering date picker", ioe);
57          }
58      }
59      
60      /**
61       * Builds the image for the icon of the date component
62       * @return the HTML for the image icon for the date component
63       */
64      protected String buildDateImage() {
65          StringBuilder dateImage = new StringBuilder();
66          dateImage.append("<img src=\"");
67          dateImage.append(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString("kr.externalizable.images.url")); 
68          dateImage.append("cal.gif\" ");
69          dateImage.append("id=\"");
70          dateImage.append(getFieldName());
71          dateImage.append("_datepicker\" ");
72          dateImage.append("style=\"cursor: pointer;\" ");
73          dateImage.append("title=\"Date selector for ");
74          dateImage.append(getField().getFieldLabel());
75          dateImage.append("\" ");
76          dateImage.append("alt=\"Date selector for ");
77          dateImage.append(this.getAccessibleTitle());
78          dateImage.append("\" ");
79          dateImage.append("onmouseover=\"this.style.backgroundColor='red';\" ");
80          dateImage.append("onmouseout=\"this.style.backgroundColor='transparent';\"");
81          dateImage.append(" />\n");
82          return dateImage.toString();
83      }
84      
85      /**
86       * Builds the JavaScript portion of the date picker
87       * @return the HTML for the javascript to make the date component work
88       */
89      protected String buildDateJavascript() {
90          StringBuilder dateJavascript = new StringBuilder();
91          dateJavascript.append("<script type=\"text/javascript\">\n");
92          dateJavascript.append("Calendar.setup(\n"); 
93          dateJavascript.append("  {\n"); 
94          dateJavascript.append(" inputField : \"");
95          dateJavascript.append(getFieldName());
96          dateJavascript.append("\", // ID of the input field\n");
97          dateJavascript.append(" ifFormat : \"%m/%d/%Y\", // the date format\n"); 
98          dateJavascript.append(" button : \"");
99          dateJavascript.append(getFieldName());
100         dateJavascript.append("_datepicker\" // ID of the button\n"); 
101         dateJavascript.append("  }\n"); 
102         dateJavascript.append(");\n");
103         dateJavascript.append("</script>");
104         
105         return dateJavascript.toString();
106     }
107 
108     /**
109      * Overridden to do nothing - date fields never need quick finders
110      * @see org.kuali.ole.sys.document.web.renderers.FieldRendererBase#renderQuickFinderIfNecessary(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.rice.krad.bo.BusinessObject)
111      */
112     @Override
113     protected void renderQuickFinderIfNecessary(PageContext pageContext, Tag parentTag) throws JspException {
114         // do nothing
115     }
116 
117     
118 }