1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.io.IOException ; 31 import java.text.DecimalFormat ; 32 import java.text.Format ; 33 import java.text.SimpleDateFormat ; 34 35 import net.sf.jasperreports.engine.JRAbstractObjectFactory; 36 import net.sf.jasperreports.engine.JRChild; 37 import net.sf.jasperreports.engine.JRElement; 38 import net.sf.jasperreports.engine.JRException; 39 import net.sf.jasperreports.engine.JRExpression; 40 import net.sf.jasperreports.engine.JRExpressionCollector; 41 import net.sf.jasperreports.engine.JRGroup; 42 import net.sf.jasperreports.engine.JRHyperlinkParameter; 43 import net.sf.jasperreports.engine.JRPrintElement; 44 import net.sf.jasperreports.engine.JRPrintHyperlinkParameters; 45 import net.sf.jasperreports.engine.JRPrintText; 46 import net.sf.jasperreports.engine.JRStyle; 47 import net.sf.jasperreports.engine.JRTextField; 48 import net.sf.jasperreports.engine.util.JRDataUtils; 49 import net.sf.jasperreports.engine.util.JRStyleResolver; 50 import net.sf.jasperreports.engine.xml.JRXmlWriter; 51 52 53 57 public class JRFillTextField extends JRFillTextElement implements JRTextField 58 { 59 60 61 64 private JRGroup evaluationGroup = null; 65 66 69 private String anchorName = null; 70 private String hyperlinkReference = null; 71 private String hyperlinkAnchor = null; 72 private Integer hyperlinkPage = null; 73 private String hyperlinkTooltip; 74 private JRPrintHyperlinkParameters hyperlinkParameters; 75 76 79 protected JRFillTextField( 80 JRBaseFiller filler, 81 JRTextField textField, 82 JRFillObjectFactory factory 83 ) 84 { 85 super(filler, textField, factory); 86 87 evaluationGroup = factory.getGroup(textField.getEvaluationGroup()); 88 } 89 90 91 protected JRFillTextField(JRFillTextField textField, JRFillCloneFactory factory) 92 { 93 super(textField, factory); 94 95 this.evaluationGroup = textField.evaluationGroup; 96 } 97 98 99 102 public boolean isStretchWithOverflow() 103 { 104 return ((JRTextField)parent).isStretchWithOverflow(); 105 } 106 107 110 public void setStretchWithOverflow(boolean isStretchWithOverflow) 111 { 112 } 113 114 117 public byte getEvaluationTime() 118 { 119 return ((JRTextField)parent).getEvaluationTime(); 120 } 121 122 125 public String getPattern() 126 { 127 return JRStyleResolver.getPattern(this); 128 } 129 130 public String getOwnPattern() 131 { 132 return ((JRTextField)parent).getOwnPattern(); 133 } 134 135 138 public void setPattern(String pattern) 139 { 140 } 141 142 145 public boolean isBlankWhenNull() 146 { 147 return JRStyleResolver.isBlankWhenNull(this); 148 } 149 150 153 public Boolean isOwnBlankWhenNull() 154 { 155 return ((JRTextField)parent).isOwnBlankWhenNull(); 156 } 157 158 161 public void setBlankWhenNull(boolean isBlank) 162 { 163 } 164 165 168 public void setBlankWhenNull(Boolean isBlank) 169 { 170 } 171 172 175 public byte getHyperlinkType() 176 { 177 return ((JRTextField)parent).getHyperlinkType(); 178 } 179 180 183 public byte getHyperlinkTarget() 184 { 185 return ((JRTextField)parent).getHyperlinkTarget(); 186 } 187 188 191 public JRGroup getEvaluationGroup() 192 { 193 return evaluationGroup; 194 } 195 196 199 public JRExpression getExpression() 200 { 201 return ((JRTextField)parent).getExpression(); 202 } 203 204 207 public JRExpression getAnchorNameExpression() 208 { 209 return ((JRTextField)parent).getAnchorNameExpression(); 210 } 211 212 215 public JRExpression getHyperlinkReferenceExpression() 216 { 217 return ((JRTextField)parent).getHyperlinkReferenceExpression(); 218 } 219 220 223 public JRExpression getHyperlinkAnchorExpression() 224 { 225 return ((JRTextField)parent).getHyperlinkAnchorExpression(); 226 } 227 228 231 public JRExpression getHyperlinkPageExpression() 232 { 233 return ((JRTextField)parent).getHyperlinkPageExpression(); 234 } 235 236 237 240 protected String getAnchorName() 241 { 242 return anchorName; 243 } 244 245 248 protected String getHyperlinkReference() 249 { 250 return hyperlinkReference; 251 } 252 253 256 protected String getHyperlinkAnchor() 257 { 258 return hyperlinkAnchor; 259 } 260 261 264 protected Integer getHyperlinkPage() 265 { 266 return hyperlinkPage; 267 } 268 269 270 protected String getHyperlinkTooltip() 271 { 272 return hyperlinkTooltip; 273 } 274 275 276 279 protected JRTemplateText getJRTemplateText() 280 { 281 JRStyle style = getStyle(); 282 JRTemplateText template = (JRTemplateText) getTemplate(style); 283 if (template == null) 284 { 285 template = new JRTemplateText(filler.getJasperPrint().getDefaultStyleProvider(), this); 286 setTemplatePattern(template); 287 288 registerTemplate(style, template); 289 } 290 291 return template; 292 } 293 294 295 protected void setTemplatePattern(JRTemplateText template) 296 { 297 if (getExpression() != null) 298 { 299 Class valueClass = getExpression().getValueClass(); 300 if (!String .class.equals(valueClass)) 301 { 302 template.setValueClassName(valueClass.getName()); 303 304 String pattern = getTemplatePattern(); 305 if (pattern != null) 306 { 307 template.setPattern(pattern); 308 } 309 310 if (!filler.hasMasterFormatFactory()) 311 { 312 template.setFormatFactoryClass(filler.getFormatFactory().getClass().getName()); 313 } 314 315 if (!filler.hasMasterLocale()) 316 { 317 template.setLocaleCode(JRDataUtils.getLocaleCode(filler.getLocale())); 318 } 319 320 if (!filler.hasMasterTimeZone() && java.util.Date .class.isAssignableFrom(valueClass)) 321 { 322 template.setTimeZoneId(JRDataUtils.getTimeZoneId(filler.getTimeZone())); 323 } 324 } 325 } 326 } 327 328 329 332 protected void evaluate( 333 byte evaluation 334 ) throws JRException 335 { 336 initDelayedEvaluations(); 337 338 reset(); 339 340 evaluatePrintWhenExpression(evaluation); 341 342 if ( 343 (isPrintWhenExpressionNull() || 344 (!isPrintWhenExpressionNull() && 345 isPrintWhenTrue())) 346 ) 347 { 348 if (isEvaluateNow()) 349 { 350 evaluateText(evaluation); 351 } 352 } 353 } 354 355 356 359 protected void evaluateText( 360 byte evaluation 361 ) throws JRException 362 { 363 Object textFieldValue = evaluateExpression(getExpression(), evaluation); 364 365 if (textFieldValue == null) 366 { 367 if (isBlankWhenNull()) 368 { 369 textFieldValue = ""; 370 } 371 } 372 else 373 { 374 Format format = getFormat(); 375 if (format != null) 376 { 377 textFieldValue = format.format(textFieldValue); 378 } 379 } 380 381 387 String newRawText = String.valueOf(textFieldValue); 388 String oldRawText = getRawText(); 389 390 setRawText(newRawText); 391 setTextStart(0); 392 setTextEnd(0); 393 394 setValueRepeating( 395 (oldRawText == null && newRawText == null) || 396 (oldRawText != null && oldRawText.equals(newRawText)) 397 ); 398 399 anchorName = (String ) evaluateExpression(getAnchorNameExpression(), evaluation); 400 hyperlinkReference = (String ) evaluateExpression(getHyperlinkReferenceExpression(), evaluation); 401 hyperlinkAnchor = (String ) evaluateExpression(getHyperlinkAnchorExpression(), evaluation); 402 hyperlinkPage = (Integer ) evaluateExpression(getHyperlinkPageExpression(), evaluation); 403 hyperlinkTooltip = (String ) evaluateExpression(getHyperlinkTooltipExpression(), evaluation); 404 hyperlinkParameters = JRFillHyperlinkHelper.evaluateHyperlinkParameters(this, expressionEvaluator, evaluation); 405 } 406 407 408 411 protected boolean prepare( 412 int availableStretchHeight, 413 boolean isOverflow 414 ) throws JRException 415 { 416 boolean willOverflow = false; 417 418 super.prepare(availableStretchHeight, isOverflow); 419 420 if (!isToPrint()) 421 { 422 return willOverflow; 423 } 424 425 boolean isToPrint = true; 426 boolean isReprinted = false; 427 428 if (isEvaluateNow()) 429 { 430 if (isOverflow) 431 { 432 if (getPositionType() == JRElement.POSITION_TYPE_FIX_RELATIVE_TO_BOTTOM) 433 { 434 setTextStart(0); 437 setTextEnd(0); 438 } 439 440 if ( 441 getTextEnd() >= getText().length() 442 || !isStretchWithOverflow() 443 || getRotation() != ROTATION_NONE 444 ) 445 { 446 449 if (isAlreadyPrinted()) 450 { 451 454 if (isPrintWhenDetailOverflows()) 455 { 456 setTextStart(0); 458 setTextEnd(0); 459 460 isReprinted = true; 461 } 462 else 463 { 464 isToPrint = false; 465 } 466 } 467 else 468 { 469 } 472 } 473 else 474 { 475 } 478 479 if ( 480 isToPrint && 481 isPrintWhenExpressionNull() && 482 !isPrintRepeatedValues() && 483 isValueRepeating() 484 ) 485 { 486 isToPrint = false; } 488 } 489 else 490 { 491 if ( 492 isPrintWhenExpressionNull() && 493 !isPrintRepeatedValues() && 494 isValueRepeating() 495 ) 496 { 497 if ( 498 ( !isPrintInFirstWholeBand() || !getBand().isFirstWholeOnPageColumn() ) && 499 ( getPrintWhenGroupChanges() == null || !getBand().isNewGroup(getPrintWhenGroupChanges()) ) 500 ) 501 { 502 isToPrint = false; 503 } 504 } 505 } 506 507 if (isToPrint) 508 { 509 if (availableStretchHeight >= getRelativeY() - getY() - getBandBottomY()) 510 { 511 513 if ( 514 getTextEnd() < getText().length() 515 || getTextEnd() == 0 516 ) 517 { 518 521 if ( 522 isStretchWithOverflow() 523 && getRotation() == ROTATION_NONE 524 ) 525 { 526 529 chopTextElement(availableStretchHeight - getRelativeY() + getY() + getBandBottomY()); 530 if (getTextEnd() < getText().length()) { 532 535 willOverflow = true; 536 } 537 } 538 else 539 { 540 543 chopTextElement(0); 544 } 545 } 546 else 547 { 548 550 isToPrint = false; 553 } 554 } 555 else 556 { 557 559 564 isToPrint = false; 565 willOverflow = true; 566 } 567 } 568 569 if ( 570 isToPrint && 571 isRemoveLineWhenBlank() && getText().substring( getTextStart(), 574 getTextEnd() 575 ).trim().length() == 0 576 ) 577 { 578 isToPrint = false; 579 } 580 } 581 else 582 { 583 if (isOverflow && isAlreadyPrinted()) 584 { 585 if (isPrintWhenDetailOverflows()) 586 { 587 isReprinted = true; 588 } 589 else 590 { 591 isToPrint = false; 592 } 593 } 594 595 if ( 596 isToPrint && 597 availableStretchHeight < this.getRelativeY() - this.getY() - this.getBandBottomY() 598 ) 599 { 600 isToPrint = false; 601 willOverflow = true; 602 } 603 } 604 605 setToPrint(isToPrint); 606 setReprinted(isReprinted); 607 608 return willOverflow; 609 } 610 611 612 615 protected JRPrintElement fill() throws JRException 616 { 617 byte evaluationType = getEvaluationTime(); 618 619 JRTemplatePrintText text; 620 JRRecordedValuesPrintText recordedValuesText; 621 if (isEvaluateAuto()) 622 { 623 text = recordedValuesText = new JRRecordedValuesPrintText(getJRTemplateText()); 624 } 625 else 626 { 627 text = new JRTemplatePrintText(getJRTemplateText()); 628 recordedValuesText = null; 629 } 630 631 text.setX(getX()); 632 text.setY(getRelativeY()); 633 text.setWidth(getWidth()); 634 if (getRotation() == ROTATION_NONE) 635 { 636 text.setHeight(getStretchHeight()); 637 } 638 else 639 { 640 text.setHeight(getHeight()); 641 } 642 text.setRunDirection(getRunDirection()); 643 644 if (isEvaluateNow()) 645 { 646 copy(text); 647 } 648 else if (isEvaluateAuto()) 649 { 650 initDelayedEvaluationPrint(recordedValuesText); 651 } 652 else 653 { 654 filler.addBoundElement(this, text, evaluationType, getEvaluationGroup(), band); 655 } 656 657 return text; 658 } 659 660 661 664 protected void copy(JRPrintText text) 665 { 666 text.setLineSpacingFactor(getLineSpacingFactor()); 667 text.setLeadingOffset(getLeadingOffset()); 668 text.setTextHeight(getTextHeight()); 669 671 text.setText(textChopper.chop(this, getTextStart(), getTextEnd())); 672 673 text.setAnchorName(getAnchorName()); 674 text.setHyperlinkReference(getHyperlinkReference()); 675 text.setHyperlinkAnchor(getHyperlinkAnchor()); 676 text.setHyperlinkPage(getHyperlinkPage()); 677 text.setHyperlinkTooltip(getHyperlinkTooltip()); 678 text.setBookmarkLevel(getBookmarkLevel()); 679 text.setHyperlinkParameters(hyperlinkParameters); 680 } 681 682 683 686 protected Format getFormat() { 688 Format format = null; 689 690 JRExpression valueExpression = getExpression(); 691 if (valueExpression != null) 692 { 693 Class valueClass = valueExpression.getValueClass(); 694 if (java.util.Date .class.isAssignableFrom(valueClass)) 695 { 696 format = filler.getDateFormat(getPattern()); 697 } 698 else if (java.lang.Number .class.isAssignableFrom(valueClass)) 699 { 700 format = filler.getNumberFormat(getPattern()); 701 } 702 } 703 704 return format; 705 } 706 707 710 protected String getTemplatePattern() { 712 String pattern = null; 713 String originalPattern = getPattern(); 714 Format format = getFormat(); 715 JRExpression valueExpression = getExpression(); 716 if (format != null && valueExpression != null) 717 { 718 Class valueClass = valueExpression.getValueClass(); 719 if (java.util.Date .class.isAssignableFrom(valueClass)) 720 { 721 if (format instanceof SimpleDateFormat ) 722 { 723 pattern = ((SimpleDateFormat ) format).toPattern(); 724 } 725 } 726 else if (Number .class.isAssignableFrom(valueClass)) 727 { 728 if (format instanceof DecimalFormat ) 729 { 730 pattern = ((DecimalFormat ) format).toPattern(); 731 } 732 } 733 } 734 735 if (pattern == null) { 737 pattern = originalPattern; 738 } 739 740 return pattern; 741 } 742 743 744 747 public JRChild getCopy(JRAbstractObjectFactory factory) 748 { 749 return factory.getTextField(this); 750 } 751 752 755 public void collectExpressions(JRExpressionCollector collector) 756 { 757 collector.collect(this); 758 } 759 760 763 public void writeXml(JRXmlWriter xmlWriter) throws IOException 764 { 765 xmlWriter.writeTextField(this); 766 } 767 768 769 protected void resolveElement(JRPrintElement element, byte evaluation) throws JRException 770 { 771 evaluateText(evaluation); 772 773 chopTextElement(0); 774 775 copy((JRPrintText) element); 776 } 777 778 779 public int getBookmarkLevel() 780 { 781 return ((JRTextField)parent).getBookmarkLevel(); 782 } 783 784 785 public JRCloneable createClone(JRFillCloneFactory factory) 786 { 787 return new JRFillTextField(this, factory); 788 } 789 790 protected void collectDelayedEvaluations() 791 { 792 collectDelayedEvaluations(getExpression()); 793 collectDelayedEvaluations(getAnchorNameExpression()); 794 collectDelayedEvaluations(getHyperlinkReferenceExpression()); 795 collectDelayedEvaluations(getHyperlinkAnchorExpression()); 796 collectDelayedEvaluations(getHyperlinkPageExpression()); 797 } 798 799 800 public JRHyperlinkParameter[] getHyperlinkParameters() 801 { 802 return ((JRTextField) parent).getHyperlinkParameters(); 803 } 804 805 806 public String getLinkType() 807 { 808 return ((JRTextField) parent).getLinkType(); 809 } 810 811 812 public JRExpression getHyperlinkTooltipExpression() 813 { 814 return ((JRTextField) parent).getHyperlinkTooltipExpression(); 815 } 816 817 } 818 | Popular Tags |