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.apache.commons.lang.StringUtils;
26  import org.kuali.ole.sys.document.web.util.RendererUtil;
27  import org.kuali.rice.core.api.util.KeyValue;
28  
29  /**
30   * This renders a drop down field to JSP
31   */
32  public class DropDownRenderer extends FieldRendererBase {
33  
34      /**
35       * 
36       * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
37       */
38      public void render(PageContext pageContext, Tag parentTag) throws JspException {
39          JspWriter out = pageContext.getOut();
40          
41          try {
42              out.write(buildSelectControl());
43              renderQuickFinderIfNecessary(pageContext, parentTag);
44              if (isShowError()) {
45                  renderErrorIcon(pageContext);
46              }
47              RendererUtil.registerEditableProperty(pageContext, getFieldName());
48          }
49          catch (IOException ioe) {
50              throw new JspException("Difficulty rendering drop down control", ioe);
51          }
52      }
53  
54      /**
55       * Builds a drop down control, based on the field
56       * @param propertyPrefix the prefix of the property from the form to the business object
57       * @return a String containing the HTML for the drop down control/select box
58       */
59      protected String buildSelectControl() {
60          StringBuilder selectControl = new StringBuilder();
61          selectControl.append("<select");
62          
63          selectControl.append(" id=\"");
64          selectControl.append(getFieldName());
65          selectControl.append("\" ");
66          
67          selectControl.append(" name=\"");
68          selectControl.append(getFieldName());
69          selectControl.append("\" ");
70          
71          selectControl.append(" title=\"");
72          selectControl.append(this.getAccessibleTitle());
73          selectControl.append("\"");
74          
75          final String onBlur = buildOnBlur();
76          if (!StringUtils.isBlank(onBlur)) {
77              selectControl.append(" onblur=\"");
78              selectControl.append(onBlur);
79              selectControl.append("\"");
80          }
81          
82          selectControl.append(">");
83          
84          selectControl.append(buildOptions());
85          
86          selectControl.append("</select>");
87          
88          return selectControl.toString();
89      }
90      
91      /**
92       * Builds the options for the select box, given the valid values in the field
93       * @return a String containing all the option tags for this drop down control
94       */
95      protected String buildOptions() {
96          StringBuilder options = new StringBuilder();
97          
98          for (Object keyLabelPairAsObj : getField().getFieldValidValues()) {
99              options.append(buildOption((KeyValue)keyLabelPairAsObj));
100         }
101         
102         return options.toString();
103     }
104     
105     /**
106      * Builds an option tag for the given key label pair
107      * @param keyLabelPair the key label pair to create an option tag from
108      * @return the String with the option tag in it
109      */
110     protected String buildOption(KeyValue keyLabelPair) {
111         StringBuilder option = new StringBuilder();
112         
113         option.append("<option value=\"");
114         option.append(keyLabelPair.getKey());
115         option.append("\"");
116         if (getField().getPropertyValue().equalsIgnoreCase(keyLabelPair.getKey().toString())) {
117             option.append(" selected=\"selected\"");
118         }
119         option.append(">");
120         
121         option.append(keyLabelPair.getValue());
122         
123         option.append("</options>");
124         
125         return option.toString();
126     }
127 
128     /**
129      * No quickfinder for us, thanks
130      * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#renderQuickfinder()
131      */
132     public boolean renderQuickfinder() {
133         return false;
134     }
135     
136 }