1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.fill;
25
26 import net.sf.jasperreports.engine.JRException;
27 import net.sf.jasperreports.engine.JRExpressionCollector;
28 import net.sf.jasperreports.engine.JRPrintElement;
29 import net.sf.jasperreports.engine.JRStaticText;
30 import net.sf.jasperreports.engine.JRVisitor;
31
32
33 /**
34  * @author Teodor Danciu (teodord@users.sourceforge.net)
35  */

36 public class JRFillStaticText extends JRFillTextElement implements JRStaticText
37 {
38
39     private final String text;
40
41     /**
42      *
43      */

44     protected JRFillStaticText(
45             JRBaseFiller filler,
46             JRStaticText staticText, 
47             JRFillObjectFactory factory
48             )
49         {
50             super(filler, staticText, factory);
51             
52             String text = processMarkupText(staticText.getText());
53             if (text == null)
54             {
55                 text = "";
56             }
57             this.text = text;
58         }
59
60
61     protected JRFillStaticText(JRFillStaticText staticText, JRFillCloneFactory factory)
62     {
63         super(staticText, factory);
64
65         this.text = staticText.text;
66     }
67
68
69     @Override
70     public void setText(String text)
71     {
72     }
73
74
75     /**
76      *
77      */

78     protected JRTemplateText getJRTemplateText()
79     {
80         return (JRTemplateText) getElementTemplate();
81     }
82
83
84     @Override
85     protected JRTemplateElement createElementTemplate()
86     {
87         JRTemplateText template = new JRTemplateText(
88                 getElementOrigin(), 
89                 filler.getJasperPrint().getDefaultStyleProvider(), 
90                 this
91                 );
92         template.copyParagraph(getPrintParagraph());
93         template.copyLineBox(getPrintLineBox());
94         return template;
95     }
96
97
98     @Override
99     protected void evaluate(
100         byte evaluation
101         ) throws JRException
102     {
103         reset();
104         
105         evaluatePrintWhenExpression(evaluation);
106         evaluateProperties(evaluation);
107         evaluateStyle(evaluation);
108
109         //setting the text each time so that super.rewind() works fine
110         setRawText(this.text);
111         resetTextChunk();
112         
113         setValueRepeating(true);
114     }
115
116
117     @Override
118     protected boolean prepare(
119         int availableHeight,
120         boolean isOverflow
121         ) throws JRException
122     {
123         boolean willOverflow = false;
124
125         super.prepare(availableHeight, isOverflow);
126         
127         if (!isToPrint())
128         {
129             return willOverflow;
130         }
131         
132         boolean isToPrint = true;
133         boolean isReprinted = false;
134
135         if (isOverflow && isAlreadyPrinted() && !isPrintWhenDetailOverflows())
136         {
137             isToPrint = false;
138         }
139
140         if (
141             isToPrint && 
142             isPrintWhenExpressionNull() &&
143             !isPrintRepeatedValues()
144             )
145         {
146             if (
147                 ( !isPrintInFirstWholeBand() || !getBand().isFirstWholeOnPageColumn()) &&
148                 ( getPrintWhenGroupChanges() == null || !getBand().isNewGroup(getPrintWhenGroupChanges()) ) &&
149                 ( !isOverflow || !isPrintWhenDetailOverflows() )
150                 )
151             {
152                 isToPrint = false;
153             }
154         }
155
156         if (
157             isToPrint && 
158             availableHeight < getRelativeY() + getHeight()
159             )
160         {
161             isToPrint = false;
162             willOverflow = true;
163         }
164         
165         if (
166             isToPrint && 
167             isOverflow && 
168             //(isAlreadyPrinted() || !isPrintRepeatedValues())
169             (isPrintWhenDetailOverflows() && (isAlreadyPrinted() || (!isAlreadyPrinted() && !isPrintRepeatedValues())))
170             )
171         {
172             isReprinted = true;
173         }
174
175         resetTextChunk();
176
177         if (isToPrint)
178         {
179             chopTextElement(0);
180         }
181         
182         setToPrint(isToPrint);
183         setReprinted(isReprinted);
184         
185         return willOverflow;
186     }
187
188
189     @Override
190     protected JRPrintElement fill()
191     {
192         JRTemplatePrintText text = new JRTemplatePrintText(getJRTemplateText(), printElementOriginator);
193         text.setUUID(getUUID());
194         text.setX(getX());
195         text.setY(getRelativeY());
196         text.setWidth(getWidth());
197 //        if (getRotation() == ROTATION_NONE)
198 //        {
199             //text.setHeight(getPrintElementHeight());
200             text.setHeight(getStretchHeight());
201 //        }
202 //        else
203 //        {
204 //            text.setHeight(getHeight());
205 //        }
206         text.setRunDirection(getRunDirectionValue());
207         text.setLineSpacingFactor(getLineSpacingFactor());
208         text.setLeadingOffset(getLeadingOffset());
209         text.setTextHeight(getTextHeight());
210         transferProperties(text);
211
212         //text.setText(getRawText());
213         setPrintText(text);
214         
215         return text;
216     }
217
218
219     @Override
220     public void collectExpressions(JRExpressionCollector collector)
221     {
222         collector.collect(this);
223     }
224
225     @Override
226     public void visit(JRVisitor visitor)
227     {
228         visitor.visitStaticText(this);
229     }
230
231     
232     @Override
233     protected void resolveElement (JRPrintElement element, byte evaluation)
234     {
235         // nothing
236     }
237
238
239     @Override
240     public JRFillCloneable createClone(JRFillCloneFactory factory)
241     {
242         return new JRFillStaticText(this, factory);
243     }
244
245
246     @Override
247     protected boolean canOverflow()
248     {
249         return false;
250     }
251
252
253     @Override
254     protected boolean scaleFontToFit()
255     {
256         return false;
257     }
258
259
260     @Override
261     public String getText()
262     {
263         return ((JRStaticText) parent).getText();
264     }
265
266 }
267