001: /*
002: * Copyright (C) 2005-2007 JasperSoft http://www.jaspersoft.com
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed WITHOUT ANY WARRANTY; and without the
010: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: * See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
015: * or write to:
016: *
017: * Free Software Foundation, Inc.,
018: * 59 Temple Place - Suite 330,
019: * Boston, MA USA 02111-1307
020: *
021: *
022: * ReportGenerator.java
023: *
024: * Created on December 20, 2006, 8:06 AM
025: *
026: * To change this template, choose Tools | Template Manager
027: * and open the template in the editor.
028: */
029:
030: package it.businesslogic.ireport.util;
031:
032: import it.businesslogic.ireport.Band;
033: import it.businesslogic.ireport.Group;
034: import it.businesslogic.ireport.JRField;
035: import it.businesslogic.ireport.Report;
036: import it.businesslogic.ireport.ReportElement;
037: import it.businesslogic.ireport.StaticTextReportElement;
038: import it.businesslogic.ireport.TextFieldReportElement;
039: import it.businesslogic.ireport.TransformationType;
040: import it.businesslogic.ireport.gui.wizard.UserChoicesWizardTemplate;
041: import java.util.Enumeration;
042: import java.util.Vector;
043:
044: /**
045: *
046: * @author gtoffoli
047: */
048: public class ReportGenerator {
049:
050: public static Report createReport(UserChoicesWizardTemplate ucwt)
051: throws Exception {
052: if (ucwt.getTemplateFile().toUpperCase().endsWith("C.XML")) {
053: return createColumnarReport(ucwt);
054: } else {
055: return createTabularReport(ucwt);
056: }
057: }
058:
059: /**********************************************************
060: *
061: */
062: static public Report createColumnarReport(
063: UserChoicesWizardTemplate ucwt) throws Exception {
064: Report template = new Report(ucwt.getTemplateFile());
065:
066: template.incrementReportChanges();
067: template.setFilename(null);
068:
069: //2. Find detail and column header bands...
070: Band detail = null;
071: Band columns = null;
072:
073: Enumeration e = template.getBands().elements();
074: while (e.hasMoreElements()) {
075: Band band = (Band) e.nextElement();
076: if (band.getName().equals("detail")) {
077: detail = band;
078: } else if (band.getName().equals("columnHeader")) {
079: columns = band;
080: }
081: }
082:
083: // 1. Normalize position to band...
084: e = template.getElements().elements();
085: while (e.hasMoreElements()) {
086: ReportElement rElement = (ReportElement) e.nextElement();
087: rElement.trasform(new java.awt.Point(0, -template
088: .getBandYLocation(rElement.getBand())),
089: TransformationType.TRANSFORMATION_MOVE);
090: }
091:
092: //1. Adding groups...
093: if (ucwt.getGroupExpressions().size() > 0) {
094: Group g = template.getGroupByName("Group1");
095: Group g1 = new Group(template, Misc.string_replace("_",
096: " ", "" + ucwt.getGroupExpressions().get(0)), g
097: .getGroupFooter().getHeight(), g.getGroupHeader()
098: .getHeight());
099: g1.setGroupExpression("$F{"
100: + ucwt.getGroupExpressions().get(0) + "}");
101: template.addGroup(g1);
102:
103: // Add to g1 all elements attached to g.header and g.footer...
104: e = template.getElements().elements();
105: while (e.hasMoreElements()) {
106: ReportElement rElement = (ReportElement) e
107: .nextElement();
108: if (rElement.getBand() == g.getGroupHeader())
109: rElement.setBand(g1.getGroupHeader());
110: else if (rElement.getBand() == g.getGroupFooter())
111: rElement.setBand(g1.getGroupFooter());
112: // Set text to Group1 label...
113: if (rElement instanceof StaticTextReportElement) {
114: StaticTextReportElement stre = (StaticTextReportElement) rElement;
115: if (stre.getText().equals("G1Label"))
116: stre.setText(""
117: + ucwt.getGroupExpressions().get(0));
118: } else if (rElement instanceof TextFieldReportElement) {
119: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
120: if (tfre.getText().equals("G1Field")) {
121: tfre.setText("$F{"
122: + ucwt.getGroupExpressions().get(0)
123: + "}");
124: // find the class type...
125: String fname = ""
126: + ucwt.getGroupExpressions().get(0);
127: String fclass = "java.lang.String";
128: for (int i = 0; i < ucwt.getDisplayFields()
129: .size(); ++i) {
130: JRField f = (JRField) ucwt
131: .getDisplayFields().get(i);
132: if (f.getName().equals(fname)) {
133: fclass = f.getClassType();
134: break;
135: }
136: }
137: tfre.setMatchingClassExpression(fclass, true);
138: }
139: }
140: }
141: }
142:
143: if (ucwt.getGroupExpressions().size() > 1) {
144: Group g = template.getGroupByName("Group2");
145: Group g2 = new Group(template, Misc.string_replace("_",
146: " ", "" + ucwt.getGroupExpressions().get(1)), g
147: .getGroupFooter().getHeight(), g.getGroupHeader()
148: .getHeight());
149: g2.setGroupExpression("$F{"
150: + ucwt.getGroupExpressions().get(1) + "}");
151: template.addGroup(g2);
152:
153: // Add to g2 all elements attached to g.header and g.footer...
154: e = template.getElements().elements();
155: while (e.hasMoreElements()) {
156: ReportElement rElement = (ReportElement) e
157: .nextElement();
158: if (rElement.getBand() == g.getGroupHeader())
159: rElement.setBand(g2.getGroupHeader());
160: else if (rElement.getBand() == g.getGroupFooter())
161: rElement.setBand(g2.getGroupFooter());
162: // Set text to Group2 label...
163: if (rElement instanceof StaticTextReportElement) {
164: StaticTextReportElement stre = (StaticTextReportElement) rElement;
165: if (stre.getText().equals("G2Label"))
166: stre.setText(""
167: + ucwt.getGroupExpressions().get(1));
168: } else if (rElement instanceof TextFieldReportElement) {
169: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
170: if (tfre.getText().equals("G2Field")) {
171: tfre.setText("$F{"
172: + ucwt.getGroupExpressions().get(1)
173: + "}");
174: // find the class type...
175: String fname = ""
176: + ucwt.getGroupExpressions().get(1);
177: String fclass = "java.lang.String";
178: for (int i = 0; i < ucwt.getDisplayFields()
179: .size(); ++i) {
180: JRField f = (JRField) ucwt
181: .getDisplayFields().get(i);
182: if (f.getName().equals(fname)) {
183: fclass = f.getClassType();
184: break;
185: }
186: }
187: tfre.setMatchingClassExpression(fclass, true);
188: }
189: }
190: }
191: }
192:
193: if (ucwt.getGroupExpressions().size() > 2) {
194: Group g = template.getGroupByName("Group3");
195: Group g3 = new Group(template, Misc.string_replace("_",
196: " ", "" + ucwt.getGroupExpressions().get(2)), g
197: .getGroupFooter().getHeight(), g.getGroupHeader()
198: .getHeight());
199: g3.setGroupExpression("$F{"
200: + ucwt.getGroupExpressions().get(2) + "}");
201: template.addGroup(g3);
202:
203: // Add to g3 all elements attached to g.header and g.footer...
204: e = template.getElements().elements();
205: while (e.hasMoreElements()) {
206: ReportElement rElement = (ReportElement) e
207: .nextElement();
208: if (rElement.getBand() == g.getGroupHeader())
209: rElement.setBand(g3.getGroupHeader());
210: else if (rElement.getBand() == g.getGroupFooter())
211: rElement.setBand(g3.getGroupFooter());
212: // Set text to Group3 label...
213: if (rElement instanceof StaticTextReportElement) {
214: StaticTextReportElement stre = (StaticTextReportElement) rElement;
215: if (stre.getText().equals("G3Label"))
216: stre.setText(""
217: + ucwt.getGroupExpressions().get(2));
218: } else if (rElement instanceof TextFieldReportElement) {
219: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
220: if (tfre.getText().equals("G3Field")) {
221: tfre.setText("$F{"
222: + ucwt.getGroupExpressions().get(2)
223: + "}");
224: // find the class type...
225: String fname = ""
226: + ucwt.getGroupExpressions().get(2);
227: String fclass = "java.lang.String";
228: for (int i = 0; i < ucwt.getDisplayFields()
229: .size(); ++i) {
230: JRField f = (JRField) ucwt
231: .getDisplayFields().get(i);
232: if (f.getName().equals(fname)) {
233: fclass = f.getClassType();
234: break;
235: }
236: }
237: tfre.setMatchingClassExpression(fclass, true);
238: }
239: }
240: }
241: }
242:
243: if (ucwt.getGroupExpressions().size() > 3) {
244: Group g = template.getGroupByName("Group4");
245: Group g4 = new Group(template, Misc.string_replace("_",
246: " ", "" + ucwt.getGroupExpressions().get(3)), g
247: .getGroupFooter().getHeight(), g.getGroupHeader()
248: .getHeight());
249: g4.setGroupExpression("$F{"
250: + ucwt.getGroupExpressions().get(3) + "}");
251: template.addGroup(g4);
252:
253: // Add to g4 all elements attached to g.header and g.footer...
254: e = template.getElements().elements();
255: while (e.hasMoreElements()) {
256: ReportElement rElement = (ReportElement) e
257: .nextElement();
258: if (rElement.getBand() == g.getGroupHeader())
259: rElement.setBand(g4.getGroupHeader());
260: else if (rElement.getBand() == g.getGroupFooter())
261: rElement.setBand(g4.getGroupFooter());
262: // Set text to Group4 label...
263: if (rElement instanceof StaticTextReportElement) {
264: StaticTextReportElement stre = (StaticTextReportElement) rElement;
265: if (stre.getText().equals("G4Label"))
266: stre.setText(""
267: + ucwt.getGroupExpressions().get(3));
268: } else if (rElement instanceof TextFieldReportElement) {
269: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
270: if (tfre.getText().equals("G4Field")) {
271: tfre.setText("$F{"
272: + ucwt.getGroupExpressions().get(3)
273: + "}");
274: // find the class type...
275: String fname = ""
276: + ucwt.getGroupExpressions().get(3);
277: String fclass = "java.lang.String";
278: for (int i = 0; i < ucwt.getDisplayFields()
279: .size(); ++i) {
280: JRField f = (JRField) ucwt
281: .getDisplayFields().get(i);
282: if (f.getName().equals(fname)) {
283: fclass = f.getClassType();
284: break;
285: }
286: }
287: tfre.setMatchingClassExpression(fclass, true);
288: }
289: }
290: }
291: }
292:
293: //1. Adding fields...
294: int currentx = template.getLeftMargin() + 10;
295: int currenty = 10;
296: e = template.getElements().elements();
297: StaticTextReportElement detailLabel = null;
298: TextFieldReportElement detailField = null;
299: while (e.hasMoreElements()
300: && (detailLabel == null || detailField == null)) {
301: ReportElement rElement = (ReportElement) e.nextElement();
302: if (rElement instanceof StaticTextReportElement) {
303: StaticTextReportElement stre = (StaticTextReportElement) rElement;
304: if (stre.getText().equalsIgnoreCase("DetailLabel"))
305: detailLabel = stre;
306: } else if (rElement instanceof TextFieldReportElement) {
307: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
308: if (tfre.getText().equalsIgnoreCase("DetailField"))
309: detailField = tfre;
310: }
311: }
312:
313: if (detailField != null)
314: template.getElements().removeElement(detailField);
315: if (detailLabel != null)
316: template.getElements().removeElement(detailLabel);
317:
318: if (detailField == null)
319: detailField = new TextFieldReportElement(0, 0, 100, 20);
320: if (detailLabel == null)
321: detailLabel = new StaticTextReportElement(0, 0, 100, 18);
322:
323: int nfields = ucwt.getDisplayFields().size();
324:
325: if (ucwt.getGroupExpressions().size() > 0)
326: nfields--;
327: if (ucwt.getGroupExpressions().size() > 1)
328: nfields--;
329: if (ucwt.getGroupExpressions().size() > 2)
330: nfields--;
331: if (ucwt.getGroupExpressions().size() > 3)
332: nfields--;
333:
334: int fwidth = template.getColumnWidth()
335: / ((nfields <= 0) ? 1 : nfields);
336: int ffheight = detailField.getPosition().y;
337:
338: for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
339: // FIELD
340: it.businesslogic.ireport.JRField f = (it.businesslogic.ireport.JRField) ucwt
341: .getDisplayFields().get(i);
342: template.addField(f);
343:
344: if (ucwt.getGroupExpressions().size() > 0
345: && f.getName().equalsIgnoreCase(
346: "" + ucwt.getGroupExpressions().get(0)))
347: continue;
348: if (ucwt.getGroupExpressions().size() > 1
349: && f.getName().equalsIgnoreCase(
350: "" + ucwt.getGroupExpressions().get(1)))
351: continue;
352: if (ucwt.getGroupExpressions().size() > 2
353: && f.getName().equalsIgnoreCase(
354: "" + ucwt.getGroupExpressions().get(2)))
355: continue;
356: if (ucwt.getGroupExpressions().size() > 3
357: && f.getName().equalsIgnoreCase(
358: "" + ucwt.getGroupExpressions().get(3)))
359: continue;
360:
361: TextFieldReportElement re = (TextFieldReportElement) detailField
362: .cloneMe();
363: re.setPosition(new java.awt.Point(
364: detailField.getPosition().x, ffheight));
365: re.updateBounds();
366: re.setText("$F{" + f.getName() + "}");
367:
368: re.setBand(detail);
369: re.setMatchingClassExpression(f.getClassType(), true);
370:
371: template.getElements().addElement(re);
372:
373: // COLUMN LABEL...
374: StaticTextReportElement sre = (StaticTextReportElement) detailLabel
375: .cloneMe();
376: sre.setPosition(new java.awt.Point(detailLabel
377: .getPosition().x, ffheight));
378: sre.setWidth(fwidth);
379: sre.updateBounds();
380:
381: //Get field description from datasource
382: //added by Felix Firgau on Oct, 11th 2006
383: String columnLabelText = f.getName();
384: if (ucwt.isSaveFieldDescriptions()) {
385: String description = f.getDescription();
386: if (description != null && description.length() > 0)
387: columnLabelText = description;
388: }
389:
390: sre.setText("" + columnLabelText + "");
391: sre.setBand(detail);
392: template.getElements().addElement(sre);
393:
394: ffheight += detailField.position.y + detailField.height
395: - 10;
396: }
397:
398: detail.setHeight(ffheight);
399:
400: // Remove template groups...****************
401: e = template.getElements().elements();
402: Vector elements_to_delete = new Vector();
403: while (e.hasMoreElements()) {
404: ReportElement rElement = (ReportElement) e.nextElement();
405: Group g = null;
406: if ((g = template.getGroupByName("Group1")) != null
407: && (rElement.getBand() == g.getGroupHeader() || rElement
408: .getBand() == g.getGroupFooter()))
409: elements_to_delete.addElement(rElement);
410: else if ((g = template.getGroupByName("Group2")) != null
411: && (rElement.getBand() == g.getGroupHeader() || rElement
412: .getBand() == g.getGroupFooter()))
413: elements_to_delete.addElement(rElement);
414: else if ((g = template.getGroupByName("Group3")) != null
415: && (rElement.getBand() == g.getGroupHeader() || rElement
416: .getBand() == g.getGroupFooter()))
417: elements_to_delete.addElement(rElement);
418: else if ((g = template.getGroupByName("Group4")) != null
419: && (rElement.getBand() == g.getGroupHeader() || rElement
420: .getBand() == g.getGroupFooter()))
421: elements_to_delete.addElement(rElement);
422: }
423: e = elements_to_delete.elements();
424: while (e.hasMoreElements()) {
425: template.getElements().removeElement(e.nextElement());
426: }
427:
428: Group g;
429: if ((g = template.getGroupByName("Group1")) != null) {
430: template.getBands().removeElement(g.getGroupFooter());
431: template.getBands().removeElement(g.getGroupHeader());
432: template.getGroups().removeElement(g);
433: }
434: if ((g = template.getGroupByName("Group2")) != null) {
435: template.getBands().removeElement(g.getGroupFooter());
436: template.getBands().removeElement(g.getGroupHeader());
437: template.getGroups().removeElement(g);
438: }
439: if ((g = template.getGroupByName("Group3")) != null) {
440: template.getBands().removeElement(g.getGroupFooter());
441: template.getBands().removeElement(g.getGroupHeader());
442: template.getGroups().removeElement(g);
443: }
444:
445: if ((g = template.getGroupByName("Group4")) != null) {
446: template.getBands().removeElement(g.getGroupFooter());
447: template.getBands().removeElement(g.getGroupHeader());
448: template.getGroups().removeElement(g);
449: }
450:
451: // Positioning columns band...
452: /*
453: if ((g=template.getGroupByName("Columns"))!=null)
454: {
455: template.getGroups().removeElement(g);
456: template.getBands().removeElement(g.getGroupHeader());
457: template.getBands().removeElement(g.getGroupFooter());
458: template.addGroup(g);
459: // we must adjust band position...
460: }
461: */
462: // Adjust vertical position of elements
463: e = template.getElements().elements();
464: while (e.hasMoreElements()) {
465: ReportElement rElement = (ReportElement) e.nextElement();
466: rElement.trasform(new java.awt.Point(0, +template
467: .getBandYLocation(rElement.getBand())),
468: TransformationType.TRANSFORMATION_MOVE);
469: }
470:
471: template.setQuery(ucwt.getQuery());
472: template.setQueryLanguage(ucwt.getQuery_language());
473:
474: return template;
475: }
476:
477: /**
478: * Create a Report from a template of user choices
479: *
480: */
481: public static Report createTabularReport(
482: UserChoicesWizardTemplate ucwt) throws Exception {
483:
484: Report template = new Report(ucwt.getTemplateFile());
485:
486: template.incrementReportChanges();
487: template.setFilename(null);
488:
489: //2. Find detail and column header bands...
490: Band detail = null;
491: Band columns = null;
492:
493: Enumeration e = template.getBands().elements();
494: while (e.hasMoreElements()) {
495: Band band = (Band) e.nextElement();
496: if (band.getName().equals("detail")) {
497: detail = band;
498: } else if (band.getName().equals("columnHeader")) {
499: columns = band;
500: }
501: }
502:
503: // 1. Normalize position to band...
504: e = template.getElements().elements();
505: while (e.hasMoreElements()) {
506: ReportElement rElement = (ReportElement) e.nextElement();
507: rElement.trasform(new java.awt.Point(0, -template
508: .getBandYLocation(rElement.getBand())),
509: TransformationType.TRANSFORMATION_MOVE);
510: }
511:
512: //1. Adding groups...
513: if (ucwt.getGroupExpressions().size() > 0) {
514: Group g = template.getGroupByName("Group1");
515: Group g1 = new Group(template, Misc.string_replace("_",
516: " ", "" + ucwt.getGroupExpressions().get(0)), g
517: .getGroupFooter().getHeight(), g.getGroupHeader()
518: .getHeight());
519: g1.setGroupExpression("$F{"
520: + ucwt.getGroupExpressions().get(0) + "}");
521: template.addGroup(g1);
522:
523: // Add to g1 all elements attached to g.header and g.footer...
524: e = template.getElements().elements();
525: while (e.hasMoreElements()) {
526: ReportElement rElement = (ReportElement) e
527: .nextElement();
528: if (rElement.getBand() == g.getGroupHeader())
529: rElement.setBand(g1.getGroupHeader());
530: else if (rElement.getBand() == g.getGroupFooter())
531: rElement.setBand(g1.getGroupFooter());
532: // Set text to Group1 label...
533: if (rElement instanceof StaticTextReportElement) {
534: StaticTextReportElement stre = (StaticTextReportElement) rElement;
535: if (stre.getText().equals("G1Label"))
536: stre.setText(""
537: + ucwt.getGroupExpressions().get(0));
538: } else if (rElement instanceof TextFieldReportElement) {
539: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
540: if (tfre.getText().equals("G1Field")) {
541: tfre.setText("$F{"
542: + ucwt.getGroupExpressions().get(0)
543: + "}");
544: // find the class type...
545: String fname = ""
546: + ucwt.getGroupExpressions().get(0);
547: String fclass = "java.lang.String";
548: for (int i = 0; i < ucwt.getDisplayFields()
549: .size(); ++i) {
550: JRField f = (JRField) ucwt
551: .getDisplayFields().get(i);
552: if (f.getName().equals(fname)) {
553: fclass = f.getClassType();
554: break;
555: }
556: }
557: tfre.setMatchingClassExpression(fclass, true);
558: }
559: }
560: }
561: }
562:
563: if (ucwt.getGroupExpressions().size() > 1) {
564: Group g = template.getGroupByName("Group2");
565: Group g2 = new Group(template, Misc.string_replace("_",
566: " ", "" + ucwt.getGroupExpressions().get(1)), g
567: .getGroupFooter().getHeight(), g.getGroupHeader()
568: .getHeight());
569: g2.setGroupExpression("$F{"
570: + ucwt.getGroupExpressions().get(1) + "}");
571: template.addGroup(g2);
572:
573: // Add to g2 all elements attached to g.header and g.footer...
574: e = template.getElements().elements();
575: while (e.hasMoreElements()) {
576: ReportElement rElement = (ReportElement) e
577: .nextElement();
578: if (rElement.getBand() == g.getGroupHeader())
579: rElement.setBand(g2.getGroupHeader());
580: else if (rElement.getBand() == g.getGroupFooter())
581: rElement.setBand(g2.getGroupFooter());
582: // Set text to Group2 label...
583: if (rElement instanceof StaticTextReportElement) {
584: StaticTextReportElement stre = (StaticTextReportElement) rElement;
585: if (stre.getText().equals("G2Label"))
586: stre.setText(""
587: + ucwt.getGroupExpressions().get(1));
588: } else if (rElement instanceof TextFieldReportElement) {
589: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
590: if (tfre.getText().equals("G2Field")) {
591: tfre.setText("$F{"
592: + ucwt.getGroupExpressions().get(1)
593: + "}");
594: // find the class type...
595: String fname = ""
596: + ucwt.getGroupExpressions().get(1);
597: String fclass = "java.lang.String";
598: for (int i = 0; i < ucwt.getDisplayFields()
599: .size(); ++i) {
600: JRField f = (JRField) ucwt
601: .getDisplayFields().get(i);
602: if (f.getName().equals(fname)) {
603: fclass = f.getClassType();
604: break;
605: }
606: }
607: tfre.setMatchingClassExpression(fclass, true);
608: }
609: }
610: }
611: }
612:
613: if (ucwt.getGroupExpressions().size() > 2) {
614: Group g = template.getGroupByName("Group3");
615: Group g3 = new Group(template, Misc.string_replace("_",
616: " ", "" + ucwt.getGroupExpressions().get(2)), g
617: .getGroupFooter().getHeight(), g.getGroupHeader()
618: .getHeight());
619: g3.setGroupExpression("$F{"
620: + ucwt.getGroupExpressions().get(2) + "}");
621: template.addGroup(g3);
622:
623: // Add to g3 all elements attached to g.header and g.footer...
624: e = template.getElements().elements();
625: while (e.hasMoreElements()) {
626: ReportElement rElement = (ReportElement) e
627: .nextElement();
628: if (rElement.getBand() == g.getGroupHeader())
629: rElement.setBand(g3.getGroupHeader());
630: else if (rElement.getBand() == g.getGroupFooter())
631: rElement.setBand(g3.getGroupFooter());
632: // Set text to Group3 label...
633: if (rElement instanceof StaticTextReportElement) {
634: StaticTextReportElement stre = (StaticTextReportElement) rElement;
635: if (stre.getText().equals("G3Label"))
636: stre.setText(""
637: + ucwt.getGroupExpressions().get(2));
638: } else if (rElement instanceof TextFieldReportElement) {
639: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
640: if (tfre.getText().equals("G3Field")) {
641: tfre.setText("$F{"
642: + ucwt.getGroupExpressions().get(2)
643: + "}");
644: // find the class type...
645: String fname = ""
646: + ucwt.getGroupExpressions().get(2);
647: String fclass = "java.lang.String";
648: for (int i = 0; i < ucwt.getDisplayFields()
649: .size(); ++i) {
650: JRField f = (JRField) ucwt
651: .getDisplayFields().get(i);
652: if (f.getName().equals(fname)) {
653: fclass = f.getClassType();
654: break;
655: }
656: }
657: tfre.setMatchingClassExpression(fclass, true);
658: }
659: }
660: }
661: }
662:
663: if (ucwt.getGroupExpressions().size() > 3) {
664: Group g = template.getGroupByName("Group4");
665: Group g4 = new Group(template, Misc.string_replace("_",
666: " ", "" + ucwt.getGroupExpressions().get(3)), g
667: .getGroupFooter().getHeight(), g.getGroupHeader()
668: .getHeight());
669: g4.setGroupExpression("$F{"
670: + ucwt.getGroupExpressions().get(3) + "}");
671: template.addGroup(g4);
672:
673: // Add to g4 all elements attached to g.header and g.footer...
674: e = template.getElements().elements();
675: while (e.hasMoreElements()) {
676: ReportElement rElement = (ReportElement) e
677: .nextElement();
678: if (rElement.getBand() == g.getGroupHeader())
679: rElement.setBand(g4.getGroupHeader());
680: else if (rElement.getBand() == g.getGroupFooter())
681: rElement.setBand(g4.getGroupFooter());
682: // Set text to Group4 label...
683: if (rElement instanceof StaticTextReportElement) {
684: StaticTextReportElement stre = (StaticTextReportElement) rElement;
685: if (stre.getText().equals("G4Label"))
686: stre.setText(""
687: + ucwt.getGroupExpressions().get(3));
688: } else if (rElement instanceof TextFieldReportElement) {
689: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
690: if (tfre.getText().equals("G4Field")) {
691: tfre.setText("$F{"
692: + ucwt.getGroupExpressions().get(3)
693: + "}");
694: // find the class type...
695: String fname = ""
696: + ucwt.getGroupExpressions().get(3);
697: String fclass = "java.lang.String";
698: for (int i = 0; i < ucwt.getDisplayFields()
699: .size(); ++i) {
700: JRField f = (JRField) ucwt
701: .getDisplayFields().get(i);
702: if (f.getName().equals(fname)) {
703: fclass = f.getClassType();
704: break;
705: }
706: }
707: tfre.setMatchingClassExpression(fclass, true);
708: }
709: }
710: }
711: }
712:
713: //1. Adding fields...
714: int currentx = template.getLeftMargin() + 10;
715: int currenty = 10;
716: e = template.getElements().elements();
717: StaticTextReportElement detailLabel = null;
718: TextFieldReportElement detailField = null;
719: while (e.hasMoreElements()
720: && (detailLabel == null || detailField == null)) {
721: ReportElement rElement = (ReportElement) e.nextElement();
722: if (rElement instanceof StaticTextReportElement) {
723: StaticTextReportElement stre = (StaticTextReportElement) rElement;
724: if (stre.getText().equalsIgnoreCase("DetailLabel"))
725: detailLabel = stre;
726: } else if (rElement instanceof TextFieldReportElement) {
727: TextFieldReportElement tfre = (TextFieldReportElement) rElement;
728: if (tfre.getText().equalsIgnoreCase("DetailField"))
729: detailField = tfre;
730: }
731: }
732:
733: if (detailField != null)
734: template.getElements().removeElement(detailField);
735: if (detailLabel != null)
736: template.getElements().removeElement(detailLabel);
737:
738: if (detailField == null)
739: detailField = new TextFieldReportElement(0, 0, 100, 20);
740: if (detailLabel == null)
741: detailLabel = new StaticTextReportElement(0, 0, 100, 18);
742:
743: int nfields = ucwt.getDisplayFields().size();
744: if (ucwt.getGroupExpressions().size() > 0)
745: nfields--;
746: if (ucwt.getGroupExpressions().size() > 1)
747: nfields--;
748: if (ucwt.getGroupExpressions().size() > 2)
749: nfields--;
750: if (ucwt.getGroupExpressions().size() > 3)
751: nfields--;
752:
753: int fwidth = template.getColumnWidth()
754: / ((nfields <= 0) ? 1 : nfields);
755:
756: for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
757: // FIELD
758: it.businesslogic.ireport.JRField f = (it.businesslogic.ireport.JRField) ucwt
759: .getDisplayFields().get(i);
760: template.addField(f);
761:
762: if (ucwt.getGroupExpressions().size() > 0
763: && f.getName().equalsIgnoreCase(
764: "" + ucwt.getGroupExpressions().get(0)))
765: continue;
766: if (ucwt.getGroupExpressions().size() > 1
767: && f.getName().equalsIgnoreCase(
768: "" + ucwt.getGroupExpressions().get(1)))
769: continue;
770: if (ucwt.getGroupExpressions().size() > 2
771: && f.getName().equalsIgnoreCase(
772: "" + ucwt.getGroupExpressions().get(2)))
773: continue;
774: if (ucwt.getGroupExpressions().size() > 3
775: && f.getName().equalsIgnoreCase(
776: "" + ucwt.getGroupExpressions().get(3)))
777: continue;
778:
779: TextFieldReportElement re = (TextFieldReportElement) detailField
780: .cloneMe();
781: re.setPosition(new java.awt.Point(currentx, detailField
782: .getPosition().y));
783: re.setWidth(fwidth);
784: re.updateBounds();
785: re.setText("$F{" + f.getName() + "}");
786:
787: re.setBand(detail);
788: re.setMatchingClassExpression(f.getClassType(), true);
789: template.getElements().addElement(re);
790:
791: // COLUMN LABEL...
792: StaticTextReportElement sre = (StaticTextReportElement) detailLabel
793: .cloneMe();
794: sre.setPosition(new java.awt.Point(currentx, detailLabel
795: .getPosition().y));
796: sre.setWidth(fwidth);
797: sre.updateBounds();
798:
799: //Get field description from datasource
800: //added by Felix Firgau on Oct, 11th 2006
801: String columnLabelText = f.getName();
802: if (ucwt.isSaveFieldDescriptions()) {
803: String description = f.getDescription();
804: if (description != null && description.length() > 0)
805: columnLabelText = description;
806: }
807:
808: sre.setText("" + columnLabelText + "");
809: sre.setBand(columns);
810: template.getElements().addElement(sre);
811:
812: currentx += fwidth;
813: currentx = Math.min(template.getWidth()
814: - template.getRightMargin() + 10 - 30, currentx);
815: }
816:
817: // Remove template groups...****************
818: e = template.getElements().elements();
819: Vector elements_to_delete = new Vector();
820: while (e.hasMoreElements()) {
821: ReportElement rElement = (ReportElement) e.nextElement();
822: Group g = null;
823: if ((g = template.getGroupByName("Group1")) != null
824: && (rElement.getBand() == g.getGroupHeader() || rElement
825: .getBand() == g.getGroupFooter()))
826: elements_to_delete.addElement(rElement);
827: else if ((g = template.getGroupByName("Group2")) != null
828: && (rElement.getBand() == g.getGroupHeader() || rElement
829: .getBand() == g.getGroupFooter()))
830: elements_to_delete.addElement(rElement);
831: else if ((g = template.getGroupByName("Group3")) != null
832: && (rElement.getBand() == g.getGroupHeader() || rElement
833: .getBand() == g.getGroupFooter()))
834: elements_to_delete.addElement(rElement);
835: else if ((g = template.getGroupByName("Group4")) != null
836: && (rElement.getBand() == g.getGroupHeader() || rElement
837: .getBand() == g.getGroupFooter()))
838: elements_to_delete.addElement(rElement);
839: }
840: e = elements_to_delete.elements();
841: while (e.hasMoreElements()) {
842: template.getElements().removeElement(e.nextElement());
843: }
844:
845: Group g;
846: if ((g = template.getGroupByName("Group1")) != null) {
847: template.getBands().removeElement(g.getGroupFooter());
848: template.getBands().removeElement(g.getGroupHeader());
849: template.getGroups().removeElement(g);
850: }
851: if ((g = template.getGroupByName("Group2")) != null) {
852: template.getBands().removeElement(g.getGroupFooter());
853: template.getBands().removeElement(g.getGroupHeader());
854: template.getGroups().removeElement(g);
855: }
856: if ((g = template.getGroupByName("Group3")) != null) {
857: template.getBands().removeElement(g.getGroupFooter());
858: template.getBands().removeElement(g.getGroupHeader());
859: template.getGroups().removeElement(g);
860: }
861:
862: if ((g = template.getGroupByName("Group4")) != null) {
863: template.getBands().removeElement(g.getGroupFooter());
864: template.getBands().removeElement(g.getGroupHeader());
865: template.getGroups().removeElement(g);
866: }
867:
868: // Positioning columns band...
869: /*
870: if ((g=template.getGroupByName("Columns"))!=null)
871: {
872: template.getGroups().removeElement(g);
873: template.getBands().removeElement(g.getGroupHeader());
874: template.getBands().removeElement(g.getGroupFooter());
875: template.addGroup(g);
876: // we must adjust band position...
877: }
878: */
879: // Adjust vertical position of elements
880: e = template.getElements().elements();
881: while (e.hasMoreElements()) {
882: ReportElement rElement = (ReportElement) e.nextElement();
883: rElement.trasform(new java.awt.Point(0, +template
884: .getBandYLocation(rElement.getBand())),
885: TransformationType.TRANSFORMATION_MOVE);
886: }
887:
888: template.setQuery(ucwt.getQuery());
889: template.setQueryLanguage(ucwt.getQuery_language());
890:
891: return template;
892: }
893: }
|