KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRFillTextField


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.io.IOException JavaDoc;
31 import java.text.DecimalFormat JavaDoc;
32 import java.text.Format JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
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 /**
54  * @author Teodor Danciu (teodord@users.sourceforge.net)
55  * @version $Id: JRFillTextField.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
56  */

57 public class JRFillTextField extends JRFillTextElement implements JRTextField
58 {
59
60
61     /**
62      *
63      */

64     private JRGroup evaluationGroup = null;
65
66     /**
67      *
68      */

69     private String JavaDoc anchorName = null;
70     private String JavaDoc hyperlinkReference = null;
71     private String JavaDoc hyperlinkAnchor = null;
72     private Integer JavaDoc hyperlinkPage = null;
73     private String JavaDoc hyperlinkTooltip;
74     private JRPrintHyperlinkParameters hyperlinkParameters;
75
76     /**
77      *
78      */

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     /**
100      *
101      */

102     public boolean isStretchWithOverflow()
103     {
104         return ((JRTextField)parent).isStretchWithOverflow();
105     }
106
107     /**
108      *
109      */

110     public void setStretchWithOverflow(boolean isStretchWithOverflow)
111     {
112     }
113
114     /**
115      *
116      */

117     public byte getEvaluationTime()
118     {
119         return ((JRTextField)parent).getEvaluationTime();
120     }
121         
122     /**
123      *
124      */

125     public String JavaDoc getPattern()
126     {
127         return JRStyleResolver.getPattern(this);
128     }
129         
130     public String JavaDoc getOwnPattern()
131     {
132         return ((JRTextField)parent).getOwnPattern();
133     }
134
135     /**
136      *
137      */

138     public void setPattern(String JavaDoc pattern)
139     {
140     }
141         
142     /**
143      *
144      */

145     public boolean isBlankWhenNull()
146     {
147         return JRStyleResolver.isBlankWhenNull(this);
148     }
149
150     /**
151      *
152      */

153     public Boolean JavaDoc isOwnBlankWhenNull()
154     {
155         return ((JRTextField)parent).isOwnBlankWhenNull();
156     }
157
158     /**
159      *
160      */

161     public void setBlankWhenNull(boolean isBlank)
162     {
163     }
164
165     /**
166      *
167      */

168     public void setBlankWhenNull(Boolean JavaDoc isBlank)
169     {
170     }
171
172     /**
173      *
174      */

175     public byte getHyperlinkType()
176     {
177         return ((JRTextField)parent).getHyperlinkType();
178     }
179         
180     /**
181      *
182      */

183     public byte getHyperlinkTarget()
184     {
185         return ((JRTextField)parent).getHyperlinkTarget();
186     }
187         
188     /**
189      *
190      */

191     public JRGroup getEvaluationGroup()
192     {
193         return evaluationGroup;
194     }
195         
196     /**
197      *
198      */

199     public JRExpression getExpression()
200     {
201         return ((JRTextField)parent).getExpression();
202     }
203
204     /**
205      *
206      */

207     public JRExpression getAnchorNameExpression()
208     {
209         return ((JRTextField)parent).getAnchorNameExpression();
210     }
211
212     /**
213      *
214      */

215     public JRExpression getHyperlinkReferenceExpression()
216     {
217         return ((JRTextField)parent).getHyperlinkReferenceExpression();
218     }
219
220     /**
221      *
222      */

223     public JRExpression getHyperlinkAnchorExpression()
224     {
225         return ((JRTextField)parent).getHyperlinkAnchorExpression();
226     }
227
228     /**
229      *
230      */

231     public JRExpression getHyperlinkPageExpression()
232     {
233         return ((JRTextField)parent).getHyperlinkPageExpression();
234     }
235
236         
237     /**
238      *
239      */

240     protected String JavaDoc getAnchorName()
241     {
242         return anchorName;
243     }
244
245     /**
246      *
247      */

248     protected String JavaDoc getHyperlinkReference()
249     {
250         return hyperlinkReference;
251     }
252
253     /**
254      *
255      */

256     protected String JavaDoc getHyperlinkAnchor()
257     {
258         return hyperlinkAnchor;
259     }
260
261     /**
262      *
263      */

264     protected Integer JavaDoc getHyperlinkPage()
265     {
266         return hyperlinkPage;
267     }
268         
269
270     protected String JavaDoc getHyperlinkTooltip()
271     {
272         return hyperlinkTooltip;
273     }
274         
275
276     /**
277      *
278      */

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 JavaDoc valueClass = getExpression().getValueClass();
300             if (!String JavaDoc.class.equals(valueClass))
301             {
302                 template.setValueClassName(valueClass.getName());
303
304                 String JavaDoc 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 JavaDoc.class.isAssignableFrom(valueClass))
321                 {
322                     template.setTimeZoneId(JRDataUtils.getTimeZoneId(filler.getTimeZone()));
323                 }
324             }
325         }
326     }
327
328
329     /**
330      *
331      */

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     /**
357      *
358      */

359     protected void evaluateText(
360         byte evaluation
361         ) throws JRException
362     {
363         Object JavaDoc textFieldValue = evaluateExpression(getExpression(), evaluation);
364
365         if (textFieldValue == null)
366         {
367             if (isBlankWhenNull())
368             {
369                 textFieldValue = "";
370             }
371         }
372         else
373         {
374             Format JavaDoc format = getFormat();
375             if (format != null)
376             {
377                 textFieldValue = format.format(textFieldValue);
378             }
379         }
380
381 /*
382         String newRawText =
383             JRStringUtil.treatNewLineChars(
384                 String.valueOf(textFieldValue)
385                 );
386 */

387         String JavaDoc newRawText = String.valueOf(textFieldValue);
388         String JavaDoc 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 JavaDoc) evaluateExpression(getAnchorNameExpression(), evaluation);
400         hyperlinkReference = (String JavaDoc) evaluateExpression(getHyperlinkReferenceExpression(), evaluation);
401         hyperlinkAnchor = (String JavaDoc) evaluateExpression(getHyperlinkAnchorExpression(), evaluation);
402         hyperlinkPage = (Integer JavaDoc) evaluateExpression(getHyperlinkPageExpression(), evaluation);
403         hyperlinkTooltip = (String JavaDoc) evaluateExpression(getHyperlinkTooltipExpression(), evaluation);
404         hyperlinkParameters = JRFillHyperlinkHelper.evaluateHyperlinkParameters(this, expressionEvaluator, evaluation);
405     }
406
407
408     /**
409      *
410      */

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                     // the content of the band bottom text fields is not
435
// consumed during overflows, because they only appear on the last overflow
436
setTextStart(0);
437                     setTextEnd(0);
438                 }
439
440                 if (
441                     getTextEnd() >= getText().length()
442                     || !isStretchWithOverflow()
443                     || getRotation() != ROTATION_NONE
444                     )
445                 {
446                     // there is no more text left in the text field to overflow
447
// on the new page, or the text field is not stretchable
448

449                     if (isAlreadyPrinted())
450                     {
451                         // the text field has already displayed all its content
452
// on the previous page even if it not stretchable
453

454                         if (isPrintWhenDetailOverflows())
455                         {
456                             // the whole content is reprinted
457
setTextStart(0);
458                             setTextEnd(0);
459
460                             isReprinted = true;
461                         }
462                         else
463                         {
464                             isToPrint = false;
465                         }
466                     }
467                     else
468                     {
469                         // the text field did not print on the previous page.
470
// we let it go since it is its first time anyway
471
}
472                 }
473                 else
474                 {
475                     // there is text left inside the stretchable text field.
476
// we simply let it go
477
}
478
479                 if (
480                     isToPrint &&
481                     isPrintWhenExpressionNull() &&
482                     !isPrintRepeatedValues() &&
483                     isValueRepeating()
484                     )
485                 {
486                     isToPrint = false; // FIXME, shouldn't we test for the first whole band and the other exceptions to the rule?
487
}
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                     // the available vertical space is sufficient
512

513                     if (
514                         getTextEnd() < getText().length()
515                         || getTextEnd() == 0
516                         )
517                     {
518                         // there is still some text left in the text field or
519
// the text field is empty
520

521                         if (
522                             isStretchWithOverflow()
523                             && getRotation() == ROTATION_NONE
524                             )
525                         {
526                             // the text field is allowed to stretch downwards in order to
527
// display all its content
528

529                             chopTextElement(availableStretchHeight - getRelativeY() + getY() + getBandBottomY());
530                             if (getTextEnd() < getText().length())// - 1)
531
{
532                                 // even after the current chop operation there is some text left
533
// that will overflow on the next page
534

535                                 willOverflow = true;
536                             }
537                         }
538                         else
539                         {
540                             // the text field is not allowed to stretch downwards in order to
541
// display all its content
542

543                             chopTextElement(0);
544                         }
545                     }
546                     else
547                     {
548                         // there is no text left in the text field and the text field was not empty
549

550                         // this section is probably unreachable since it is most likely that
551
// the isToPrint flag was already set on false in the code above.
552
isToPrint = false;
553                     }
554                 }
555                 else
556                 {
557                     // the available vertical space is not sufficient
558

559                     // no matter if there is some text left inside or not,
560
// there was an explicit request to display it,
561
// even if we are on an overflow.
562
// since there is no space available, it will overflow
563

564                     isToPrint = false;
565                     willOverflow = true;
566                 }
567             }
568
569             if (
570                 isToPrint &&
571                 isRemoveLineWhenBlank() && //FIXME if the line won't be removed due to other elements
572
getText().substring( // present on that line, the background does not appear
573
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     /**
613      *
614      */

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     /**
662      *
663      */

664     protected void copy(JRPrintText text)
665     {
666         text.setLineSpacingFactor(getLineSpacingFactor());
667         text.setLeadingOffset(getLeadingOffset());
668         text.setTextHeight(getTextHeight());
669         //FIXME rotation and run direction?
670

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     /**
684      *
685      */

686     protected Format JavaDoc getFormat()//FIXMEFORMAT optimize this with an interface
687
{
688         Format JavaDoc format = null;
689
690         JRExpression valueExpression = getExpression();
691         if (valueExpression != null)
692         {
693             Class JavaDoc valueClass = valueExpression.getValueClass();
694             if (java.util.Date JavaDoc.class.isAssignableFrom(valueClass))
695             {
696                 format = filler.getDateFormat(getPattern());
697             }
698             else if (java.lang.Number JavaDoc.class.isAssignableFrom(valueClass))
699             {
700                 format = filler.getNumberFormat(getPattern());
701             }
702         }
703         
704         return format;
705     }
706
707     /**
708      *
709      */

710     protected String JavaDoc getTemplatePattern()//FIXMEFORMAT optimize this with an interface
711
{
712         String JavaDoc pattern = null;
713         String JavaDoc originalPattern = getPattern();
714         Format JavaDoc format = getFormat();
715         JRExpression valueExpression = getExpression();
716         if (format != null && valueExpression != null)
717         {
718             Class JavaDoc valueClass = valueExpression.getValueClass();
719             if (java.util.Date JavaDoc.class.isAssignableFrom(valueClass))
720             {
721                 if (format instanceof SimpleDateFormat JavaDoc)
722                 {
723                     pattern = ((SimpleDateFormat JavaDoc) format).toPattern();
724                 }
725             }
726             else if (Number JavaDoc.class.isAssignableFrom(valueClass))
727             {
728                 if (format instanceof DecimalFormat JavaDoc)
729                 {
730                     pattern = ((DecimalFormat JavaDoc) format).toPattern();
731                 }
732             }
733         }
734         
735         if (pattern == null)//fallback to the original pattern
736
{
737             pattern = originalPattern;
738         }
739         
740         return pattern;
741     }
742     
743     
744     /**
745      *
746      */

747     public JRChild getCopy(JRAbstractObjectFactory factory)
748     {
749         return factory.getTextField(this);
750     }
751
752     /**
753      *
754      */

755     public void collectExpressions(JRExpressionCollector collector)
756     {
757         collector.collect(this);
758     }
759
760     /**
761      *
762      */

763     public void writeXml(JRXmlWriter xmlWriter) throws IOException JavaDoc
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 JavaDoc 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