KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRFillElementGroup


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.io.IOException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.List JavaDoc;
34
35 import net.sf.jasperreports.engine.JRAbstractObjectFactory;
36 import net.sf.jasperreports.engine.JRChild;
37 import net.sf.jasperreports.engine.JRElement;
38 import net.sf.jasperreports.engine.JRElementGroup;
39 import net.sf.jasperreports.engine.xml.JRXmlWriter;
40
41
42 /**
43  * @author Teodor Danciu (teodord@users.sourceforge.net)
44  * @version $Id: JRFillElementGroup.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
45  */

46 public class JRFillElementGroup implements JRElementGroup, JRCloneable
47 {
48
49
50     /**
51      *
52      */

53     protected List JavaDoc children = new ArrayList JavaDoc();
54     protected JRElementGroup elementGroup = null;
55
56     /**
57      *
58      */

59     protected JRFillElement[] elements = null;
60
61     /**
62      *
63      */

64     private JRElement topElementInGroup = null;
65     private JRElement bottomElementInGroup = null;
66     private int stretchHeightDiff = 0;
67
68
69     /**
70      *
71      */

72     protected JRFillElementGroup(
73             JRElementGroup elementGrp,
74             JRFillObjectFactory factory
75             )
76         {
77             factory.put(elementGrp, this);
78
79             if (elementGrp != null)
80             {
81                 /* */
82                 List JavaDoc list = elementGrp.getChildren();
83                 if (list != null && list.size() > 0)
84                 {
85                     for(int i = 0; i < list.size(); i++)
86                     {
87                         JRChild child = (JRChild)list.get(i);
88                         child = child.getCopy(factory);
89                         children.add(child);
90                     }
91                 }
92         
93                 /* */
94                 this.getElements();
95         
96                 this.elementGroup = factory.getElementGroup(elementGrp.getElementGroup());
97             }
98         }
99
100     
101     protected JRFillElementGroup(JRFillElementGroup elementGrp, JRFillCloneFactory factory)
102     {
103         factory.put(elementGrp, this);
104
105         List JavaDoc list = elementGrp.getChildren();
106         if (list != null)
107         {
108             for (int i = 0; i < list.size(); i++)
109             {
110                 JRCloneable child = (JRCloneable) list.get(i);
111                 JRCloneable clone = child.createClone(factory);
112                 children.add(clone);
113             }
114         }
115
116         getElements();
117
118         elementGroup = (JRFillElementGroup) factory.getClone((JRFillElementGroup) elementGrp.getElementGroup());
119     }
120
121
122     /**
123      *
124      */

125     public List JavaDoc getChildren()
126     {
127         return this.children;
128     }
129
130
131     /**
132      *
133      */

134     public JRElementGroup getElementGroup()
135     {
136         return this.elementGroup;
137     }
138
139
140     /**
141      *
142      */

143     public JRElement[] getElements()
144     {
145         if (this.elements == null)
146         {
147             if (this.children != null)
148             {
149                 List JavaDoc allElements = new ArrayList JavaDoc();
150                 Object JavaDoc child = null;
151                 JRElement[] childElementArray = null;
152                 for(int i = 0; i < this.children.size(); i++)
153                 {
154                     child = this.children.get(i);
155                     if (child instanceof JRFillElement)
156                     {
157                         allElements.add(child);
158                     }
159                     else if (child instanceof JRFillElementGroup)
160                     {
161                         childElementArray = ((JRFillElementGroup)child).getElements();
162                         if (childElementArray != null)
163                         {
164                             allElements.addAll( Arrays.asList(childElementArray) );
165                         }
166                     }
167                 }
168                 
169                 this.elements = new JRFillElement[allElements.size()];
170                 allElements.toArray(this.elements);
171             }
172         }
173         
174         return this.elements;
175     }
176
177
178     /**
179      *
180      */

181     public JRElement getElementByKey(String JavaDoc key)
182     {
183         return null;
184     }
185
186
187     /**
188      *
189      */

190     protected void reset()
191     {
192         topElementInGroup = null;
193     }
194
195
196     /**
197      *
198      */

199     protected int getStretchHeightDiff()
200     {
201         if (topElementInGroup == null)
202         {
203             stretchHeightDiff = 0;
204             
205             setTopBottomElements();
206
207             JRElement[] allElements = getElements();
208
209             if (allElements != null && allElements.length > 0)
210             {
211                 JRFillElement topElem = null;
212                 JRFillElement bottomElem = null;
213
214                 for(int i = 0; i < allElements.length; i++)
215                 {
216                     JRFillElement element = (JRFillElement)allElements[i];
217                     //if (element != this && element.isToPrint())
218
if (element.isToPrint())
219                     {
220                         if (
221                             topElem == null ||
222                             (topElem != null &&
223                             element.getRelativeY() + element.getStretchHeight() <
224                             topElem.getRelativeY() + topElem.getStretchHeight())
225                             )
226                         {
227                             topElem = element;
228                         }
229
230                         if (
231                             bottomElem == null ||
232                             (bottomElem != null &&
233                             element.getRelativeY() + element.getStretchHeight() >
234                             bottomElem.getRelativeY() + bottomElem.getStretchHeight())
235                             )
236                         {
237                             bottomElem = element;
238                         }
239                     }
240                 }
241
242                 if (topElem != null)
243                 {
244                     stretchHeightDiff =
245                         bottomElem.getRelativeY() + bottomElem.getStretchHeight() - topElem.getRelativeY() -
246                         (bottomElementInGroup.getY() + bottomElementInGroup.getHeight() - topElementInGroup.getY());
247                 }
248
249                 if (stretchHeightDiff < 0)
250                 {
251                     stretchHeightDiff = 0;
252                 }
253             }
254         }
255         
256         return stretchHeightDiff;
257     }
258
259
260     /**
261      *
262      */

263     private void setTopBottomElements()
264     {
265         JRElement[] allElements = getElements();
266     
267         if (allElements != null && allElements.length > 0)
268         {
269             for(int i = 0; i < allElements.length; i++)
270             {
271                 if (
272                     topElementInGroup == null ||
273                     (topElementInGroup != null &&
274                     allElements[i].getY() + allElements[i].getHeight() <
275                     topElementInGroup.getY() + topElementInGroup.getHeight())
276                     )
277                 {
278                     topElementInGroup = allElements[i];
279                 }
280
281                 if (
282                     bottomElementInGroup == null ||
283                     (bottomElementInGroup != null &&
284                     allElements[i].getY() + allElements[i].getHeight() >
285                     bottomElementInGroup.getY() + bottomElementInGroup.getHeight())
286                     )
287                 {
288                     bottomElementInGroup = allElements[i];
289                 }
290             }
291         }
292     }
293
294
295     /**
296      *
297      */

298     public JRChild getCopy(JRAbstractObjectFactory factory)
299     {
300         return factory.getElementGroup(this);
301     }
302
303
304     /**
305      *
306      */

307     public void writeXml(JRXmlWriter xmlWriter) throws IOException JavaDoc
308     {
309         xmlWriter.writeElementGroup(this);
310     }
311
312
313     public JRCloneable createClone(JRFillCloneFactory factory)
314     {
315         return new JRFillElementGroup(this, factory);
316     }
317 }
318
Popular Tags