1 package com.blandware.atleap.webapp.taglib.core.logic; 2 3 import com.blandware.atleap.common.util.ConvertUtil; 4 5 import javax.servlet.jsp.JspException ; 6 import javax.servlet.jsp.PageContext ; 7 import javax.servlet.jsp.tagext.JspFragment ; 8 import javax.servlet.jsp.tagext.SimpleTagSupport ; 9 import java.io.IOException ; 10 import java.io.Serializable ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import org.apache.struts.taglib.TagUtils; 15 16 97 public class IteratorTag extends SimpleTagSupport { 98 99 102 protected Object items; 103 104 107 protected List itemsList; 108 109 112 protected Integer begin; 113 114 117 protected Integer end; 118 119 122 protected Integer step; 123 124 127 protected String var; 128 129 132 protected String scope = "page"; 133 134 137 protected String statusVar; 138 139 142 protected String statusScope = "page"; 143 144 147 protected IterationStatus status; 148 149 152 public IteratorTag() { 153 } 154 155 165 public Object getItems() { 166 return items; 167 } 168 169 175 public void setItems(Object items) { 176 this.items = items; 177 } 178 179 189 public Integer getBegin() { 190 return begin; 191 } 192 193 199 public void setBegin(Integer begin) { 200 this.begin = begin; 201 } 202 203 213 public Integer getEnd() { 214 return end; 215 } 216 217 223 public void setEnd(Integer end) { 224 this.end = end; 225 } 226 227 237 public Integer getStep() { 238 return step; 239 } 240 241 247 public void setStep(Integer step) { 248 this.step = step; 249 } 250 251 261 public String getVar() { 262 return var; 263 } 264 265 271 public void setVar(String var) { 272 this.var = var; 273 } 274 275 285 public String getScope() { 286 return scope; 287 } 288 289 295 public void setScope(String scope) { 296 this.scope = scope; 297 } 298 299 310 public String getStatusVar() { 311 return statusVar; 312 } 313 314 321 public void setStatusVar(String statusVar) { 322 this.statusVar = statusVar; 323 } 324 325 335 public String getStatusScope() { 336 return statusScope; 337 } 338 339 345 public void setStatusScope(String statusScope) { 346 this.statusScope = statusScope; 347 } 348 349 354 public IterationStatus getStatus() { 355 return status; 356 } 357 358 364 public void doTag() throws JspException , IOException { 365 366 PageContext pageContext = (PageContext ) getJspContext(); 367 368 itemsList = convertItemsToList(); 370 371 int begin = this.begin != null ? this.begin.intValue() : 0; 373 int end = this.end != null ? this.end.intValue() : itemsList.size() - 1; 374 int step = this.step != null ? this.step.intValue() : 1; 375 376 if (step <= 0) { 377 throw new JspException ("Step must be positive"); 378 } 379 380 JspFragment body = getJspBody(); 382 if ( body == null ) { 383 return; 384 } 385 386 status = new IterationStatus(this.begin, this.end, this.step); 388 389 391 int count = 1; 393 for ( int i = begin; i <= end; i += step, count++ ) { 394 Object current = (Object ) itemsList.get(i); 395 396 status.setCurrent(current); 398 399 status.setCount(count); 401 402 status.setIndex(i); 404 405 TagUtils tagUtils = TagUtils.getInstance(); 407 if ( var != null ) { 408 int varScope = PageContext.PAGE_SCOPE; 409 if ( scope != null ) { 410 varScope = tagUtils.getScope(scope); 411 } 412 pageContext.setAttribute(var, current, varScope); 413 } 414 415 if ( statusVar != null ) { 417 int statusVarScope = PageContext.PAGE_SCOPE; 418 if ( statusScope != null ) { 419 statusVarScope = tagUtils.getScope(statusScope); 420 } 421 pageContext.setAttribute(statusVar, status, statusVarScope); 422 } 423 424 body.invoke(null); 426 } 427 } 428 429 432 protected List convertItemsToList() { 433 List list = new ArrayList (); 434 if ( items == null ) { 435 int begin = this.begin != null ? this.begin.intValue() : 0; 437 int end = this.end != null ? this.end.intValue() : 0; 438 for ( int i = begin; i < end; i++ ) { 439 list.add(new Integer (i)); 440 } 441 } else { 442 list = ConvertUtil.convertCollectionToList(items); 443 } 444 return list; 445 } 446 447 461 public class IterationStatus implements Serializable { 462 463 466 protected Integer begin; 467 468 471 protected Integer end; 472 473 476 protected Integer step; 477 478 481 protected int index; 482 483 489 protected int count; 490 491 494 protected Object current; 495 496 502 public IterationStatus(Integer begin, Integer end, Integer step) { 503 this.begin = begin; 504 this.end = end; 505 this.step = step; 506 } 507 508 517 public int getIndex() { 518 return index; 519 } 520 521 525 void setIndex(int index) { 526 this.index = index; 527 } 528 529 540 public int getCount() { 541 return count; 542 } 543 544 548 void setCount(int count) { 549 this.count = count; 550 } 551 552 560 public Object getCurrent() { 561 return current; 562 } 563 564 565 569 void setCurrent(Object current) { 570 this.current = current; 571 } 572 573 580 public Integer getBegin() { 581 return begin; 582 } 583 584 591 public Integer getEnd() { 592 return end; 593 } 594 595 602 public Integer getStep() { 603 return step; 604 } 605 606 617 public boolean isFirst() { 618 int begin = this.begin != null ? this.begin.intValue() : 0; 619 return index == begin; 620 } 621 622 633 public boolean isLast() { 634 int end = this.end != null ? this.end.intValue() : itemsList.size() - 1; 635 int step = this.step != null ? this.step.intValue() : 1; 636 return index + step > end; 637 } 638 639 } 640 641 } 642 | Popular Tags |