| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SelectTag |
|
| 2.625;2.625 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | package net.sourceforge.pebble.web.tagext; | |
| 33 | ||
| 34 | import javax.servlet.jsp.JspException; | |
| 35 | import javax.servlet.jsp.JspTagException; | |
| 36 | import javax.servlet.jsp.JspWriter; | |
| 37 | import javax.servlet.jsp.tagext.TagSupport; | |
| 38 | import java.lang.reflect.Method; | |
| 39 | import java.util.Arrays; | |
| 40 | import java.util.Collection; | |
| 41 | import java.util.Iterator; | |
| 42 | ||
| 43 | /** | |
| 44 | * Given a Collection or array, this tag produces a HTML select (dropdown) list | |
| 45 | * based upon the items contained within. | |
| 46 | * | |
| 47 | * @author Simon Brown | |
| 48 | */ | |
| 49 | 0 | public class SelectTag extends TagSupport { |
| 50 | ||
| 51 | /** the items over which this tag should iterate */ | |
| 52 | private Collection items; | |
| 53 | ||
| 54 | /** the name of the select control */ | |
| 55 | private String name; | |
| 56 | ||
| 57 | /** the size of the select control */ | |
| 58 | 0 | private int size = -1; |
| 59 | ||
| 60 | /** the multiple attribute */ | |
| 61 | 0 | private boolean multiple = false; |
| 62 | ||
| 63 | /** the name of the property to be used as the displayed label */ | |
| 64 | private String label; | |
| 65 | ||
| 66 | /** the name of the property to be used as the hidden value */ | |
| 67 | private String value; | |
| 68 | ||
| 69 | /** the selected value */ | |
| 70 | private Object selected; | |
| 71 | ||
| 72 | /** | |
| 73 | * Called when the starting tag is encountered. | |
| 74 | */ | |
| 75 | public int doStartTag() throws JspException { | |
| 76 | // setup the iterator to be used | |
| 77 | 0 | Iterator iterator = items.iterator(); |
| 78 | ||
| 79 | try { | |
| 80 | 0 | JspWriter out = pageContext.getOut(); |
| 81 | ||
| 82 | // write the starting tag of the select control | |
| 83 | 0 | out.print("<select name=\""); |
| 84 | 0 | out.print(name); |
| 85 | 0 | out.print("\""); |
| 86 | ||
| 87 | 0 | if (size > 0) { |
| 88 | 0 | out.print(" size=\""); |
| 89 | 0 | out.print(size); |
| 90 | 0 | out.print("\""); |
| 91 | } | |
| 92 | ||
| 93 | 0 | if (multiple) { |
| 94 | 0 | out.print(" multiple=\"true\""); |
| 95 | } | |
| 96 | ||
| 97 | 0 | out.print(">"); |
| 98 | ||
| 99 | 0 | while (iterator.hasNext()) { |
| 100 | // get the next JavaBean from the items | |
| 101 | 0 | Object o = iterator.next(); |
| 102 | ||
| 103 | // and the property used to represent the hidden value | |
| 104 | String hiddenValue; | |
| 105 | 0 | if (value != null) { |
| 106 | 0 | Method m = o.getClass().getMethod("get" + value.substring(0, 1).toUpperCase() + value.substring(1), new Class[] {}); |
| 107 | 0 | hiddenValue = m.invoke(o, new Object[]{}).toString(); |
| 108 | 0 | } else { |
| 109 | 0 | hiddenValue = o.toString(); |
| 110 | } | |
| 111 | ||
| 112 | // and now generate the HTML | |
| 113 | 0 | out.print("<option value=\""); |
| 114 | ||
| 115 | // call the accessor method for the value property | |
| 116 | // (this is the same as calling get<PropertyName>() on | |
| 117 | // the JavaBean instance) | |
| 118 | 0 | out.print(hiddenValue); |
| 119 | 0 | out.print("\""); |
| 120 | ||
| 121 | 0 | if (selected != null) { |
| 122 | 0 | if (selected instanceof Collection) { |
| 123 | 0 | Collection coll = (Collection)selected; |
| 124 | 0 | if (coll.contains(hiddenValue)) { |
| 125 | 0 | out.print(" selected=\"true\""); |
| 126 | } | |
| 127 | 0 | } else if (selected.toString().equals(hiddenValue)) { |
| 128 | 0 | out.print(" selected=\"true\""); |
| 129 | } | |
| 130 | } | |
| 131 | 0 | out.print(">"); |
| 132 | ||
| 133 | 0 | if (label != null) { |
| 134 | // and do the same for the label property | |
| 135 | // and use it to create a description of the property used | |
| 136 | // to represent the displayable label | |
| 137 | 0 | Method m = o.getClass().getMethod("get" + label.substring(0, 1).toUpperCase() + label.substring(1), new Class[] {}); |
| 138 | 0 | out.print( |
| 139 | m.invoke(o, new Object[]{})); | |
| 140 | 0 | } else { |
| 141 | 0 | out.print(o.toString()); |
| 142 | } | |
| 143 | 0 | out.print("</option>"); |
| 144 | 0 | } |
| 145 | ||
| 146 | // write the ending tag of the select control | |
| 147 | 0 | out.print("</select>"); |
| 148 | 0 | } catch (Exception e) { |
| 149 | 0 | e.printStackTrace(); |
| 150 | 0 | throw new JspTagException(e.getMessage()); |
| 151 | 0 | } |
| 152 | ||
| 153 | // and skip the body | |
| 154 | 0 | return SKIP_BODY; |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Sets the items over which this tag should iterate. | |
| 159 | * | |
| 160 | * @param items a Collection or array | |
| 161 | */ | |
| 162 | public void setItems(Object items) { | |
| 163 | 0 | if (items instanceof Collection) { |
| 164 | 0 | this.items = (Collection)items; |
| 165 | 0 | } else if (items instanceof Object[]) { |
| 166 | 0 | this.items = Arrays.asList((Object[])items); |
| 167 | } | |
| 168 | 0 | } |
| 169 | ||
| 170 | /** | |
| 171 | * Sets the name for the generated select control. | |
| 172 | * | |
| 173 | * @param name the name as a String | |
| 174 | */ | |
| 175 | public void setName(String name) { | |
| 176 | 0 | this.name = name; |
| 177 | 0 | } |
| 178 | ||
| 179 | /** | |
| 180 | * Sets the size of the generated select control. | |
| 181 | * | |
| 182 | * @param size the size | |
| 183 | */ | |
| 184 | public void setSize(int size) { | |
| 185 | 0 | this.size = size; |
| 186 | 0 | } |
| 187 | ||
| 188 | /** | |
| 189 | * Sets the multiple attribute on the underlying select control. | |
| 190 | * | |
| 191 | * @param multiple a boolean | |
| 192 | */ | |
| 193 | public void setMultiple(boolean multiple) { | |
| 194 | 0 | this.multiple = multiple; |
| 195 | 0 | } |
| 196 | ||
| 197 | /** | |
| 198 | * Sets the name of the property to display. | |
| 199 | * | |
| 200 | * @param label the name of the label property | |
| 201 | */ | |
| 202 | public void setLabel(String label) { | |
| 203 | 0 | this.label = label; |
| 204 | 0 | } |
| 205 | ||
| 206 | /** | |
| 207 | * Sets the name of the property to use as the hidden value. | |
| 208 | * | |
| 209 | * @param value the name of the value property | |
| 210 | */ | |
| 211 | public void setValue(String value) { | |
| 212 | 0 | this.value = value; |
| 213 | 0 | } |
| 214 | ||
| 215 | /** | |
| 216 | * Sets the selected value. | |
| 217 | * | |
| 218 | * @param selected the selected value | |
| 219 | */ | |
| 220 | public void setSelected(Object selected) { | |
| 221 | 0 | this.selected = selected; |
| 222 | 0 | } |
| 223 | ||
| 224 | } |