001/*
002 * Copyright 2008 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.sys.document.web.renderers;
017
018import java.io.IOException;
019
020import javax.servlet.jsp.JspException;
021import javax.servlet.jsp.JspWriter;
022import javax.servlet.jsp.PageContext;
023import javax.servlet.jsp.tagext.Tag;
024
025import org.apache.commons.lang.StringUtils;
026import org.kuali.ole.sys.document.web.util.RendererUtil;
027import org.kuali.rice.core.api.util.KeyValue;
028
029/**
030 * Renders a control as a group of radio buttons
031 */
032public class RadioButtonGroupRenderer extends FieldRendererBase {
033
034    /**
035     * 
036     * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
037     */
038    public void render(PageContext pageContext, Tag parentTag) throws JspException {
039        JspWriter out = pageContext.getOut();
040        
041        try {
042            out.write(buildRadioButtons());
043            renderQuickFinderIfNecessary(pageContext, parentTag);
044            if (isShowError()) {
045                renderErrorIcon(pageContext);
046            }
047            RendererUtil.registerEditableProperty(pageContext, getFieldName());
048        }
049        catch (IOException ioe) {
050            throw new JspException("Difficulty rendering radio buttons", ioe);
051        }
052    }
053
054    /**
055     * Builds radio buttons for all the valid values on the field
056     * @param propertyPrefix the property path from the form to the business object being rendered
057     * @return a String containing the HTML for all the radio buttons
058     */
059    protected String buildRadioButtons() {
060        StringBuilder radioButtons = new StringBuilder();
061        for (Object keyLabelPairAsObject : getField().getFieldValidValues()) {
062            radioButtons.append(buildRadioButton((KeyValue)keyLabelPairAsObject));
063        }
064        return radioButtons.toString();
065    }
066    
067    /**
068     * Given a KeyValue, generates a radio buttion representing it
069     * @param keyLabelPair the key label pair to turn into a radio button
070     * @param propertyPrefix the property path from the form to the business object being rendered
071     * @return the HTML for the represented radio button
072     */
073    protected String buildRadioButton(KeyValue keyLabelPair) {
074        StringBuilder radioButton = new StringBuilder();
075        
076        radioButton.append("<input type=\"radio\"");
077        
078        if (getField().getPropertyValue().equalsIgnoreCase(keyLabelPair.getKey().toString())) {
079            radioButton.append(" checked=\"checked\"");
080        }
081        
082        radioButton.append(" title=\"");
083        radioButton.append(this.getAccessibleTitle());
084        radioButton.append("\"");
085        
086        radioButton.append(" name=\"");
087        radioButton.append(getFieldName());
088        radioButton.append("\"");
089        
090        radioButton.append(" id=\"");
091        radioButton.append(getFieldName()+"_"+keyLabelPair.getKey().toString().replaceAll("\\W", "_"));
092        radioButton.append("\"");
093        
094        radioButton.append(" value=\"");
095        radioButton.append(keyLabelPair.getKey());
096        radioButton.append("\"");
097        
098        String onBlur = buildOnBlur();
099        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}