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 package ar.com.fdvs.dj.core.layout;
31
32 import ar.com.fdvs.dj.core.DJException;
33 import ar.com.fdvs.dj.domain.*;
34 import ar.com.fdvs.dj.domain.builders.DataSetFactory;
35 import ar.com.fdvs.dj.domain.constants.Transparency;
36 import ar.com.fdvs.dj.domain.entities.DJColSpan;
37 import ar.com.fdvs.dj.domain.entities.DJGroup;
38 import ar.com.fdvs.dj.domain.entities.columns.*;
39 import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionalStyle;
40 import ar.com.fdvs.dj.util.ExpressionUtils;
41 import ar.com.fdvs.dj.util.HyperLinkUtil;
42 import ar.com.fdvs.dj.util.LayoutUtils;
43 import ar.com.fdvs.dj.util.Utils;
44 import net.sf.jasperreports.charts.design.JRDesignBarPlot;
45 import net.sf.jasperreports.engine.*;
46 import net.sf.jasperreports.engine.base.JRBaseChartPlot;
47 import net.sf.jasperreports.engine.base.JRBaseVariable;
48 import net.sf.jasperreports.engine.design.*;
49 import net.sf.jasperreports.engine.type.*;
50 import net.sf.jasperreports.engine.util.JRExpressionUtil;
51 import org.apache.commons.collections.CollectionUtils;
52 import org.apache.commons.collections.MultiHashMap;
53 import org.apache.commons.collections.MultiMap;
54 import org.apache.commons.collections.Predicate;
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57
58 import java.awt.*;
59 import java.util.*;
60 import java.util.List;
61
62
63
64
65
66
67
68
69
70 public abstract class AbstractLayoutManager implements LayoutManager {
71
72 static final Log log = LogFactory.getLog(AbstractLayoutManager.class);
73 protected static final String EXPRESSION_TRUE_WHEN_ODD = "new java.lang.Boolean(((Number)$V{REPORT_COUNT}).doubleValue() % 2 == 0)";
74 protected static final String EXPRESSION_TRUE_WHEN_EVEN = "new java.lang.Boolean(((Number)$V{REPORT_COUNT}).doubleValue() % 2 != 0)";
75
76 JasperDesign design;
77 private DynamicReport report;
78
79 protected abstract void transformDetailBandTextField(AbstractColumn column, JRDesignTextField textField);
80
81 private HashMap reportStyles = new HashMap();
82
83
84
85
86
87
88 protected List realGroups = new ArrayList();
89
90 public HashMap getReportStyles() {
91 return reportStyles;
92 }
93
94 public void setReportStyles(HashMap reportStyles) {
95 this.reportStyles = reportStyles;
96 }
97
98 public void applyLayout(JasperDesign design, DynamicReport report) throws LayoutException {
99 log.debug("Applying Layout...");
100 try {
101 setDesign(design);
102 setReport(report);
103 ensureDJStyles();
104 startLayout();
105 transformDetailBand();
106 endLayout();
107 setWhenNoDataBand();
108 setBandsFinalHeight();
109 registerRemainingStyles();
110 } catch (RuntimeException e) {
111 throw new LayoutException(e.getMessage(),e);
112 }
113 }
114
115
116
117
118
119 protected void setWhenNoDataBand() {
120 log.debug("setting up WHEN NO DATA band");
121 String whenNoDataText = getReport().getWhenNoDataText();
122 Style style = getReport().getWhenNoDataStyle();
123 if (whenNoDataText == null || "".equals(whenNoDataText))
124 return;
125 JRDesignBand band = new JRDesignBand();
126 getDesign().setNoData(band);
127
128 JRDesignTextField text = new JRDesignTextField();
129 JRDesignExpression expression = ExpressionUtils.createStringExpression("\""+whenNoDataText+"\"");
130 text.setExpression(expression);
131
132 if (style == null){
133 style = getReport().getOptions().getDefaultDetailStyle();
134 }
135
136 if (getReport().isWhenNoDataShowTitle()){
137 LayoutUtils.copyBandElements(band, getDesign().getTitle());
138 LayoutUtils.copyBandElements(band, getDesign().getPageHeader());
139 }
140 if (getReport().isWhenNoDataShowColumnHeader())
141 LayoutUtils.copyBandElements(band, getDesign().getColumnHeader());
142
143 int offset = LayoutUtils.findVerticalOffset(band);
144 text.setY(offset);
145 applyStyleToElement(style, text);
146 text.setWidth(getReport().getOptions().getPrintableWidth());
147 text.setHeight(50);
148 band.addElement(text);
149 log.debug("OK setting up WHEN NO DATA band");
150
151 }
152
153 protected void startLayout() {
154 setColumnsFinalWidth();
155 realGroups.addAll(getDesign().getGroupsList());
156 }
157
158 protected void endLayout() {
159 layoutCharts();
160 setBandsFinalHeight();
161 }
162
163 protected void registerRemainingStyles() {
164
165
166 }
167
168
169
170
171
172 protected void ensureDJStyles() {
173
174 for (Iterator iterator = getReport().getStyles().values().iterator(); iterator.hasNext();) {
175 Style style = (Style) iterator.next();
176 addStyleToDesign(style);
177 }
178
179 Style defaultDetailStyle = getReport().getOptions().getDefaultDetailStyle();
180
181 Style defaultHeaderStyle = getReport().getOptions().getDefaultHeaderStyle();
182 for (Iterator iter = report.getColumns().iterator(); iter.hasNext();) {
183 AbstractColumn column = (AbstractColumn) iter.next();
184 if (column.getStyle() == null)
185 column.setStyle(defaultDetailStyle);
186 if (column.getHeaderStyle() == null)
187 column.setHeaderStyle(defaultHeaderStyle);
188 }
189 }
190
191
192
193
194
195 public void addStyleToDesign(Style style) {
196 JRDesignStyle jrstyle = style.transform();
197 try {
198 if (jrstyle.getName() == null) {
199 String name = createUniqueStyleName();
200 jrstyle.setName(name);
201 style.setName(name);
202 getReportStyles().put(name, jrstyle);
203 design.addStyle(jrstyle);
204 }
205
206 JRStyle old = (JRStyle) design.getStylesMap().get(jrstyle.getName());
207 if (old != null && style.isOverridesExistingStyle()){
208 log.debug("Overriding style with name \""+ style.getName() +"\"");
209
210 design.removeStyle(style.getName());
211 design.addStyle(jrstyle);
212 } else if (old == null){
213 log.debug("Registering new style with name \""+ style.getName() +"\"");
214 design.addStyle(jrstyle);
215 } else {
216 if (style.getName() != null)
217 log.debug("Using existing style for style with name \""+ style.getName() +"\"");
218 }
219 } catch (JRException e) {
220 log.debug("Duplicated style (it's ok): " + e.getMessage());
221 }
222 }
223
224 protected String createUniqueStyleName() {
225 synchronized (this) {
226 int counter = getReportStyles().values().size() + 1;
227 String tryName = "dj_style_" + counter + "_";
228 while (design.getStylesMap().get(tryName) != null){
229 counter++;
230 tryName = "dj_style_" + counter;
231 }
232 return tryName;
233 }
234 }
235
236
237
238
239 protected void transformDetailBand() {
240 log.debug("transforming Detail Band...");
241
242 JRDesignSection detailSection = (JRDesignSection) design.getDetailSection();
243
244
245 JRDesignBand detail = null;
246 if (detailSection.getBandsList().isEmpty()){
247 detail = new JRDesignBand();
248 detailSection.getBandsList().add(detail);
249 } else {
250 detail = (JRDesignBand) detailSection.getBandsList().iterator().next();
251 }
252
253 detail.setHeight(report.getOptions().getDetailHeight().intValue());
254
255 for (Iterator iter = getVisibleColumns().iterator(); iter.hasNext();) {
256
257 AbstractColumn column = (AbstractColumn)iter.next();
258
259
260
261
262 if (column instanceof BarCodeColumn) {
263 BarCodeColumn barcodeColumn = (BarCodeColumn)column;
264 JRDesignImage image = new JRDesignImage(new JRDesignStyle().getDefaultStyleProvider());
265 JRDesignExpression imageExp = new JRDesignExpression();
266
267
268
269
270 String applicationIdentifier = barcodeColumn.getApplicationIdentifier();
271 if (applicationIdentifier != null && !"".equals(applicationIdentifier.trim()) ){
272 applicationIdentifier = "$F{" + applicationIdentifier + "}";
273 } else {
274 applicationIdentifier = "\"\"";
275 }
276 imageExp.setText("ar.com.fdvs.dj.core.BarcodeHelper.getBarcodeImage("+barcodeColumn.getBarcodeType() + ", "+ column.getTextForExpression()+ ", "+ barcodeColumn.isShowText() + ", " + barcodeColumn.isCheckSum() + ", " + applicationIdentifier + ",0,0 )" );
277
278
279 imageExp.setValueClass(java.awt.Image.class);
280 image.setExpression(imageExp);
281 image.setHeight(getReport().getOptions().getDetailHeight().intValue());
282 image.setWidth(column.getWidth().intValue());
283 image.setX(column.getPosX().intValue());
284 image.setScaleImage(ScaleImageEnum.getByValue(barcodeColumn.getScaleMode().getValue()));
285
286 image.setOnErrorType(OnErrorTypeEnum.ICON );
287
288 if (column.getLink() != null) {
289 String name = "column_" + getReport().getColumns().indexOf(column);
290 HyperLinkUtil.applyHyperLinkToElement((DynamicJasperDesign) getDesign(), column.getLink(),image,name);
291 }
292
293 applyStyleToElement(column.getStyle(), image);
294
295 detail.addElement(image);
296 }
297
298
299
300 else if (column instanceof ImageColumn) {
301 ImageColumn imageColumn = (ImageColumn)column;
302 JRDesignImage image = new JRDesignImage(new JRDesignStyle().getDefaultStyleProvider());
303 JRDesignExpression imageExp = new JRDesignExpression();
304 imageExp.setText(column.getTextForExpression());
305
306 imageExp.setValueClassName(imageColumn.getValueClassNameForExpression());
307 image.setExpression(imageExp);
308 image.setHeight(getReport().getOptions().getDetailHeight().intValue());
309 image.setWidth(column.getWidth().intValue());
310 image.setX(column.getPosX().intValue());
311 image.setScaleImage(ScaleImageEnum.getByValue(imageColumn.getScaleMode().getValue()));
312
313 applyStyleToElement(column.getStyle(), image);
314
315 if (column.getLink() != null) {
316 String name = "column_" + getReport().getColumns().indexOf(column);
317 HyperLinkUtil.applyHyperLinkToElement((DynamicJasperDesign) getDesign(),column.getLink(), image,name);
318 }
319
320 detail.addElement(image);
321 }
322
323
324
325 else {
326 if (getReport().getOptions().isShowDetailBand()){
327 JRDesignTextField textField = generateTextFieldFromColumn(column, getReport().getOptions().getDetailHeight().intValue(), null);
328
329 if (column.getLink() != null) {
330 String name = getDesign().getName() + "_column_" + getReport().getColumns().indexOf(column);
331 HyperLinkUtil.applyHyperLinkToElement((DynamicJasperDesign) getDesign(),column.getLink(),textField,name);
332 }
333
334 transformDetailBandTextField(column, textField);
335
336 if (textField.getExpression() != null)
337 detail.addElement(textField);
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 protected void generateHeaderBand(JRDesignBand band) {
376 log.debug("Adding column names in header band.");
377 band.setHeight(report.getOptions().getHeaderHeight());
378
379 for (AbstractColumn col : getVisibleColumns()) {
380
381 if (col.getTitle() == null)
382 continue;
383
384 Style headerStyle = col.getHeaderStyle();
385 if (headerStyle == null)
386 headerStyle = report.getOptions().getDefaultHeaderStyle();
387
388 this.generateColspanHeader(col,band);
389
390 JRDesignExpression expression = new JRDesignExpression();
391 JRDesignTextField textField = new JRDesignTextField();
392 expression.setText("\"" + col.getTitle() + "\"");
393
394
395 if (col.getHeaderMarkup() != null)
396 textField.setMarkup(col.getHeaderMarkup().toLowerCase());
397
398 expression.setValueClass(String.class);
399
400 textField.setKey("header_" + col.getTitle());
401 textField.setExpression(expression);
402
403 if (col.hasParentCol()) {
404 textField.setY(col.getPosY() + band.getHeight() / 2);
405 textField.setHeight(band.getHeight() / 2);
406
407 } else {
408 textField.setY(col.getPosY());
409 textField.setHeight(band.getHeight());
410 }
411
412 textField.setX(col.getPosX().intValue());
413 textField.setWidth(col.getWidth().intValue());
414
415 textField.setPrintWhenDetailOverflows(true);
416 textField.setBlankWhenNull(true);
417
418 applyStyleToElement(headerStyle, textField);
419 band.addElement(textField);
420 }
421 }
422
423 private void generateColspanHeader(AbstractColumn col,JRDesignBand band) {
424
425 DJColSpan colSpan = col.getColSpan();
426 if (colSpan != null && colSpan.isFirstColum(col)) {
427
428 JRDesignTextField spanTitle = new JRDesignTextField();
429 JRDesignExpression colspanExpression = new JRDesignExpression();
430 colspanExpression.setValueClassName(String.class.getName());
431 colspanExpression.setText("\"" + col.getColSpan().getTitle() + "\"");
432
433 spanTitle.setExpression(colspanExpression);
434 spanTitle.setKey("colspan-header" + col.getTitle());
435
436 spanTitle.setX(col.getPosX().intValue());
437 spanTitle.setY(col.getPosY());
438 spanTitle.setHeight(band.getHeight() / 2);
439 spanTitle.setWidth(colSpan.getWidth());
440
441 Style spanStyle = colSpan.getColspanHeaderStyle();
442
443 if (spanStyle == null) {
444 spanStyle = report.getOptions().getDefaultHeaderStyle();
445 }
446
447 applyStyleToElement(spanStyle, spanTitle);
448 band.addElement(spanTitle);
449 }
450 }
451
452
453
454
455
456
457
458
459
460
461 public void applyStyleToElement(Style style, JRDesignElement designElemen) {
462 if (style == null){
463
464 JRDesignStyle style_ = new JRDesignStyle();
465 style_.setName( createUniqueStyleName());
466 designElemen.setStyle(style_);
467 try {
468 getDesign().addStyle(style_);
469 } catch (JRException e) {
470
471 }
472
473 return;
474 }
475 boolean existsInDesign = style.getName() != null
476 && design.getStylesMap().get(style.getName()) != null;
477
478
479 JRDesignStyle jrstyle = null;
480
481 if (existsInDesign && !style.isOverridesExistingStyle()){
482 jrstyle = (JRDesignStyle) design.getStylesMap().get(style.getName());
483 } else {
484 addStyleToDesign(style);
485 jrstyle = style.transform();
486 }
487
488 designElemen.setStyle(jrstyle);
489 if (designElemen instanceof JRDesignTextElement ) {
490 JRDesignTextElement textField = (JRDesignTextElement) designElemen;
491 if (style.getStreching() != null)
492 textField.setStretchType(StretchTypeEnum.getByValue( style.getStreching().getValue() ));
493 textField.setPositionType(PositionTypeEnum.FLOAT);
494
495 }
496 if (designElemen instanceof JRDesignTextField ) {
497 JRDesignTextField textField = (JRDesignTextField) designElemen;
498 textField.setStretchWithOverflow(style.isStretchWithOverflow());
499
500 if (!textField.isBlankWhenNull() && style.isBlankWhenNull())
501 textField.setBlankWhenNull(true);
502 }
503
504 if (designElemen instanceof JRDesignGraphicElement) {
505 JRDesignGraphicElement graphicElement = (JRDesignGraphicElement) designElemen;
506 graphicElement.setStretchType(StretchTypeEnum.getByValue(style.getStreching().getValue()));
507 graphicElement.setPositionType(PositionTypeEnum.FLOAT);
508 }
509 }
510
511
512
513
514
515
516
517 protected void setColumnsFinalWidth() {
518 log.debug("Setting columns final width.");
519 float factor = 1;
520 int printableArea = report.getOptions().getColumnWidth();
521
522
523 List visibleColums = getVisibleColumns();
524
525
526
527 if (report.getOptions().isUseFullPageWidth()) {
528 int columnsWidth = 0;
529 int notRezisableWidth = 0;
530
531
532 for (Iterator iterator = visibleColums.iterator(); iterator.hasNext();) {
533 AbstractColumn col = (AbstractColumn) iterator.next();
534 columnsWidth += col.getWidth().intValue();
535 if (col.getFixedWidth().booleanValue())
536 notRezisableWidth += col.getWidth().intValue();
537 }
538
539
540 factor = (float) (printableArea-notRezisableWidth) / (float) (columnsWidth-notRezisableWidth);
541
542 log.debug("printableArea = " + printableArea
543 + ", columnsWidth = "+ columnsWidth
544 + ", columnsWidth = "+ columnsWidth
545 + ", notRezisableWidth = "+ notRezisableWidth
546 + ", factor = "+ factor);
547
548 int acumulated = 0;
549 int colFinalWidth = 0;
550
551
552 Collection resizableColumns = CollectionUtils.select( visibleColums,new Predicate() {
553 public boolean evaluate(Object arg0) {
554 return !((AbstractColumn)arg0).getFixedWidth().booleanValue();
555 }
556
557 }) ;
558
559
560 for (Iterator iter = resizableColumns.iterator(); iter.hasNext();) {
561 AbstractColumn col = (AbstractColumn) iter.next();
562
563 if (!iter.hasNext()) {
564 col.setWidth(new Integer(printableArea - notRezisableWidth - acumulated));
565 } else {
566 colFinalWidth = (new Float(col.getWidth().intValue() * factor)).intValue();
567 acumulated += colFinalWidth;
568 col.setWidth(new Integer(colFinalWidth));
569 }
570 }
571 }
572
573
574 int posx = 0;
575 for (Iterator iterator = visibleColums.iterator(); iterator.hasNext();) {
576 AbstractColumn col = (AbstractColumn) iterator.next();
577 col.setPosX(new Integer(posx));
578 posx += col.getWidth().intValue();
579 }
580 }
581
582
583
584
585 protected List<AbstractColumn> getVisibleColumns() {
586 return new ArrayList<AbstractColumn>(report.getColumns());
587 }
588
589
590
591
592 protected void setBandsFinalHeight() {
593 log.debug("Setting bands final height...");
594
595 List<JRBand> bands = new ArrayList<JRBand>();
596
597 Utils.addNotNull(bands, (JRDesignBand) design.getPageHeader());
598 Utils.addNotNull(bands, (JRDesignBand) design.getPageFooter());
599 Utils.addNotNull(bands, (JRDesignBand) design.getColumnHeader());
600 Utils.addNotNull(bands, (JRDesignBand) design.getColumnFooter());
601 Utils.addNotNull(bands, (JRDesignBand) design.getSummary());
602 Utils.addNotNull(bands, (JRDesignBand) design.getBackground());
603 bands.addAll(((JRDesignSection) design.getDetailSection()).getBandsList());
604 Utils.addNotNull(bands, (JRDesignBand) design.getLastPageFooter());
605 Utils.addNotNull(bands, (JRDesignBand) design.getTitle());
606 Utils.addNotNull(bands, (JRDesignBand) design.getPageFooter());
607 Utils.addNotNull(bands, (JRDesignBand) design.getNoData());
608
609 for (Iterator iter = design.getGroupsList().iterator(); iter.hasNext();) {
610 JRGroup jrgroup = (JRGroup) iter.next();
611 DJGroup djGroup = (DJGroup) getReferencesMap().get(jrgroup.getName());
612 JRDesignSection headerSection = (JRDesignSection) jrgroup.getGroupHeaderSection();
613 JRDesignSection footerSection = (JRDesignSection) jrgroup.getGroupFooterSection();
614 if (djGroup != null){
615 for (JRBand headerBand : (List<JRBand>)headerSection.getBandsList()) {
616 setBandFinalHeight((JRDesignBand) headerBand,djGroup.getHeaderHeight(), djGroup.isFitHeaderHeightToContent());
617
618 }
619 for (JRBand footerBand : (List<JRBand>)footerSection.getBandsList()) {
620 setBandFinalHeight((JRDesignBand) footerBand,djGroup.getFooterHeight(), djGroup.isFitFooterHeightToContent());
621
622 }
623 } else {
624 bands.addAll(headerSection.getBandsList());
625 bands.addAll(footerSection.getBandsList());
626 }
627 }
628
629 for (JRBand jrDesignBand : bands) {
630 setBandFinalHeight((JRDesignBand)jrDesignBand);
631 }
632 }
633
634
635
636
637
638
639
640
641 private void setBandFinalHeight(JRDesignBand band, int currHeigth, boolean fitToContent) {
642 if (band != null) {
643 int finalHeight = LayoutUtils.findVerticalOffset(band);
644 if (finalHeight < currHeigth && !fitToContent){
645
646 } else {
647 band.setHeight(finalHeight);
648 }
649 }
650
651 }
652
653
654
655
656
657 protected void setBandFinalHeight(JRDesignBand band) {
658 if (band != null) {
659 int finalHeight = LayoutUtils.findVerticalOffset(band);
660 band.setHeight(finalHeight);
661 }
662 }
663
664
665
666
667
668
669
670
671 protected JRDesignTextField generateTextFieldFromColumn(AbstractColumn col, int height, DJGroup group) {
672 JRDesignTextField textField = new JRDesignTextField();
673 JRDesignExpression exp = new JRDesignExpression();
674
675 if (col.getPattern() != null && "".equals(col.getPattern().trim())) {
676 textField.setPattern(col.getPattern());
677 }
678
679 if (col.getTruncateSuffix() != null){
680 textField.getPropertiesMap().setProperty(JRTextElement.PROPERTY_TRUNCATE_SUFFIX, col.getTruncateSuffix());
681 }
682
683 List columnsGroups = getReport().getColumnsGroups();
684 if (col instanceof PercentageColumn) {
685 PercentageColumn pcol = (PercentageColumn) col;
686
687 if (group==null) {
688 DJGroup innerMostGroup = (DJGroup) columnsGroups.get(columnsGroups.size()-1);
689 exp.setText(pcol.getTextForExpression(innerMostGroup));
690 } else {
691 exp.setText(pcol.getTextForExpression(group));
692 }
693
694 textField.setEvaluationTime(EvaluationTimeEnum.AUTO);
695 } else {
696 exp.setText(col.getTextForExpression());
697
698 }
699
700 exp.setValueClassName(col.getValueClassNameForExpression());
701 textField.setExpression(exp);
702 textField.setWidth(col.getWidth().intValue());
703 textField.setX(col.getPosX().intValue());
704 textField.setY(col.getPosY().intValue());
705 textField.setHeight(height);
706
707 textField.setBlankWhenNull(col.getBlankWhenNull());
708
709 textField.setPattern(col.getPattern());
710
711 if (col.getMarkup() != null)
712 textField.setMarkup(col.getMarkup().toLowerCase());
713
714 textField.setPrintRepeatedValues(col.getPrintRepeatedValues().booleanValue());
715
716 textField.setPrintWhenDetailOverflows(true);
717
718 Style columnStyle = col.getStyle();
719 if (columnStyle == null)
720 columnStyle = report.getOptions().getDefaultDetailStyle();
721
722 applyStyleToElement(columnStyle, textField);
723 JRDesignStyle jrstyle = (JRDesignStyle) textField.getStyle();
724
725 if (group != null) {
726 int index = columnsGroups.indexOf(group);
727
728 JRDesignGroup previousGroup = getJRGroupFromDJGroup(group);
729 textField.setPrintWhenGroupChanges(previousGroup);
730
731
732
733
734
735
736
737 JRDesignStyle groupStyle = Utils.cloneStyle(jrstyle);
738
739 groupStyle.setName(groupStyle.getFontName() +"_for_group_"+index + "_");
740 textField.setStyle(groupStyle);
741 try {
742 design.addStyle(groupStyle);
743 } catch (JRException e) { }
744
745 } else {
746
747 JRDesignStyle alternateStyle = Utils.cloneStyle(jrstyle);
748
749 alternateStyle.setName(alternateStyle.getFontName() +"_for_column_"+col.getName() + "_");
750 alternateStyle.getConditionalStyleList().clear();
751 textField.setStyle(alternateStyle);
752 try {
753 design.addStyle(alternateStyle);
754 } catch (JRException e) { }
755
756
757 setUpConditionStyles(alternateStyle, col );
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775 }
776 return textField;
777 }
778
779
780
781
782
783
784 private void setUpConditionStyles(JRDesignStyle jrstyle, AbstractColumn column) {
785
786 if (getReport().getOptions().isPrintBackgroundOnOddRows() && Utils.isEmpty(column.getConditionalStyles())){
787 JRDesignExpression expression = new JRDesignExpression();
788 expression.setValueClass(Boolean.class);
789 expression.setText(EXPRESSION_TRUE_WHEN_ODD);
790
791 Style oddRowBackgroundStyle = getReport().getOptions().getOddRowBackgroundStyle();
792
793 JRDesignConditionalStyle condStyle = new JRDesignConditionalStyle();
794 condStyle.setBackcolor(oddRowBackgroundStyle.getBackgroundColor());
795 condStyle.setMode(ModeEnum.OPAQUE );
796
797 condStyle.setConditionExpression(expression);
798 jrstyle.addConditionalStyle(condStyle);
799
800 return;
801 }
802
803 if (Utils.isEmpty(column.getConditionalStyles()))
804 return;
805
806 for (Iterator iterator = column.getConditionalStyles().iterator(); iterator.hasNext();) {
807 ConditionalStyle condition = (ConditionalStyle) iterator.next();
808
809 if (getReport().getOptions().isPrintBackgroundOnOddRows()
810 && Transparency.TRANSPARENT == condition.getStyle().getTransparency() ){
811
812 JRDesignExpression expressionForConditionalStyle = ExpressionUtils.getExpressionForConditionalStyle(condition, column.getTextForExpression());
813 String expStr = JRExpressionUtil.getExpressionText(expressionForConditionalStyle);
814
815
816 JRDesignExpression expressionOdd = new JRDesignExpression();
817 expressionOdd.setValueClass(Boolean.class);
818 expressionOdd.setText("new java.lang.Boolean(" +EXPRESSION_TRUE_WHEN_ODD+".booleanValue() && ((java.lang.Boolean)" + expStr + ").booleanValue() )");
819
820 Style oddRowBackgroundStyle = getReport().getOptions().getOddRowBackgroundStyle();
821
822 JRDesignConditionalStyle condStyleOdd = makeConditionalStyle( condition.getStyle());
823
824 condStyleOdd.setBackcolor(oddRowBackgroundStyle.getBackgroundColor());
825 condStyleOdd.setMode( ModeEnum.OPAQUE );
826 condStyleOdd.setConditionExpression(expressionOdd);
827 jrstyle.addConditionalStyle(condStyleOdd);
828
829
830 JRDesignExpression expressionEven = new JRDesignExpression();
831 expressionEven.setValueClass(Boolean.class);
832 expressionEven.setText("new java.lang.Boolean(" +EXPRESSION_TRUE_WHEN_EVEN+".booleanValue() && ((java.lang.Boolean)" + expStr + ").booleanValue() )");
833
834 JRDesignConditionalStyle condStyleEven = makeConditionalStyle( condition.getStyle());
835 condStyleEven.setConditionExpression(expressionEven);
836 jrstyle.addConditionalStyle(condStyleEven);
837
838 } else {
839 JRDesignExpression expression = ExpressionUtils.getExpressionForConditionalStyle(condition, column.getTextForExpression());
840 JRDesignConditionalStyle condStyle = makeConditionalStyle( condition.getStyle());
841 condStyle.setConditionExpression(expression);
842 jrstyle.addConditionalStyle(condStyle);
843 }
844 }
845
846
847
848 if (getReport().getOptions().isPrintBackgroundOnOddRows() ){
849
850 JRDesignExpression expressionOdd = new JRDesignExpression();
851 expressionOdd.setValueClass(Boolean.class);
852 expressionOdd.setText(EXPRESSION_TRUE_WHEN_ODD);
853
854 Style oddRowBackgroundStyle = getReport().getOptions().getOddRowBackgroundStyle();
855
856 JRDesignConditionalStyle condStyleOdd = new JRDesignConditionalStyle();
857 condStyleOdd.setBackcolor(oddRowBackgroundStyle.getBackgroundColor());
858 condStyleOdd.setMode( ModeEnum.OPAQUE );
859 condStyleOdd.setConditionExpression(expressionOdd);
860
861 jrstyle.addConditionalStyle(condStyleOdd);
862
863
864 JRDesignExpression expressionEven = new JRDesignExpression();
865 expressionEven.setValueClass(Boolean.class);
866 expressionEven.setText(EXPRESSION_TRUE_WHEN_EVEN);
867
868 JRDesignConditionalStyle condStyleEven = new JRDesignConditionalStyle();
869 condStyleEven.setBackcolor(jrstyle.getBackcolor());
870 condStyleEven.setMode( jrstyle.getModeValue() );
871 condStyleEven.setConditionExpression(expressionEven);
872
873 jrstyle.addConditionalStyle(condStyleEven);
874 }
875 }
876
877
878 protected JRDesignConditionalStyle makeConditionalStyle( Style style ) {
879 JRDesignConditionalStyle condStyle = style.transformAsConditinalStyle();
880 return condStyle;
881 }
882
883
884
885
886 protected void layoutCharts() {
887
888 MultiMap mmap = new MultiHashMap();
889 for (Iterator iter = getReport().getCharts().iterator(); iter.hasNext();) {
890 DJChart djChart = (DJChart) iter.next();
891 mmap.put(djChart.getColumnsGroup(), djChart);
892 }
893
894 for (Iterator iterator = mmap.keySet().iterator(); iterator.hasNext();) {
895 Object key = iterator.next();
896 Collection charts = (Collection) mmap.get(key);
897 ArrayList l = new ArrayList(charts);
898
899 for (int i = l.size(); i > 0; i--) {
900 DJChart djChart = (DJChart) l.get(i-1);
901 JRDesignChart chart = createChart(djChart);
902
903
904 JRDesignBand band = createGroupForChartAndGetBand(djChart);
905 band.addElement(chart);
906 }
907 }
908
909
910 mmap = new MultiHashMap();
911 for (Iterator iter = getReport().getNewCharts().iterator(); iter.hasNext();) {
912 ar.com.fdvs.dj.domain.chart.DJChart djChart = (ar.com.fdvs.dj.domain.chart.DJChart) iter.next();
913 mmap.put(djChart.getDataset().getColumnsGroup(), djChart);
914 }
915
916 for (Iterator iterator = mmap.keySet().iterator(); iterator.hasNext();) {
917 Object key = iterator.next();
918 Collection charts = (Collection) mmap.get(key);
919 ArrayList l = new ArrayList(charts);
920
921 for (int i = l.size(); i > 0; i--) {
922 ar.com.fdvs.dj.domain.chart.DJChart djChart = (ar.com.fdvs.dj.domain.chart.DJChart) l.get(i-1);
923 String name = "chart_" + (i-1);
924 JRDesignChart chart = createChart(djChart, name);
925
926 if (djChart.getLink() != null)
927 HyperLinkUtil.applyHyperLinkToElement((DynamicJasperDesign) getDesign(), djChart.getLink(), chart, name + "_hyperlink");
928
929
930 JRDesignBand band = createGroupForChartAndGetBand(djChart);
931 band.addElement(chart);
932 }
933 }
934 }
935
936 protected JRDesignBand createGroupForChartAndGetBand(DJChart djChart) {
937 JRDesignGroup jrGroup = getJRGroupFromDJGroup(djChart.getColumnsGroup());
938 JRDesignGroup parentGroup = getParent(jrGroup);
939 JRDesignGroup jrGroupChart = null;
940 try {
941
942 jrGroupChart = new JRDesignGroup();
943 jrGroupChart.setExpression(parentGroup.getExpression());
944 ((JRDesignSection)jrGroupChart.getGroupFooterSection()).addBand(new JRDesignBand());
945 ((JRDesignSection)jrGroupChart.getGroupHeaderSection()).addBand(new JRDesignBand());
946 jrGroupChart.setName(jrGroupChart.getName()+"_Chart" + getReport().getCharts().indexOf(djChart));
947 } catch (Exception e) {
948 throw new DJException("Problem creating band for chart: " + e.getMessage(),e);
949 }
950
951
952
953
954
955 if (jrGroup.equals(parentGroup)){
956 jrGroupChart.setExpression(ExpressionUtils.createStringExpression("\"dummy_for_chart\""));
957 getDesign().getGroupsList().add( getDesign().getGroupsList().indexOf(jrGroup) , jrGroupChart);
958 } else {
959 int index = getDesign().getGroupsList().indexOf(parentGroup);
960 getDesign().getGroupsList().add(index, jrGroupChart);
961 }
962
963 JRDesignBand band = null;
964 switch (djChart.getOptions().getPosition()) {
965 case DJChartOptions.POSITION_HEADER:
966 band = (JRDesignBand) ((JRDesignSection)jrGroupChart.getGroupHeaderSection()).getBandsList().get(0);
967 break;
968 case DJChartOptions.POSITION_FOOTER:
969 band = (JRDesignBand) ((JRDesignSection)jrGroupChart.getGroupFooterSection()).getBandsList().get(0);
970 }
971 return band;
972 }
973
974
975
976
977
978
979 protected JRDesignChart createChart(DJChart djChart){
980 JRDesignGroup jrGroupChart = getJRGroupFromDJGroup(djChart.getColumnsGroup());
981
982 JRDesignChart chart = new JRDesignChart(new JRDesignStyle().getDefaultStyleProvider(), djChart.getType());
983 JRDesignGroup parentGroup = getParent(jrGroupChart);
984 List chartVariables = registerChartVariable(djChart);
985 JRDesignChartDataset chartDataset = DataSetFactory.getDataset(djChart, jrGroupChart, parentGroup, chartVariables);
986 chart.setDataset(chartDataset);
987 interpeterOptions(djChart, chart);
988
989 chart.setEvaluationTime( EvaluationTimeEnum.GROUP );
990 chart.setEvaluationGroup(jrGroupChart);
991 return chart;
992 }
993
994 protected void interpeterOptions(DJChart djChart, JRDesignChart chart) {
995 DJChartOptions options = djChart.getOptions();
996
997
998 if (options.isCentered())
999 chart.setWidth(getReport().getOptions().getPrintableWidth());
1000 else
1001 chart.setWidth(options.getWidth());
1002
1003 chart.setHeight(options.getHeight());
1004
1005
1006 chart.setX(options.getX());
1007
1008
1009 chart.setY(options.getY());
1010
1011
1012 chart.setShowLegend(options.isShowLegend());
1013 chart.setBackcolor(options.getBackColor());
1014
1015
1016
1017
1018
1019 if (options.getColors() != null){
1020 int i = 1;
1021 for (Iterator iter = options.getColors().iterator(); iter.hasNext();i++) {
1022 Color color = (Color) iter.next();
1023 chart.getPlot().getSeriesColors().add(new JRBaseChartPlot.JRBaseSeriesColor(i, color));
1024 }
1025 }
1026
1027 if (djChart.getType() == DJChart.BAR_CHART)
1028 ((JRDesignBarPlot) chart.getPlot()).setShowTickLabels(options.isShowLabels());
1029 }
1030
1031
1032
1033
1034
1035
1036
1037 protected List registerChartVariable(DJChart chart) {
1038
1039 JRDesignGroup group = getJRGroupFromDJGroup(chart.getColumnsGroup());
1040 List vars = new ArrayList();
1041
1042 int serieNum = 0;
1043 for (Iterator iterator = chart.getColumns().iterator(); iterator.hasNext();) {
1044 AbstractColumn col = (AbstractColumn) iterator.next();
1045
1046 Class clazz = null;
1047
1048 JRDesignExpression expression = new JRDesignExpression();
1049 if (col instanceof ExpressionColumn) {
1050 try { clazz = Class.forName(((ExpressionColumn) col).getExpression().getClassName());
1051 } catch (ClassNotFoundException e) {
1052 throw new DJException("Exeption creating chart variable: " + e.getMessage(),e);
1053 }
1054
1055 ExpressionColumn expCol = (ExpressionColumn) col;
1056 expression.setText(expCol.getTextForExpression());
1057 expression.setValueClassName(expCol.getExpression().getClassName());
1058 }
1059 else
1060 {
1061 try { clazz = Class.forName(((PropertyColumn) col).getColumnProperty().getValueClassName());
1062 } catch (ClassNotFoundException e) {
1063 throw new DJException("Exeption creating chart variable: " + e.getMessage(),e);
1064 }
1065
1066 expression.setText("$F{" + ((PropertyColumn) col).getColumnProperty().getProperty() + "}");
1067 expression.setValueClass(clazz);
1068 }
1069
1070
1071
1072 JRDesignVariable var = new JRDesignVariable();
1073 var.setValueClass(clazz);
1074 var.setExpression(expression);
1075 var.setCalculation(CalculationEnum.getByValue(chart.getOperation()));
1076 var.setResetGroup(group);
1077 var.setResetType( ResetTypeEnum.GROUP );
1078
1079
1080
1081 int chartIndex = getReport().getCharts().indexOf(chart);
1082 var.setName("CHART_[" + chartIndex +"_s" +serieNum + "+]_" + group.getName() + "_" + col.getTitle() + "_" + chart.getOperation());
1083
1084 try {
1085 getDesign().addVariable(var);
1086 vars.add(var);
1087 } catch (JRException e) {
1088 throw new LayoutException(e.getMessage(),e);
1089 }
1090 serieNum++;
1091 }
1092 return vars;
1093 }
1094
1095 protected JRDesignGroup getChartColumnsGroup(ar.com.fdvs.dj.domain.chart.DJChart djChart) {
1096 PropertyColumn columnsGroup = djChart.getDataset().getColumnsGroup();
1097 for (Iterator iterator = getReport().getColumnsGroups().iterator(); iterator.hasNext();) {
1098 DJGroup djGroup = (DJGroup) iterator.next();
1099 if (djGroup.getColumnToGroupBy() == columnsGroup)
1100 return getJRGroupFromDJGroup(djGroup);
1101 }
1102 return null;
1103 }
1104
1105 protected JRDesignBand createGroupForChartAndGetBand(ar.com.fdvs.dj.domain.chart.DJChart djChart) {
1106 JRDesignGroup jrGroup = getChartColumnsGroup(djChart);
1107 JRDesignGroup parentGroup = getParent(jrGroup);
1108 JRDesignGroup jrGroupChart = null;
1109 try {
1110 jrGroupChart = new JRDesignGroup();
1111 jrGroupChart.setExpression(parentGroup.getExpression());
1112 ((JRDesignSection)jrGroupChart.getGroupFooterSection()).addBand(new JRDesignBand());
1113 ((JRDesignSection)jrGroupChart.getGroupHeaderSection()).addBand(new JRDesignBand());
1114 jrGroupChart.setName(jrGroupChart.getName()+"_Chart" + getReport().getCharts().indexOf(djChart));
1115 } catch (Exception e) {
1116 throw new DJException("Problem creating band for chart: " + e.getMessage(),e);
1117 }
1118
1119
1120
1121
1122
1123 if (jrGroup.equals(parentGroup)){
1124 jrGroupChart.setExpression(ExpressionUtils.createStringExpression("\"dummy_for_chart\""));
1125 getDesign().getGroupsList().add( getDesign().getGroupsList().indexOf(jrGroup) , jrGroupChart);
1126 } else {
1127 int index = getDesign().getGroupsList().indexOf(parentGroup);
1128 getDesign().getGroupsList().add(index, jrGroupChart);
1129 }
1130
1131 JRDesignBand band = null;
1132 switch (djChart.getOptions().getPosition()) {
1133 case DJChartOptions.POSITION_HEADER:
1134 band = (JRDesignBand) ((JRDesignSection)jrGroupChart.getGroupHeaderSection()).getBandsList().get(0);
1135 break;
1136 case DJChartOptions.POSITION_FOOTER:
1137 band = (JRDesignBand) ((JRDesignSection)jrGroupChart.getGroupFooterSection()).getBandsList().get(0);
1138 }
1139 return band;
1140 }
1141
1142
1143
1144
1145
1146
1147 protected JRDesignChart createChart(ar.com.fdvs.dj.domain.chart.DJChart djChart, String name){
1148 JRDesignGroup jrGroupChart = getChartColumnsGroup(djChart);
1149 JRDesignGroup parentGroup = getParent(jrGroupChart);
1150 Map chartVariables = registerChartVariable(djChart);
1151 return djChart.transform((DynamicJasperDesign) getDesign(), name, jrGroupChart, parentGroup, chartVariables, getReport().getOptions().getPrintableWidth());
1152 }
1153
1154
1155
1156
1157
1158
1159 protected Map registerChartVariable(ar.com.fdvs.dj.domain.chart.DJChart chart) {
1160
1161 JRDesignGroup group = getChartColumnsGroup(chart);
1162 Map vars = new HashMap();
1163
1164 int serieNum = 0;
1165 for (Iterator iterator = chart.getDataset().getColumns().iterator(); iterator.hasNext();) {
1166 AbstractColumn col = (AbstractColumn) iterator.next();
1167
1168
1169 Class clazz = null;
1170
1171
1172
1173
1174
1175 JRDesignExpression expression = new JRDesignExpression();
1176
1177 if (col instanceof ExpressionColumn) {
1178 try { clazz = Class.forName(((ExpressionColumn) col).getExpression().getClassName());
1179 } catch (ClassNotFoundException e) {
1180 throw new DJException("Exeption creating chart variable: " + e.getMessage(),e);
1181 }
1182
1183 ExpressionColumn expCol = (ExpressionColumn) col;
1184 expression.setText(expCol.getTextForExpression());
1185 expression.setValueClassName(expCol.getExpression().getClassName());
1186 }
1187 else {
1188 try { clazz = Class.forName(((PropertyColumn) col).getColumnProperty().getValueClassName());
1189 } catch (ClassNotFoundException e) {
1190 throw new DJException("Exeption creating chart variable: " + e.getMessage(),e);
1191 }
1192
1193 expression.setText("$F{" + ((PropertyColumn) col).getColumnProperty().getProperty() + "}");
1194 expression.setValueClass(clazz);
1195 }
1196
1197 JRDesignVariable var = new JRDesignVariable();
1198 var.setValueClass(clazz);
1199 var.setExpression(expression);
1200 var.setCalculation(CalculationEnum.getByValue(chart.getOperation()));
1201 var.setResetGroup(group);
1202 var.setResetType( ResetTypeEnum.GROUP );
1203
1204
1205
1206 int chartIndex = getReport().getNewCharts().indexOf(chart);
1207 var.setName("CHART_[" + chartIndex +"_s" +serieNum + "+]_" + group.getName() + "_" + col.getTitle() + "_" + chart.getOperation());
1208
1209 try {
1210 getDesign().addVariable(var);
1211 vars.put(col, var);
1212 } catch (JRException e) {
1213 throw new LayoutException(e.getMessage(),e);
1214 }
1215 serieNum++;
1216 }
1217 return vars;
1218 }
1219
1220
1221
1222
1223
1224
1225 protected JRDesignGroup getParent(JRDesignGroup group){
1226 int index = realGroups.indexOf(group);
1227 JRDesignGroup parentGroup = (index > 0) ? (JRDesignGroup) realGroups.get(index-1): group;
1228 return parentGroup;
1229 }
1230
1231
1232
1233
1234
1235
1236 protected JRDesignGroup getJRGroupFromDJGroup(DJGroup group){
1237 int index = getReport().getColumnsGroups().indexOf(group);
1238 return (JRDesignGroup) realGroups.get(index);
1239 }
1240
1241
1242 protected DJGroup getDJGroup(AbstractColumn col) {
1243 Iterator it = getReport().getColumnsGroups().iterator();
1244 while (it.hasNext()) {
1245 DJGroup group = (DJGroup) it.next();
1246 if (group.getColumnToGroupBy().equals(col))
1247 return group;
1248 }
1249 return null;
1250 }
1251
1252
1253
1254
1255
1256
1257 protected boolean existsGroupWithColumnNames() {
1258 Iterator it = getReport().getColumnsGroups().iterator();
1259 while (it.hasNext()) {
1260 DJGroup group = (DJGroup) it.next();
1261 if (group.getLayout().isShowColumnName())
1262 return true;
1263 }
1264 return false;
1265 }
1266
1267 protected JasperDesign getDesign() {
1268 return design;
1269 }
1270
1271 protected void setDesign(JasperDesign design) {
1272 this.design = design;
1273 }
1274
1275 protected DynamicReport getReport() {
1276 return report;
1277 }
1278
1279 protected void setReport(DynamicReport report) {
1280 this.report = report;
1281 }
1282
1283 }