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.fill;
029:
030: import java.io.IOException;
031: import java.util.ArrayList;
032: import java.util.Arrays;
033: import java.util.List;
034:
035: import net.sf.jasperreports.engine.JRAbstractObjectFactory;
036: import net.sf.jasperreports.engine.JRChild;
037: import net.sf.jasperreports.engine.JRElement;
038: import net.sf.jasperreports.engine.JRElementGroup;
039: import net.sf.jasperreports.engine.xml.JRXmlWriter;
040:
041: /**
042: * @author Teodor Danciu (teodord@users.sourceforge.net)
043: * @version $Id: JRFillElementGroup.java 1229 2006-04-19 10:27:35Z teodord $
044: */
045: public class JRFillElementGroup implements JRElementGroup, JRCloneable {
046:
047: /**
048: *
049: */
050: protected List children = new ArrayList();
051: protected JRElementGroup elementGroup = null;
052:
053: /**
054: *
055: */
056: protected JRFillElement[] elements = null;
057:
058: /**
059: *
060: */
061: private JRElement topElementInGroup = null;
062: private JRElement bottomElementInGroup = null;
063: private int stretchHeightDiff = 0;
064:
065: /**
066: *
067: */
068: protected JRFillElementGroup(JRElementGroup elementGrp,
069: JRFillObjectFactory factory) {
070: factory.put(elementGrp, this );
071:
072: if (elementGrp != null) {
073: /* */
074: List list = elementGrp.getChildren();
075: if (list != null && list.size() > 0) {
076: for (int i = 0; i < list.size(); i++) {
077: JRChild child = (JRChild) list.get(i);
078: child = child.getCopy(factory);
079: children.add(child);
080: }
081: }
082:
083: /* */
084: this .getElements();
085:
086: this .elementGroup = factory.getElementGroup(elementGrp
087: .getElementGroup());
088: }
089: }
090:
091: protected JRFillElementGroup(JRFillElementGroup elementGrp,
092: JRFillCloneFactory factory) {
093: factory.put(elementGrp, this );
094:
095: List list = elementGrp.getChildren();
096: if (list != null) {
097: for (int i = 0; i < list.size(); i++) {
098: JRCloneable child = (JRCloneable) list.get(i);
099: JRCloneable clone = child.createClone(factory);
100: children.add(clone);
101: }
102: }
103:
104: getElements();
105:
106: elementGroup = (JRFillElementGroup) factory
107: .getClone((JRFillElementGroup) elementGrp
108: .getElementGroup());
109: }
110:
111: /**
112: *
113: */
114: public List getChildren() {
115: return this .children;
116: }
117:
118: /**
119: *
120: */
121: public JRElementGroup getElementGroup() {
122: return this .elementGroup;
123: }
124:
125: /**
126: *
127: */
128: public JRElement[] getElements() {
129: if (this .elements == null) {
130: if (this .children != null) {
131: List allElements = new ArrayList();
132: Object child = null;
133: JRElement[] childElementArray = null;
134: for (int i = 0; i < this .children.size(); i++) {
135: child = this .children.get(i);
136: if (child instanceof JRFillElement) {
137: allElements.add(child);
138: } else if (child instanceof JRFillElementGroup) {
139: childElementArray = ((JRFillElementGroup) child)
140: .getElements();
141: if (childElementArray != null) {
142: allElements.addAll(Arrays
143: .asList(childElementArray));
144: }
145: }
146: }
147:
148: this .elements = new JRFillElement[allElements.size()];
149: allElements.toArray(this .elements);
150: }
151: }
152:
153: return this .elements;
154: }
155:
156: /**
157: *
158: */
159: public JRElement getElementByKey(String key) {
160: return null;
161: }
162:
163: /**
164: *
165: */
166: protected void reset() {
167: topElementInGroup = null;
168: }
169:
170: /**
171: *
172: */
173: protected int getStretchHeightDiff() {
174: if (topElementInGroup == null) {
175: stretchHeightDiff = 0;
176:
177: setTopBottomElements();
178:
179: JRElement[] allElements = getElements();
180:
181: if (allElements != null && allElements.length > 0) {
182: JRFillElement topElem = null;
183: JRFillElement bottomElem = null;
184:
185: for (int i = 0; i < allElements.length; i++) {
186: JRFillElement element = (JRFillElement) allElements[i];
187: //if (element != this && element.isToPrint())
188: if (element.isToPrint()) {
189: if (topElem == null
190: || (topElem != null && element
191: .getRelativeY()
192: + element.getStretchHeight() < topElem
193: .getRelativeY()
194: + topElem.getStretchHeight())) {
195: topElem = element;
196: }
197:
198: if (bottomElem == null
199: || (bottomElem != null && element
200: .getRelativeY()
201: + element.getStretchHeight() > bottomElem
202: .getRelativeY()
203: + bottomElem.getStretchHeight())) {
204: bottomElem = element;
205: }
206: }
207: }
208:
209: if (topElem != null) {
210: stretchHeightDiff = bottomElem.getRelativeY()
211: + bottomElem.getStretchHeight()
212: - topElem.getRelativeY()
213: - (bottomElementInGroup.getY()
214: + bottomElementInGroup.getHeight() - topElementInGroup
215: .getY());
216: }
217:
218: if (stretchHeightDiff < 0) {
219: stretchHeightDiff = 0;
220: }
221: }
222: }
223:
224: return stretchHeightDiff;
225: }
226:
227: /**
228: *
229: */
230: private void setTopBottomElements() {
231: JRElement[] allElements = getElements();
232:
233: if (allElements != null && allElements.length > 0) {
234: for (int i = 0; i < allElements.length; i++) {
235: if (topElementInGroup == null
236: || (topElementInGroup != null && allElements[i]
237: .getY()
238: + allElements[i].getHeight() < topElementInGroup
239: .getY()
240: + topElementInGroup.getHeight())) {
241: topElementInGroup = allElements[i];
242: }
243:
244: if (bottomElementInGroup == null
245: || (bottomElementInGroup != null && allElements[i]
246: .getY()
247: + allElements[i].getHeight() > bottomElementInGroup
248: .getY()
249: + bottomElementInGroup.getHeight())) {
250: bottomElementInGroup = allElements[i];
251: }
252: }
253: }
254: }
255:
256: /**
257: *
258: */
259: public JRChild getCopy(JRAbstractObjectFactory factory) {
260: return factory.getElementGroup(this );
261: }
262:
263: /**
264: *
265: */
266: public void writeXml(JRXmlWriter xmlWriter) throws IOException {
267: xmlWriter.writeElementGroup(this );
268: }
269:
270: public JRCloneable createClone(JRFillCloneFactory factory) {
271: return new JRFillElementGroup(this, factory);
272: }
273: }
|