1 23 24 package org.dbforms.taglib; 25 26 import org.dbforms.event.WebEvent; 27 import org.dbforms.event.eventtype.EventType; 28 29 import org.dbforms.util.*; 30 31 import java.util.List ; 32 33 import javax.servlet.http.*; 34 import javax.servlet.jsp.*; 35 36 37 38 46 public class DbSelectTag extends DbBaseHandlerTag implements DataContainer, 47 javax.servlet.jsp.tagext.TryCatchFinally { 48 private List embeddedData = null; 49 private String customEntry; 50 private String ifEmptyDontDraw = "false"; 51 private String ifEmptyItem = null; 52 private String overrideReadOnly = "false"; 53 private String selectedIndex; 54 private String size; 55 56 61 public void setCustomEntry(String customEntry) { 62 this.customEntry = customEntry; 63 } 64 65 66 71 public String getCustomEntry() { 72 return customEntry; 73 } 74 75 76 84 public void setEmbeddedData(List embeddedData) { 85 this.embeddedData = embeddedData; 86 } 87 88 89 94 public String getFormattedFieldValue() { 95 String res = getFieldValue(); 96 97 if (res == null) { 98 res = ParseUtil.getParameter((HttpServletRequest) this.pageContext 99 .getRequest(), getFormFieldName()); 100 } 101 102 return res; 103 } 104 105 106 111 public void setIfEmptyDontDraw(String b) { 112 ifEmptyDontDraw = b; 113 } 114 115 116 121 public boolean hasIfEmptyDontDrawSet() { 122 return Util.getTrue(ifEmptyDontDraw); 123 } 124 125 126 131 public void setIfEmptyItem(String string) { 132 ifEmptyItem = string; 133 } 134 135 136 141 public String getIfEmptyItem() { 142 return ifEmptyItem; 143 } 144 145 146 151 public void setOverrideReadOnly(String b) { 152 overrideReadOnly = b; 153 } 154 155 156 161 public void setSelectedIndex(String selectedIndex) { 162 this.selectedIndex = selectedIndex; 163 } 164 165 166 171 public String getSelectedIndex() { 172 return selectedIndex; 173 } 174 175 176 181 public void setSize(String size) { 182 this.size = size; 183 } 184 185 186 191 public String getSize() { 192 return size; 193 } 194 195 196 199 public void doCatch(Throwable t) throws Throwable { 200 throw t; 201 } 202 203 204 212 public int doEndTag() throws javax.servlet.jsp.JspException { 213 HttpServletRequest request = (HttpServletRequest) this.pageContext 214 .getRequest(); 215 List errors = (List ) request.getAttribute("errors"); 216 WebEvent we = getParentForm() 217 .getWebEvent(); 218 219 StringBuffer tagBuf = new StringBuffer (); 220 StringBuffer selectedOptions = new StringBuffer (); 221 int embeddedDataSize = 0; 222 223 String currentValue = getFormFieldValue(); 224 225 if (embeddedData != null) { 226 String ce = null; 229 230 if (((ce = this.getCustomEntry()) != null) && (ce.trim() 231 .length() > 0)) { 232 boolean isSelected = false; 233 String aKey = org.dbforms.util.StringUtil 234 .getEmbeddedStringWithoutDots(ce, 0, ','); 235 String aValue = org.dbforms.util.StringUtil 236 .getEmbeddedStringWithoutDots(ce, 1, ','); 237 238 if ((getParentForm() 241 .hasRedisplayFieldsOnErrorSet() && (errors != null) 242 && (errors.size() > 0)) 243 || ((we != null) 244 && (we.getType().equals(EventType.EVENT_NAVIGATION_RELOAD)))) { 245 isSelected = (currentValue.equals(aKey)); 246 } else { 247 isSelected = "true".equals(org.dbforms.util.StringUtil 248 .getEmbeddedStringWithoutDots(ce, 2, 249 ',')); 250 } 251 252 if (isSelected) { 253 selectedOptions.append("-") 254 .append(aKey); 255 } 256 257 tagBuf.append(generateTagString(aKey, aValue, isSelected)); 258 } 259 260 embeddedDataSize = embeddedData.size(); 261 262 boolean drawIt = true; 264 String me = null; 265 266 if (embeddedDataSize == 0) { 267 if (hasIfEmptyDontDrawSet()) { 268 drawIt = false; 269 } else if ((me = getIfEmptyItem()) != null) { 270 drawIt = false; 271 272 String aKey = org.dbforms.util.StringUtil 273 .getEmbeddedStringWithoutDots(me, 0, ','); 274 String aValue = org.dbforms.util.StringUtil 275 .getEmbeddedStringWithoutDots(me, 1, ','); 276 277 selectedOptions.append("-") 279 .append(aKey); 280 tagBuf.append(generateTagString(aKey, aValue, true)); 281 } 282 } 283 284 if (drawIt) { 285 for (int i = 0; i < embeddedDataSize; i++) { 286 KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i); 287 String aKey = aKeyValuePair.getKey(); 288 String aValue = aKeyValuePair.getValue(); 289 290 boolean isSelected = aKey.equals(currentValue); 292 293 if (isSelected) { 294 selectedOptions.append("-") 295 .append(aKey); 296 } 297 298 tagBuf.append(generateTagString(aKey, aValue, isSelected)); 299 } 300 } 301 } 302 303 tagBuf.append("</select>"); 304 305 if (((!hasOverrideReadOnlySet()) && hasReadOnlySet()) 311 || getParentForm() 312 .hasReadOnlySet()) { 313 selectedOptions.append("-"); 314 315 String onChange = "resetSelect(this,'" + selectedOptions.toString() 316 + "');"; 317 setOnChange(onChange + ((getOnChange() != null) ? getOnChange() 318 : "")); 319 320 if (!getParentForm() 321 .existJavascriptFunction("resetSelect")) { 322 StringBuffer buf = new StringBuffer (); 323 buf.append("\nfunction resetSelect(sel,x){\n"); 324 buf.append(" for(i=0;i<sel.length;i++){\n"); 325 buf.append(" var tmp = sel.options[i].value;\n"); 326 buf.append(" if(x.indexOf(('-'+tmp+'-'))!=-1){\n"); 327 buf.append(" sel.options[i].selected=true;\n"); 328 buf.append(" }else{;\n"); 329 buf.append(" sel.options[i].selected=false;\n"); 330 buf.append(" }\n"); 331 buf.append(" }\n"); 332 buf.append("}\n"); 333 334 getParentForm() 335 .addJavascriptFunction("resetSelect", buf); 336 } 337 } 338 339 getParentForm() 341 .addChildName(getName(), getFormFieldName()); 342 343 try { 344 if ((embeddedDataSize > 0) || !hasIfEmptyDontDrawSet()) { 345 pageContext.getOut() 346 .write(generateSelectHeader()); 347 pageContext.getOut() 348 .write(tagBuf.toString()); 349 writeOutSpecialValues(); 350 } 351 } catch (java.io.IOException ioe) { 352 throw new JspException("IO Error: " + ioe.getMessage()); 353 } 354 355 return EVAL_PAGE; 356 } 357 358 359 362 public void doFinally() { 363 embeddedData = null; 364 selectedIndex = null; 365 customEntry = null; 366 size = null; 367 ifEmptyDontDraw = "false"; 368 ifEmptyItem = null; 369 overrideReadOnly = "false"; 370 super.doFinally(); 371 } 372 373 374 381 public int doStartTag() throws javax.servlet.jsp.JspException { 382 return EVAL_BODY_BUFFERED; 383 } 384 385 386 391 public boolean hasOverrideReadOnlySet() { 392 return overrideReadOnly.equalsIgnoreCase("true"); 393 } 394 395 396 401 protected String typicalDefaultValue() { 402 String val; 403 404 if (((val = this.getSelectedIndex()) != null) 406 && (val.trim() 407 .length() != 0)) { 408 return val; 409 } 410 411 return (super.typicalDefaultValue()); 413 } 414 415 416 private String generateSelectHeader() throws javax.servlet.jsp.JspException { 417 StringBuffer tagBuf = new StringBuffer (); 419 420 tagBuf.append("<select name=\""); 421 422 tagBuf.append(getFormFieldName()); 423 tagBuf.append("\""); 424 425 if (size != null) { 426 tagBuf.append(" size=\""); 427 tagBuf.append(size); 428 tagBuf.append("\""); 429 } 430 431 if (getAccessKey() != null) { 432 tagBuf.append(" accesskey=\""); 433 tagBuf.append(getAccessKey()); 434 tagBuf.append("\""); 435 } 436 437 if (getTabIndex() != null) { 438 tagBuf.append(" tabindex=\""); 439 tagBuf.append(getTabIndex()); 440 tagBuf.append("\""); 441 } 442 443 tagBuf.append(prepareStyles()); 444 tagBuf.append(prepareEventHandlers()); 445 tagBuf.append(">"); 446 447 return tagBuf.toString(); 448 } 449 450 451 private String generateTagString(String value, 452 String description, 453 boolean selected) { 454 StringBuffer tagBuf = new StringBuffer (); 455 tagBuf.append("<option value=\""); 456 tagBuf.append(escapeHTML(value)); 457 tagBuf.append("\""); 458 459 if (selected) { 460 tagBuf.append(" selected=\"selected\" "); 461 } 462 463 tagBuf.append(">"); 465 tagBuf.append(escapeHTML(description)); 466 467 tagBuf.append("</option>"); 469 470 return tagBuf.toString(); 471 } 472 } 473 | Popular Tags |