1 23 24 package org.dbforms.taglib; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.util.*; 30 31 import java.util.List ; 32 33 import javax.servlet.jsp.*; 34 35 36 37 44 public class DbRadioTag extends DbBaseHandlerTag implements DataContainer, 45 javax.servlet.jsp.tagext.TryCatchFinally { 46 private static Log logCat = LogFactory.getLog(DbRadioTag.class.getName()); private List embeddedData = null; 48 private String growDirection; private String growSize = "0"; 51 59 public void setEmbeddedData(List embeddedData) { 60 this.embeddedData = embeddedData; 61 } 62 63 64 69 public void setGrowDirection(String growDirection) { 70 this.growDirection = growDirection; 71 } 72 73 74 79 public String getGrowDirection() { 80 return growDirection; 81 } 82 83 84 89 public void setGrowSize(String growSize) { 90 this.growSize = growSize; 91 } 92 93 94 97 public void doCatch(Throwable t) throws Throwable { 98 throw t; 99 } 100 101 102 110 public int doEndTag() throws javax.servlet.jsp.JspException { 111 StringBuffer tagBuf = new StringBuffer (); 112 113 String currentValue = getFormFieldValue(); 115 116 if (Util.isNull(currentValue)) { 117 currentValue = getDefaultValue(); 118 } 119 120 if (embeddedData != null) { 121 int embeddedDataSize = embeddedData.size(); 122 123 if (hasReadOnlySet() || getParentForm() 127 .hasReadOnlySet()) { 128 for (int i = 0; i < embeddedDataSize; i++) { 130 KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i); 131 String aKey = aKeyValuePair.getKey(); 132 133 if (aKey.equals(currentValue)) { 134 String onclick = (getOnClick() != null) ? getOnClick() 135 : ""; 136 137 if (onclick.lastIndexOf(";") != (onclick.length() - 1)) { 138 onclick += ";"; } 140 141 setOnClick("document.dbform['" + getFormFieldName() + "'][" 142 + i + "].checked=true;" + onclick); 143 144 break; 145 } 146 } 147 } 148 149 int maxSize = growSize(); 150 151 tagBuf.append("<table BORDER=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">"); 152 153 for (int i = 0; i < embeddedDataSize; i++) { 154 KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i); 155 String aKey = aKeyValuePair.getKey(); 156 String aValue = aKeyValuePair.getValue(); 157 158 boolean isSelected = aKey.equals(currentValue); 160 161 if ("horizontal".equals(getGrowDirection()) 162 && (maxSize != 0) 163 && ((i % maxSize) == 0) 164 && (i != 0)) { 165 tagBuf.append("</tr><tr valign=\"top\">"); 166 } 167 168 if ("vertical".equals(getGrowDirection()) && (i != 0)) { 169 tagBuf.append("</tr><tr valign=\"top\">"); 170 } 171 172 tagBuf.append("<td "); 173 tagBuf.append(prepareStyles()); 174 tagBuf.append(">") 175 .append(generateTagString(aKey, aValue, isSelected)) 176 .append("</td>"); 177 } 178 179 tagBuf.append("</tr></table>"); 180 } 181 182 getParentForm() 184 .addChildName(getName(), getFormFieldName()); 185 186 try { 187 pageContext.getOut() 188 .write(tagBuf.toString()); 189 190 writeOutSpecialValues(); 192 } catch (java.io.IOException ioe) { 193 throw new JspException("IO Error: " + ioe.getMessage()); 194 } 195 196 return EVAL_PAGE; 197 } 198 199 200 203 public void doFinally() { 204 embeddedData = null; 205 growDirection = null; 206 growSize = "0"; 207 super.doFinally(); 208 } 209 210 211 218 public int doStartTag() throws javax.servlet.jsp.JspException { 219 return EVAL_BODY_BUFFERED; 220 } 221 222 223 private String generateTagString(String value, 224 String description, 225 boolean selected) { 226 StringBuffer tagBuf = new StringBuffer (); 227 tagBuf.append("<input type=\"radio\" name=\""); 228 tagBuf.append(getFormFieldName()); 229 tagBuf.append("\" value=\""); 230 tagBuf.append(value); 231 tagBuf.append("\" "); 232 233 if (selected) { 234 tagBuf.append(" checked=\"checked\" "); 235 } 236 237 if (getAccessKey() != null) { 238 tagBuf.append(" accesskey=\""); 239 tagBuf.append(getAccessKey()); 240 tagBuf.append("\""); 241 } 242 243 if (getTabIndex() != null) { 244 tagBuf.append(" tabindex=\""); 245 tagBuf.append(getTabIndex()); 246 tagBuf.append("\""); 247 } 248 249 tagBuf.append(prepareStyles()); 250 tagBuf.append(prepareEventHandlers()); 251 tagBuf.append(">\n"); 252 tagBuf.append(description); 253 tagBuf.append("</input>"); 254 255 return tagBuf.toString(); 256 } 257 258 259 264 private int growSize() { 265 int res = 0; 266 267 try { 268 res = Integer.parseInt(growSize); 269 } catch (NumberFormatException nfe) { 270 logCat.warn(" setGrowSize(" + growSize + ") NumberFormatException : " 271 + nfe.getMessage()); 272 } 273 274 return res; 275 } 276 } 277 | Popular Tags |