KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > util > ReportGenerator


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  *
21  *
22  * ReportGenerator.java
23  *
24  * Created on December 20, 2006, 8:06 AM
25  *
26  * To change this template, choose Tools | Template Manager
27  * and open the template in the editor.
28  */

29
30 package it.businesslogic.ireport.util;
31
32 import it.businesslogic.ireport.Band;
33 import it.businesslogic.ireport.Group;
34 import it.businesslogic.ireport.JRField;
35 import it.businesslogic.ireport.Report;
36 import it.businesslogic.ireport.ReportElement;
37 import it.businesslogic.ireport.StaticTextReportElement;
38 import it.businesslogic.ireport.TextFieldReportElement;
39 import it.businesslogic.ireport.TransformationType;
40 import it.businesslogic.ireport.gui.wizard.UserChoicesWizardTemplate;
41 import java.util.Enumeration JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 /**
45  *
46  * @author gtoffoli
47  */

48 public class ReportGenerator {
49     
50     public static Report createReport(UserChoicesWizardTemplate ucwt) throws Exception JavaDoc
51     {
52         if (ucwt.getTemplateFile().toUpperCase().endsWith("C.XML"))
53         {
54             return createColumnarReport(ucwt);
55         }
56         else
57         {
58             return createTabularReport(ucwt);
59         }
60     }
61     
62     /**********************************************************
63      *
64      */

65     static public Report createColumnarReport(UserChoicesWizardTemplate ucwt) throws Exception JavaDoc
66     {
67         Report template = new Report(ucwt.getTemplateFile());
68         
69         template.incrementReportChanges();
70         template.setFilename(null);
71         
72         //2. Find detail and column header bands...
73
Band detail=null;
74         Band columns=null;
75         
76         Enumeration JavaDoc e = template.getBands().elements();
77         while (e.hasMoreElements()) {
78             Band band = (Band)e.nextElement();
79             if (band.getName().equals("detail")) {
80                 detail = band;
81             } else if (band.getName().equals("columnHeader")) {
82                 columns = band;
83             }
84         }
85         
86         // 1. Normalize position to band...
87
e = template.getElements().elements();
88         while (e.hasMoreElements()) {
89             ReportElement rElement = (ReportElement)e.nextElement();
90             rElement.trasform(new java.awt.Point JavaDoc(0,- template.getBandYLocation( rElement.getBand() )),TransformationType.TRANSFORMATION_MOVE );
91         }
92         
93         //1. Adding groups...
94
if (ucwt.getGroupExpressions().size() > 0) {
95             Group g = template.getGroupByName("Group1");
96             Group g1 = new Group( template,Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(0)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
97             g1.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(0) + "}" );
98             template.addGroup(g1);
99             
100             // Add to g1 all elements attached to g.header and g.footer...
101
e = template.getElements().elements();
102             while (e.hasMoreElements()) {
103                 ReportElement rElement = (ReportElement)e.nextElement();
104                 if (rElement.getBand() == g.getGroupHeader())
105                     rElement.setBand(g1.getGroupHeader() );
106                 else if (rElement.getBand() == g.getGroupFooter())
107                     rElement.setBand(g1.getGroupFooter() );
108                 // Set text to Group1 label...
109
if (rElement instanceof StaticTextReportElement) {
110                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
111                     if (stre.getText().equals("G1Label"))
112                         stre.setText( ""+ucwt.getGroupExpressions().get(0));
113                 } else if (rElement instanceof TextFieldReportElement) {
114                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
115                     if (tfre.getText().equals("G1Field")) {
116                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(0)+"}");
117                         // find the class type...
118
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(0);
119                         String JavaDoc fclass = "java.lang.String";
120                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
121                         {
122                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
123                             if (f.getName().equals(fname))
124                             {
125                                 fclass = f.getClassType();
126                                 break;
127                             }
128                         }
129                         tfre.setMatchingClassExpression( fclass, true );
130                     }
131                 }
132             }
133         }
134         
135         if (ucwt.getGroupExpressions().size() > 1) {
136             Group g = template.getGroupByName("Group2");
137             Group g2 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(1)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
138             g2.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(1) + "}" );
139             template.addGroup(g2);
140             
141             // Add to g2 all elements attached to g.header and g.footer...
142
e = template.getElements().elements();
143             while (e.hasMoreElements()) {
144                 ReportElement rElement = (ReportElement)e.nextElement();
145                 if (rElement.getBand() == g.getGroupHeader())
146                     rElement.setBand(g2.getGroupHeader() );
147                 else if (rElement.getBand() == g.getGroupFooter())
148                     rElement.setBand(g2.getGroupFooter() );
149                 // Set text to Group2 label...
150
if (rElement instanceof StaticTextReportElement) {
151                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
152                     if (stre.getText().equals("G2Label"))
153                         stre.setText( ""+ucwt.getGroupExpressions().get(1));
154                 } else if (rElement instanceof TextFieldReportElement) {
155                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
156                     if (tfre.getText().equals("G2Field")) {
157                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(1)+"}");
158                         // find the class type...
159
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(1);
160                         String JavaDoc fclass = "java.lang.String";
161                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
162                         {
163                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
164                             if (f.getName().equals(fname))
165                             {
166                                 fclass = f.getClassType();
167                                 break;
168                             }
169                         }
170                         tfre.setMatchingClassExpression( fclass, true );
171                     }
172                 }
173             }
174         }
175         
176         if (ucwt.getGroupExpressions().size() > 2) {
177             Group g = template.getGroupByName("Group3");
178             Group g3 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(2)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
179             g3.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(2) + "}" );
180             template.addGroup(g3);
181             
182             // Add to g3 all elements attached to g.header and g.footer...
183
e = template.getElements().elements();
184             while (e.hasMoreElements()) {
185                 ReportElement rElement = (ReportElement)e.nextElement();
186                 if (rElement.getBand() == g.getGroupHeader())
187                     rElement.setBand(g3.getGroupHeader() );
188                 else if (rElement.getBand() == g.getGroupFooter())
189                     rElement.setBand(g3.getGroupFooter() );
190                 // Set text to Group3 label...
191
if (rElement instanceof StaticTextReportElement) {
192                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
193                     if (stre.getText().equals("G3Label"))
194                         stre.setText( ""+ucwt.getGroupExpressions().get(2));
195                 } else if (rElement instanceof TextFieldReportElement) {
196                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
197                     if (tfre.getText().equals("G3Field")) {
198                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(2)+"}");
199                         // find the class type...
200
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(2);
201                         String JavaDoc fclass = "java.lang.String";
202                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
203                         {
204                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
205                             if (f.getName().equals(fname))
206                             {
207                                 fclass = f.getClassType();
208                                 break;
209                             }
210                         }
211                         tfre.setMatchingClassExpression( fclass, true );
212                     }
213                 }
214             }
215         }
216         
217         if (ucwt.getGroupExpressions().size() > 3) {
218             Group g = template.getGroupByName("Group4");
219             Group g4 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(3)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
220             g4.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(3) + "}" );
221             template.addGroup(g4);
222             
223             // Add to g4 all elements attached to g.header and g.footer...
224
e = template.getElements().elements();
225             while (e.hasMoreElements()) {
226                 ReportElement rElement = (ReportElement)e.nextElement();
227                 if (rElement.getBand() == g.getGroupHeader())
228                     rElement.setBand(g4.getGroupHeader() );
229                 else if (rElement.getBand() == g.getGroupFooter())
230                     rElement.setBand(g4.getGroupFooter() );
231                 // Set text to Group4 label...
232
if (rElement instanceof StaticTextReportElement) {
233                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
234                     if (stre.getText().equals("G4Label"))
235                         stre.setText( ""+ucwt.getGroupExpressions().get(3));
236                 } else if (rElement instanceof TextFieldReportElement) {
237                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
238                     if (tfre.getText().equals("G4Field")) {
239                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(3)+"}");
240                         // find the class type...
241
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(3);
242                         String JavaDoc fclass = "java.lang.String";
243                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
244                         {
245                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
246                             if (f.getName().equals(fname))
247                             {
248                                 fclass = f.getClassType();
249                                 break;
250                             }
251                         }
252                         tfre.setMatchingClassExpression( fclass, true );
253                     }
254                 }
255             }
256         }
257         
258         //1. Adding fields...
259
int currentx = template.getLeftMargin()+10;
260         int currenty = 10;
261         e = template.getElements().elements();
262         StaticTextReportElement detailLabel = null;
263         TextFieldReportElement detailField = null;
264         while (e.hasMoreElements() && (detailLabel==null || detailField == null) ) {
265             ReportElement rElement = (ReportElement)e.nextElement();
266             if (rElement instanceof StaticTextReportElement) {
267                 StaticTextReportElement stre = (StaticTextReportElement)rElement;
268                 if (stre.getText().equalsIgnoreCase("DetailLabel"))
269                     detailLabel =stre;
270             } else if (rElement instanceof TextFieldReportElement) {
271                 TextFieldReportElement tfre = (TextFieldReportElement)rElement;
272                 if (tfre.getText().equalsIgnoreCase("DetailField"))
273                     detailField = tfre;
274             }
275         }
276         
277         if (detailField != null)
278             template.getElements().removeElement(detailField);
279         if (detailLabel != null)
280             template.getElements().removeElement(detailLabel);
281         
282         if (detailField == null)
283             detailField = new TextFieldReportElement(0,0,100,20);
284         if (detailLabel == null)
285             detailLabel = new StaticTextReportElement(0,0,100,18);
286         
287         int nfields = ucwt.getDisplayFields().size();
288         
289         
290         if (ucwt.getGroupExpressions().size()>0) nfields--;
291         if (ucwt.getGroupExpressions().size()>1) nfields--;
292         if (ucwt.getGroupExpressions().size()>2) nfields--;
293         if (ucwt.getGroupExpressions().size()>3) nfields--;
294         
295         int fwidth = template.getColumnWidth()/ ( (nfields <= 0) ? 1 : nfields);
296         int ffheight = detailField.getPosition().y;
297         
298         for (int i=0; i<ucwt.getDisplayFields().size(); ++i) {
299             // FIELD
300
it.businesslogic.ireport.JRField f = (it.businesslogic.ireport.JRField)ucwt.getDisplayFields().get(i);
301             template.addField(f);
302             
303             if (ucwt.getGroupExpressions().size()>0 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(0) ))
304                 continue;
305             if (ucwt.getGroupExpressions().size()>1 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(1) ))
306                 continue;
307             if (ucwt.getGroupExpressions().size()>2 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(2) ))
308                 continue;
309             if (ucwt.getGroupExpressions().size()>3 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(3) ))
310                 continue;
311             
312             
313             TextFieldReportElement re = (TextFieldReportElement)detailField.cloneMe();
314             re.setPosition(new java.awt.Point JavaDoc(detailField.getPosition().x, ffheight));
315             re.updateBounds();
316             re.setText("$F{"+ f.getName() +"}");
317             
318             re.setBand(detail);
319             re.setMatchingClassExpression( f.getClassType(), true );
320             
321             template.getElements().addElement(re);
322             
323             // COLUMN LABEL...
324
StaticTextReportElement sre = (StaticTextReportElement)detailLabel.cloneMe();
325             sre.setPosition(new java.awt.Point JavaDoc(detailLabel.getPosition().x,ffheight));
326             sre.setWidth(fwidth);
327             sre.updateBounds();
328             
329             //Get field description from datasource
330
//added by Felix Firgau on Oct, 11th 2006
331
String JavaDoc columnLabelText=f.getName();
332             if(ucwt.isSaveFieldDescriptions()) {
333                 String JavaDoc description = f.getDescription();
334                 if(description!=null && description.length() > 0)
335                     columnLabelText = description;
336             }
337             
338             sre.setText(""+ columnLabelText +"");
339             sre.setBand(detail);
340             template.getElements().addElement(sre);
341             
342             ffheight += detailField.position.y+detailField.height-10;
343         }
344         
345         detail.setHeight(ffheight);
346         
347         // Remove template groups...****************
348
e = template.getElements().elements();
349         Vector JavaDoc elements_to_delete = new Vector JavaDoc();
350         while (e.hasMoreElements()) {
351             ReportElement rElement = (ReportElement)e.nextElement();
352             Group g = null;
353             if ((g=template.getGroupByName("Group1"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
354                 elements_to_delete.addElement(rElement);
355             else if ((g=template.getGroupByName("Group2"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
356                 elements_to_delete.addElement(rElement);
357             else if ((g=template.getGroupByName("Group3"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
358                 elements_to_delete.addElement(rElement);
359             else if ((g=template.getGroupByName("Group4"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
360                 elements_to_delete.addElement(rElement);
361         }
362         e =elements_to_delete.elements();
363         while (e.hasMoreElements()) {
364             template.getElements().removeElement(e.nextElement());
365         }
366         
367         Group g;
368         if ((g=template.getGroupByName("Group1"))!=null) {
369             template.getBands().removeElement(g.getGroupFooter());
370             template.getBands().removeElement(g.getGroupHeader());
371             template.getGroups().removeElement(g);
372         }
373         if ((g=template.getGroupByName("Group2"))!=null) {
374             template.getBands().removeElement(g.getGroupFooter());
375             template.getBands().removeElement(g.getGroupHeader());
376             template.getGroups().removeElement(g);
377         }
378         if ((g=template.getGroupByName("Group3"))!=null) {
379             template.getBands().removeElement(g.getGroupFooter());
380             template.getBands().removeElement(g.getGroupHeader());
381             template.getGroups().removeElement(g);
382         }
383         
384         if ((g=template.getGroupByName("Group4"))!=null) {
385             template.getBands().removeElement(g.getGroupFooter());
386             template.getBands().removeElement(g.getGroupHeader());
387             template.getGroups().removeElement(g);
388         }
389         
390         
391         // Positioning columns band...
392
/*
393         if ((g=template.getGroupByName("Columns"))!=null)
394         {
395                 template.getGroups().removeElement(g);
396                 template.getBands().removeElement(g.getGroupHeader());
397                 template.getBands().removeElement(g.getGroupFooter());
398                 template.addGroup(g);
399                 // we must adjust band position...
400         }
401          */

402         // Adjust vertical position of elements
403
e = template.getElements().elements();
404         while (e.hasMoreElements()) {
405             ReportElement rElement = (ReportElement)e.nextElement();
406             rElement.trasform(new java.awt.Point JavaDoc(0,+ template.getBandYLocation( rElement.getBand() )),TransformationType.TRANSFORMATION_MOVE );
407         }
408         
409         template.setQuery( ucwt.getQuery() );
410         template.setQueryLanguage( ucwt.getQuery_language() );
411         
412         return template;
413     }
414     
415     
416     
417         
418      /**
419       * Create a Report from a template of user choices
420       *
421       */

422     public static Report createTabularReport(UserChoicesWizardTemplate ucwt) throws Exception JavaDoc
423     {
424
425         Report template = new Report(ucwt.getTemplateFile());
426         
427         template.incrementReportChanges();
428         template.setFilename(null);
429         
430         //2. Find detail and column header bands...
431
Band detail=null;
432         Band columns=null;
433         
434         Enumeration JavaDoc e = template.getBands().elements();
435         while (e.hasMoreElements()) {
436             Band band = (Band)e.nextElement();
437             if (band.getName().equals("detail")) {
438                 detail = band;
439             } else if (band.getName().equals("columnHeader")) {
440                 columns = band;
441             }
442         }
443         
444         // 1. Normalize position to band...
445
e = template.getElements().elements();
446         while (e.hasMoreElements()) {
447             ReportElement rElement = (ReportElement)e.nextElement();
448             rElement.trasform(new java.awt.Point JavaDoc(0,- template.getBandYLocation( rElement.getBand() )),TransformationType.TRANSFORMATION_MOVE );
449         }
450         
451         //1. Adding groups...
452
if (ucwt.getGroupExpressions().size() > 0) {
453             Group g = template.getGroupByName("Group1");
454             Group g1 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(0)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
455             g1.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(0) + "}" );
456             template.addGroup(g1);
457             
458             // Add to g1 all elements attached to g.header and g.footer...
459
e = template.getElements().elements();
460             while (e.hasMoreElements()) {
461                 ReportElement rElement = (ReportElement)e.nextElement();
462                 if (rElement.getBand() == g.getGroupHeader())
463                     rElement.setBand(g1.getGroupHeader() );
464                 else if (rElement.getBand() == g.getGroupFooter())
465                     rElement.setBand(g1.getGroupFooter() );
466                 // Set text to Group1 label...
467
if (rElement instanceof StaticTextReportElement) {
468                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
469                     if (stre.getText().equals("G1Label"))
470                         stre.setText( ""+ucwt.getGroupExpressions().get(0));
471                 } else if (rElement instanceof TextFieldReportElement) {
472                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
473                     if (tfre.getText().equals("G1Field")) {
474                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(0)+"}");
475                         // find the class type...
476
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(0);
477                         String JavaDoc fclass = "java.lang.String";
478                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
479                         {
480                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
481                             if (f.getName().equals(fname))
482                             {
483                                 fclass = f.getClassType();
484                                 break;
485                             }
486                         }
487                         tfre.setMatchingClassExpression( fclass, true );
488                     }
489                 }
490             }
491         }
492         
493         if (ucwt.getGroupExpressions().size() > 1) {
494             Group g = template.getGroupByName("Group2");
495             Group g2 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(1)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
496             g2.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(1) + "}" );
497             template.addGroup(g2);
498             
499             // Add to g2 all elements attached to g.header and g.footer...
500
e = template.getElements().elements();
501             while (e.hasMoreElements()) {
502                 ReportElement rElement = (ReportElement)e.nextElement();
503                 if (rElement.getBand() == g.getGroupHeader())
504                     rElement.setBand(g2.getGroupHeader() );
505                 else if (rElement.getBand() == g.getGroupFooter())
506                     rElement.setBand(g2.getGroupFooter() );
507                 // Set text to Group2 label...
508
if (rElement instanceof StaticTextReportElement) {
509                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
510                     if (stre.getText().equals("G2Label"))
511                         stre.setText( ""+ucwt.getGroupExpressions().get(1));
512                 } else if (rElement instanceof TextFieldReportElement) {
513                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
514                     if (tfre.getText().equals("G2Field")) {
515                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(1)+"}");
516                         // find the class type...
517
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(1);
518                         String JavaDoc fclass = "java.lang.String";
519                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
520                         {
521                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
522                             if (f.getName().equals(fname))
523                             {
524                                 fclass = f.getClassType();
525                                 break;
526                             }
527                         }
528                         tfre.setMatchingClassExpression( fclass, true );
529                     }
530                 }
531             }
532         }
533         
534         if (ucwt.getGroupExpressions().size() > 2) {
535             Group g = template.getGroupByName("Group3");
536             Group g3 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(2)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
537             g3.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(2) + "}" );
538             template.addGroup(g3);
539             
540             // Add to g3 all elements attached to g.header and g.footer...
541
e = template.getElements().elements();
542             while (e.hasMoreElements()) {
543                 ReportElement rElement = (ReportElement)e.nextElement();
544                 if (rElement.getBand() == g.getGroupHeader())
545                     rElement.setBand(g3.getGroupHeader() );
546                 else if (rElement.getBand() == g.getGroupFooter())
547                     rElement.setBand(g3.getGroupFooter() );
548                 // Set text to Group3 label...
549
if (rElement instanceof StaticTextReportElement) {
550                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
551                     if (stre.getText().equals("G3Label"))
552                         stre.setText( ""+ucwt.getGroupExpressions().get(2));
553                 } else if (rElement instanceof TextFieldReportElement) {
554                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
555                     if (tfre.getText().equals("G3Field")) {
556                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(2)+"}");
557                         // find the class type...
558
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(2);
559                         String JavaDoc fclass = "java.lang.String";
560                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
561                         {
562                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
563                             if (f.getName().equals(fname))
564                             {
565                                 fclass = f.getClassType();
566                                 break;
567                             }
568                         }
569                         tfre.setMatchingClassExpression( fclass, true );
570                     }
571                 }
572             }
573         }
574         
575         if (ucwt.getGroupExpressions().size() > 3) {
576             Group g = template.getGroupByName("Group4");
577             Group g4 = new Group( template, Misc.string_replace("_"," ",""+ucwt.getGroupExpressions().get(3)), g.getGroupFooter().getHeight(),g.getGroupHeader().getHeight());
578             g4.setGroupExpression("$F{"+ ucwt.getGroupExpressions().get(3) + "}" );
579             template.addGroup(g4);
580             
581             // Add to g4 all elements attached to g.header and g.footer...
582
e = template.getElements().elements();
583             while (e.hasMoreElements()) {
584                 ReportElement rElement = (ReportElement)e.nextElement();
585                 if (rElement.getBand() == g.getGroupHeader())
586                     rElement.setBand(g4.getGroupHeader() );
587                 else if (rElement.getBand() == g.getGroupFooter())
588                     rElement.setBand(g4.getGroupFooter() );
589                 // Set text to Group4 label...
590
if (rElement instanceof StaticTextReportElement) {
591                     StaticTextReportElement stre = (StaticTextReportElement)rElement;
592                     if (stre.getText().equals("G4Label"))
593                         stre.setText( ""+ucwt.getGroupExpressions().get(3));
594                 } else if (rElement instanceof TextFieldReportElement) {
595                     TextFieldReportElement tfre = (TextFieldReportElement)rElement;
596                     if (tfre.getText().equals("G4Field")) {
597                         tfre.setText("$F{"+ucwt.getGroupExpressions().get(3)+"}");
598                         // find the class type...
599
String JavaDoc fname = ""+ucwt.getGroupExpressions().get(3);
600                         String JavaDoc fclass = "java.lang.String";
601                         for (int i=0; i<ucwt.getDisplayFields().size(); ++i)
602                         {
603                             JRField f = (JRField)ucwt.getDisplayFields().get(i);
604                             if (f.getName().equals(fname))
605                             {
606                                 fclass = f.getClassType();
607                                 break;
608                             }
609                         }
610                         tfre.setMatchingClassExpression( fclass, true );
611                     }
612                 }
613             }
614         }
615         
616         //1. Adding fields...
617
int currentx = template.getLeftMargin()+10;
618         int currenty = 10;
619         e = template.getElements().elements();
620         StaticTextReportElement detailLabel = null;
621         TextFieldReportElement detailField = null;
622         while (e.hasMoreElements() && (detailLabel==null || detailField == null) ) {
623             ReportElement rElement = (ReportElement)e.nextElement();
624             if (rElement instanceof StaticTextReportElement) {
625                 StaticTextReportElement stre = (StaticTextReportElement)rElement;
626                 if (stre.getText().equalsIgnoreCase("DetailLabel"))
627                     detailLabel =stre;
628             } else if (rElement instanceof TextFieldReportElement) {
629                 TextFieldReportElement tfre = (TextFieldReportElement)rElement;
630                 if (tfre.getText().equalsIgnoreCase("DetailField"))
631                     detailField = tfre;
632             }
633         }
634         
635         if (detailField != null)
636             template.getElements().removeElement(detailField);
637         if (detailLabel != null)
638             template.getElements().removeElement(detailLabel);
639         
640         if (detailField == null)
641             detailField = new TextFieldReportElement(0,0,100,20);
642         if (detailLabel == null)
643             detailLabel = new StaticTextReportElement(0,0,100,18);
644         
645         int nfields = ucwt.getDisplayFields().size();
646         if (ucwt.getGroupExpressions().size()>0) nfields--;
647         if (ucwt.getGroupExpressions().size()>1) nfields--;
648         if (ucwt.getGroupExpressions().size()>2) nfields--;
649         if (ucwt.getGroupExpressions().size()>3) nfields--;
650         
651         int fwidth = template.getColumnWidth()/( (nfields <= 0) ? 1 : nfields);
652         
653         for (int i=0; i<ucwt.getDisplayFields().size(); ++i) {
654             // FIELD
655
it.businesslogic.ireport.JRField f = (it.businesslogic.ireport.JRField)ucwt.getDisplayFields().get(i);
656             template.addField(f);
657             
658             if (ucwt.getGroupExpressions().size()>0 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(0) ))
659                 continue;
660             if (ucwt.getGroupExpressions().size()>1 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(1) ))
661                 continue;
662             if (ucwt.getGroupExpressions().size()>2 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(2) ))
663                 continue;
664             if (ucwt.getGroupExpressions().size()>3 && f.getName().equalsIgnoreCase( ""+ucwt.getGroupExpressions().get(3) ))
665                 continue;
666             
667             
668             TextFieldReportElement re = (TextFieldReportElement)detailField.cloneMe();
669             re.setPosition(new java.awt.Point JavaDoc(currentx,detailField.getPosition().y));
670             re.setWidth( fwidth);
671             re.updateBounds();
672             re.setText("$F{"+ f.getName() +"}");
673             
674             re.setBand(detail);
675             re.setMatchingClassExpression( f.getClassType(), true );
676             template.getElements().addElement(re);
677             
678             // COLUMN LABEL...
679
StaticTextReportElement sre = (StaticTextReportElement)detailLabel.cloneMe();
680             sre.setPosition(new java.awt.Point JavaDoc(currentx,detailLabel.getPosition().y));
681             sre.setWidth(fwidth);
682             sre.updateBounds();
683             
684             //Get field description from datasource
685
//added by Felix Firgau on Oct, 11th 2006
686
String JavaDoc columnLabelText=f.getName();
687             if(ucwt.isSaveFieldDescriptions()) {
688                 String JavaDoc description = f.getDescription();
689                 if(description!=null && description.length() > 0)
690                     columnLabelText = description;
691             }
692             
693             sre.setText(""+ columnLabelText +"");
694             sre.setBand(columns);
695             template.getElements().addElement(sre);
696             
697             currentx += fwidth;
698             currentx = Math.min(template.getWidth() - template.getRightMargin()+10-30, currentx);
699         }
700         
701         // Remove template groups...****************
702
e = template.getElements().elements();
703         Vector JavaDoc elements_to_delete = new Vector JavaDoc();
704         while (e.hasMoreElements()) {
705             ReportElement rElement = (ReportElement)e.nextElement();
706             Group g = null;
707             if ((g=template.getGroupByName("Group1"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
708                 elements_to_delete.addElement(rElement);
709             else if ((g=template.getGroupByName("Group2"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
710                 elements_to_delete.addElement(rElement);
711             else if ((g=template.getGroupByName("Group3"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
712                 elements_to_delete.addElement(rElement);
713             else if ((g=template.getGroupByName("Group4"))!=null && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
714                 elements_to_delete.addElement(rElement);
715         }
716         e =elements_to_delete.elements();
717         while (e.hasMoreElements()) {
718             template.getElements().removeElement(e.nextElement());
719         }
720         
721         Group g;
722         if ((g=template.getGroupByName("Group1"))!=null) {
723             template.getBands().removeElement(g.getGroupFooter());
724             template.getBands().removeElement(g.getGroupHeader());
725             template.getGroups().removeElement(g);
726         }
727         if ((g=template.getGroupByName("Group2"))!=null) {
728             template.getBands().removeElement(g.getGroupFooter());
729             template.getBands().removeElement(g.getGroupHeader());
730             template.getGroups().removeElement(g);
731         }
732         if ((g=template.getGroupByName("Group3"))!=null) {
733             template.getBands().removeElement(g.getGroupFooter());
734             template.getBands().removeElement(g.getGroupHeader());
735             template.getGroups().removeElement(g);
736         }
737         
738         if ((g=template.getGroupByName("Group4"))!=null) {
739             template.getBands().removeElement(g.getGroupFooter());
740             template.getBands().removeElement(g.getGroupHeader());
741             template.getGroups().removeElement(g);
742         }
743         
744         // Positioning columns band...
745
/*
746         if ((g=template.getGroupByName("Columns"))!=null)
747         {
748                 template.getGroups().removeElement(g);
749                 template.getBands().removeElement(g.getGroupHeader());
750                 template.getBands().removeElement(g.getGroupFooter());
751                 template.addGroup(g);
752                 // we must adjust band position...
753         }
754          */

755         // Adjust vertical position of elements
756
e = template.getElements().elements();
757         while (e.hasMoreElements()) {
758             ReportElement rElement = (ReportElement)e.nextElement();
759             rElement.trasform(new java.awt.Point JavaDoc(0,+ template.getBandYLocation( rElement.getBand() )),TransformationType.TRANSFORMATION_MOVE );
760         }
761         
762         template.setQuery( ucwt.getQuery() );
763         template.setQueryLanguage( ucwt.getQuery_language() );
764         
765         return template;
766     }
767 }
768
Popular Tags