1 /*** 2 * Copyright (c) 2003 held jointly by the individual authors. 3 * 4 * This library is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU Lesser General Public License as published 6 * by the Free Software Foundation; either version 2.1 of the License, or 7 * (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; with out even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this library; if not, write to the Free Software Foundation, 16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 17 * 18 * > http://www.gnu.org/copyleft/lesser.html 19 * > http://www.opensource.org/licenses/lgpl-license.php 20 */ 21 package net.mlw.vlh.web.tag; 22 23 import java.util.Arrays; 24 import java.util.Collections; 25 import java.util.Iterator; 26 import java.util.List; 27 28 import javax.servlet.jsp.JspException; 29 import javax.servlet.jsp.tagext.BodyTagSupport; 30 31 import net.mlw.vlh.ValueList; 32 import net.mlw.vlh.web.util.JspUtils; 33 34 import org.apache.commons.beanutils.BeanUtils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 /*** 39 * Generate a select box from a ValueList <br> 40 * 41 * @author Matthew L. Wilson 42 * @version $Revision: 1.8 $ $Date: 2005/11/23 15:02:16 $ 43 */ 44 public class DefaultSelectTag extends BodyTagSupport 45 { 46 47 private static final Log LOGGER = LogFactory.getLog(DefaultSelectTag.class); 48 49 private String name; 50 51 private String attributes; 52 53 private String value; 54 55 private String text; 56 57 /*** 58 * @see javax.servlet.jsp.tagext.TagSupport#doEndTag() 59 */ 60 public int doEndTag() throws JspException 61 { 62 ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class); 63 64 ValueList valueList = rootTag.getValueList(); 65 66 StringBuffer sb = new StringBuffer(); 67 68 sb.append("<select name=").append("'").append(name).append("'").append(attributes).append(">"); 69 70 if (bodyContent != null && bodyContent.getString() != null) 71 { 72 sb.append(bodyContent.getString()); 73 } 74 75 try 76 { 77 String[] svalues = pageContext.getRequest().getParameterValues(name); 78 List values = (svalues == null) ? Collections.EMPTY_LIST : Arrays.asList(svalues); 79 80 for (Iterator iter = valueList.getList().iterator(); iter.hasNext();) 81 { 82 Object bean = iter.next(); 83 String value = BeanUtils.getProperty(bean, this.value); 84 85 sb.append("<option "); 86 if (values.contains(value)) 87 { 88 sb.append("selected='true' "); 89 } 90 sb.append("value='").append(value).append("'>"); 91 sb.append(BeanUtils.getProperty(bean, text)); 92 sb.append("</option>"); 93 } 94 } 95 catch (Exception e) 96 { 97 LOGGER.error("DefaultSelectTag.doEndTag() exception...", e); 98 throw new JspException(e); 99 } 100 sb.append("</select>"); 101 102 JspUtils.write(pageContext, sb.toString()); 103 104 release(); 105 106 return EVAL_PAGE; 107 } 108 109 /*** 110 * @return Returns the attributes. 111 */ 112 public String getAttributes() 113 { 114 return attributes; 115 } 116 117 /*** 118 * @param attributes 119 * The attributes to set. 120 */ 121 public void setAttributes(String attributes) 122 { 123 this.attributes = attributes; 124 } 125 126 /*** 127 * @return Returns the name. 128 */ 129 public String getName() 130 { 131 return name; 132 } 133 134 /*** 135 * @param name 136 * The name to set. 137 */ 138 public void setName(String name) 139 { 140 this.name = name; 141 } 142 143 /*** 144 * @return Returns the text. 145 */ 146 public String getText() 147 { 148 return text; 149 } 150 151 /*** 152 * @param text 153 * The text to set. 154 */ 155 public void setText(String text) 156 { 157 this.text = text; 158 } 159 160 /*** 161 * @return Returns the value. 162 */ 163 public String getValue() 164 { 165 return value; 166 } 167 168 /*** 169 * @param value 170 * The value to set. 171 */ 172 public void setValue(String value) 173 { 174 this.value = value; 175 } 176 177 private void reset() 178 { 179 this.attributes = null; 180 this.name = null; 181 this.text = null; 182 this.value = null; 183 } 184 185 /*** 186 * Called on a Tag handler to release state. 187 * The page compiler guarantees that JSP page implementation 188 * objects will invoke this method on all tag handlers, 189 * but there may be multiple invocations on doStartTag and doEndTag in between. 190 * 191 * @see javax.servlet.jsp.tagext.Tag#release() 192 */ 193 public void release() 194 { 195 super.release(); 196 reset(); 197 } 198 }