1 package fr.improve.struts.taglib.layout.collection; 2 3 import java.text.DecimalFormat ; 4 import java.text.DecimalFormatSymbols ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 import java.util.StringTokenizer ; 8 9 import javax.servlet.jsp.JspException ; 10 11 import org.apache.struts.util.ResponseUtils; 12 13 import fr.improve.struts.taglib.layout.LayoutTagSupport; 14 import fr.improve.struts.taglib.layout.collection.header.CollectionItemEvent; 15 import fr.improve.struts.taglib.layout.collection.header.MultiLevelHeader; 16 import fr.improve.struts.taglib.layout.el.Expression; 17 import fr.improve.struts.taglib.layout.event.EndLayoutEvent; 18 import fr.improve.struts.taglib.layout.event.LayoutEventListener; 19 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 20 import fr.improve.struts.taglib.layout.formatter.FormatException; 21 import fr.improve.struts.taglib.layout.sort.JavascriptSortUtil; 22 import fr.improve.struts.taglib.layout.util.LayoutUtils; 23 import fr.improve.struts.taglib.layout.util.NestedHelper; 24 import fr.improve.struts.taglib.layout.util.TagUtils; 25 26 34 public class FastCollectionItemTag extends LayoutTagSupport implements LayoutEventListener { 35 protected String name; 36 protected String property; 37 38 protected String title; 39 protected String jspTitle; 40 41 protected String arg0; 42 protected String arg1; 43 protected String tooltip; 44 45 protected String footer; 46 protected String footerArg0; 47 protected String footerArg1; 48 49 protected String jspFooter; 50 51 protected String action; 52 protected String url; 53 protected String forward; 54 protected String page; 55 protected String target; 56 protected String param; 57 protected String paramId; 58 protected String paramName; 59 protected String paramProperty; 60 protected String onclick; 61 protected boolean sortable = false; 62 63 protected String type; 64 protected String jspType; 65 66 protected String width; 67 protected String styleClass; 68 protected String style; 69 protected boolean filter = true; 70 protected CollectionTag collectionTag; 71 protected boolean skip = false; 72 73 protected SimpleItemContext context = createItemContext(); 74 75 80 protected SimpleItemContext createItemContext() { 81 context = new SimpleItemContext(); 82 return context; 83 } 84 85 86 public static final String SPAN_KEY = "fr.improve.struts.taglib.layout.collection.CollectionItemTag.SPAN_KEY"; 87 88 94 protected String mathOperation; 95 96 100 protected String mathPattern; 101 102 106 public String getMathOperation() { 107 return mathOperation; 108 } 109 110 114 public void setMathOperation(String mathOperation) { 115 this.mathOperation = mathOperation; 116 } 117 118 public int doEndLayoutTag() throws JspException { 119 if (skip) { 120 skip = false; 121 collectionTag.incrementColumn(); 122 return EVAL_PAGE; 123 } 124 125 Object lc_cell = buildContent(); 127 128 if (mathOperation!=null) { 130 double lc_mathValue = LayoutUtils.getDouble(lc_cell); 131 collectionTag.storeMathData(lc_mathValue); 132 } 133 134 String lc_hiddenValue = null; 136 if (collectionTag.sortType==BaseCollectionTag.SORT_JAVASCRIPT && sortable) { 137 lc_hiddenValue = JavascriptSortUtil.makeSortable(lc_cell); 138 } 139 140 String lc_url = buildUrl(); 142 143 boolean lc_filter = buildFilter(); 144 145 if (lc_cell != null && lc_filter && lc_cell instanceof String ) { 146 lc_cell = ResponseUtils.filter(lc_cell.toString()); 149 } 150 if (lc_cell!=null && mathPattern!=null){ 151 double lc_value = LayoutUtils.getDouble(lc_cell); 152 DecimalFormatSymbols lc_symbols = new DecimalFormatSymbols (LayoutUtils.getLocale(pageContext)); 153 lc_cell = new DecimalFormat (mathPattern, lc_symbols).format(lc_value); 154 } 155 if (lc_cell != null && type != null) { 156 158 try { 159 lc_cell = LayoutUtils.getSkin(pageContext.getSession()).getFormatter().format(lc_cell, type, pageContext); 160 } catch (FormatException fe) { 161 throw new JspException ("format " + type + " failed (" + fe.getMessage() + ")"); 162 167 } 168 } 169 170 if (lc_hiddenValue!=null) { 171 lc_cell = lc_hiddenValue + lc_cell; 172 } 173 174 if (styleClass!=null) { 175 collectionTag.setTempStyleClass(styleClass); 176 } 177 if (style!=null) { 178 collectionTag.addTempStyle(style); 179 } 180 181 int lc_size = tryToGuessBufferSize(lc_cell, lc_url); 183 StringBuffer buffer = new StringBuffer (lc_size); 184 185 context.arg0 = arg0; 187 context.arg1 = arg1; 188 context.item = lc_cell !=null ? lc_cell.toString() : ""; 189 190 context.footer = footer; 191 context.footerArg0 = footerArg0; 192 context.footerArg1 = footerArg1; 193 194 context.onclick = onclick; 195 context.property = property; 196 context.sortProperty = sortable ? (property==null ? "" : property) : null; 197 context.target = target; 198 context.title = title; 199 context.url = lc_url; 200 context.width = width; 201 context.mathOperation = mathOperation; 202 context.mathPattern = mathPattern; 203 204 collectionTag.addItem(buffer, context); 205 TagUtils.write(pageContext, buffer.toString()); 206 207 if (styleClass!=null) { 208 collectionTag.setTempStyleClass(null); 209 } 210 if (style!=null) { 211 collectionTag.removeTempStyle(); 212 } 213 215 pageContext.removeAttribute(SPAN_KEY); 216 217 return EVAL_PAGE; 218 } 219 222 private int tryToGuessBufferSize(Object in_value, String in_url) { 223 int lc_size = 16; 224 if (in_url!=null) { 225 lc_size += in_url.length() + 15; 227 } 228 229 if (onclick!=null) { 230 lc_size += onclick.length() + 10; 232 } 233 if (styleClass!=null) { 234 lc_size += styleClass.length() + 8; 235 } 236 if (style!=null) { 237 lc_size += style.length() + 8; 238 } 239 240 if (in_value!=null) { 241 lc_size += in_value.toString().length(); 242 } 243 244 return lc_size; 245 } 246 247 protected Object buildContent() throws JspException { 248 Object lc_cell = null; 249 if (name == null) { 250 lc_cell = collectionTag.getBean(); 252 } else { 253 lc_cell = pageContext.findAttribute(name); 255 } 256 if (lc_cell != null && property != null) { 257 lc_cell = getPropertyValue(lc_cell, property); 259 } 260 return lc_cell; 261 } 262 263 protected String buildUrl() throws JspException { 264 String lc_url = null; 265 Object lc_cell = null; 266 if (name == null) { 267 lc_cell = collectionTag.getBean(); 269 } else { 270 lc_cell = pageContext.findAttribute(name); 272 } 273 if (lc_cell!=null && (url != null || page != null || forward!=null || action!=null)) { 274 lc_url = getLink(url, lc_cell, param, paramId, paramName, paramProperty); 276 } 277 return lc_url; 278 } 279 280 protected boolean buildFilter() { 281 return filter; 282 } 283 284 public int doStartLayoutTag() throws JspException { 285 context.reset(); 286 try { 287 collectionTag = 288 (CollectionTag) findAncestorWithClass(this, CollectionTag.class); 289 if (collectionTag.isFirst()) { 290 MultiLevelHeader lc_header = new MultiLevelHeader(title, arg0, arg1, styleClass, true); 291 lc_header.setTooltip(tooltip); 292 lc_header.setWidth(width); 293 lc_header.setSortProperty(sortable ? property : null); 294 new CollectionItemEvent(this, lc_header).send(); 295 return SKIP_BODY; 296 } 297 if (name!=null) { 298 if (!collectionTag.getSpans().containsKey(name)) { 299 skip = true; 300 return SKIP_BODY; 301 } else { 302 pageContext.setAttribute(SPAN_KEY, collectionTag.getSpans().get(name)); 303 } 304 } 305 306 } catch (ClassCastException e) { 307 throw new JspException ("Invalid use of collectionItem tag"); 308 } catch (NullPointerException e) { 309 throw new JspException ("Invalid use of collectionItem tag"); 310 } 311 return EVAL_BODY_INCLUDE; 312 } 313 314 326 protected String getLink(String in_url, Object in_bean, String in_oldParam, String in_paramId, String in_paramName, String in_paramProperty) throws JspException { 327 if (in_oldParam!=null) { 328 return url + LayoutUtils.getProperty(in_bean, param); 330 } else { 331 Map lc_params = new HashMap (); 333 if(in_paramId!= null) { 334 StringTokenizer lc_paramIdTokens = new StringTokenizer (in_paramId, ","); 335 StringTokenizer lc_paramPropertyTokens = null; 336 if (in_paramProperty!=null) { 337 lc_paramPropertyTokens = new StringTokenizer (in_paramProperty, ","); 338 } 339 StringTokenizer lc_paramNameTokens = null; 340 if (in_paramName!=null) { 341 lc_paramNameTokens = new StringTokenizer (in_paramName, ","); 342 } 343 if (lc_paramPropertyTokens!=null && lc_paramPropertyTokens.countTokens()!=lc_paramIdTokens.countTokens()) { 344 throw new JspException ("paramId (" + paramId + ") and paramProperty (" + paramProperty + ") don't specify the same number of parameters"); 345 } 346 if (lc_paramNameTokens!=null && lc_paramNameTokens.countTokens()!=lc_paramIdTokens.countTokens()) { 347 throw new JspException ("paramId (" + paramId + ") and paramName (" + paramName + ") don't specify the same number of parameters"); 348 } 349 350 while (lc_paramIdTokens.hasMoreTokens()) { 351 String lc_paramId = lc_paramIdTokens.nextToken(); 352 Object lc_bean = in_bean; 353 if (lc_paramNameTokens!=null) { 354 lc_bean = pageContext.findAttribute(lc_paramNameTokens.nextToken()); 355 } 356 String lc_paramProperty = null; 357 if (lc_paramPropertyTokens!=null) { 358 lc_paramProperty = lc_paramPropertyTokens.nextToken(); 359 } 360 lc_params.put(NestedHelper.getAdjustedProperty(lc_paramId, pageContext), LayoutUtils.getProperty(lc_bean, lc_paramProperty)); 361 } 362 } 363 String lc_url = LayoutUtils.computeURL(pageContext, forward, url, page, action, null, lc_params, null, false, target); 364 365 return lc_url; 366 382 } 383 } 384 385 389 protected Object getPropertyValue(Object in_bean, String in_property) throws JspException { 390 return LayoutUtils.getProperty(in_bean, in_property); 391 } 392 393 394 398 public java.lang.String getOnclick() { 399 return onclick; 400 } 401 405 public java.lang.String getParam() { 406 return param; 407 } 408 413 public java.lang.String getType() { 414 return type; 415 } 416 420 public java.lang.String getUrl() { 421 return url; 422 } 423 public void release() { 424 super.release(); 425 property = null; 426 427 title = null; 428 arg0 = null; 429 arg1 = null; 430 431 tooltip = null; 432 433 footer = null; 434 footerArg0 = null; 435 footerArg1= null; 436 437 name = null; 438 action = null; 439 url = null; 440 forward = null; 441 page = null; 442 param = null; 443 paramId = null; 444 paramName = null; 445 paramProperty = null; 446 onclick = null; 447 sortable = false; 448 type = null; 449 target = null; 450 width = null; 451 styleClass = null; 452 style = null; 453 filter = true; 454 skip = false; 455 mathOperation = null; 456 } 457 protected void reset() { 458 collectionTag = null; 459 460 type = jspType; 461 jspType = null; 462 463 footer = jspFooter; 464 jspFooter = null; 465 466 title = jspTitle; 467 jspTitle = null; 468 } 469 public void setName(String name) { 470 this.name = name; 471 } 472 public String getName() { 473 return name; 474 } 475 479 public void setOnclick(java.lang.String newOnclick) { 480 onclick = newOnclick; 481 } 482 486 public void setParam(java.lang.String newParam) { 487 param = newParam; 488 } 489 494 public void setProperty(java.lang.String newProperty) { 495 property = newProperty; 496 } 497 public String getProperty() { 498 return property; 499 } 500 public void setSortable(String sortable) { 501 if ("true".equalsIgnoreCase(sortable)) { 502 this.sortable = true; 503 } else { 504 this.sortable = false; 505 } 506 } 507 public String getSortable() { 508 return String.valueOf(sortable); 509 } 510 515 public void setTitle(java.lang.String newHeader) { 516 title = newHeader; 517 } 518 public String getTitle() { 519 return title; 520 } 521 526 public void setType(java.lang.String newType) { 527 type = newType; 528 } 529 533 public void setUrl(java.lang.String newUrl) { 534 url = newUrl; 535 } 536 public void setHref(String in_href) { 537 url = in_href; 538 } 539 public void setPage(String in_page) { 540 page = in_page; 541 } 542 public void setForward(String in_forward) { 543 forward = in_forward; 544 } 545 549 public String getTarget() { 550 return target; 551 } 552 553 557 public void setTarget(String target) { 558 this.target = target; 559 } 560 561 565 public String getWidth() { 566 return width; 567 } 568 569 573 public void setWidth(String width) { 574 this.width = width; 575 } 576 577 581 public String getParamId() { 582 return paramId; 583 } 584 585 589 public void setParamId(String paramId) { 590 this.paramId = paramId; 591 } 592 593 597 public String getParamProperty() { 598 return paramProperty; 599 } 600 601 605 public void setParamProperty(String paramProperty) { 606 this.paramProperty = paramProperty; 607 } 608 609 public Object processStartLayoutEvent(StartLayoutEvent in_event) { 610 return Boolean.FALSE; 611 } 612 public Object processEndLayoutEvent(EndLayoutEvent in_event) { 613 return Boolean.FALSE; 614 615 } 616 620 public void setStyleClass(String styleClass) { 621 this.styleClass = styleClass; 622 } 623 624 628 public void setStyle(String style) { 629 this.style = style; 630 } 631 632 636 public void setFilter(boolean filter) { 637 this.filter = filter; 638 } 639 640 644 public void setParamName(String paramName) { 645 this.paramName = paramName; 646 } 647 648 651 public String getArg0() { 652 return arg0; 653 } 654 657 public void setArg0(String arg0) { 658 this.arg0 = arg0; 659 } 660 663 public String getArg1() { 664 return arg1; 665 } 666 669 public void setArg1(String arg1) { 670 this.arg1 = arg1; 671 } 672 public String getAction() { 673 return action; 674 } 675 public void setAction(String action) { 676 this.action = action; 677 } 678 protected void initDynamicValues() { 679 super.initDynamicValues(); 680 681 jspType = type; 682 type = Expression.evaluate(type, pageContext); 683 684 jspFooter = footer; 685 footer = Expression.evaluate(footer, pageContext); 686 687 jspTitle = title; 688 title = Expression.evaluate(title, pageContext); 689 } 690 public void setFooter(String footer) { 691 this.footer = footer; 692 } 693 public void setFooterArg0(String arg0) { 694 footerArg0 = arg0; 695 } 696 public void setFooterArg1(String arg1){ 697 footerArg1 = arg1; 698 } 699 702 public String getMathPattern() { 703 return mathPattern; 704 } 705 708 public void setMathPattern(String mathPattern) { 709 this.mathPattern = mathPattern; 710 } 711 714 public void setTooltip(String tooltip) { 715 this.tooltip = tooltip; 716 } 717 } | Popular Tags |