001 /* 002 * Copyright 2011 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 */ 016 package org.kuali.kfs.sys.document.web.renderers; 017 018 import java.io.IOException; 019 020 import javax.servlet.jsp.JspException; 021 import javax.servlet.jsp.JspWriter; 022 import javax.servlet.jsp.PageContext; 023 import javax.servlet.jsp.tagext.Tag; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.kuali.kfs.sys.document.web.util.RendererUtil; 027 import org.kuali.rice.core.util.KeyLabelPair; 028 029 /** 030 * This renders a drop down field to JSP 031 */ 032 public class DropDownRenderer extends FieldRendererBase { 033 034 /** 035 * 036 * @see org.kuali.kfs.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(buildSelectControl()); 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 drop down control", ioe); 051 } 052 } 053 054 /** 055 * Builds a drop down control, based on the field 056 * @param propertyPrefix the prefix of the property from the form to the business object 057 * @return a String containing the HTML for the drop down control/select box 058 */ 059 protected String buildSelectControl() { 060 StringBuilder selectControl = new StringBuilder(); 061 selectControl.append("<select"); 062 063 selectControl.append(" id=\""); 064 selectControl.append(getFieldName()); 065 selectControl.append("\" "); 066 067 selectControl.append(" name=\""); 068 selectControl.append(getFieldName()); 069 selectControl.append("\" "); 070 071 selectControl.append(" title=\""); 072 selectControl.append(this.getAccessibleTitle()); 073 selectControl.append("\""); 074 075 final String onBlur = buildOnBlur(); 076 if (!StringUtils.isBlank(onBlur)) { 077 selectControl.append(" onblur=\""); 078 selectControl.append(onBlur); 079 selectControl.append("\""); 080 } 081 082 selectControl.append(">"); 083 084 selectControl.append(buildOptions()); 085 086 selectControl.append("</select>"); 087 088 return selectControl.toString(); 089 } 090 091 /** 092 * Builds the options for the select box, given the valid values in the field 093 * @return a String containing all the option tags for this drop down control 094 */ 095 protected String buildOptions() { 096 StringBuilder options = new StringBuilder(); 097 098 for (Object keyLabelPairAsObj : getField().getFieldValidValues()) { 099 options.append(buildOption((KeyLabelPair)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(KeyLabelPair 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.getLabel()); 122 123 option.append("</options>"); 124 125 return option.toString(); 126 } 127 128 /** 129 * No quickfinder for us, thanks 130 * @see org.kuali.kfs.sys.document.web.renderers.FieldRenderer#renderQuickfinder() 131 */ 132 public boolean renderQuickfinder() { 133 return false; 134 } 135 136 }