KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
31
32 import net.sf.jasperreports.engine.JRException;
33 import net.sf.jasperreports.engine.JRExpression;
34 import net.sf.jasperreports.engine.JRGroup;
35 import net.sf.jasperreports.engine.JRPrintElement;
36 import net.sf.jasperreports.engine.JRReport;
37 import net.sf.jasperreports.engine.JRRuntimeException;
38 import net.sf.jasperreports.engine.JRVariable;
39 import net.sf.jasperreports.engine.JasperReport;
40
41
42 /**
43  * @author Teodor Danciu (teodord@users.sourceforge.net)
44  * @version $Id: JRVerticalFiller.java 1500 2006-11-21 18:10:31 +0200 (Tue, 21 Nov 2006) teodord $
45  */

46 public class JRVerticalFiller extends JRBaseFiller
47 {
48
49
50     /**
51      *
52      */

53     protected JRVerticalFiller(JasperReport jasperReport) throws JRException
54     {
55         this(jasperReport, null, null);
56     }
57
58     /**
59      *
60      */

61     protected JRVerticalFiller(JasperReport jasperReport, JRBaseFiller parentFiller) throws JRException
62     {
63         super(jasperReport, null, parentFiller);
64
65         setPageHeight(pageHeight);
66     }
67
68     /**
69      *
70      */

71     protected JRVerticalFiller(JasperReport jasperReport, JREvaluator evaluator, JRBaseFiller parentFiller) throws JRException
72     {
73         super(jasperReport, evaluator, parentFiller);
74
75         setPageHeight(pageHeight);
76     }
77
78
79     /**
80      *
81      */

82     protected void setPageHeight(int pageHeight)
83     {
84         this.pageHeight = pageHeight;
85
86         columnFooterOffsetY = pageHeight - bottomMargin;
87         if (pageFooter != null)
88             columnFooterOffsetY -= pageFooter.getHeight();
89         if (columnFooter != null)
90             columnFooterOffsetY -= columnFooter.getHeight();
91
92         lastPageColumnFooterOffsetY = pageHeight - bottomMargin;
93         if (lastPageFooter != null)
94             lastPageColumnFooterOffsetY -= lastPageFooter.getHeight();
95         if (columnFooter != null)
96             lastPageColumnFooterOffsetY -= columnFooter.getHeight();
97     }
98
99
100     /**
101      *
102      */

103     protected synchronized void fillReport() throws JRException
104     {
105         setLastPageFooter(false);
106
107         if (next())
108         {
109             fillReportStart();
110
111             while (next())
112             {
113                 fillReportContent();
114             }
115             
116             fillReportEnd();
117         }
118         else
119         {
120             switch (whenNoDataType)
121             {
122                 case JRReport.WHEN_NO_DATA_TYPE_ALL_SECTIONS_NO_DETAIL :
123                 {
124                     scriptlet.callBeforeReportInit();
125                     calculator.initializeVariables(JRVariable.RESET_TYPE_REPORT);
126                     scriptlet.callAfterReportInit();
127             
128                     printPage = newPage();
129                     addPage(printPage);
130                     setFirstColumn();
131                     offsetY = topMargin;
132             
133                     fillBackground();
134                     
135                     fillTitle();
136                     
137                     fillPageHeader(JRExpression.EVALUATION_DEFAULT);
138             
139                     fillColumnHeader(JRExpression.EVALUATION_DEFAULT);
140             
141                     fillGroupHeaders(true);
142             
143                     fillGroupFooters(true);
144             
145                     fillSummary();
146
147                     break;
148                 }
149                 case JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE :
150                 {
151                     printPage = newPage();
152                     addPage(printPage);
153                     break;
154                 }
155                 case JRReport.WHEN_NO_DATA_TYPE_NO_PAGES :
156                 default :
157                 {
158                 }
159             }
160         }
161
162         if (isSubreport())
163         {
164             //if (
165
// columnIndex == 0 ||
166
// (columnIndex > 0 && printPageStretchHeight < offsetY + bottomMargin)
167
// )
168
//{
169
printPageStretchHeight = offsetY + bottomMargin;
170             //}
171

172             if (fillContext.isUsingVirtualizer())
173             {
174                 removePageIdentityDataProvider();
175             }
176         }
177         
178         if (fillContext.isIgnorePagination())
179         {
180             jasperPrint.setPageHeight(offsetY + bottomMargin);
181         }
182     }
183
184
185     /**
186      *
187      */

188     private void fillReportStart() throws JRException
189     {
190         scriptlet.callBeforeReportInit();
191         calculator.initializeVariables(JRVariable.RESET_TYPE_REPORT);
192         scriptlet.callAfterReportInit();
193
194         printPage = newPage();
195         addPage(printPage);
196         setFirstColumn();
197         offsetY = topMargin;
198
199         fillBackground();
200
201         fillTitle();
202         
203         fillPageHeader(JRExpression.EVALUATION_DEFAULT);
204
205         fillColumnHeader(JRExpression.EVALUATION_DEFAULT);
206
207         fillGroupHeaders(true);
208
209         fillDetail();
210     }
211
212
213     /**
214      *
215      */

216     private void fillReportContent() throws JRException
217     {
218         calculator.estimateGroupRuptures();
219
220         fillGroupFooters(false);
221
222         resolveGroupBoundElements(JRExpression.EVALUATION_OLD, false);
223         scriptlet.callBeforeGroupInit();
224         calculator.initializeVariables(JRVariable.RESET_TYPE_GROUP);
225         scriptlet.callAfterGroupInit();
226
227         fillGroupHeaders(false);
228
229         fillDetail();
230     }
231
232
233     /**
234      *
235      */

236     private void fillReportEnd() throws JRException
237     {
238         fillGroupFooters(true);
239
240         fillSummary();
241     }
242
243
244     /**
245      *
246      */

247      private void fillTitle() throws JRException
248      {
249         title.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
250
251         if (title.isToPrint())
252         {
253             while (
254                 title.getHeight() > pageHeight - bottomMargin - offsetY
255                 )
256             {
257                 addPage(false);
258             }
259     
260             title.evaluate(JRExpression.EVALUATION_DEFAULT);
261
262             JRPrintBand printBand = title.fill(pageHeight - bottomMargin - offsetY - title.getHeight());
263             
264             if (title.willOverflow() && !title.isSplitAllowed() && isSubreport())
265             {
266                 resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, false);
267                 resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
268                 resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
269                 scriptlet.callBeforePageInit();
270                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
271                 scriptlet.callAfterPageInit();
272     
273                 addPage(false);
274     
275                 printBand = title.refill(pageHeight - bottomMargin - offsetY - title.getHeight());
276             }
277
278             fillBand(printBand);
279             offsetY += printBand.getHeight();
280     
281             while (title.willOverflow())
282             {
283                 resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, false);
284                 resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
285                 resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
286                 scriptlet.callBeforePageInit();
287                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
288                 scriptlet.callAfterPageInit();
289     
290                 addPage(false);
291     
292                 printBand = title.fill(pageHeight - bottomMargin - offsetY - title.getHeight());
293     
294                 fillBand(printBand);
295                 offsetY += printBand.getHeight();
296             }
297
298             resolveBandBoundElements(title, JRExpression.EVALUATION_DEFAULT);
299             
300             if (isTitleNewPage)
301             {
302                 resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, false);
303                 resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
304                 resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
305                 scriptlet.callBeforePageInit();
306                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
307                 scriptlet.callAfterPageInit();
308     
309                 addPage(false);
310             }
311         }
312     }
313
314
315     /**
316      *
317      */

318     private void fillPageHeader(byte evaluation) throws JRException
319     {
320         setNewPageColumnInBands();
321
322         pageHeader.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
323
324         if (pageHeader.isToPrint())
325         {
326             int reattempts = getMasterColumnCount();
327             if (isCreatingNewPage)
328             {
329                 --reattempts;
330             }
331             
332             boolean filled = fillBandNoOverflow(pageHeader, evaluation);
333             
334             for (int i = 0; !filled && i < reattempts; ++i)
335             {
336                 resolveGroupBoundElements(evaluation, false);
337                 resolveColumnBoundElements(evaluation);
338                 resolvePageBoundElements(evaluation);
339                 scriptlet.callBeforePageInit();
340                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
341                 scriptlet.callAfterPageInit();
342         
343                 addPage(false);
344                 
345                 filled = fillBandNoOverflow(pageHeader, evaluation);
346             }
347             
348             if (!filled)
349             {
350                 throw new JRRuntimeException("Infinite loop creating new page due to page header overflow.");
351             }
352         }
353
354         columnHeaderOffsetY = offsetY;
355         
356         isNewPage = true;
357     }
358
359     
360     private boolean fillBandNoOverflow(JRFillBand band, byte evaluation) throws JRException
361     {
362         int availableStretch = columnFooterOffsetY - offsetY - band.getHeight();
363         boolean overflow = availableStretch < 0;
364         
365         if (!overflow)
366         {
367             band.evaluate(evaluation);
368             JRPrintBand printBand = band.fill(availableStretch);
369             
370             overflow = band.willOverflow();
371             if (overflow)
372             {
373                 band.rewind();
374             }
375             else
376             {
377                 fillBand(printBand);
378                 offsetY += printBand.getHeight();
379                 
380                 resolveBandBoundElements(band, evaluation);
381             }
382         }
383         
384         return !overflow;
385     }
386
387
388     /**
389      *
390      */

391     private void fillColumnHeader(byte evaluation) throws JRException
392     {
393         setNewPageColumnInBands();
394
395         columnHeader.evaluatePrintWhenExpression(evaluation);
396         
397         if (columnHeader.isToPrint())
398         {
399             int reattempts = getMasterColumnCount();
400             if (isCreatingNewPage)
401             {
402                 --reattempts;
403             }
404             
405             boolean filled = fillBandNoOverflow(columnHeader, evaluation);
406             
407             for (int i = 0; !filled && i < reattempts; ++i)
408             {
409                 while (columnIndex < columnCount - 1)
410                 {
411                     resolveGroupBoundElements(evaluation, false);
412                     resolveColumnBoundElements(evaluation);
413                     scriptlet.callBeforeColumnInit();
414                     calculator.initializeVariables(JRVariable.RESET_TYPE_COLUMN);
415                     scriptlet.callAfterColumnInit();
416         
417                     columnIndex += 1;
418                     offsetX = leftMargin + columnIndex * (columnSpacing + columnWidth);
419                     offsetY = columnHeaderOffsetY;
420
421                     setColumnNumberVar();
422                 }
423                 
424                 fillPageFooter(evaluation);
425                 
426                 resolveGroupBoundElements(evaluation, false);
427                 resolveColumnBoundElements(evaluation);
428                 resolvePageBoundElements(evaluation);
429                 scriptlet.callBeforePageInit();
430                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
431                 scriptlet.callAfterPageInit();
432         
433                 addPage(false);
434         
435                 fillPageHeader(evaluation);
436                 
437                 filled = fillBandNoOverflow(columnHeader, evaluation);
438             }
439             
440             if (!filled)
441             {
442                 throw new JRRuntimeException("Infinite loop creating new page due to column header overflow.");
443             }
444         }
445
446         isNewColumn = true;
447     }
448
449
450     /**
451      *
452      */

453     private void fillGroupHeaders(boolean isFillAll) throws JRException
454     {
455         if (groups != null && groups.length > 0)
456         {
457             for(int i = 0; i < groups.length; i++)
458             {
459                 if(isFillAll)
460                 {
461                     fillGroupHeader(groups[i]);
462                 }
463                 else
464                 {
465                     if (groups[i].hasChanged())
466                     {
467                         fillGroupHeader(groups[i]);
468                     }
469                 }
470             }
471         }
472     }
473
474
475     /**
476      *
477      */

478     private void fillGroupHeader(JRFillGroup group) throws JRException
479     {
480         byte evalPrevPage = (group.isTopLevelChange()?JRExpression.EVALUATION_OLD:JRExpression.EVALUATION_DEFAULT);
481
482         if ( (group.isStartNewPage() || group.isResetPageNumber()) && !isNewPage )
483         {
484             fillPageBreak(
485                 group.isResetPageNumber(),
486                 evalPrevPage,
487                 JRExpression.EVALUATION_DEFAULT,
488                 true
489                 );
490         }
491         else if ( group.isStartNewColumn() && !isNewColumn )
492         {
493             fillColumnBreak(
494                 evalPrevPage,
495                 JRExpression.EVALUATION_DEFAULT
496                 );
497         }
498         
499         JRFillBand groupHeader = (JRFillBand)group.getGroupHeader();
500
501         groupHeader.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
502         
503         if (groupHeader.isToPrint())
504         {
505             while (
506                 groupHeader.getHeight() > columnFooterOffsetY - offsetY ||
507                 group.getMinHeightToStartNewPage() > columnFooterOffsetY - offsetY
508                 )
509             {
510                 fillColumnBreak(
511                     evalPrevPage,
512                     JRExpression.EVALUATION_DEFAULT
513                     );
514             }
515         }
516
517         setNewGroupInBands(group);
518
519         group.setFooterPrinted(false);
520
521         if (groupHeader.isToPrint())
522         {
523             fillColumnBand(groupHeader, JRExpression.EVALUATION_DEFAULT);
524         }
525
526         group.setHeaderPrinted(true);
527
528         isNewGroup = true;
529     }
530
531
532     /**
533      *
534      */

535     private void fillGroupHeadersReprint(byte evaluation) throws JRException
536     {
537         if (groups != null && groups.length > 0)
538         {
539             for(int i = 0; i < groups.length; i++)
540             {
541                 fillGroupHeaderReprint(groups[i], evaluation);
542             }
543         }
544     }
545
546
547     /**
548      *
549      */

550      private void fillGroupHeaderReprint(JRFillGroup group, byte evaluation) throws JRException
551      {
552         if (
553             group.isReprintHeaderOnEachPage() &&
554             (!group.hasChanged() || (group.hasChanged() && group.isHeaderPrinted()))
555             )
556         {
557             JRFillBand groupHeader = (JRFillBand)group.getGroupHeader();
558
559             groupHeader.evaluatePrintWhenExpression(evaluation);
560         
561             if (groupHeader.isToPrint())
562             {
563                 while (
564                     groupHeader.getHeight() > columnFooterOffsetY - offsetY ||
565                     group.getMinHeightToStartNewPage() > columnFooterOffsetY - offsetY
566                     )
567                 {
568                     fillColumnBreak(evaluation, evaluation);
569                 }
570     
571                 fillColumnBand(groupHeader, evaluation);
572             }
573         }
574     }
575
576
577     /**
578      *
579      */

580     private void fillDetail() throws JRException
581     {
582         if (!detail.isPrintWhenExpressionNull())
583         {
584             calculator.estimateVariables();
585             detail.evaluatePrintWhenExpression(JRExpression.EVALUATION_ESTIMATED);
586         }
587     
588         if (detail.isToPrint())
589         {
590             while (
591                 detail.getHeight() > columnFooterOffsetY - offsetY
592                 )
593             {
594                 byte evalPrevPage = (isNewGroup?JRExpression.EVALUATION_DEFAULT:JRExpression.EVALUATION_OLD);
595                 
596                 fillColumnBreak(
597                     evalPrevPage,
598                     JRExpression.EVALUATION_DEFAULT
599                     );
600             }
601         }
602
603         scriptlet.callBeforeDetailEval();
604         calculator.calculateVariables();
605         scriptlet.callAfterDetailEval();
606
607         if (!detail.isPrintWhenExpressionNull())
608         {
609             detail.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
610         }
611
612         if (detail.isToPrint())
613         {
614             fillColumnBand(detail, JRExpression.EVALUATION_DEFAULT);
615         }
616
617         isNewPage = false;
618         isNewColumn = false;
619         isNewGroup = false;
620     }
621
622
623     /**
624      *
625      */

626     private void fillGroupFooters(boolean isFillAll) throws JRException
627     {
628         if (groups != null && groups.length > 0)
629         {
630             byte evaluation = (isFillAll)?JRExpression.EVALUATION_DEFAULT:JRExpression.EVALUATION_OLD;
631             
632             for(int i = groups.length - 1; i >= 0; i--)
633             {
634                 if (isFillAll)
635                 {
636                     fillGroupFooter(groups[i], evaluation);
637                 }
638                 else
639                 {
640                     if (groups[i].hasChanged())
641                     {
642                         fillGroupFooter(groups[i], evaluation);
643                     }
644                 }
645             }
646         }
647     }
648
649
650     /**
651      *
652      */

653     private void fillGroupFooter(JRFillGroup group, byte evaluation) throws JRException
654     {
655         JRFillBand groupFooter = (JRFillBand)group.getGroupFooter();
656         
657         groupFooter.evaluatePrintWhenExpression(evaluation);
658     
659         if (groupFooter.isToPrint())
660         {
661             if (
662                 groupFooter.getHeight() > columnFooterOffsetY - offsetY
663                 )
664             {
665                 fillColumnBreak(evaluation, evaluation);
666             }
667     
668             fillColumnBand(groupFooter, evaluation);
669         }
670
671         isNewPage = false;
672         isNewColumn = false;
673
674         group.setHeaderPrinted(false);
675         group.setFooterPrinted(true);
676     }
677
678
679     /**
680      *
681      */

682      private void fillColumnFooter(byte evaluation) throws JRException
683      {
684         /*
685         if (!isSubreport)
686         {
687             offsetY = columnFooterOffsetY;
688         }
689         */

690
691         if (isSubreport() && columnIndex == 0)
692         {
693             columnFooterOffsetY = offsetY;
694         }
695
696         int oldOffsetY = offsetY;
697         if (!isFloatColumnFooter && !fillContext.isIgnorePagination())
698         {
699             offsetY = columnFooterOffsetY;
700         }
701
702         columnFooter.evaluatePrintWhenExpression(evaluation);
703
704         if (columnFooter.isToPrint())
705         {
706             fillFixedBand(columnFooter, evaluation);
707         }
708         
709         if (isFloatColumnFooter && !fillContext.isIgnorePagination())
710         {
711             offsetY += columnFooterOffsetY - oldOffsetY;
712         }
713     }
714
715
716     /**
717      *
718      */

719     private void fillPageFooter(byte evaluation) throws JRException
720     {
721         JRFillBand crtPageFooter = getCurrentPageFooter();
722         
723         offsetX = leftMargin;
724
725         if (!isSubreport() && !fillContext.isIgnorePagination())
726         {
727             offsetY = pageHeight - crtPageFooter.getHeight() - bottomMargin;
728         }
729
730         crtPageFooter.evaluatePrintWhenExpression(evaluation);
731
732         if (crtPageFooter.isToPrint())
733         {
734             fillFixedBand(crtPageFooter, evaluation);
735         }
736     }
737
738
739     /**
740      *
741      */

742     private void fillSummary() throws JRException
743     {
744         if (lastPageFooter == missingFillBand)
745         {
746             if (
747                 !isSummaryNewPage
748                 && columnIndex == 0
749                 && summary.getHeight() <= columnFooterOffsetY - offsetY
750                 )
751             {
752                 fillSummarySamePage();
753             }
754             else
755             {
756                 fillSummaryNewPage();
757             }
758         }
759         else
760         {
761             if (
762                 !isSummaryNewPage
763                 && columnIndex == 0
764                 && summary.getHeight() <= lastPageColumnFooterOffsetY - offsetY
765                 )
766             {
767                 setLastPageFooter(true);
768
769                 fillSummarySamePage();
770             }
771             else if (
772                 !isSummaryNewPage
773                 && columnIndex == 0
774                 && summary.getHeight() <= columnFooterOffsetY - offsetY
775                 )
776             {
777                 fillSummarySamePageMixedFooters();
778             }
779             else if (columnIndex == 0 && offsetY <= lastPageColumnFooterOffsetY)
780             {
781                 setLastPageFooter(true);
782
783                 fillSummaryNewPage();
784             }
785             else
786             {
787                 fillPageBreak(false, JRExpression.EVALUATION_DEFAULT, JRExpression.EVALUATION_DEFAULT, false);
788             
789                 setLastPageFooter(true);
790             
791                 if (isSummaryNewPage)
792                 {
793                     fillSummaryNewPage();
794                 }
795                 else
796                 {
797                     fillSummarySamePage();
798                 }
799             }
800         }
801
802         resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
803         resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
804         resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
805         resolveReportBoundElements();
806     }
807         
808         
809     /**
810      *
811      */

812     private void fillSummarySamePage() throws JRException
813     {
814         summary.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
815
816         if (summary != missingFillBand && summary.isToPrint())
817         {
818             summary.evaluate(JRExpression.EVALUATION_DEFAULT);
819         
820             JRPrintBand printBand = summary.fill(columnFooterOffsetY - offsetY - summary.getHeight());
821                 
822             if (summary.willOverflow() && !summary.isSplitAllowed())
823             {
824                 fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
825             
826                 fillPageFooter(JRExpression.EVALUATION_DEFAULT);
827         
828                 resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
829                 resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
830                 resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
831                 scriptlet.callBeforePageInit();
832                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
833                 scriptlet.callAfterPageInit();
834         
835                 addPage(false);
836         
837                 printBand = summary.refill(pageHeight - bottomMargin - offsetY - summary.getHeight());
838         
839                 fillBand(printBand);
840                 offsetY += printBand.getHeight();
841             }
842             else
843             {
844                 fillBand(printBand);
845                 offsetY += printBand.getHeight();
846             
847                 fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
848             
849                 fillPageFooter(JRExpression.EVALUATION_DEFAULT);
850             }
851     
852             while (summary.willOverflow())
853             {
854                 resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
855                 resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
856                 resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
857                 scriptlet.callBeforePageInit();
858                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
859                 scriptlet.callAfterPageInit();
860         
861                 addPage(false);
862         
863                 printBand = summary.fill(pageHeight - bottomMargin - offsetY - summary.getHeight());
864         
865                 fillBand(printBand);
866                 offsetY += printBand.getHeight();
867             }
868             
869             resolveBandBoundElements(summary, JRExpression.EVALUATION_DEFAULT);
870         }
871         else
872         {
873             fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
874         
875             fillPageFooter(JRExpression.EVALUATION_DEFAULT);
876         }
877     }
878
879
880     /**
881      *
882      */

883     private void fillSummarySamePageMixedFooters() throws JRException
884     {
885         summary.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
886
887         if (summary != missingFillBand && summary.isToPrint())
888         {
889             summary.evaluate(JRExpression.EVALUATION_DEFAULT);
890         
891             JRPrintBand printBand = summary.fill(columnFooterOffsetY - offsetY - summary.getHeight());
892                 
893             if (summary.willOverflow() && !summary.isSplitAllowed())
894             {
895                 if (offsetY <= lastPageColumnFooterOffsetY)
896                 {
897                     setLastPageFooter(true);
898
899                     fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
900         
901                     fillPageFooter(JRExpression.EVALUATION_DEFAULT);
902
903                     resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
904                     resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
905                     resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
906                     scriptlet.callBeforePageInit();
907                     calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
908                     scriptlet.callAfterPageInit();
909         
910                     addPage(false);
911         
912                     printBand = summary.refill(pageHeight - bottomMargin - offsetY - summary.getHeight());
913
914                     fillBand(printBand);
915                     offsetY += printBand.getHeight();
916                 }
917                 else
918                 {
919                     fillPageBreak(false, JRExpression.EVALUATION_DEFAULT, JRExpression.EVALUATION_DEFAULT, false);
920         
921                     setLastPageFooter(true);
922
923                     printBand = summary.refill(lastPageColumnFooterOffsetY - offsetY - summary.getHeight());
924                     //printBand = summary.refill(pageHeight - bottomMargin - offsetY - summary.getHeight());
925

926                     fillBand(printBand);
927                     offsetY += printBand.getHeight();
928
929                     fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
930         
931                     fillPageFooter(JRExpression.EVALUATION_DEFAULT);
932                 }
933             }
934             else
935             {
936                 fillBand(printBand);
937                 offsetY += printBand.getHeight();
938                 
939                 fillPageBreak(false, JRExpression.EVALUATION_DEFAULT, JRExpression.EVALUATION_DEFAULT, false);
940
941                 setLastPageFooter(true);
942                 
943                 if (summary.willOverflow())
944                 {
945                     printBand = summary.fill(lastPageColumnFooterOffsetY - offsetY - summary.getHeight());
946
947                     fillBand(printBand);
948                     offsetY += printBand.getHeight();
949                 }
950
951                 fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
952         
953                 fillPageFooter(JRExpression.EVALUATION_DEFAULT);
954             }
955
956             while (summary.willOverflow())
957             {
958                 resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
959                 resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
960                 resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
961                 scriptlet.callBeforePageInit();
962                 calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
963                 scriptlet.callAfterPageInit();
964         
965                 addPage(false);
966         
967                 printBand = summary.fill(pageHeight - bottomMargin - offsetY - summary.getHeight());
968         
969                 fillBand(printBand);
970                 offsetY += printBand.getHeight();
971             }
972             
973             resolveBandBoundElements(summary, JRExpression.EVALUATION_DEFAULT);
974         }
975         else
976         {
977             if(offsetY > lastPageColumnFooterOffsetY)
978             {
979                 fillPageBreak(false, JRExpression.EVALUATION_DEFAULT, JRExpression.EVALUATION_DEFAULT, false);
980             }
981
982             setLastPageFooter(true);
983
984             fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
985         
986             fillPageFooter(JRExpression.EVALUATION_DEFAULT);
987         }
988     }
989
990
991     /**
992      *
993      */

994     private void fillSummaryNewPage() throws JRException
995     {
996         fillColumnFooter(JRExpression.EVALUATION_DEFAULT);
997     
998         fillPageFooter(JRExpression.EVALUATION_DEFAULT);
999
1000        summary.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
1001    
1002        if (summary != missingFillBand && summary.isToPrint())
1003        {
1004            resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
1005            resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
1006            resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
1007            scriptlet.callBeforePageInit();
1008            calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
1009            scriptlet.callAfterPageInit();
1010    
1011            addPage(false);
1012    
1013            columnIndex = -1;// FIXME why?
1014

1015            summary.evaluate(JRExpression.EVALUATION_DEFAULT);
1016                
1017            JRPrintBand printBand = summary.fill(pageHeight - bottomMargin - offsetY - summary.getHeight());
1018                
1019            if (summary.willOverflow() && !summary.isSplitAllowed() && isSubreport())
1020            {
1021                resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
1022                resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
1023                resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
1024                scriptlet.callBeforePageInit();
1025                calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
1026                scriptlet.callAfterPageInit();
1027        
1028                addPage(false);
1029        
1030                printBand = summary.refill(pageHeight - bottomMargin - offsetY - summary.getHeight());
1031            }
1032
1033            fillBand(printBand);
1034            offsetY += printBand.getHeight();
1035        
1036            while (summary.willOverflow())
1037            {
1038                resolveGroupBoundElements(JRExpression.EVALUATION_DEFAULT, true);
1039                resolveColumnBoundElements(JRExpression.EVALUATION_DEFAULT);
1040                resolvePageBoundElements(JRExpression.EVALUATION_DEFAULT);
1041                scriptlet.callBeforePageInit();
1042                calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
1043                scriptlet.callAfterPageInit();
1044        
1045                addPage(false);
1046        
1047                printBand = summary.fill(pageHeight - bottomMargin - offsetY - summary.getHeight());
1048        
1049                fillBand(printBand);
1050                offsetY += printBand.getHeight();
1051            }
1052            
1053            resolveBandBoundElements(summary, JRExpression.EVALUATION_DEFAULT);
1054        }
1055    }
1056
1057
1058    /**
1059     *
1060     */

1061    private void fillBackground() throws JRException
1062    {
1063        //offsetX = leftMargin;
1064

1065        //if (!isSubreport)
1066
//{
1067
// offsetY = pageHeight - pageFooter.getHeight() - bottomMargin;
1068
//}
1069

1070        if (background.getHeight() <= pageHeight - bottomMargin - offsetY)
1071        {
1072            background.evaluatePrintWhenExpression(JRExpression.EVALUATION_DEFAULT);
1073    
1074            if (background.isToPrint())
1075            {
1076                    background.evaluate(JRExpression.EVALUATION_DEFAULT);
1077    
1078                    JRPrintBand printBand = background.fill(pageHeight - bottomMargin - offsetY - background.getHeight());
1079    
1080                    fillBand(printBand);
1081                    //offsetY += printBand.getHeight();
1082
}
1083        }
1084    }
1085
1086
1087    /**
1088     *
1089     */

1090    private void addPage(boolean isResetPageNumber) throws JRException
1091    {
1092        if (isSubreport())
1093        {
1094            if (!parentFiller.isBandOverFlowAllowed())
1095            {
1096                throw new JRRuntimeException("Subreport overflowed on a band that does not support overflow.");
1097            }
1098            
1099            //if (
1100
// columnIndex == 0 ||
1101
// (columnIndex > 0 && printPageStretchHeight < offsetY + bottomMargin)
1102
// )
1103
//{
1104
printPageStretchHeight = offsetY + bottomMargin;
1105            //}
1106

1107            if (fillContext.isUsingVirtualizer())
1108            {
1109                removePageIdentityDataProvider();
1110            }
1111
1112            suspendSubreportRunner();
1113        }
1114        
1115        printPage = newPage();
1116        
1117        if (isSubreport() && fillContext.isUsingVirtualizer())
1118        {
1119            addPageIdentityDataProvider();
1120        }
1121        
1122        if (isResetPageNumber)
1123        {
1124            calculator.getPageNumber().setValue(new Integer JavaDoc(1));
1125        }
1126        else
1127        {
1128            calculator.getPageNumber().setValue(
1129                new Integer JavaDoc(((Number JavaDoc)calculator.getPageNumber().getValue()).intValue() + 1)
1130                );
1131        }
1132
1133        calculator.getPageNumber().setOldValue(
1134            calculator.getPageNumber().getValue()
1135            );
1136
1137        addPage(printPage);
1138        
1139        setFirstColumn();
1140        offsetY = topMargin;
1141            
1142        fillBackground();
1143    }
1144
1145    private void setFirstColumn()
1146    {
1147        columnIndex = 0;
1148        offsetX = leftMargin;
1149        setColumnNumberVar();
1150    }
1151
1152    private void setColumnNumberVar()
1153    {
1154        JRFillVariable columnNumber = calculator.getColumnNumber();
1155        columnNumber.setValue(new Integer JavaDoc(columnIndex + 1));
1156        columnNumber.setOldValue(columnNumber.getValue());
1157    }
1158
1159    /**
1160     *
1161     */

1162    private void fillPageBreak(
1163        boolean isResetPageNumber,
1164        byte evalPrevPage,
1165        byte evalNextPage,
1166        boolean isReprintGroupHeaders
1167        ) throws JRException
1168    {
1169        if (isCreatingNewPage)
1170        {
1171            throw new JRException("Infinite loop creating new page.");
1172        }
1173        
1174        isCreatingNewPage = true;
1175        
1176        fillColumnFooter(evalPrevPage);
1177
1178        fillPageFooter(evalPrevPage);
1179        
1180        resolveGroupBoundElements(evalPrevPage, false);
1181        resolveColumnBoundElements(evalPrevPage);
1182        resolvePageBoundElements(evalPrevPage);
1183        scriptlet.callBeforePageInit();
1184        calculator.initializeVariables(JRVariable.RESET_TYPE_PAGE);
1185        scriptlet.callAfterPageInit();
1186
1187        addPage(isResetPageNumber);
1188
1189        fillPageHeader(evalNextPage);
1190
1191        fillColumnHeader(evalNextPage);
1192
1193        if (isReprintGroupHeaders)
1194        {
1195            fillGroupHeadersReprint(evalNextPage);
1196        }
1197
1198        isCreatingNewPage = false;
1199    }
1200
1201
1202    /**
1203     *
1204     */

1205    private void fillColumnBreak(
1206        byte evalPrevPage,
1207        byte evalNextPage
1208        ) throws JRException
1209    {
1210        if (columnIndex == columnCount - 1)
1211        {
1212            fillPageBreak(false, evalPrevPage, evalNextPage, true);
1213        }
1214        else
1215        {
1216            fillColumnFooter(evalPrevPage);
1217
1218            resolveGroupBoundElements(evalPrevPage, false);
1219            resolveColumnBoundElements(evalPrevPage);
1220            scriptlet.callBeforeColumnInit();
1221            calculator.initializeVariables(JRVariable.RESET_TYPE_COLUMN);
1222            scriptlet.callAfterColumnInit();
1223
1224            columnIndex += 1;
1225            offsetX = leftMargin + columnIndex * (columnSpacing + columnWidth);
1226            offsetY = columnHeaderOffsetY;
1227
1228            setColumnNumberVar();
1229    
1230            fillColumnHeader(evalNextPage);
1231        }
1232    }
1233
1234
1235    /**
1236     *
1237     */

1238    protected void fillPageBand(JRFillBand band, byte evaluation) throws JRException
1239    {
1240        band.evaluate(evaluation);
1241
1242        JRPrintBand printBand = band.fill(columnFooterOffsetY - offsetY - band.getHeight());
1243        
1244        if (band.willOverflow() && !band.isSplitAllowed())
1245        {
1246            fillPageBreak(false, evaluation, evaluation, true);
1247
1248            printBand = band.refill(columnFooterOffsetY - offsetY - band.getHeight());
1249        }
1250        
1251        fillBand(printBand);
1252        offsetY += printBand.getHeight();
1253
1254        while (band.willOverflow())
1255        {
1256            fillPageBreak(false, evaluation, evaluation, true);
1257
1258            printBand = band.fill(columnFooterOffsetY - offsetY - band.getHeight());
1259
1260            fillBand(printBand);
1261            offsetY += printBand.getHeight();
1262        }
1263        
1264        resolveBandBoundElements(band, evaluation);
1265    }
1266
1267
1268    /**
1269     *
1270     */

1271    protected void fillColumnBand(JRFillBand band, byte evaluation) throws JRException
1272    {
1273        band.evaluate(evaluation);
1274        
1275        JRPrintBand printBand = band.fill(columnFooterOffsetY - offsetY - band.getHeight());
1276        
1277        if (band.willOverflow() && !band.isSplitAllowed())
1278        {
1279            fillColumnBreak(evaluation, evaluation);
1280
1281            printBand = band.refill(columnFooterOffsetY - offsetY - band.getHeight());
1282        }
1283        
1284        fillBand(printBand);
1285        offsetY += printBand.getHeight();
1286
1287        while (band.willOverflow())
1288        {
1289            fillColumnBreak(evaluation, evaluation);
1290
1291            printBand = band.fill(columnFooterOffsetY - offsetY - band.getHeight());
1292
1293            fillBand(printBand);
1294            offsetY += printBand.getHeight();
1295        }
1296        
1297        resolveBandBoundElements(band, evaluation);
1298    }
1299
1300
1301    /**
1302     *
1303     */

1304    protected void fillFixedBand(JRFillBand band, byte evaluation) throws JRException
1305    {
1306        band.evaluate(evaluation);
1307
1308        JRPrintBand printBand = band.fill();
1309
1310        fillBand(printBand);
1311        offsetY += printBand.getHeight();
1312        
1313        resolveBandBoundElements(band, evaluation);
1314    }
1315
1316
1317    /**
1318     *
1319     */

1320    protected void fillBand(JRPrintBand band)
1321    {
1322        java.util.List JavaDoc elements = band.getElements();
1323        
1324        if (elements != null && elements.size() > 0)
1325        {
1326            JRPrintElement element = null;
1327            for(Iterator JavaDoc it = elements.iterator(); it.hasNext();)
1328            {
1329                element = (JRPrintElement)it.next();
1330                element.setX(element.getX() + offsetX);
1331                element.setY(element.getY() + offsetY);
1332                printPage.addElement(element);
1333            }
1334        }
1335    }
1336
1337
1338    /**
1339     *
1340     */

1341    private void setNewPageColumnInBands()
1342    {
1343        title.setNewPageColumn(true);
1344        pageHeader.setNewPageColumn(true);
1345        columnHeader.setNewPageColumn(true);
1346        detail.setNewPageColumn(true);
1347        columnFooter.setNewPageColumn(true);
1348        pageFooter.setNewPageColumn(true);
1349        lastPageFooter.setNewPageColumn(true);
1350        summary.setNewPageColumn(true);
1351        
1352        if (groups != null && groups.length > 0)
1353        {
1354            for(int i = 0; i < groups.length; i++)
1355            {
1356                ((JRFillBand)groups[i].getGroupHeader()).setNewPageColumn(true);
1357                ((JRFillBand)groups[i].getGroupFooter()).setNewPageColumn(true);
1358            }
1359        }
1360    }
1361
1362
1363    /**
1364     *
1365     */

1366    private void setNewGroupInBands(JRGroup group)
1367    {
1368        title.setNewGroup(group, true);
1369        pageHeader.setNewGroup(group, true);
1370        columnHeader.setNewGroup(group, true);
1371        detail.setNewGroup(group, true);
1372        columnFooter.setNewGroup(group, true);
1373        pageFooter.setNewGroup(group, true);
1374        lastPageFooter.setNewGroup(group, true);
1375        summary.setNewGroup(group, true);
1376        
1377        if (groups != null && groups.length > 0)
1378        {
1379            for(int i = 0; i < groups.length; i++)
1380            {
1381                ((JRFillBand)groups[i].getGroupHeader()).setNewGroup(group, true);
1382                ((JRFillBand)groups[i].getGroupFooter()).setNewGroup(group, true);
1383            }
1384        }
1385    }
1386
1387    
1388    /**
1389     *
1390     */

1391    private JRFillBand getCurrentPageFooter()
1392    {
1393        return isLastPageFooter ? lastPageFooter : pageFooter;
1394    }
1395
1396
1397    /**
1398     *
1399     */

1400    private void setLastPageFooter(boolean isLastPageFooter)
1401    {
1402        this.isLastPageFooter = isLastPageFooter;
1403        
1404        if (isLastPageFooter)
1405        {
1406            columnFooterOffsetY = lastPageColumnFooterOffsetY;
1407        }
1408    }
1409
1410
1411}
1412
Popular Tags