KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > Report


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * Report.java
28  *
29  * Created on 10 febbraio 2003, 19.32
30  *
31  */

32
33 package it.businesslogic.ireport;
34
35 //import java.util.*;
36
import it.businesslogic.ireport.ReportWriter;
37 import it.businesslogic.ireport.chart.SectionItemHyperlink;
38 import it.businesslogic.ireport.crosstab.CrosstabParameter;
39 import it.businesslogic.ireport.gui.MainFrame;
40 import it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedEvent;
41 import it.businesslogic.ireport.gui.event.ReportElementChangedEvent;
42 import it.businesslogic.ireport.gui.event.ReportSubDatasetChangedEvent;
43 import it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent;
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.util.Vector JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Properties JavaDoc;
48 import java.util.Enumeration JavaDoc;
49
50 //import it.businesslogic.ireport.util.*;
51
import it.businesslogic.ireport.util.Misc;
52 import it.businesslogic.ireport.util.PageSize;
53 import it.businesslogic.ireport.gui.JReportFrame;
54 import it.businesslogic.ireport.chart.*;
55 import it.businesslogic.ireport.crosstab.CrosstabCell;
56 import it.businesslogic.ireport.crosstab.CrosstabColumnGroup;
57 import it.businesslogic.ireport.crosstab.CrosstabGroup;
58 import it.businesslogic.ireport.crosstab.CrosstabRowGroup;
59 import it.businesslogic.ireport.crosstab.Measure;
60 import it.businesslogic.ireport.gui.event.StyleChangedEvent;
61 import it.businesslogic.ireport.gui.event.SubDatasetObjectChangedListener;
62 import net.sf.jasperreports.engine.JRGroup;
63
64
65 import org.apache.xerces.parsers.DOMParser;
66
67 //import org.w3c.dom.*;
68
import org.w3c.dom.Document JavaDoc;
69 import org.w3c.dom.NamedNodeMap JavaDoc;
70 import org.w3c.dom.Node JavaDoc;
71 import org.w3c.dom.NodeList JavaDoc;
72
73 import org.xml.sax.SAXException JavaDoc;
74
75 import java.io.IOException JavaDoc;
76 import java.io.File JavaDoc;
77 import java.io.PrintWriter JavaDoc;
78
79 /**
80  *
81  * @author Administrator
82  */

83 public class Report extends SubDataset implements SubDatasetObjectChangedListener {
84
85     public static int SCRIPTLET_IREPORT_INTERNAL_HANDLING = 1;
86     public static int SCRIPTLET_NOT_USED = 0;
87     public static int SCRIPTLET_CLASSIC_HANDLING = 2;
88
89     private int nextGroupId = 0;
90
91     private IReportFont defaultFont = new IReportFont();
92
93     private boolean readOnly = false;
94
95     private int height = 0;
96
97     private int width = 0;
98
99     private Vector JavaDoc elements = new Vector JavaDoc();
100
101     private Vector JavaDoc bands = new Vector JavaDoc();
102
103     private Vector JavaDoc imports = new Vector JavaDoc();
104
105     private String JavaDoc filename = "";
106
107     private Vector JavaDoc subDatasets = new Vector JavaDoc();
108
109     private String JavaDoc encoding = "UTF-8";
110
111     private int columnCount = 1;
112
113     private String JavaDoc printOrder = "Vertical";
114
115     private String JavaDoc orientation = "Portrait";
116
117     private String JavaDoc whenNoDataType = "NoPages";
118
119     private int columnWidth = 535;
120
121     private int columnSpacing = 0 ;
122
123     private int leftMargin = 20;
124
125     private int rightMargin = 20;
126
127     private int topMargin = 30;
128
129     private int bottomMargin = 30;
130
131     private boolean isTitleNewPage = false;
132
133     private boolean isSummaryNewPage = false;
134
135     private long loadTime = 0;
136
137     private Vector JavaDoc fonts = new Vector JavaDoc();
138
139     private Vector JavaDoc styles = new Vector JavaDoc();
140
141     private JReportFrame reportFrame;
142
143     private String JavaDoc reportFormat;
144
145     static int untitledId = 1;
146
147     private int reportChanges = 1; // By default, a report is changed...
148

149     private boolean floatColumnFooter = false;
150
151     private boolean ignorePagination = false;
152
153     /** used to tell the report when outputting to XML that it needs to set isCode="true" in expressions */
154     protected boolean usingMultiLineExpressions = false;
155
156     private String JavaDoc language = "java";
157
158     private ScriptletCode scripletCode;
159
160     private int scriptletHandling = 2;
161
162     private java.util.HashMap JavaDoc proposedValues = null;
163
164     private boolean loadingFile = false;
165     
166     private String JavaDoc formatFactoryClass = "";
167
168     /** Creates a new instance of Report */
169
170     public Report() {
171         super();
172         setName(it.businesslogic.ireport.util.I18n.getString("untitledReport", "untitled_report_") + untitledId);
173         untitledId++;
174
175         proposedValues = new java.util.HashMap JavaDoc();
176         // Add basic variables...
177

178
179         getKeywordLookup().addKeyword("$R{*}");
180
181         //addParameter(new JRParameter("COLUMN_COUNT", "", ));
182

183         bands.addElement( new Band(this,"background",0) );
184         bands.addElement( new Band(this,"title", 50));
185         bands.addElement( new Band(this,"pageHeader", 50) );
186         bands.addElement( new Band(this,"columnHeader",30) );
187         bands.addElement( new Band(this,"detail",100));
188         bands.addElement( new Band(this,"columnFooter",30) );
189         bands.addElement( new Band(this,"pageFooter",50));
190         bands.addElement( new Band(this,"lastPageFooter",50));
191         bands.addElement( new Band(this,"summary",50) );
192
193         //addImport("it.businesslogic.ireport.IReportScriptlet");
194
addImport("java.util.*");
195         addImport("net.sf.jasperreports.engine.*");
196         addImport("net.sf.jasperreports.engine.data.*");
197
198         try {
199             scripletCode = new ScriptletCode(ScriptletCode.class.getClassLoader().getResourceAsStream("scriptlet_template.jav"));
200         } catch (Exception JavaDoc ex) {
201             ex.printStackTrace();
202         }
203
204         // Set the user option default...
205
this.setLanguage( it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("DefaultScriptingLanguage","java") );
206
207         this.addSubDatasetObjectChangedListener(this);
208         
209         // Add default style...
210
String JavaDoc rDefStyle = it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("DefaultStyle","");
211         if (rDefStyle.length() > 0)
212         {
213             Vector JavaDoc stylesLib = (Vector JavaDoc)it.businesslogic.ireport.gui.MainFrame.getMainInstance().getStyleLibrarySet();
214             for (int i=0; i<stylesLib.size(); ++i)
215             {
216                 Style s = (Style)stylesLib.get(i);
217                 if (s.getName() != null && s.getName().equals(rDefStyle))
218                 {
219                     Style s1 = new Style(s);
220                     s1.getAttributes().put( s1.ATTRIBUTE_isDefault, "true" );
221                     this.addStyle( s1 );
222                 }
223             }
224             
225         }
226     }
227
228     /** Creates a new instance of Report reading it by a file...*/
229     public Report(String JavaDoc xmlFile) throws Exception JavaDoc {
230
231         this();
232
233         untitledId--;
234         
235         // Remove default style...
236
while (getStyles().size() > 0)
237         {
238             this.removeStyle( (Style) this.getStyles().get(0) );
239         }
240         
241         // Set the real default...
242
ReportReader rr = new ReportReader(this);
243         rr.readFile( xmlFile );
244     }
245
246
247
248         static public String JavaDoc readPCDATA(Node JavaDoc textNode) {
249         return readPCDATA(textNode,true);
250     }
251
252     static public String JavaDoc readPCDATA(Node JavaDoc textNode, boolean trim) {
253         NodeList JavaDoc list_child = textNode.getChildNodes();
254         for (int ck=0; ck< list_child.getLength(); ck++) {
255             Node JavaDoc child_child = (Node JavaDoc)list_child.item(ck);
256
257             // --- start solution: if there is another node this should be the PCDATA-node
258
Node JavaDoc ns = child_child.getNextSibling();
259             if (ns != null)
260             child_child = ns;
261             // --- end solution
262

263             final short nt = child_child.getNodeType();
264             if ((nt == Node.CDATA_SECTION_NODE) || (nt == Node.TEXT_NODE)) {
265                if (trim) return ((String JavaDoc)child_child.getNodeValue()).trim();
266                 return (String JavaDoc)child_child.getNodeValue();
267             }
268         }
269         return "";
270     }
271
272      static java.awt.Color JavaDoc decodeColor(String JavaDoc colorString)
273     {
274         java.awt.Color JavaDoc color = null;
275         char firstChar = colorString.charAt(0);
276         if (firstChar == '#')
277         {
278                color = new java.awt.Color JavaDoc(Integer.parseInt(colorString.substring(1), 16));
279         }
280         else if ('0' <= firstChar && firstChar <= '9')
281         {
282                color = new java.awt.Color JavaDoc(Integer.parseInt(colorString));
283         }
284         else
285         {
286                 if (net.sf.jasperreports.engine.xml.JRXmlConstants.getColorMap().containsKey(colorString))
287                 {
288                         color = (java.awt.Color JavaDoc)net.sf.jasperreports.engine.xml.JRXmlConstants.getColorMap().get(colorString);
289                 }
290                 else
291                 {
292                         color = java.awt.Color.black;
293                 }
294         }
295         return color;
296
297     }
298
299     public String JavaDoc createChildGroup(String JavaDoc parentElementGroup)
300     {
301         nextGroupId++;
302         if (parentElementGroup.length() > 0 ) parentElementGroup += ".";
303         return parentElementGroup + nextGroupId;
304     }
305
306     /** Getter for property bands.
307      * @return Value of property bands.
308      *
309      */

310     public java.util.Vector JavaDoc getBands() {
311         return bands;
312     }
313
314     /** Setter for property bands.
315      * @param bands New value of property bands.
316      *
317      */

318     public void setBands(java.util.Vector JavaDoc bands) {
319         this.bands = bands;
320         incrementReportChanges();
321     }
322
323     /** Getter for property bottomMargin.
324      * @return Value of property bottomMargin.
325      *
326      */

327     public int getBottomMargin() {
328         return bottomMargin;
329     }
330
331     /** Setter for property bottomMargin.
332      * @param bottomMargin New value of property bottomMargin.
333      *
334      */

335     public void setBottomMargin(int bottomMargin) {
336         if (this.bottomMargin == bottomMargin) return;
337         this.bottomMargin = bottomMargin;
338         incrementReportChanges();
339     }
340
341     /** Getter for property columnCount.
342      * @return Value of property columnCount.
343      *
344      */

345     public int getColumnCount() {
346         return columnCount;
347     }
348
349     /** Setter for property columnCount.
350      * @param columnCount New value of property columnCount.
351      *
352      */

353     public void setColumnCount(int columnCount) {
354         if (this.columnCount == columnCount) return;
355         this.columnCount = columnCount;
356          incrementReportChanges();
357     }
358
359     /** Getter for property columnSpacing.
360      * @return Value of property columnSpacing.
361      *
362      */

363     public int getColumnSpacing() {
364         return columnSpacing;
365     }
366
367     /** Setter for property columnSpacing.
368      * @param columnSpacing New value of property columnSpacing.
369      *
370      */

371     public void setColumnSpacing(int columnSpacing) {
372         if (this.columnSpacing == columnSpacing) return;
373         this.columnSpacing = columnSpacing;
374         incrementReportChanges();
375     }
376
377     /** Getter for property columnWidth.
378      * @return Value of property columnWidth.
379      *
380      */

381     public int getColumnWidth() {
382         return columnWidth;
383     }
384
385     /** Setter for property columnWidth.
386      * @param columnWidth New value of property columnWidth.
387      *
388      */

389     public void setColumnWidth(int columnWidth) {
390         if (this.columnWidth == columnWidth) return;
391         this.columnWidth = columnWidth;
392         incrementReportChanges();
393     }
394
395     /** Getter for property elements.
396      * @return Value of property elements.
397      *
398      */

399     public java.util.Vector JavaDoc getElements() {
400         return elements;
401     }
402
403     /** Setter for property elements.
404      * @param elements New value of property elements.
405      *
406      */

407     public void setElements(java.util.Vector JavaDoc elements) {
408         this.elements = elements;
409         incrementReportChanges();
410     }
411
412     /** Getter for property encoding.
413      * @return Value of property encoding.
414      *
415      */

416     public java.lang.String JavaDoc getEncoding() {
417         return encoding;
418     }
419
420     /** Setter for property encoding.
421      * @param encoding New value of property encoding.
422      *
423      */

424     public void setEncoding(java.lang.String JavaDoc encoding) {
425         if ((this.encoding == null) ? encoding == null : this.encoding.equals(encoding)) return;
426         this.encoding = encoding;
427         incrementReportChanges();
428     }
429
430
431
432
433
434     /** Getter for property filename.
435      * @return Value of property filename.
436      *
437      */

438     public java.lang.String JavaDoc getFilename() {
439         return filename;
440     }
441
442     /** Setter for property filename.
443      * @param filename New value of property filename.
444      *
445      */

446     public void setFilename(java.lang.String JavaDoc filename) {
447         this.filename = filename;
448     }
449
450     /** Getter for property fonts.
451      * @return Value of property fonts.
452      *
453      */

454     public java.util.Vector JavaDoc getFonts() {
455         return fonts;
456     }
457
458     /** Setter for property fonts.
459      * @param fonts New value of property fonts.
460      *
461      */

462     public void setFonts(java.util.Vector JavaDoc fonts) {
463         this.fonts = fonts;
464         incrementReportChanges();
465     }
466
467
468
469     /** Getter for property height.
470      * @return Value of property height.
471      *
472      */

473     public int getHeight() {
474         return height;
475     }
476
477     /** Setter for property height.
478      * @param height New value of property height.
479      *
480      */

481     public void setHeight(int height) {
482         if (this.height == height) return;
483         this.height = height;
484         incrementReportChanges();
485     }
486
487     /** Getter for property isSummaryNewPage.
488      * @return Value of property isSummaryNewPage.
489      *
490      */

491     public boolean isIsSummaryNewPage() {
492         return isSummaryNewPage;
493     }
494
495     /** Setter for property isSummaryNewPage.
496      * @param isSummaryNewPage New value of property isSummaryNewPage.
497      *
498      */

499     public void setIsSummaryNewPage(boolean isSummaryNewPage) {
500         if (this.isSummaryNewPage == isSummaryNewPage) return;
501         this.isSummaryNewPage = isSummaryNewPage;
502         incrementReportChanges();
503     }
504
505     /** Getter for property isTitleNewPage.
506      * @return Value of property isTitleNewPage.
507      *
508      */

509     public boolean isIsTitleNewPage() {
510         return isTitleNewPage;
511     }
512
513     /** Setter for property isTitleNewPage.
514      * @param isTitleNewPage New value of property isTitleNewPage.
515      *
516      */

517     public void setIsTitleNewPage(boolean isTitleNewPage) {
518         if (this.isTitleNewPage == isTitleNewPage) return;
519         this.isTitleNewPage = isTitleNewPage;
520         incrementReportChanges();
521     }
522
523     /** Getter for property leftMargin.
524      * @return Value of property leftMargin.
525      *
526      */

527     public int getLeftMargin() {
528         return leftMargin;
529     }
530
531     /** Setter for property leftMargin.
532      * @param leftMargin New value of property leftMargin.
533      *
534      */

535     public void setLeftMargin(int leftMargin) {
536         if (this.leftMargin == leftMargin) return;
537         this.leftMargin = leftMargin;
538         incrementReportChanges();
539     }
540
541     /** Getter for property loadTime.
542      * @return Value of property loadTime.
543      *
544      */

545     public long getLoadTime() {
546         return loadTime;
547     }
548
549     /** Setter for property loadTime.
550      * @param loadTime New value of property loadTime.
551      *
552      */

553     public void setLoadTime(long loadTime) {
554         this.loadTime = loadTime;
555     }
556
557     /** Getter for property orientation.
558      * @return Value of property orientation.
559      *
560      */

561     public java.lang.String JavaDoc getOrientation() {
562         return orientation;
563     }
564
565     /** Setter for property orientation.
566      * @param orientation New value of property orientation.
567      *
568      */

569     public void setOrientation(java.lang.String JavaDoc orientation) {
570         if ((this.orientation == null) ? orientation == null : this.orientation.equals(orientation)) return;
571         this.orientation = orientation;
572         incrementReportChanges();
573     }
574
575     /** Getter for property printOrder.
576      * @return Value of property printOrder.
577      *
578      */

579     public java.lang.String JavaDoc getPrintOrder() {
580         return printOrder;
581     }
582
583     /** Setter for property printOrder.
584      * @param printOrder New value of property printOrder.
585      *
586      */

587     public void setPrintOrder(java.lang.String JavaDoc printOrder) {
588          if ((this.printOrder == null) ? printOrder == null : this.printOrder.equals(printOrder)) return;
589         this.printOrder = printOrder;
590         incrementReportChanges();
591     }
592
593     /** Getter for property rightMargin.
594      * @return Value of property rightMargin.
595      *
596      */

597     public int getRightMargin() {
598         return rightMargin;
599     }
600
601     /** Setter for property rightMargin.
602      * @param rightMargin New value of property rightMargin.
603      *
604      */

605     public void setRightMargin(int rightMargin) {
606         if (this.rightMargin == rightMargin) return;
607         this.rightMargin = rightMargin;
608         incrementReportChanges();
609     }
610
611
612
613     /** Getter for property topMargin.
614      * @return Value of property topMargin.
615      *
616      */

617     public int getTopMargin() {
618         return topMargin;
619     }
620
621     /** Setter for property topMargin.
622      * @param topMargin New value of property topMargin.
623      *
624      */

625     public void setTopMargin(int topMargin) {
626         if (this.topMargin == topMargin) return;
627         this.topMargin = topMargin;
628         incrementReportChanges();
629     }
630
631     public void addImport(String JavaDoc _import) {
632         if (!imports.contains(_import)) {
633             this.imports.add(_import);
634             incrementReportChanges();
635         }
636     }
637
638     /** Getter for property whenNoDataType.
639      * @return Value of property whenNoDataType.
640      *
641      */

642     public java.lang.String JavaDoc getWhenNoDataType() {
643         return whenNoDataType;
644     }
645
646     /** Setter for property whenNoDataType.
647      * @param whenNoDataType New value of property whenNoDataType.
648      *
649      */

650     public void setWhenNoDataType(java.lang.String JavaDoc whenNoDataType) {
651         if ((this.whenNoDataType == null) ? whenNoDataType == null : this.whenNoDataType.equals(whenNoDataType)) return;
652         this.whenNoDataType = whenNoDataType;
653         incrementReportChanges();
654     }
655
656     /** Getter for property width.
657      * @return Value of property width.
658      *
659      */

660     public int getWidth() {
661         return width;
662     }
663
664     /** Setter for property width.
665      * @param width New value of property width.
666      *
667      */

668     public void setWidth(int width) {
669         if (this.width == width) return;
670         this.width = width;
671         incrementReportChanges();
672     }
673
674     /**
675      * This function compute the Y location of the band.
676      * In the designer panel, there is as space of 10 pixels (the space
677      * between the top border of the panel, and the begin of the
678      * virtual page) that are not added to this measure. This is
679      * important because permit to multiply this height for a
680      * zoom factor.
681      *
682      * @param b it.businesslogic.ireport.Band The band to compute.
683      */

684     public int getBandYLocation(Band b) {
685         int y = topMargin;
686
687         for (Iterator JavaDoc i = this.bands.iterator(); i.hasNext(); ) {
688             Band band = (Band) i.next();
689             if (band != b) {
690                 y += band.getHeight();
691             } else {
692                 break;
693             }
694         }
695
696         return y;
697     }
698
699     /** Getter for property reportFrame.
700      * @return Value of property reportFrame.
701      *
702      */

703     public it.businesslogic.ireport.gui.JReportFrame getReportFrame() {
704         return reportFrame;
705     }
706
707     /** Setter for property reportFrame.
708      * @param reportFrame New value of property reportFrame.
709      *
710      */

711     public void setReportFrame(it.businesslogic.ireport.gui.JReportFrame reportFrame) {
712         this.reportFrame = reportFrame;
713     }
714
715     /** Getter for property reportFormat.
716      * @return Value of property reportFormat.
717      *
718      */

719     public java.lang.String JavaDoc getReportFormat() {
720         return reportFormat;
721     }
722
723     /** Setter for property reportFormat.
724      * @param reportFormat New value of property reportFormat.
725      *
726      */

727     public void setReportFormat(java.lang.String JavaDoc reportFormat) {
728         this.reportFormat = reportFormat;
729     }
730
731     /**
732      * This method return the height needed by a graphics to
733      * draw all the document.
734      */

735     public int getDesignHeight() {
736         int y = topMargin + bottomMargin;
737
738         for (Iterator JavaDoc i = this.bands.iterator(); i.hasNext(); ) {
739             y += ((Band) i.next()).getHeight();
740         }
741
742         return y;
743     }
744
745     public int getDesignHeightNoBG() {
746         int y=0;
747         y += topMargin;
748         y += bottomMargin;
749
750         for (Iterator JavaDoc i = this.bands.iterator(); i.hasNext(); ) {
751             Band b = (Band) i.next();
752             if (!b.getName().equals("background"))
753                 y += b.getHeight();
754         }
755
756         return y;
757     }
758
759     /** Add a group to the reprot and perform an adjust on jReportFrame
760      * @param group New group to be inserted.
761      *
762      */

763     public void addGroup(Group grp) {
764         addGroup(grp,true);
765     }
766
767     /** Add a group to the reprot and perform an adjust on jReportFrame if
768      * specified
769      * @param group New group to be inserted.
770      * @param adjust If true, perform an adjust on jReportFrame
771      */

772     public void addGroup(Group grp, boolean adjust) {
773         incrementReportChanges();
774         if (!getGroups().contains(grp)) {
775             getGroups().addElement(grp);
776         }
777         String JavaDoc bandsstr = "";
778
779         /*
780          * start looking for the detail band.
781          * and place header and footer of new group around it if (Adjust)
782          */

783         for (int i = 0; i < bands.size(); i++) {
784             Band b = (Band)bands.elementAt(i);
785
786             if (b.getName().equals("detail")) {
787
788                 if (adjust) {
789
790                     int bandYLocation = getBandYLocation(b);
791
792                     for (Iterator JavaDoc i2 = elements.iterator(); i2.hasNext(); ) {
793                         ReportElement elem = (ReportElement) i2.next();
794                         int delta_y = grp.getGroupHeader().getHeight();
795
796                         if (elem.band == grp.getGroupHeader() || elem.band == grp.getGroupFooter()) {
797                             // Do nothing
798
}
799                         else if (elem.position.y >= bandYLocation + 10 + b.getHeight()) {
800                             delta_y += grp.getGroupFooter().getHeight();
801                             elem.trasform(new java.awt.Point JavaDoc(0, delta_y), TransformationType.TRANSFORMATION_MOVE);
802                         }
803                         else if (elem.position.y >= bandYLocation + 10) {
804                             elem.trasform(new java.awt.Point JavaDoc(0, delta_y), TransformationType.TRANSFORMATION_MOVE);
805                         }
806                     }
807                 }
808
809                 // All elements with top > detail.top must be shifted of groupHeader height
810
bands.insertElementAt(grp.getGroupHeader(),i);
811                 // All elements with top > detail.top + detail.bottom must be shifted of groupFooter height
812
bands.insertElementAt(grp.getGroupFooter(),i+2);
813
814                 break;
815             }
816         }
817         addVariable(new it.businesslogic.ireport.JRVariable(grp.getName()+"_COUNT","java.lang.Integer", true));
818
819     }
820
821     /**
822      * grpOne will be palce within grpTwo
823      * This means the header band of grpOne will come after the header band of grpTwo
824      * And the footer band of grpOne will come before the footer band of grpTwo
825      * @author: Robert Lamping
826      * @param direction 1 = down, -1 -up
827      * @param grp The group that must be moved
828      */

829     public synchronized void moveGroup(Group grp, int direction ) {
830
831         Group grpOne = null;
832         Group grpTwo = null;
833
834         if ( getGroups().size() <=1 ) {
835             return; // nothing to do
836
}
837         incrementReportChanges();
838         if (direction == 1) {
839             // down
840
// there must be at least one element below:
841
if ( getGroups().lastElement() == grp ) {
842                 return ; // nothing to do
843
}
844             grpOne = grp;
845             grpTwo = (Group) getGroups().get( getGroups().indexOf(grp) + 1);
846
847         } else {
848             // up
849
// there must be at least one element above
850
if ( getGroups().firstElement() == grp ) {
851                 return ; // nothing to do
852
}
853             grpTwo = grp;
854             grpOne = (Group) getGroups().get( getGroups().indexOf(grp) - 1);
855         }
856
857         Band bandGrpOneHeader = getBandByName( grpOne.getGroupHeader().getName());
858         Band bandGrpTwoHeader = getBandByName( grpTwo.getGroupHeader().getName());
859         Band bandGrpOneFooter = getBandByName( grpOne.getGroupFooter().getName());
860         Band bandGrpTwoFooter = getBandByName( grpTwo.getGroupFooter().getName());
861
862         int delta1 = bandGrpOneHeader.getHeight();
863         int delta2 = bandGrpTwoHeader.getHeight();
864         int delta3 = bandGrpOneFooter.getHeight();
865         int delta4 = bandGrpTwoFooter.getHeight();
866
867         // Move the elements
868
for (Iterator JavaDoc i = elements.iterator(); i.hasNext(); ) {
869             ReportElement re = (ReportElement) i.next();
870
871             if (re.band == bandGrpOneHeader ) {
872                 re.trasform(new java.awt.Point JavaDoc(0, delta2 ), TransformationType.TRANSFORMATION_MOVE);
873             } else if (re.band == bandGrpTwoHeader ) {
874                 re.trasform(new java.awt.Point JavaDoc(0, -delta1 ), TransformationType.TRANSFORMATION_MOVE);
875             } else if ( re.band == bandGrpTwoFooter ) {
876                 re.trasform(new java.awt.Point JavaDoc(0, delta3 ), TransformationType.TRANSFORMATION_MOVE);
877             } else if ( re.band == bandGrpOneFooter ) {
878                 re.trasform(new java.awt.Point JavaDoc(0, -delta4 ), TransformationType.TRANSFORMATION_MOVE);
879             }
880         }
881
882         // Now move the groups withing the bands array.
883
// Move Band Group Headers
884
int pos1 = bands.indexOf( bandGrpOneHeader);
885         Band bandBuffer = (Band) bands.get(pos1);
886         bands.remove(pos1);
887         bands.insertElementAt(bandBuffer, pos1+1);
888
889         // Move Band Group Footers
890
int pos3 = bands.indexOf( bandGrpTwoFooter);
891         bandBuffer = (Band) bands.get(pos3);
892         bands.remove(pos3);
893         bands.insertElementAt(bandBuffer, pos3+1);
894
895         // Now move the groups within the group array
896
int groupPos = getGroups().indexOf( grpOne );
897         // groupTwo is always after grpOne
898
Group groupBuffer = (Group) getGroups().get( groupPos);
899         getGroups().remove(groupPos);
900         getGroups().insertElementAt( groupBuffer, groupPos + 1);
901
902     }
903
904     public void delGroup(Group grp) {
905         if (!getGroups().contains(grp)) return;
906         incrementReportChanges();
907
908         String JavaDoc bandsstr = "";
909
910         for (int i=0; i<getVariables().size(); ++i)
911         {
912             JRVariable var = (JRVariable)getVariables().elementAt(i);
913             if (var.isBuiltin() && var.getName().equalsIgnoreCase(grp.getName()+"_COUNT"))
914             {
915                 removeVariable(var);
916                 break;
917             }
918         }
919
920         for (int i=0; i<elements.size(); ++i) {
921             // Remove all elements of this group...
922
ReportElement elem = (ReportElement)elements.elementAt(i);
923             if (elem.getBand() == grp.getGroupHeader() || elem.getBand() == grp.getGroupFooter()) {
924                 elements.removeElementAt(i);
925                 i--;
926                 continue;
927             }
928             // Adjust elements posisitions...
929
// Il element
930
int delta_y = grp.getGroupHeader().getHeight();
931             if (elem.position.y >= getBandYLocation(grp.getGroupFooter())+10+grp.getGroupFooter().getHeight()) {
932                 delta_y += grp.getGroupFooter().getHeight();
933                 elem.trasform(new java.awt.Point JavaDoc(0, -delta_y), TransformationType.TRANSFORMATION_MOVE);
934             }
935             else if (elem.position.y >= getBandYLocation(grp.getGroupHeader())+10+grp.getGroupHeader().getHeight()) {
936                 elem.trasform(new java.awt.Point JavaDoc(0, -delta_y), TransformationType.TRANSFORMATION_MOVE);
937             }
938         }
939
940         bands.removeElement(grp.getGroupHeader());
941         bands.removeElement(grp.getGroupFooter());
942         getGroups().removeElement(grp);
943     }
944
945     public Group getGroupByName(String JavaDoc name) {
946         for (int i = 0; i < getGroups().size(); i++) {
947             Group grp = (Group)getGroups().elementAt(i);
948             if (grp.getName().equals(name)) {
949                 return grp;
950             }
951         }
952         return null;
953     }
954
955     public Band getBandByName(String JavaDoc name) {
956         for (int i=0; i< bands.size(); i++) {
957             Band band = (Band)bands.elementAt(i);
958             if (band.getName().equals(name)) {
959                 return band;
960             }
961         }
962         return null;
963     }
964
965
966
967     public void saveXMLFile() {
968         saveXMLFile( this.getFilename());
969
970     }
971
972     public void saveXMLFile( String JavaDoc aFilename ) {
973         if(isReadOnly()){
974             javax.swing.JOptionPane.showMessageDialog(getReportFrame().getMainFrame(),
975             it.businesslogic.ireport.util.I18n.getString("fileReadOnly", "File is read only and cannot be saved."),
976             it.businesslogic.ireport.util.I18n.getString("save","Save"),javax.swing.JOptionPane.INFORMATION_MESSAGE);
977             return;
978         }
979
980         ReportWriter wr = new ReportWriter( this );
981         wr.saveXMLFile( aFilename );
982     }
983
984
985
986     /**
987      * Getter for property usingMultiLineExpressions.
988      * @return Value of property usingMultiLineExpressions.
989      */

990     public boolean isUsingMultiLineExpressions() {
991         return false; //usingMultiLineExpressions;
992
}
993
994     /**
995      * Setter for property usingMultiLineExpressions.
996      * @param usingMultiLineExpressions New value of property usingMultiLineExpressions.
997      */

998     public void setUsingMultiLineExpressions(boolean usingMultiLineExpressions) {
999         this.usingMultiLineExpressions = false; //usingMultiLineExpressions;
1000
}
1001
1002
1003
1004    /** Getter for property scripletCode.
1005     * @return Value of property scripletCode.
1006     *
1007     */

1008    public it.businesslogic.ireport.ScriptletCode getScripletCode() {
1009        return scripletCode;
1010    }
1011
1012    /** Setter for property scripletCode.
1013     * @param scripletCode New value of property scripletCode.
1014     *
1015     */

1016    public void setScripletCode(it.businesslogic.ireport.ScriptletCode scripletCode) {
1017        this.scripletCode = scripletCode;
1018    }
1019
1020    /** Getter for property scriptletHandling.
1021     * @return Value of property scriptletHandling.
1022     *
1023     */

1024    public int getScriptletHandling() {
1025        return scriptletHandling;
1026    }
1027
1028    /** Setter for property scriptletHandling.
1029     * @param scriptletHandling New value of property scriptletHandling.
1030     *
1031     */

1032    public void setScriptletHandling(int scriptletHandling) {
1033        if (this.scriptletHandling == scriptletHandling) return;
1034        this.scriptletHandling = scriptletHandling;
1035        incrementReportChanges();
1036
1037    }
1038
1039
1040    public String JavaDoc getScriptletFileName() {
1041        return this.getScriptletFileName( this.getFilename() );
1042    }
1043
1044    public String JavaDoc getScriptletFileName(String JavaDoc file) {
1045        if (file == null) return "Scriptlet.java";
1046
1047        // we assume /xxx/yyY/qqq/myreport.xml as input file name
1048
if (file.length() > 3) {
1049            file = Misc.changeFileExtension(file, "");
1050            file = file.substring(0, file.length() -1 );
1051            File JavaDoc f = new File JavaDoc( file );
1052            String JavaDoc className = f.getName() + "Scriptlet";
1053
1054            className = Misc.string_replace("_",".", className);
1055            className = Misc.string_replace("_"," ",className);
1056            if ( Character.isDigit(className.charAt( 0 ) )) {
1057                className = "_"+ className;
1058            }
1059            className += ".java";
1060            return f.getPath().substring(0, f.getPath().length() - f.getName().length()) + className;
1061        }
1062
1063        return "Scriptlet.java";
1064    }
1065
1066
1067    /**
1068     * Default font specified in xml file.
1069     * @return
1070     */

1071    public IReportFont getDefaultFont() {
1072        return defaultFont;
1073    }
1074
1075    public boolean isReadOnly(){
1076        return readOnly;
1077    }
1078    public void checkReadOnlyFlag(){
1079        checkReadOnlyFlag(getFilename());
1080    }
1081    public void checkReadOnlyFlag(String JavaDoc oF){
1082        if (oF == null) return;
1083        File JavaDoc f = new File JavaDoc(oF);
1084        try {
1085        if (!f.exists()) {
1086            readOnly = false;
1087        } else {
1088            readOnly = !f.canWrite();
1089        }
1090        } catch (Exception JavaDoc ex) { }
1091        readOnly = false;
1092    }
1093
1094    /**
1095     * return the band that encloses the parameter originY
1096     * @param originY
1097     * @return
1098     *
1099     * an originY < 0 will return nothing
1100     * an originY > designheight will return the Summary band
1101     */

1102    public Band getBandByPosition( int originY ){
1103        Band bname = null;
1104        for (Iterator JavaDoc i = this.getBands().iterator(); i.hasNext(); ) {
1105            Band b = (Band) i.next();
1106            if ( this.getBandYLocation(b) + 10 <= originY) {
1107                bname = b;
1108            }
1109        }
1110        if (bname != null && (this.getBandYLocation(bname) + 10 + bname.getHeight() < originY)) return null;
1111        return bname;
1112    }
1113
1114    public Vector JavaDoc getImports() {
1115        return imports;
1116    }
1117
1118    public void setImports(Vector JavaDoc imports) {
1119        this.imports = imports;
1120        incrementReportChanges();
1121    }
1122
1123    public boolean isFloatColumnFooter() {
1124        return floatColumnFooter;
1125    }
1126
1127    public void setFloatColumnFooter(boolean floatColumnFooter) {
1128        if (this.floatColumnFooter == floatColumnFooter) return;
1129        this.floatColumnFooter = floatColumnFooter;
1130        incrementReportChanges();
1131    }
1132
1133    public String JavaDoc getLanguage() {
1134        return language;
1135    }
1136
1137    public void setLanguage(String JavaDoc language) {
1138        if ((this.language == null) ? language == null : this.language.equals(language)) return;
1139        this.language = language;
1140        incrementReportChanges();
1141    }
1142
1143    public java.util.HashMap JavaDoc getProposedValues() {
1144        return proposedValues;
1145    }
1146
1147    public void setProposedValues(java.util.HashMap JavaDoc proposedValues) {
1148        this.proposedValues = proposedValues;
1149    }
1150
1151    /**
1152     * Recalculate the Columnwidth bases on the available space.
1153     * Introduces at June, 6 to allow PageTransformation.java to calculate the column width again.
1154     *
1155    */

1156    public void recalcColumnWidth() {
1157
1158        //this.columns = (int)jNumberFieldColumns.getValue();
1159
// calculate space...
1160
// this.setColumnCount( report.getColumnCount() );
1161
// this.setColumnSpacing( report.getColumnSpacing());
1162

1163        int avail = getWidth() - getLeftMargin() - getRightMargin() - (getColumnCount()-1) * getColumnSpacing();
1164        int dim = avail;
1165        dim = (int)( (double)dim / (double)this.getColumnCount());
1166
1167        while ( (dim*this.getColumnCount()) > avail) {
1168            dim--;
1169        }
1170        this.setColumnWidth(dim);
1171    }
1172
1173    public IReportFont getReportFontByName(String JavaDoc name)
1174    {
1175        if (name == null || name.length() == 0) return null;
1176        for (int i=0; i<getFonts().size(); ++i)
1177        {
1178                IReportFont font = (IReportFont)getFonts().elementAt(i);
1179                if (font.getReportFont().equals(name))
1180                {
1181                    return font;
1182                }
1183        }
1184        return null;
1185    }
1186
1187    public Vector JavaDoc getStyles() {
1188        return styles;
1189    }
1190
1191    public void setStyles(Vector JavaDoc styles) {
1192        this.styles = styles;
1193        incrementReportChanges();
1194    }
1195
1196    public void elementGroupResort(Band band)
1197    {
1198
1199        it.businesslogic.ireport.util.TreeNode root = extractBandElementsTree(band, true);
1200        addElementsTree( root );
1201        incrementReportChanges();
1202    }
1203
1204    public it.businesslogic.ireport.util.TreeNode extractBandElementsTree(Band band, boolean remove)
1205    {
1206        java.util.List JavaDoc tmpElements = new java.util.ArrayList JavaDoc();
1207        it.businesslogic.ireport.util.TreeNode root = new it.businesslogic.ireport.util.TreeNode("");
1208        for (int i=0; i< getElements().size(); ++i)
1209        {
1210            ReportElement element = (ReportElement)getElements().elementAt(i);
1211            if (element.getBand() == band)
1212            {
1213                tmpElements.add(element);
1214                if (remove)
1215                {
1216                    getElements().removeElementAt(i);
1217                    i--;
1218                }
1219            }
1220        }
1221
1222        if (tmpElements.size() > 0)
1223        {
1224            // we have to reorder all....
1225
for (int i=0; i< tmpElements.size(); ++i)
1226            {
1227                ReportElement element = (ReportElement)tmpElements.get(i);
1228                String JavaDoc elementGroup = element.getElementGroup();
1229                try {
1230                        if (element.getParentElement() != null)
1231                        {
1232                            it.businesslogic.ireport.util.TreeNode tmpRoot = findElementInTreeNode(root,element.getParentElement());
1233                            if (tmpRoot != null) addElementToGroup(tmpRoot, elementGroup, element );
1234                        }
1235                        else
1236                        {
1237                            addElementToGroup(root, elementGroup, element );
1238                        }
1239                } catch (Exception JavaDoc ex)
1240                {
1241                      ex.printStackTrace();
1242                }
1243            }
1244        }
1245
1246        return root;
1247    }
1248
1249
1250    public void moveElementDown(ReportElement element)
1251    {
1252        incrementReportChanges();
1253        it.businesslogic.ireport.util.TreeNode root = extractBandElementsTree(element.getBand(), true);
1254        root.moveDown( element );
1255        addElementsTree( root );
1256    }
1257
1258    public void moveElementUp(ReportElement element)
1259    {
1260        incrementReportChanges();
1261
1262         it.businesslogic.ireport.util.TreeNode root = extractBandElementsTree(element.getBand(), true);
1263        root.moveUp( element );
1264        addElementsTree( root );
1265
1266    }
1267
1268    private void addElementsTree(it.businesslogic.ireport.util.TreeNode root)
1269    {
1270        for (int i=0; i< root.getChilds().size(); ++i)
1271        {
1272            it.businesslogic.ireport.util.TreeNode node =(it.businesslogic.ireport.util.TreeNode)(root.getChilds().get(i));
1273            Object JavaDoc obj = node.getUserObject();
1274            if (obj instanceof ReportElement)
1275            {
1276                getElements().add( obj );
1277                if (obj instanceof FrameReportElement)
1278                {
1279                    addElementsTree( node );
1280                }
1281            }
1282            else
1283            {
1284                addElementsTree( node );
1285            }
1286        }
1287    }
1288    /*
1289    public void addElementToGroup(NamedList parentNode, String subGroup, ReportElement element)
1290    {
1291        // 1. We have to find the group node...
1292        if (subGroup.equals(""))
1293        {
1294            parentNode.add( element);
1295            return;
1296        }
1297
1298        String levelGroupName = "";
1299        if (subGroup.indexOf(".")>=0)
1300        {
1301            levelGroupName = subGroup.substring(0, subGroup.indexOf("."));
1302            subGroup = subGroup.substring(subGroup.indexOf(".")+1);
1303        }
1304        else
1305        {
1306            levelGroupName = subGroup;
1307            subGroup = "";
1308        }
1309
1310        // 2. Look for the node named levelGroupName
1311        for (int i =0; i<parentNode.size(); ++i)
1312        {
1313            Object node = parentNode.get(i);
1314            if (node instanceof NamedList)
1315            {
1316                NamedList nl = (NamedList)node;
1317                if (nl.getName().equals( levelGroupName ))
1318                {
1319                    addElementToGroup(nl, subGroup, element);
1320                    return;
1321                }
1322            }
1323        }
1324
1325        // Node doesn't exists....
1326        NamedList nl = new NamedList(levelGroupName);
1327        addElementToGroup(nl, subGroup, element);
1328        parentNode.add( nl );
1329    }
1330    */

1331
1332    public void addElementToGroup(it.businesslogic.ireport.util.TreeNode parentNode, String JavaDoc subGroup, ReportElement element)
1333    {
1334        incrementReportChanges();
1335        // 1. We have to find the group node...
1336
if (subGroup.equals(""))
1337        {
1338            parentNode.addChild(new it.businesslogic.ireport.util.TreeNode( element ));
1339            return;
1340        }
1341
1342        String JavaDoc levelGroupName = "";
1343        if (subGroup.indexOf(".")>=0)
1344        {
1345            levelGroupName = subGroup.substring(0, subGroup.indexOf("."));
1346            subGroup = subGroup.substring(subGroup.indexOf(".")+1);
1347        }
1348        else
1349        {
1350            levelGroupName = subGroup;
1351            subGroup = "";
1352        }
1353
1354        // 2. Look for the node named levelGroupName
1355
for (int i =0; i<parentNode.getChilds().size(); ++i)
1356        {
1357            it.businesslogic.ireport.util.TreeNode node = (it.businesslogic.ireport.util.TreeNode)(parentNode.getChilds().get(i));
1358            if (node.getName().equals( levelGroupName ) )
1359            {
1360                 addElementToGroup(node, subGroup, element);
1361                 return;
1362            }
1363        }
1364
1365        // Node doesn't exists....
1366
it.businesslogic.ireport.util.TreeNode nl = new it.businesslogic.ireport.util.TreeNode(levelGroupName);
1367        addElementToGroup(nl, subGroup, element);
1368        parentNode.addChild( nl );
1369    }
1370
1371     public it.businesslogic.ireport.util.TreeNode findElementInTreeNode(it.businesslogic.ireport.util.TreeNode parentNode, ReportElement re)
1372    {
1373        for (int k=0; k<parentNode.getChilds().size(); ++k) {
1374            it.businesslogic.ireport.util.TreeNode child = (it.businesslogic.ireport.util.TreeNode)(parentNode.getChilds().get(k));
1375            if (child.getUserObject() == re) {
1376                return child;
1377            }
1378            if (re.getElementGroup().equals("") || re.getParentElement() != null)
1379            {
1380                if (child.getUserObject() instanceof FrameReportElement ||
1381                    child.getUserObject() instanceof String JavaDoc)
1382                {
1383                    it.businesslogic.ireport.util.TreeNode newchild = findElementInTreeNode(child,re);
1384                    if (newchild != null) return newchild;
1385                }
1386            }
1387        }
1388        return null;
1389    }
1390
1391    public Vector JavaDoc getSubDatasets() {
1392        return subDatasets;
1393    }
1394
1395    public void setSubDatasets(Vector JavaDoc subDatasets) {
1396        this.subDatasets = subDatasets;
1397        incrementReportChanges();
1398    }
1399
1400    public boolean isIgnorePagination() {
1401        return ignorePagination;
1402    }
1403
1404    public void setIgnorePagination(boolean ignorePagination) {
1405        if (this.ignorePagination == ignorePagination) return;
1406        this.ignorePagination = ignorePagination;
1407        incrementReportChanges();
1408    }
1409
1410    /**
1411     * Utility field used by event firing mechanism.
1412     */

1413    private javax.swing.event.EventListenerList JavaDoc listenerList = null;
1414
1415
1416    public void addSubDataset(SubDataset subDataset) {
1417        this.getSubDatasets().add( subDataset);
1418        incrementReportChanges();
1419        this.fireReportSubDatasetChangedListenerReportSubDatasetChanged( new ReportSubDatasetChangedEvent(this, subDataset, ReportSubDatasetChangedEvent.ADDED, ReportSubDatasetChangedEvent.OBJECT_SUBDATASET));
1420        subDataset.addSubDatasetObjectChangedListener( this );
1421    }
1422
1423    public void removeSubDataset(SubDataset subDataset) {
1424        this.getSubDatasets().remove(subDataset);
1425        this.fireReportSubDatasetChangedListenerReportSubDatasetChanged( new ReportSubDatasetChangedEvent(this,subDataset, ReportSubDatasetChangedEvent.REMOVED, ReportSubDatasetChangedEvent.OBJECT_SUBDATASET));
1426        subDataset.removeSubDatasetObjectChangedListener( this );
1427        incrementReportChanges();
1428    }
1429
1430    public void removeStyle(Style s) {
1431
1432        this.getStyles().remove(s);
1433        removeStyleFromElements( s, getElements());
1434        this.fireStyleChangedListenerStyleChanged( new StyleChangedEvent( this,StyleChangedEvent.DELETED , s,s));
1435        incrementReportChanges();
1436    }
1437
1438    public void addStyle(Style s)
1439    {
1440        this.getStyles().add(s);
1441        incrementReportChanges();
1442        this.fireStyleChangedListenerStyleChanged( new StyleChangedEvent( this,StyleChangedEvent.ADDED , s,s));
1443    }
1444
1445    private void removeStyleFromElements(Style s, Vector JavaDoc thisElements)
1446    {
1447        for (int i=0; i<thisElements.size(); ++i)
1448        {
1449            ReportElement re = (ReportElement)thisElements.elementAt(i);
1450
1451            if (re.getStyle() == s) re.setStyle(null);
1452
1453            if (re instanceof CrosstabReportElement)
1454            {
1455                removeStyleFromElements(s, ((CrosstabReportElement)re).getElements());
1456            }
1457        }
1458        incrementReportChanges();
1459    }
1460
1461    /**
1462     * Registers ReportSubDatasetChangedListener to receive events.
1463     * @param listener The listener to register.
1464     */

1465    public synchronized void addReportSubDatasetChangedListener(it.businesslogic.ireport.gui.event.ReportSubDatasetChangedListener listener) {
1466        if (listenerList == null ) {
1467            listenerList = new javax.swing.event.EventListenerList JavaDoc();
1468        }
1469        listenerList.add (it.businesslogic.ireport.gui.event.ReportSubDatasetChangedListener.class, listener);
1470    }
1471
1472    /**
1473     * Removes ReportSubDatasetChangedListener from the list of listeners.
1474     * @param listener The listener to remove.
1475     */

1476    public synchronized void removeReportSubDatasetChangedListener(it.businesslogic.ireport.gui.event.ReportSubDatasetChangedListener listener) {
1477        listenerList.remove (it.businesslogic.ireport.gui.event.ReportSubDatasetChangedListener.class, listener);
1478    }
1479
1480    /**
1481     * Notifies all registered listeners about the event.
1482     *
1483     * @param event The event to be fired
1484     */

1485    public void fireReportSubDatasetChangedListenerReportSubDatasetChanged(it.businesslogic.ireport.gui.event.ReportSubDatasetChangedEvent event) {
1486        if (listenerList == null) return;
1487        Object JavaDoc[] listeners = listenerList.getListenerList ();
1488        for (int i = listeners.length - 2; i >= 0; i -= 2) {
1489            if (listeners[i]==it.businesslogic.ireport.gui.event.ReportSubDatasetChangedListener.class) {
1490                ((it.businesslogic.ireport.gui.event.ReportSubDatasetChangedListener)listeners[i+1]).reportSubDatasetChanged (event);
1491            }
1492        }
1493    }
1494
1495    /**
1496     * Replace oldString with newString in all the expressions related to the gived subDataset...
1497     * The method can fire required events
1498     *
1499     * Checks for:
1500     *
1501     * - Filter expression
1502     * - Query
1503     * - variables (initial expression and expression)
1504     * - group expressions
1505     *
1506     * If Report instance:
1507     *
1508     * - band print when
1509     * - elements PrintWhenExpression
1510     * - TextFieldReportElement expression
1511     * - ImageReportElement imageExpression
1512     * - SubReportElement connection, subreportExpression, parametersExpressions, returnValue variable names
1513     * - Hyperlinks (anchor, parameters, etc...)
1514     * - Crosstab parameters...
1515     *
1516     * TODO:
1517     * - SortFields...
1518     * - Visit all the chart expressions checking the used datasource
1519     * - Explore sub-elements
1520     * - change the class for textfields (if possible)
1521     *
1522     */

1523    public void replaceInReportExpressions(String JavaDoc oldString, String JavaDoc newString, SubDataset subDataset)
1524    {
1525        if (subDataset.getFilterExpression() != null && subDataset.getFilterExpression().indexOf(oldString) >= 0)
1526        {
1527            subDataset.setFilterExpression( Misc.string_replace(newString, oldString, subDataset.getFilterExpression()) );
1528            this.incrementReportChanges();
1529        }
1530        
1531        if (subDataset.getQuery() != null && subDataset.getQuery().indexOf(oldString) >= 0)
1532        {
1533            subDataset.setQuery( Misc.string_replace(newString, oldString, subDataset.getQuery()) );
1534            subDataset.fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
1535                       new SubDatasetObjectChangedEvent(subDataset, SubDatasetObjectChangedEvent.QUERY, SubDatasetObjectChangedEvent.MODIFIED, subDataset.getQuery(),subDataset.getQuery()));
1536        }
1537        
1538        for (int i=0; i<subDataset.getVariables().size(); ++i)
1539        {
1540            JRVariable var = (JRVariable)subDataset.getVariables().get(i);
1541            boolean fireEvent = false;
1542            if (var.getInitialValueExpression().indexOf(oldString) >= 0)
1543            {
1544                var.setInitialValueExpression( Misc.string_replace(newString, oldString, var.getInitialValueExpression()) );
1545                // alert
1546
fireEvent = true;
1547            }
1548            
1549            if (var.getExpression().indexOf(oldString) >= 0)
1550            {
1551                var.setExpression( Misc.string_replace(newString, oldString, var.getExpression()) );
1552                // alert
1553
fireEvent = true;
1554            }
1555            
1556            if (fireEvent)
1557            {
1558               subDataset.fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
1559                       new SubDatasetObjectChangedEvent(subDataset, SubDatasetObjectChangedEvent.VARIABLE, SubDatasetObjectChangedEvent.MODIFIED, var,var));
1560            }
1561        }
1562        
1563        for (int i=0; i<subDataset.getGroups().size(); ++i)
1564        {
1565            Group grp = (Group)subDataset.getGroups().get(i);
1566            if (grp.getGroupExpression().indexOf(oldString) >= 0)
1567            {
1568                grp.setGroupExpression( Misc.string_replace(newString, oldString, grp.getGroupExpression()) );
1569            }
1570        }
1571        
1572        if (subDataset instanceof Report)
1573        {
1574            Report rep = (Report)subDataset;
1575        
1576            for (int i=0; i<rep.getBands().size(); ++i)
1577            {
1578                Band band = (Band)rep.getBands().get(i);
1579                if (band.getPrintWhenExpression().indexOf(oldString) >= 0)
1580                {
1581                    band.setPrintWhenExpression( Misc.string_replace(newString, oldString, band.getPrintWhenExpression()) );
1582                }
1583            }
1584            
1585            // Modify elements...
1586
Vector JavaDoc changedElements = new Vector JavaDoc();
1587            for (int i=0; i<rep.getElements().size(); ++i)
1588            {
1589                boolean add=false;
1590                ReportElement re = (ReportElement)rep.getElements().get(i);
1591                if (re.getPrintWhenExpression().indexOf(oldString) >= 0)
1592                {
1593                    re.setPrintWhenExpression(Misc.string_replace(newString, oldString, re.getPrintWhenExpression()) );
1594                    add=true;
1595                }
1596                
1597                if (re instanceof TextFieldReportElement)
1598                {
1599                    TextFieldReportElement tre = (TextFieldReportElement)re;
1600                    if (tre.getText().indexOf(oldString) >= 0)
1601                    {
1602                        tre.setText(Misc.string_replace(newString, oldString, tre.getText()) );
1603                        add=true;
1604                    }
1605                }
1606                
1607                if (re instanceof ImageReportElement)
1608                {
1609                    ImageReportElement tre = (ImageReportElement)re;
1610                    if (tre.getImageExpression().indexOf(oldString) >= 0)
1611                    {
1612                        tre.setImageExpression(Misc.string_replace(newString, oldString, tre.getImageExpression()) );
1613                        add=true;
1614                    }
1615                }
1616                
1617                if (re instanceof SubReportElement)
1618                {
1619                    SubReportElement tre = (SubReportElement)re;
1620                    if (tre.getSubreportExpression().indexOf(oldString) >= 0)
1621                    {
1622                        tre.setSubreportExpression(Misc.string_replace(newString, oldString, tre.getSubreportExpression()) );
1623                        add=true;
1624                    }
1625                    
1626                    if (tre.getParametersMapExpression().indexOf(oldString) >= 0)
1627                    {
1628                        tre.setParametersMapExpression(Misc.string_replace(newString, oldString, tre.getParametersMapExpression()) );
1629                        add=true;
1630                    }
1631                    
1632                    if (tre.getDataSourceExpression().indexOf(oldString) >= 0)
1633                    {
1634                        tre.setDataSourceExpression(Misc.string_replace(newString, oldString, tre.getDataSourceExpression()) );
1635                        add=true;
1636                    }
1637                    
1638                    if (tre.getConnectionExpression().indexOf(oldString) >= 0)
1639                    {
1640                        tre.setConnectionExpression(Misc.string_replace(newString, oldString, tre.getConnectionExpression()) );
1641                        add=true;
1642                    }
1643                    
1644                    for (int k=0; k<tre.getSubreportParameters().size(); ++k)
1645                    {
1646                        JRSubreportParameter para = (JRSubreportParameter)tre.getSubreportParameters().get(k);
1647                        if (para.getExpression().indexOf(oldString) >= 0)
1648                        {
1649                            para.setExpression(Misc.string_replace(newString, oldString, para.getExpression()) );
1650                            add=true;
1651                        }
1652                    }
1653                    
1654                    for (int k=0; k<tre.getReturnValues().size(); ++k)
1655                    {
1656                        JRSubreportReturnValue para = (JRSubreportReturnValue)tre.getReturnValues().get(k);
1657                        if (oldString.equals("$V{" + para.getToVariable() + "}"))
1658                        {
1659                            String JavaDoc nv = newString.substring(3, newString.length()-1);
1660                            para.setToVariable(nv);
1661                            add=true;
1662                        }
1663                    }
1664                }
1665                
1666                if (re instanceof HyperLinkableReportElement)
1667                {
1668                    HyperLinkableReportElement tre = (HyperLinkableReportElement)re;
1669                    if (tre.getAnchorNameExpression().indexOf(oldString) >= 0)
1670                    {
1671                        tre.setAnchorNameExpression(Misc.string_replace(newString, oldString, tre.getAnchorNameExpression()) );
1672                        add=true;
1673                    }
1674                    if (tre.getHyperlinkAnchorExpression().indexOf(oldString) >= 0)
1675                    {
1676                        tre.setHyperlinkAnchorExpression(Misc.string_replace(newString, oldString, tre.getHyperlinkAnchorExpression()) );
1677                        add=true;
1678                    }
1679                    if (tre.getHyperlinkPageExpression().indexOf(oldString) >= 0)
1680                    {
1681                        tre.setHyperlinkPageExpression(Misc.string_replace(newString, oldString, tre.getHyperlinkPageExpression()) );
1682                        add=true;
1683                    }
1684                    if (tre.getHyperlinkReferenceExpression().indexOf(oldString) >= 0)
1685                    {
1686                        tre.setHyperlinkReferenceExpression(Misc.string_replace(newString, oldString, tre.getHyperlinkReferenceExpression()) );
1687                        add=true;
1688                    }
1689                    for (int k=0; k<tre.getLinkParameters().size(); ++k)
1690                    {
1691                        JRLinkParameter para = (JRLinkParameter)tre.getLinkParameters().get(k);
1692                        if (para.getExpression().indexOf(oldString) >= 0)
1693                        {
1694                            para.setExpression(Misc.string_replace(newString, oldString, para.getExpression()) );
1695                            add=true;
1696                        }
1697                    }
1698                }
1699                
1700                if (re instanceof CrosstabReportElement)
1701                {
1702                    CrosstabReportElement tre = (CrosstabReportElement)re;
1703                    
1704                    for (int k=0; k<tre.getCrosstabParameters().size(); ++k)
1705                    {
1706                        CrosstabParameter para = (CrosstabParameter)tre.getCrosstabParameters().get(k);
1707                        if (para.getParameterValueExpression().indexOf(oldString) >= 0)
1708                        {
1709                            para.setParameterValueExpression(Misc.string_replace(newString, oldString, para.getParameterValueExpression()) );
1710                            add=true;
1711                        }
1712                    }
1713                }
1714                
1715                if (add)
1716                {
1717                    changedElements.add(re);
1718                }
1719            }
1720            
1721            if (changedElements.size() > 0)
1722            {
1723                CrosstabReportElement cre = ( getReportFrame().getSelectedCrosstabEditorPanel() != null) ? getReportFrame().getSelectedCrosstabEditorPanel().getCrosstabElement() : null;
1724                getReportFrame().fireReportListenerReportElementsChanged(
1725                        new ReportElementChangedEvent(getReportFrame(),cre,changedElements , ReportElementChangedEvent.CHANGED));
1726                        
1727                getReportFrame().repaint();
1728                MainFrame.getMainInstance().getElementPropertiesDialog().updateSelection();
1729            }
1730        }
1731        
1732    }
1733    
1734    public void subDatasetObjectChanged(it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent evt) {
1735
1736
1737        if (evt.getType() == evt.FIELD && evt.getAction() == evt.MODIFIED && evt.getSource() == this)
1738        {
1739            String JavaDoc oldString = "$F{" + evt.getOldValue() + "}";
1740            String JavaDoc newString = "$F{" + evt.getNewValue() + "}";
1741            if (evt.getOldValue() != null &&
1742                evt.getNewValue() != null &&
1743                !oldString.equals(newString))
1744            {
1745                replaceInReportExpressions(oldString, newString, this);
1746            }
1747        }
1748        
1749        if (evt.getType() == evt.PARAMETER && evt.getAction() == evt.MODIFIED && evt.getSource() == this)
1750        {
1751            String JavaDoc oldString = "$P{" + evt.getOldValue() + "}";
1752            String JavaDoc newString = "$P{" + evt.getNewValue() + "}";
1753            if (evt.getOldValue() != null &&
1754                evt.getNewValue() != null &&
1755                !oldString.equals(newString))
1756            {
1757                replaceInReportExpressions(oldString, newString, this);
1758            }
1759        }
1760        
1761        if (evt.getType() == evt.VARIABLE && evt.getAction() == evt.MODIFIED && evt.getSource() == this)
1762        {
1763            String JavaDoc oldString = "$V{" + evt.getOldValue() + "}";
1764            String JavaDoc newString = "$V{" + evt.getNewValue() + "}";
1765            if (evt.getOldValue() != null &&
1766                evt.getNewValue() != null &&
1767                !oldString.equals(newString))
1768            {
1769                replaceInReportExpressions(oldString, newString, this);
1770            }
1771        }
1772        
1773        //System.out.println(evt.getSource() + " " + this);
1774
//fireSubDatasetObjectChangedListenerSubDatasetObjectChanged( evt );
1775
if (getListenerList() != null && evt.getSource() != this)
1776        {
1777            Object JavaDoc[] listeners = getListenerList().getListenerList ();
1778             for (int i = listeners.length - 2; i >= 0; i -= 2) {
1779                if (listeners[i+1] == this) continue;
1780                //if (listeners[i+1] == evt.getSource()) continue;
1781
if (listeners[i]==it.businesslogic.ireport.gui.event.SubDatasetObjectChangedListener.class) {
1782                ((it.businesslogic.ireport.gui.event.SubDatasetObjectChangedListener)listeners[i+1]).subDatasetObjectChanged (evt);
1783                }
1784             }
1785        }
1786
1787        incrementReportChanges();
1788    }
1789
1790    /**
1791     * Registers StyleChangedListener to receive events.
1792     * @param listener The listener to register.
1793     */

1794    public synchronized void addStyleChangedListener(it.businesslogic.ireport.gui.event.StyleChangedListener listener) {
1795        if (listenerList == null ) {
1796            listenerList = new javax.swing.event.EventListenerList JavaDoc();
1797        }
1798        listenerList.add (it.businesslogic.ireport.gui.event.StyleChangedListener.class, listener);
1799    }
1800
1801    /**
1802     * Removes StyleChangedListener from the list of listeners.
1803     * @param listener The listener to remove.
1804     */

1805    public synchronized void removeStyleChangedListener(it.businesslogic.ireport.gui.event.StyleChangedListener listener) {
1806        listenerList.remove (it.businesslogic.ireport.gui.event.StyleChangedListener.class, listener);
1807    }
1808
1809    /**
1810     * Notifies all registered listeners about the event.
1811     *
1812     * @param event The event to be fired
1813     */

1814    public void fireStyleChangedListenerStyleChanged(it.businesslogic.ireport.gui.event.StyleChangedEvent event) {
1815
1816        // We have to apply again the style...
1817
Enumeration JavaDoc enum_elements = getElements().elements();
1818        while (enum_elements.hasMoreElements())
1819        {
1820            ReportElement re = (ReportElement)enum_elements.nextElement();
1821            if (re.getStyle() == event.getNewValue())
1822            {
1823                re.setStyle( event.getNewValue());
1824            }
1825        }
1826
1827        if (listenerList == null) return;
1828        Object JavaDoc[] listeners = listenerList.getListenerList ();
1829        for (int i = listeners.length - 2; i >= 0; i -= 2) {
1830            if (listeners[i]==it.businesslogic.ireport.gui.event.StyleChangedListener.class) {
1831                ((it.businesslogic.ireport.gui.event.StyleChangedListener)listeners[i+1]).styleChanged (event);
1832            }
1833        }
1834    }
1835
1836    public Style getDefaultStyle()
1837    {
1838        for (int i=0; i<getStyles().size(); ++i)
1839        {
1840         Style s = (Style)getStyles().elementAt(i);
1841         if (s.getAttributeBoolean( s.ATTRIBUTE_isDefault, false ))
1842         {
1843             return s;
1844         }
1845        }
1846        return null;
1847    }
1848
1849
1850
1851    public int getReportChanges() {
1852        return reportChanges;
1853    }
1854
1855    /*
1856     * Keep track of report changes. A ReportDocumentStatusChanged
1857     * event is thrown in the following cases:
1858     *
1859     * currentChanges == 0 && newChanges > 0 => ReportDocumentStatusChanged to DIRTY
1860     * currentChanges > 0 && newChanges == 0 => ReportDocumentStatusChanged to NO_DIRTY
1861     *
1862     */

1863    public void setReportChanges(int newChanges) {
1864
1865        boolean fireEvent = false;
1866
1867        if (reportChanges == 0 && newChanges > 0) fireEvent = true;
1868        else if (reportChanges > 0 && newChanges == 0) fireEvent = true;
1869
1870        this.reportChanges = newChanges;
1871
1872        if (fireEvent)
1873        {
1874            fireReportDocumentStatusChangedListenerReportDocumentStatusChanged(
1875                new ReportDocumentStatusChangedEvent(this));
1876        }
1877    }
1878
1879    public void incrementReportChanges() {
1880        setReportChanges( getReportChanges() + 1);
1881    }
1882
1883    public void decrementReportChanges() {
1884        if (getReportChanges() > 0)
1885        {
1886            setReportChanges( getReportChanges() - 1);
1887        }
1888    }
1889
1890    /**
1891     * Alias for getReportChanges() > 0
1892     */

1893    public boolean isModified()
1894    {
1895        return getReportChanges() > 0;
1896    }
1897
1898    public boolean isLoadingFile() {
1899        return loadingFile;
1900    }
1901
1902    public void setLoadingFile(boolean loading) {
1903        this.loadingFile = loading;
1904    }
1905
1906    /**
1907     * Registers ReportDocumentStatusChangedListener to receive events.
1908     * @param listener The listener to register.
1909     */

1910    public synchronized void addReportDocumentStatusChangedListener(it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener listener) {
1911
1912        if (listenerList == null ) {
1913            listenerList = new javax.swing.event.EventListenerList JavaDoc();
1914        }
1915        listenerList.add (it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener.class, listener);
1916    }
1917
1918    /**
1919     * Removes ReportDocumentStatusChangedListener from the list of listeners.
1920     * @param listener The listener to remove.
1921     */

1922    public synchronized void removeReportDocumentStatusChangedListener(it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener listener) {
1923
1924        listenerList.remove (it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener.class, listener);
1925    }
1926
1927    /**
1928     * Notifies all registered listeners about the event.
1929     *
1930     * @param event The event to be fired
1931     */

1932    private void fireReportDocumentStatusChangedListenerReportDocumentStatusChanged(it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedEvent event) {
1933
1934        if (listenerList == null) return;
1935        Object JavaDoc[] listeners = listenerList.getListenerList ();
1936        for (int i = listeners.length - 2; i >= 0; i -= 2) {
1937            if (listeners[i]==it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener.class) {
1938                ((it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener)listeners[i+1]).reportDocumentStatusChanged (event);
1939            }
1940        }
1941    }
1942
1943    /**
1944     * Return the available vertical space to add new bands or resize the current ones
1945     *
1946     */

1947    public int getAvailableVerticalSpace()
1948    {
1949        int ph = getHeight();
1950        ph -= getTopMargin();
1951        ph -= getBottomMargin();
1952
1953        for (int i=0; i< getBands().size(); ++i)
1954        {
1955            Band b = (Band)getBands().elementAt(i);
1956            if (b.getName().equals("title") && isTitleNewPage) continue;
1957            if (b.getName().equals("background")) continue;
1958            //if (b.getName().equals("lastPageFooter")) continue;
1959
if (b.getName().equals("summary") && isSummaryNewPage) continue;
1960
1961            ph -= b.getHeight();
1962        }
1963
1964        return ph;
1965    }
1966
1967    public void setDefaultFont(IReportFont defaultFont) {
1968        this.defaultFont = defaultFont;
1969    }
1970
1971    public static String JavaDoc writeEncodedColor(java.awt.Color JavaDoc c) {
1972        String JavaDoc nums = "0123456789ABCDEF";
1973        String JavaDoc s = "#";
1974        s += nums.charAt( c.getRed()/16 );
1975        s += nums.charAt( c.getRed()%16 );
1976        s += nums.charAt( c.getGreen()/16 );
1977        s += nums.charAt( c.getGreen()%16 );
1978        s += nums.charAt( c.getBlue()/16 );
1979        s += nums.charAt( c.getBlue()%16 );
1980        return s;
1981    }
1982
1983    public static String JavaDoc getCDATAString( String JavaDoc string, int tabCount ) {
1984        //if ( ! Utils.needToBeInsideCDATA( string ) )
1985
// return string;
1986
String JavaDoc CRLF = System.getProperty("line.separator");
1987        String JavaDoc tab = "";
1988        for ( int i = 0; i < tabCount; i++ ) {
1989            tab += "\t";
1990        }
1991        return "<![CDATA[" + string + "]]>"; // + CRLF + tab ;
1992
}
1993
1994    public String JavaDoc getFormatFactoryClass() {
1995        return formatFactoryClass;
1996    }
1997
1998    public void setFormatFactoryClass(String JavaDoc formatFactoryClass) {
1999        this.formatFactoryClass = formatFactoryClass;
2000    }
2001
2002
2003}
2004
Popular Tags