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.event.WebEvent; 30 import org.dbforms.event.eventtype.EventType; 31 32 import org.dbforms.util.*; 33 34 import java.util.*; 35 36 import javax.servlet.jsp.*; 37 38 39 40 47 public class DbCheckboxTag extends DbBaseHandlerTag implements DataContainer, 48 javax.servlet.jsp.tagext.TryCatchFinally { 49 private static Log logCat = LogFactory.getLog(DbCheckboxTag.class); private List embeddedData = null; 51 private String checked; private String growDirection; private String growSize = "0"; private String noValue; 55 private String value; 56 private String force = "false"; 57 58 63 public void setChecked(String checked) { 64 this.checked = checked; 65 } 66 67 68 76 public void setEmbeddedData(List embeddedData) { 77 this.embeddedData = embeddedData; 78 } 79 80 81 86 public void setForce(String b) { 87 force = b; 88 } 89 90 91 96 public String getForce() { 97 return force; 98 } 99 100 101 106 public void setGrowDirection(String growDirection) { 107 this.growDirection = growDirection; 108 } 109 110 111 116 public String getGrowDirection() { 117 return growDirection; 118 } 119 120 121 126 public void setGrowSize(String growSize) { 127 try { 128 int grow = Integer.parseInt(growSize); 129 130 if (grow > 0) { 131 this.growSize = growSize; 132 } else { 133 this.growSize = "0"; 134 } 135 } catch (NumberFormatException nfe) { 136 logCat.warn(" setGrowSize(" + growSize + ") NumberFormatException : " 137 + nfe.getMessage()); 138 this.growSize = "0"; 139 } 140 } 141 142 143 148 public String getGrowSize() { 149 return growSize; 150 } 151 152 153 158 public void setNovalue(String noValue) { 159 this.noValue = noValue; 160 } 161 162 163 168 public String getNovalue() { 169 return noValue; 170 } 171 172 173 178 public void setValue(String string) { 179 value = string; 180 } 181 182 183 188 public String getValue() { 189 return value; 190 } 191 192 193 196 public void doCatch(Throwable t) throws Throwable { 197 throw t; 198 } 199 200 201 209 public int doEndTag() throws javax.servlet.jsp.JspException { 210 StringBuffer tagBuf = new StringBuffer (); 211 WebEvent we = getParentForm() 212 .getWebEvent(); 213 214 String currentValue = getFormFieldValue(); 216 217 String onclick = (getOnClick() != null) ? getOnClick() 220 : ""; 221 222 if (onclick.lastIndexOf(";") != (onclick.length() - 1)) { 223 onclick += ";"; } 225 226 getParentForm() 228 .addChildName(getName(), getFormFieldName()); 229 230 if (embeddedData == null) { 232 boolean isSelected = ((!getParentForm() 234 .isFooterReached() 235 || ((we != null) 236 && we.getType() 237 .equals(EventType.EVENT_NAVIGATION_RELOAD))) 238 && (getValue() != null) 239 && getValue() 240 .equals(currentValue)) 241 || (getParentForm() 242 .isFooterReached() && hasCheckedSet()); 243 244 if (Util.getTrue(getForce())) { 246 isSelected = hasCheckedSet(); 247 } 248 249 if (hasReadOnlySet() || getParentForm() 250 .hasReadOnlySet()) { 251 setOnClick("this.checked=" + isSelected + ";" + onclick); 252 } 253 254 tagBuf.append(generateTagString(getValue(), "", isSelected)); 255 } else { 256 int embeddedDataSize = embeddedData.size(); 257 int maxSize = Integer.parseInt(getGrowSize()); 258 259 tagBuf.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">"); 260 261 for (int i = 0; i < embeddedDataSize; i++) { 262 KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i); 263 String aKey = aKeyValuePair.getKey(); 264 String aValue = aKeyValuePair.getValue(); 265 266 boolean isSelected = aKey.equals(currentValue); 268 269 if (hasReadOnlySet() || getParentForm() 270 .hasReadOnlySet()) { 271 setOnClick("this.checked=" + isSelected + ";" + onclick); 272 } 273 274 if ("horizontal".equals(getGrowDirection()) 275 && (maxSize != 0) 276 && ((i % maxSize) == 0) 277 && (i != 0)) { 278 tagBuf.append("</tr><tr valign=\"top\">"); 279 } 280 281 if ("vertical".equals(getGrowDirection()) && (i != 0)) { 282 tagBuf.append("</tr><tr valign=\"top\">"); 283 } 284 285 tagBuf.append("<td>") 286 .append(generateTagString(aKey, aValue, isSelected)) 287 .append(" </td>"); 288 } 289 290 tagBuf.append("</tr></table>"); 291 } 292 293 if (!Util.isNull(getNovalue())) { 294 tagBuf.append("<input type=\"hidden\" name=\""); 298 tagBuf.append(getFormFieldName()); 299 tagBuf.append("\" value =\""); 300 tagBuf.append(getNovalue()); 301 tagBuf.append("\" "); 302 tagBuf.append("/>"); 303 } 304 305 try { 306 pageContext.getOut() 307 .write(tagBuf.toString()); 308 309 writeOutSpecialValues(); 311 } catch (java.io.IOException ioe) { 312 throw new JspException("IO Error: " + ioe.getMessage()); 313 } 314 315 return EVAL_PAGE; 316 } 317 318 319 322 public void doFinally() { 323 embeddedData = null; 324 checked = null; 325 growDirection = null; 326 growSize = "0"; 327 noValue = null; 328 value = null; 329 force = "false"; 330 super.doFinally(); 331 } 332 333 334 341 public int doStartTag() throws javax.servlet.jsp.JspException { 342 return EVAL_BODY_BUFFERED; 343 } 344 345 346 351 public boolean hasCheckedSet() { 352 return Util.getTrue(checked); 353 } 354 355 356 private String generateTagString(String avalue, 357 String description, 358 boolean selected) { 359 StringBuffer tagBuf = new StringBuffer (); 360 361 tagBuf.append("<input type=\"checkbox\" name=\""); 362 tagBuf.append(getFormFieldName()); 363 tagBuf.append("\" value =\""); 364 tagBuf.append(avalue); 365 tagBuf.append("\" "); 366 367 if (selected) { 368 tagBuf.append(" checked=\"checked\" "); 369 } 370 371 if (getAccessKey() != null) { 372 tagBuf.append(" accesskey=\""); 373 tagBuf.append(getAccessKey()); 374 tagBuf.append("\""); 375 } 376 377 if (getTabIndex() != null) { 378 tagBuf.append(" tabindex=\""); 379 tagBuf.append(getTabIndex()); 380 tagBuf.append("\""); 381 } 382 383 tagBuf.append(prepareStyles()); 384 tagBuf.append(prepareEventHandlers()); 385 tagBuf.append(">"); 386 tagBuf.append(description); 387 tagBuf.append("</input>"); 388 389 return tagBuf.toString(); 390 } 391 } 392 | Popular Tags |