KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Color JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import net.sf.jasperreports.engine.JRAbstractObjectFactory;
37 import net.sf.jasperreports.engine.JRBox;
38 import net.sf.jasperreports.engine.JRChild;
39 import net.sf.jasperreports.engine.JRElement;
40 import net.sf.jasperreports.engine.JRException;
41 import net.sf.jasperreports.engine.JRExpressionCollector;
42 import net.sf.jasperreports.engine.JRFrame;
43 import net.sf.jasperreports.engine.JRPrintElement;
44 import net.sf.jasperreports.engine.JRStyle;
45 import net.sf.jasperreports.engine.base.JRBaseBox;
46 import net.sf.jasperreports.engine.base.JRBaseElementGroup;
47 import net.sf.jasperreports.engine.util.JRStyleResolver;
48 import net.sf.jasperreports.engine.xml.JRXmlWriter;
49
50 /**
51  * Fill time implementation of a frame element.
52  *
53  * @author Lucian Chirita (lucianc@users.sourceforge.net)
54  * @version $Id: JRFillFrame.java 1473 2006-11-09 19:48:03 +0200 (Thu, 09 Nov 2006) lucianc $
55  */

56 public class JRFillFrame extends JRFillElement implements JRFrame
57 {
58     protected final JRFrame parentFrame;
59     
60     /**
61      * Element container used for filling.
62      */

63     private JRFillFrameElements frameContainer;
64     
65     /**
66      * Template frame without the bottom border.
67      */

68     private Map JavaDoc bottomTemplateFrames;
69     
70     /**
71      * Template frame without the top border
72      */

73     private Map JavaDoc topTemplateFrames;
74     
75     /**
76      * Template frame without the top and bottom borders
77      */

78     private Map JavaDoc topBottomTemplateFrames;
79     
80     /**
81      * Whether the current frame chunk is the first one.
82      */

83     private boolean first;
84     
85     private boolean fillBottomBorder;
86     
87     /**
88      * Whether the frame has started filling and not ended.
89      */

90     private boolean filling;
91
92     public JRFillFrame(JRBaseFiller filler, JRFrame frame, JRFillObjectFactory factory)
93     {
94         super(filler, frame, factory);
95         
96         parentFrame = frame;
97         
98         frameContainer = new JRFillFrameElements(factory);
99         
100         bottomTemplateFrames = new HashMap JavaDoc();
101         topTemplateFrames = new HashMap JavaDoc();
102         topBottomTemplateFrames = new HashMap JavaDoc();
103         
104         setShrinkable(true);
105     }
106
107     protected JRFillFrame(JRFillFrame frame, JRFillCloneFactory factory)
108     {
109         super(frame, factory);
110         
111         parentFrame = frame.parentFrame;
112         
113         frameContainer = new JRFillFrameElements(frame.frameContainer, factory);
114         
115         bottomTemplateFrames = frame.bottomTemplateFrames;
116         topTemplateFrames = frame.topTemplateFrames;
117         topBottomTemplateFrames = frame.topBottomTemplateFrames;
118     }
119
120     /**
121      *
122      */

123     public byte getMode()
124     {
125         return JRStyleResolver.getMode(this, MODE_TRANSPARENT);
126     }
127
128     protected void evaluate(byte evaluation) throws JRException
129     {
130         reset();
131
132         evaluatePrintWhenExpression(evaluation);
133
134         if (isPrintWhenExpressionNull() || isPrintWhenTrue())
135         {
136             frameContainer.evaluate(evaluation);
137             
138             boolean repeating = true;
139             JRFillElement[] elements = (JRFillElement[]) getElements();
140             for (int i = 0; repeating && i < elements.length; i++)
141             {
142                 repeating &= elements[i].isValueRepeating();
143             }
144             setValueRepeating(repeating);
145         }
146         
147         filling = false;
148     }
149
150     protected void rewind() throws JRException
151     {
152         frameContainer.rewind();
153         
154         filling = false;
155     }
156
157     protected boolean prepare(int availableStretchHeight, boolean isOverflow) throws JRException
158     {
159         super.prepare(availableStretchHeight, isOverflow);
160
161         if (!isToPrint())
162         {
163             return false;
164         }
165         
166         first = !isOverflow || !filling;
167         int topPadding = first ? getTopPadding() : 0;
168         int bottomPadding = getBottomPadding();
169         
170         if (availableStretchHeight < getRelativeY() - getY() - getBandBottomY() - topPadding)
171         {
172             setToPrint(false);
173             return true;
174         }
175         
176         if (!filling && !isPrintRepeatedValues() && isValueRepeating() &&
177                 (!isPrintInFirstWholeBand() || !getBand().isFirstWholeOnPageColumn()) &&
178                 (getPrintWhenGroupChanges() == null || !getBand().isNewGroup(getPrintWhenGroupChanges())) &&
179                 (!isOverflow || !isPrintWhenDetailOverflows())
180             )
181         {
182             setToPrint(false);
183             return false;
184         }
185
186         // FIXME reprinted when isAlreadyPrinted() || !isPrintRepeatedValues()?
187
if (!filling && isOverflow && isAlreadyPrinted())
188         {
189             if (isPrintWhenDetailOverflows())
190             {
191                 rewind();
192                 setReprinted(true);
193             }
194             else
195             {
196                 setToPrint(false);
197                 return false;
198             }
199         }
200         
201         int stretchHeight = availableStretchHeight - getRelativeY() + getY() + getBandBottomY();
202         
203         frameContainer.initFill();
204         frameContainer.resetElements();
205         int frameElemsAvailableHeight = stretchHeight + bottomPadding + getTopPadding() - topPadding;
206         frameContainer.prepareElements(frameElemsAvailableHeight, true);
207         
208         boolean willOverflow = frameContainer.willOverflow();
209         if (willOverflow)
210         {
211             fillBottomBorder = false;
212             setStretchHeight(getHeight() + stretchHeight);
213         }
214         else
215         {
216             int neededStretch = frameContainer.getStretchHeight() - frameContainer.getFirstY() + topPadding + bottomPadding;
217             if (neededStretch <= getHeight() + stretchHeight)
218             {
219                 fillBottomBorder = true;
220                 setStretchHeight(neededStretch);
221             }
222             else //don't overflow because of the bottom padding
223
{
224                 fillBottomBorder = false;
225                 setStretchHeight(getHeight() + stretchHeight);
226             }
227         }
228
229         filling = willOverflow;
230
231         return willOverflow;
232     }
233
234     protected void setStretchHeight(int stretchHeight)
235     {
236         super.setStretchHeight(stretchHeight);
237         
238         int topPadding = first ? getTopPadding() : 0;
239         int bottomPadding = fillBottomBorder ? getBottomPadding() : 0;
240         frameContainer.setStretchHeight(stretchHeight + frameContainer.getFirstY() - topPadding - bottomPadding);
241     }
242     
243     
244     protected void stretchHeightFinal()
245     {
246         frameContainer.stretchElements();
247         frameContainer.moveBandBottomElements();
248         frameContainer.removeBlankElements();
249
250         int topPadding = first ? getTopPadding() : 0;
251         int bottomPadding = fillBottomBorder ? getBottomPadding() : 0;
252         super.setStretchHeight(frameContainer.getStretchHeight() - frameContainer.getFirstY() + topPadding + bottomPadding);
253     }
254
255
256     protected JRPrintElement fill() throws JRException
257     {
258         JRTemplatePrintFrame printFrame = new JRTemplatePrintFrame(getTemplate());
259         printFrame.setX(getX());
260         printFrame.setY(getRelativeY());
261         printFrame.setWidth(getWidth());
262         
263         frameContainer.fillElements(printFrame);
264         
265         printFrame.setHeight(getStretchHeight());
266         
267         return printFrame;
268     }
269
270     protected JRTemplateFrame getTemplate()
271     {
272         JRStyle style = getStyle();
273
274         Map JavaDoc templatesMap;
275         if (first)
276         {
277             if (fillBottomBorder)
278             {
279                 templatesMap = templates;
280             }
281             else //remove the bottom border
282
{
283                 templatesMap = bottomTemplateFrames;
284             }
285         }
286         else
287         {
288             if (fillBottomBorder) //remove the top border
289
{
290                 templatesMap = topTemplateFrames;
291             }
292             else //remove the top and bottom borders
293
{
294                 templatesMap = topBottomTemplateFrames;
295             }
296         }
297         
298         JRTemplateFrame boxTemplate = (JRTemplateFrame) templatesMap.get(style);
299         if (boxTemplate == null)
300         {
301             boxTemplate = new JRTemplateFrame(filler.getJasperPrint().getDefaultStyleProvider(), this);
302             if (first)
303             {
304                 if (!fillBottomBorder) //remove the bottom border
305
{
306                     JRBox bottomBox = new JRBaseBox(this, false, false, false, true);
307                     boxTemplate.setBox(bottomBox);
308                 }
309             }
310             else
311             {
312                 if (fillBottomBorder) //remove the top border
313
{
314                     JRBox topBox = new JRBaseBox(this, false, false, true, false);
315                     boxTemplate.setBox(topBox);
316                 }
317                 else //remove the top and bottom borders
318
{
319                     JRBox topBottomBox = new JRBaseBox(this, false, false, true, true);
320                     boxTemplate.setBox(topBottomBox);
321                 }
322             }
323             
324             templatesMap.put(style, boxTemplate);
325         }
326         
327         return boxTemplate;
328     }
329
330     protected void resolveElement(JRPrintElement element, byte evaluation)
331     {
332         // nothing
333
}
334
335     public JRElement[] getElements()
336     {
337         return frameContainer.getElements();
338     }
339     
340     public List JavaDoc getChildren()
341     {
342         return frameContainer.getChildren();
343     }
344
345     public void collectExpressions(JRExpressionCollector collector)
346     {
347         collector.collect(this);
348     }
349
350     public JRChild getCopy(JRAbstractObjectFactory factory)
351     {
352         return factory.getFrame(this);
353     }
354
355     public void writeXml(JRXmlWriter writer) throws IOException JavaDoc
356     {
357         writer.writeFrame(this);
358     }
359
360     
361     public JRElement getElementByKey(String JavaDoc key)
362     {
363         return JRBaseElementGroup.getElementByKey(getElements(), key);
364     }
365
366     public JRCloneable createClone(JRFillCloneFactory factory)
367     {
368         return new JRFillFrame(this, factory);
369     }
370     
371
372     /**
373      * Frame element container filler.
374      */

375     protected class JRFillFrameElements extends JRFillElementContainer
376     {
377         JRFillFrameElements(JRFillObjectFactory factory)
378         {
379             super(JRFillFrame.this.filler, parentFrame, factory);
380             initElements();
381         }
382
383         JRFillFrameElements(JRFillFrameElements frameElements, JRFillCloneFactory factory)
384         {
385             super(frameElements, factory);
386             initElements();
387         }
388
389         protected int getContainerHeight()
390         {
391             return JRFillFrame.this.getHeight() - getTopPadding() - getBottomPadding();
392         }
393     }
394     
395     //box
396

397     public byte getBorder()
398     {
399         return JRStyleResolver.getBorder(this);
400     }
401
402     public Byte JavaDoc getOwnBorder()
403     {
404         return parentFrame.getOwnBorder();
405     }
406
407     public void setBorder(byte border)
408     {
409     }
410
411     public Color JavaDoc getBorderColor()
412     {
413         return JRStyleResolver.getBorderColor(this, getForecolor());
414     }
415
416     public Color JavaDoc getOwnBorderColor()
417     {
418         return parentFrame.getOwnBorderColor();
419     }
420
421     public void setBorderColor(Color JavaDoc borderColor)
422     {
423     }
424
425     public int getPadding()
426     {
427         return JRStyleResolver.getPadding(this);
428     }
429
430     public Integer JavaDoc getOwnPadding()
431     {
432         return parentFrame.getOwnPadding();
433     }
434
435     public void setPadding(int padding)
436     {
437     }
438
439     public byte getTopBorder()
440     {
441         return JRStyleResolver.getTopBorder(this);
442     }
443
444     public Byte JavaDoc getOwnTopBorder()
445     {
446         return parentFrame.getOwnTopBorder();
447     }
448
449     public void setTopBorder(byte topBorder)
450     {
451     }
452
453     public Color JavaDoc getTopBorderColor()
454     {
455         return JRStyleResolver.getTopBorderColor(this, getForecolor());
456     }
457
458     public Color JavaDoc getOwnTopBorderColor()
459     {
460         return parentFrame.getOwnTopBorderColor();
461     }
462
463     public void setTopBorderColor(Color JavaDoc topBorderColor)
464     {
465     }
466
467     public int getTopPadding()
468     {
469         return JRStyleResolver.getTopPadding(this);
470     }
471
472     public Integer JavaDoc getOwnTopPadding()
473     {
474         return parentFrame.getOwnTopPadding();
475     }
476
477     public void setTopPadding(int topPadding)
478     {
479     }
480
481     public byte getLeftBorder()
482     {
483         return JRStyleResolver.getLeftBorder(this);
484     }
485
486     public Byte JavaDoc getOwnLeftBorder()
487     {
488         return parentFrame.getOwnLeftBorder();
489     }
490
491     public void setLeftBorder(byte leftBorder)
492     {
493     }
494
495     public Color JavaDoc getLeftBorderColor()
496     {
497         return JRStyleResolver.getLeftBorderColor(this, getForecolor());
498     }
499
500     public Color JavaDoc getOwnLeftBorderColor()
501     {
502         return parentFrame.getOwnLeftBorderColor();
503     }
504
505     public void setLeftBorderColor(Color JavaDoc leftBorderColor)
506     {
507     }
508
509     public int getLeftPadding()
510     {
511         return JRStyleResolver.getLeftPadding(this);
512     }
513
514     public Integer JavaDoc getOwnLeftPadding()
515     {
516         return parentFrame.getOwnLeftPadding();
517     }
518
519     public void setLeftPadding(int leftPadding)
520     {
521     }
522
523     public byte getBottomBorder()
524     {
525         return JRStyleResolver.getBottomBorder(this);
526     }
527
528     public Byte JavaDoc getOwnBottomBorder()
529     {
530         return parentFrame.getOwnBottomBorder();
531     }
532
533     public void setBottomBorder(byte bottomBorder)
534     {
535     }
536
537     public Color JavaDoc getBottomBorderColor()
538     {
539         return JRStyleResolver.getBottomBorderColor(this, getForecolor());
540     }
541
542     public Color JavaDoc getOwnBottomBorderColor()
543     {
544         return parentFrame.getOwnBottomBorderColor();
545     }
546
547     public void setBottomBorderColor(Color JavaDoc bottomBorderColor)
548     {
549     }
550
551     public int getBottomPadding()
552     {
553         return JRStyleResolver.getBottomPadding(this);
554     }
555
556     public Integer JavaDoc getOwnBottomPadding()
557     {
558         return parentFrame.getOwnBottomPadding();
559     }
560
561     public void setBottomPadding(int bottomPadding)
562     {
563     }
564
565     public byte getRightBorder()
566     {
567         return JRStyleResolver.getRightBorder(this);
568     }
569
570     public Byte JavaDoc getOwnRightBorder()
571     {
572         return parentFrame.getOwnRightBorder();
573     }
574
575     public void setRightBorder(byte rightBorder)
576     {
577     }
578
579     public Color JavaDoc getRightBorderColor()
580     {
581         return JRStyleResolver.getRightBorderColor(this, getForecolor());
582     }
583
584     public Color JavaDoc getOwnRightBorderColor()
585     {
586         return parentFrame.getOwnRightBorderColor();
587     }
588
589     public void setRightBorderColor(Color JavaDoc rightBorderColor)
590     {
591     }
592
593     public int getRightPadding()
594     {
595         return JRStyleResolver.getRightPadding(this);
596     }
597
598     public Integer JavaDoc getOwnRightPadding()
599     {
600         return parentFrame.getOwnRightPadding();
601     }
602
603     public void setRightPadding(int rightPadding)
604     {
605     }
606
607     public void setBorder(Byte JavaDoc border)
608     {
609     }
610
611     public void setPadding(Integer JavaDoc padding)
612     {
613     }
614
615     public void setTopBorder(Byte JavaDoc topBorder)
616     {
617     }
618
619     public void setTopPadding(Integer JavaDoc topPadding)
620     {
621     }
622
623     public void setLeftBorder(Byte JavaDoc leftBorder)
624     {
625     }
626
627     public void setLeftPadding(Integer JavaDoc leftPadding)
628     {
629     }
630
631     public void setBottomBorder(Byte JavaDoc bottomBorder)
632     {
633     }
634
635     public void setBottomPadding(Integer JavaDoc bottomPadding)
636     {
637     }
638
639     public void setRightBorder(Byte JavaDoc rightBorder)
640     {
641     }
642
643     public void setRightPadding(Integer JavaDoc rightPadding)
644     {
645     }
646 }
647
Popular Tags