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   * Renders a control as a group of radio buttons
31   */
32  public class RadioButtonGroupRenderer 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(buildRadioButtons());
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 radio buttons", ioe);
51          }
52      }
53  
54      /**
55       * Builds radio buttons for all the valid values on the field
56       * @param propertyPrefix the property path from the form to the business object being rendered
57       * @return a String containing the HTML for all the radio buttons
58       */
59      protected String buildRadioButtons() {
60          StringBuilder radioButtons = new StringBuilder();
61          for (Object keyLabelPairAsObject : getField().getFieldValidValues()) {
62              radioButtons.append(buildRadioButton((KeyValue)keyLabelPairAsObject));
63          }
64          return radioButtons.toString();
65      }
66      
67      /**
68       * Given a KeyValue, generates a radio buttion representing it
69       * @param keyLabelPair the key label pair to turn into a radio button
70       * @param propertyPrefix the property path from the form to the business object being rendered
71       * @return the HTML for the represented radio button
72       */
73      protected String buildRadioButton(KeyValue keyLabelPair) {
74          StringBuilder radioButton = new StringBuilder();
75          
76          radioButton.append("<input type=\"radio\"");
77          
78          if (getField().getPropertyValue().equalsIgnoreCase(keyLabelPair.getKey().toString())) {
79              radioButton.append(" checked=\"checked\"");
80          }
81          
82          radioButton.append(" title=\"");
83          radioButton.append(this.getAccessibleTitle());
84          radioButton.append("\"");
85          
86          radioButton.append(" name=\"");
87          radioButton.append(getFieldName());
88          radioButton.append("\"");
89          
90          radioButton.append(" id=\"");
91          radioButton.append(getFieldName()+"_"+keyLabelPair.getKey().toString().replaceAll("\\W", "_"));
92          radioButton.append("\"");
93          
94          radioButton.append(" value=\"");
95          radioButton.append(keyLabelPair.getKey());
96          radioButton.append("\"");
97          
98          String onBlur = buildOnBlur();
99          if (!StringUtils.isBlank(onBlur)) {
100             radioButton.append(" ");
101             radioButton.append(onBlur);
102         }
103         
104         radioButton.append(" /> ");
105         radioButton.append(keyLabelPair.getValue());
106         radioButton.append(" ");
107         
108         return radioButton.toString();
109     }
110 
111     /**
112      * No quickfinder
113      * @see org.kuali.ole.sys.document.web.renderers.FieldRenderer#renderQuickfinder()
114      */
115     public boolean renderQuickfinder() {
116         return false;
117     }
118     
119 }