/
JRFillBand.java
676 lines (553 loc) · 13.8 KB
/
JRFillBand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2018 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.jasperreports.engine.fill;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jasperreports.engine.CommonReturnValue;
import net.sf.jasperreports.engine.ExpressionReturnValue;
import net.sf.jasperreports.engine.JRBand;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.JRGroup;
import net.sf.jasperreports.engine.JROrigin;
import net.sf.jasperreports.engine.JRPropertiesHolder;
import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.type.BandTypeEnum;
import net.sf.jasperreports.engine.type.PrintOrderEnum;
import net.sf.jasperreports.engine.type.SplitTypeEnum;
/**
* @author Teodor Danciu (teodord@users.sourceforge.net)
*/
public class JRFillBand extends JRFillElementContainer implements JRBand, JROriginProvider
{
private static final Log log = LogFactory.getLog(JRFillBand.class);
/**
*
*/
private JRBand parent;
private boolean isPrintWhenTrue = true;
/**
*
*/
private boolean isNewPageColumn;
private boolean isFirstWholeOnPageColumn;
private Map<JRGroup,Boolean> isNewGroupMap = new HashMap<JRGroup,Boolean>();
private Set<JREvaluationTime> nowEvaluationTimes;
// used by subreports to save values of variables used as return receptacles
// so that the values can be restored when the bands gets rewound
private Map<String,Object> savedVariableValues = new HashMap<String,Object>();
protected JROrigin origin;
private SplitTypeEnum splitType;
private Integer breakHeight;
private FillReturnValues returnValues;
private FillReturnValues.SourceContext returnValuesContext = new FillReturnValues.SourceContext()
{
@Override
public Object getValue(CommonReturnValue returnValue)
{
ExpressionReturnValue expressionReturnValue = (ExpressionReturnValue)returnValue;
Object value = null;
try
{
value = filler.evaluateExpression(expressionReturnValue.getExpression(), JRExpression.EVALUATION_DEFAULT);
}
catch (JRException e)
{
throw new JRRuntimeException(e);
}
return value;
}
@Override
public void check(CommonReturnValue returnValue) throws JRException
{
//FIXMERETURN check something
}
@Override
public JRFillVariable getToVariable(String name)
{
return filler.getVariable(name);
}
};
private Set<FillReturnValues> returnValuesSet;
/**
*
*/
protected JRFillBand(
JRBaseFiller filler,
JRBand band,
JRFillObjectFactory factory
)
{
super(filler, band, factory);
parent = band;
// we need to do this before setBand()
returnValuesSet = new LinkedHashSet<FillReturnValues>();
if (deepElements.length > 0)
{
for(int i = 0; i < deepElements.length; i++)
{
deepElements[i].setBand(this);
}
}
List<ExpressionReturnValue> expRetValues = getReturnValues();
returnValues =
new FillReturnValues(
expRetValues == null ? null : (ExpressionReturnValue[]) expRetValues.toArray(new ExpressionReturnValue[expRetValues.size()]), //FIXMERETURN make special class for constructor differentiation
factory,
filler
);
registerReturnValues(returnValues);
initElements();
initConditionalStyles();
nowEvaluationTimes = new HashSet<JREvaluationTime>();
}
@Override
public JROrigin getOrigin()
{
return origin;
}
/**
*
*/
protected void setOrigin(JROrigin origin)
{
if (log.isDebugEnabled())
{
log.debug("Origin " + origin + " for band " + getId());
}
this.origin = origin;
this.filler.getJasperPrint().addOrigin(origin);
}
/**
*
*/
protected void setNewPageColumn(boolean isNew)
{
this.isNewPageColumn = isNew;
}
/**
*
*/
protected boolean isNewPageColumn()
{
return isNewPageColumn;
}
/**
* Decides whether this band is the for whole band on the page/column.
*
* @return whether this band is the for whole band on the page/column
*/
protected boolean isFirstWholeOnPageColumn()
{
return isFirstWholeOnPageColumn;
}
/**
*
*/
protected void setNewGroup(JRGroup group, boolean isNew)
{
isNewGroupMap.put(group, isNew ? Boolean.TRUE : Boolean.FALSE);
}
/**
*
*/
protected boolean isNewGroup(JRGroup group)
{
Boolean value = isNewGroupMap.get(group);
if (value == null)
{
value = Boolean.FALSE;
}
return value.booleanValue();
}
@Override
public int getHeight()
{
return (parent == null ? 0 : parent.getHeight());
}
/**
*
*/
public int getBreakHeight()
{
// needs to be lazy calculated because it depends on splitType, which is itself lazy loaded
if (breakHeight == null)
{
breakHeight = getHeight();
if (
SplitTypeEnum.IMMEDIATE == getSplitTypeValue()
&& elements != null && elements.length > 0
)
{
for(int i = 0; i < elements.length; i++)
{
JRElement element = elements[i];
int bottom = element.getY() + element.getHeight();
breakHeight = bottom < breakHeight ? bottom : breakHeight;
}
}
}
return breakHeight;
}
@Override
public SplitTypeEnum getSplitTypeValue()
{
// needs to be lazy loaded because in JRFillBand constructor above, the filler.getMainDataset() is not yet set,
// when the band is a group band
if (splitType == null)
{
splitType = (parent == null ? null : parent.getSplitTypeValue());
if (splitType == null)
{
splitType =
SplitTypeEnum.getByName(
filler.getPropertiesUtil().getProperty(filler.getMainDataset(), JRBand.PROPERTY_SPLIT_TYPE)
);
}
}
return splitType;
}
@Override
public void setSplitType(SplitTypeEnum splitType)
{
throw new UnsupportedOperationException();
}
@Override
public JRExpression getPrintWhenExpression()
{
return (parent == null ? null : parent.getPrintWhenExpression());
}
/**
*
*/
protected boolean isSplitPrevented()
{
return SplitTypeEnum.PREVENT == getSplitTypeValue();
}
/**
*
*/
protected boolean isPrintWhenExpressionNull()
{
return (getPrintWhenExpression() == null);
}
/**
*
*/
protected boolean isPrintWhenTrue()
{
return isPrintWhenTrue;
}
/**
*
*/
protected void setPrintWhenTrue(boolean isPrintWhenTrue)
{
this.isPrintWhenTrue = isPrintWhenTrue;
}
/**
*
*/
protected boolean isToPrint()
{
return
this != filler.missingFillBand
&& (isPrintWhenExpressionNull()
|| (!isPrintWhenExpressionNull() && isPrintWhenTrue()));
}
/**
*
*/
protected void evaluatePrintWhenExpression(
byte evaluation
) throws JRException
{
boolean isPrintTrue = false;
JRExpression expression = getPrintWhenExpression();
if (expression != null)
{
Boolean printWhenExpressionValue = (Boolean)filler.evaluateExpression(expression, evaluation);
if (printWhenExpressionValue == null)
{
isPrintTrue = false;
}
else
{
isPrintTrue = printWhenExpressionValue.booleanValue();
}
}
setPrintWhenTrue(isPrintTrue);
}
/**
*
*/
protected JRPrintBand refill(
byte evaluation,
int availableHeight
) throws JRException
{
rewind();
restoreSavedVariables();
JRPrintBand printBand = null;
@SuppressWarnings("deprecation")
boolean isLegacyBandEvaluationEnabled = filler.getFillContext().isLegacyBandEvaluationEnabled();
if (isLegacyBandEvaluationEnabled)
{
printBand = fill(availableHeight);
}
else
{
evaluatePrintWhenExpression(evaluation);
if (isToPrint())
{
evaluate(evaluation);
printBand = fill(availableHeight);
}
else
{
printBand = new JRPrintBand();
}
}
return printBand;
}
/**
*
*/
protected JRPrintBand fill() throws JRException
{
return fill(getHeight(), false);
}
/**
*
*/
protected JRPrintBand fill(
int availableHeight
) throws JRException
{
return fill(availableHeight, true);
}
/**
*
*/
protected JRPrintBand fill(
int availableHeight,
boolean isOverflowAllowed
) throws JRException
{
filler.checkInterrupted();
filler.setBandOverFlowAllowed(isOverflowAllowed);
initFill();
if (isNewPageColumn && !isOverflow)
{
isFirstWholeOnPageColumn = true;
}
resetElements();
prepareElements(availableHeight, isOverflowAllowed);
if (isLegacyElementStretchEnabled())
{
stretchElements();
moveBandBottomElements();
removeBlankElements();
}
isFirstWholeOnPageColumn = isNewPageColumn && isOverflow;
isNewPageColumn = false;
isNewGroupMap = new HashMap<JRGroup,Boolean>();
JRPrintBand printBand = new JRPrintBand();
fillElements(printBand);
if (!willOverflow())
{
returnValues.copyValues(returnValuesContext);
}
return printBand;
}
protected boolean willOverflowWithElements()
{
return willOverflowWithElements;
}
@Override
protected int getContainerHeight()
{
return getHeight();
}
@Override
protected int getActualContainerHeight()
{
return getContainerHeight();
}
protected boolean isVariableUsedInReturns(String variableName)
{
boolean used = false;
for (FillReturnValues returnValues : returnValuesSet)
{
if (returnValues.usesForReturnValue(variableName))
{
used = true;
break;
}
}
return used;
}
protected void addNowEvaluationTime(JREvaluationTime evaluationTime)
{
nowEvaluationTimes.add(evaluationTime);
}
protected void addNowEvaluationTimes(JREvaluationTime[] evaluationTimes)
{
for (int i = 0; i < evaluationTimes.length; i++)
{
nowEvaluationTimes.add(evaluationTimes[i]);
}
}
protected boolean isNowEvaluationTime(JREvaluationTime evaluationTime)
{
return nowEvaluationTimes.contains(evaluationTime);
}
protected int getId()
{
//FIXME this is not necessarily unique
return System.identityHashCode(this);
}
@Override
protected void evaluate(byte evaluation) throws JRException
{
resetSavedVariables();
evaluateConditionalStyles(evaluation);
super.evaluate(evaluation);
}
protected void resetSavedVariables()
{
savedVariableValues.clear();
}
protected void saveVariable(String variableName)
{
if (!savedVariableValues.containsKey(variableName))
{
Object value = filler.getVariableValue(variableName);
savedVariableValues.put(variableName, value);
}
}
protected void restoreSavedVariables()
{
for (Iterator<Map.Entry<String,Object>> it = savedVariableValues.entrySet().iterator(); it.hasNext();)
{
Map.Entry<String,Object> entry = it.next();
String variableName = entry.getKey();
Object value = entry.getValue();
JRFillVariable variable = filler.getVariable(variableName);
variable.setOldValue(value);
variable.setValue(value);
variable.setIncrementedValue(value);
}
}
protected boolean isEmpty()
{
return this == filler.missingFillBand
|| (getHeight() == 0
&& (getElements() == null || getElements().length == 0)
&& getPrintWhenExpression() == null);
}
protected boolean isColumnBand()
{
BandTypeEnum bandType = origin.getBandTypeValue();
return
bandType == BandTypeEnum.GROUP_HEADER
|| bandType == BandTypeEnum.GROUP_FOOTER
|| bandType == BandTypeEnum.DETAIL;
}
protected boolean isPageBreakInhibited()
{
boolean isPageBreakInhibited = filler.isFirstPageBand && !atLeastOneElementIsToPrint;
if (isPageBreakInhibited && filler.isSubreport())
{
isPageBreakInhibited = filler.bandReportParent.isPageBreakInhibited();
}
return isPageBreakInhibited;
}
protected boolean isSplitTypePreventInhibited()
{
return isSplitTypePreventInhibited(true);
}
@Override
public boolean isSplitTypePreventInhibited(boolean isTopLevelCall)
{
boolean isSplitTypePreventInhibited = false;
if (
((filler.printOrder == PrintOrderEnum.VERTICAL && filler.isFirstColumnBand)
|| (filler.printOrder == PrintOrderEnum.HORIZONTAL && filler.isFirstPageBand))
&& (isTopLevelCall || !atLeastOneElementIsToPrint)
)
{
if (isColumnBand() && filler.columnIndex < filler.columnCount - 1)
{
isSplitTypePreventInhibited = true;
}
else
{
if (filler.isSubreport())
{
isSplitTypePreventInhibited = filler.bandReportParent.isSplitTypePreventInhibited(false);
}
else
{
isSplitTypePreventInhibited = true;
}
}
}
return isSplitTypePreventInhibited;
}
@Override
public boolean hasProperties()
{
return parent.hasProperties();
}
// not doing anything with the properties at fill time
@Override
public JRPropertiesMap getPropertiesMap()
{
return parent.getPropertiesMap();
}
@Override
public JRPropertiesHolder getParentProperties()
{
return null;
}
@Override
public List<ExpressionReturnValue> getReturnValues()
{
return parent == null ? null : parent.getReturnValues();
}
public void registerReturnValues(FillReturnValues fillReturnValues)
{
returnValuesSet.add(fillReturnValues);
}
}