Source code: jbreport/core/ReportCompositeImpl.java
1 /*
2 * $Id: ReportCompositeImpl.java,v 1.1 2000/08/31 13:53:17 grantfin Exp $
3 *
4 * jbReport - A reporting library for Java
5 * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package jbreport.core;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import jbreport.Constants;
29 import jbreport.ReportComposite;
30 import jbreport.ReportElement;
31 import jbreport.ReportException;
32 import jbreport.data.QueryResult;
33
34 /**
35 * The default concrete implementation of the ReportComposite interface. This
36 * is public as classes in other packages might need to extend this.
37 *
38 * @author Grant Finnemore
39 * @version $Revision: 1.1 $
40 */
41 public
42 class ReportCompositeImpl extends AbstractReportElement
43 implements ReportComposite {
44
45 /** The list of elements that exist as children of this Composite */
46 private List children;
47
48 //
49 // Public interface
50 //
51
52 public void addElement(ReportElement elem) {
53 if(elem == null) {
54 throw new NullPointerException("elem cannot be null");
55 }
56 if(children == null) {
57 children = new ArrayList();
58 }
59 // Add the group element if it was not catered for.
60 if(!"group".equals(getType()) && "section".equals(elem)) {
61 try {
62 ReportComposite group =
63 (ReportComposite)elem.getFactory().createElement("group");
64 group.addElement(elem);
65 elem = group;
66 }
67 catch(ReportException e) {
68 e.printStackTrace();
69 throw new RuntimeException(e.toString());
70 }
71 }
72 children.add(elem);
73 elem.setParent(this);
74 }
75
76 //
77 // Methods overloaded from AbstractReportElement
78 //
79
80 public ReportElement copy(boolean deep) {
81 ReportCompositeImpl result = (ReportCompositeImpl)super.copy(deep);
82 if(deep == DEEP) {
83 if(children != null && children.size() > 0) {
84 result.children = new ArrayList();
85 for(Iterator it = children.iterator(); it.hasNext();) {
86 result.addElement(((ReportElement)it.next()).copy(deep));
87 }
88 }
89 }
90 return result;
91 }
92
93 public List getList(String property) {
94 List result = null;
95 if(Constants.RE_CHILDREN.equals(property)) {
96 result = (children != null ? children : Collections.EMPTY_LIST);
97 }
98 else {
99 result = super.getList(property);
100 }
101 return result;
102 }
103
104 public void loadData() throws ReportException {
105 if(children != null && children.size() > 0) {
106 for(Iterator it = children.iterator(); it.hasNext();) {
107 ((ReportElement)it.next()).loadData();
108 }
109 }
110 }
111
112 public void updateData(QueryResult queryResult) throws ReportException {
113 if(children != null && children.size() > 0) {
114 for(Iterator it = children.iterator(); it.hasNext();) {
115 ((AbstractReportElement)it.next()).updateData(queryResult);
116 }
117 }
118 }
119
120 public void xmlEndChild(ReportElement elem) throws ReportException {
121 addElement(elem);
122 }
123
124 public void accept(ReportVisitor visitor, ReportVisitorState state)
125 throws ReportException {
126 visitor.visitReportComposite(this, state);
127 }
128
129 //
130 // Implementation methods
131 //
132
133 protected void clearElements() {
134 // Create a new list. If we just call clear on the list, the copy()
135 // method in Group will break.
136 children = new ArrayList();
137 }
138
139 }
140
141