KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > base > JRBaseElementGroup


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.base;
29
30 import java.io.IOException JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.List JavaDoc;
35
36 import net.sf.jasperreports.crosstabs.JRCrosstab;
37 import net.sf.jasperreports.engine.JRAbstractObjectFactory;
38 import net.sf.jasperreports.engine.JRChild;
39 import net.sf.jasperreports.engine.JRConstants;
40 import net.sf.jasperreports.engine.JRElement;
41 import net.sf.jasperreports.engine.JRElementGroup;
42 import net.sf.jasperreports.engine.JRFrame;
43 import net.sf.jasperreports.engine.xml.JRXmlWriter;
44
45
46 /**
47  * @author Teodor Danciu (teodord@users.sourceforge.net)
48  * @version $Id: JRBaseElementGroup.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
49  */

50 public class JRBaseElementGroup implements JRElementGroup, Serializable JavaDoc
51 {
52
53
54     /**
55      *
56      */

57     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
58
59     /**
60      *
61      */

62     protected List JavaDoc children = new ArrayList JavaDoc();
63     protected JRElementGroup elementGroup = null;
64
65
66     /**
67      *
68      */

69     protected JRBaseElementGroup()
70     {
71     }
72     
73     
74     /**
75      *
76      */

77     protected JRBaseElementGroup(JRElementGroup elementGrp, JRBaseObjectFactory factory)
78     {
79         factory.put(elementGrp, this);
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         this.elementGroup = factory.getElementGroup(elementGrp.getElementGroup());
94     }
95         
96
97     /**
98      *
99      */

100     public List JavaDoc getChildren()
101     {
102         return this.children;
103     }
104
105
106     /**
107      *
108      */

109     public JRElementGroup getElementGroup()
110     {
111         return this.elementGroup;
112     }
113
114
115     /**
116      *
117      */

118     public static JRElement[] getElements(List JavaDoc children)
119     {
120         JRElement[] elements = null;
121         
122         if (children != null)
123         {
124             List JavaDoc allElements = new ArrayList JavaDoc();
125             Object JavaDoc child = null;
126             JRElement[] childElementArray = null;
127             for(int i = 0; i < children.size(); i++)
128             {
129                 child = children.get(i);
130                 if (child instanceof JRElement)
131                 {
132                     allElements.add(child);
133                 }
134                 else if (child instanceof JRElementGroup)
135                 {
136                     childElementArray = ((JRElementGroup)child).getElements();
137                     if (childElementArray != null)
138                     {
139                         allElements.addAll( Arrays.asList(childElementArray) );
140                     }
141                 }
142             }
143             
144             elements = new JRElement[allElements.size()];
145             allElements.toArray(elements);
146         }
147         
148         return elements;
149     }
150
151     
152     public JRElement[] getElements()
153     {
154         return getElements(children);
155     }
156     
157
158     /**
159      *
160      */

161     public static JRElement getElementByKey(JRElement[] elements, String JavaDoc key)
162     {
163         JRElement element = null;
164         
165         if (key != null)
166         {
167             if (elements != null)
168             {
169                 int i = 0;
170                 while (element == null && i < elements.length)
171                 {
172                     JRElement elem = elements[i];
173                     if (key.equals(elem.getKey()))
174                     {
175                         element = elem;
176                     }
177                     else if (elem instanceof JRFrame)
178                     {
179                         element = ((JRFrame) elem).getElementByKey(key);
180                     }
181                     else if (elem instanceof JRCrosstab)
182                     {
183                         element = ((JRCrosstab) elem).getElementByKey(key);
184                     }
185                     i++;
186                 }
187             }
188         }
189         
190         return element;
191     }
192
193     
194     public JRElement getElementByKey(String JavaDoc key)
195     {
196         return getElementByKey(getElements(), key);
197     }
198
199     
200     /**
201      *
202      */

203     public JRChild getCopy(JRAbstractObjectFactory factory)
204     {
205         return factory.getElementGroup(this);
206     }
207
208
209     /**
210      *
211      */

212     public void writeXml(JRXmlWriter xmlWriter) throws IOException JavaDoc
213     {
214         xmlWriter.writeElementGroup(this);
215     }
216
217
218 }
219
Popular Tags