001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.base;
029:
030: import java.io.IOException;
031: import java.io.Serializable;
032: import java.util.ArrayList;
033: import java.util.Arrays;
034: import java.util.List;
035:
036: import net.sf.jasperreports.crosstabs.JRCrosstab;
037: import net.sf.jasperreports.engine.JRAbstractObjectFactory;
038: import net.sf.jasperreports.engine.JRChild;
039: import net.sf.jasperreports.engine.JRConstants;
040: import net.sf.jasperreports.engine.JRElement;
041: import net.sf.jasperreports.engine.JRElementGroup;
042: import net.sf.jasperreports.engine.JRFrame;
043: import net.sf.jasperreports.engine.xml.JRXmlWriter;
044:
045: /**
046: * @author Teodor Danciu (teodord@users.sourceforge.net)
047: * @version $Id: JRBaseElementGroup.java 1229 2006-04-19 10:27:35Z teodord $
048: */
049: public class JRBaseElementGroup implements JRElementGroup, Serializable {
050:
051: /**
052: *
053: */
054: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
055:
056: /**
057: *
058: */
059: protected List children = new ArrayList();
060: protected JRElementGroup elementGroup = null;
061:
062: /**
063: *
064: */
065: protected JRBaseElementGroup() {
066: }
067:
068: /**
069: *
070: */
071: protected JRBaseElementGroup(JRElementGroup elementGrp,
072: JRBaseObjectFactory factory) {
073: factory.put(elementGrp, this );
074:
075: /* */
076: List list = elementGrp.getChildren();
077: if (list != null && list.size() > 0) {
078: for (int i = 0; i < list.size(); i++) {
079: JRChild child = (JRChild) list.get(i);
080: child = child.getCopy(factory);
081: children.add(child);
082: }
083: }
084:
085: this .elementGroup = factory.getElementGroup(elementGrp
086: .getElementGroup());
087: }
088:
089: /**
090: *
091: */
092: public List getChildren() {
093: return this .children;
094: }
095:
096: /**
097: *
098: */
099: public JRElementGroup getElementGroup() {
100: return this .elementGroup;
101: }
102:
103: /**
104: *
105: */
106: public static JRElement[] getElements(List children) {
107: JRElement[] elements = null;
108:
109: if (children != null) {
110: List allElements = new ArrayList();
111: Object child = null;
112: JRElement[] childElementArray = null;
113: for (int i = 0; i < children.size(); i++) {
114: child = children.get(i);
115: if (child instanceof JRElement) {
116: allElements.add(child);
117: } else if (child instanceof JRElementGroup) {
118: childElementArray = ((JRElementGroup) child)
119: .getElements();
120: if (childElementArray != null) {
121: allElements.addAll(Arrays
122: .asList(childElementArray));
123: }
124: }
125: }
126:
127: elements = new JRElement[allElements.size()];
128: allElements.toArray(elements);
129: }
130:
131: return elements;
132: }
133:
134: public JRElement[] getElements() {
135: return getElements(children);
136: }
137:
138: /**
139: *
140: */
141: public static JRElement getElementByKey(JRElement[] elements,
142: String key) {
143: JRElement element = null;
144:
145: if (key != null) {
146: if (elements != null) {
147: int i = 0;
148: while (element == null && i < elements.length) {
149: JRElement elem = elements[i];
150: if (key.equals(elem.getKey())) {
151: element = elem;
152: } else if (elem instanceof JRFrame) {
153: element = ((JRFrame) elem).getElementByKey(key);
154: } else if (elem instanceof JRCrosstab) {
155: element = ((JRCrosstab) elem)
156: .getElementByKey(key);
157: }
158: i++;
159: }
160: }
161: }
162:
163: return element;
164: }
165:
166: public JRElement getElementByKey(String key) {
167: return getElementByKey(getElements(), key);
168: }
169:
170: /**
171: *
172: */
173: public JRChild getCopy(JRAbstractObjectFactory factory) {
174: return factory.getElementGroup(this );
175: }
176:
177: /**
178: *
179: */
180: public void writeXml(JRXmlWriter xmlWriter) throws IOException {
181: xmlWriter.writeElementGroup(this);
182: }
183:
184: }
|