1 28 package net.sf.jasperreports.engine.base; 29 30 import java.io.IOException ; 31 import java.io.Serializable ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.List ; 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 50 public class JRBaseElementGroup implements JRElementGroup, Serializable 51 { 52 53 54 57 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 58 59 62 protected List children = new ArrayList (); 63 protected JRElementGroup elementGroup = null; 64 65 66 69 protected JRBaseElementGroup() 70 { 71 } 72 73 74 77 protected JRBaseElementGroup(JRElementGroup elementGrp, JRBaseObjectFactory factory) 78 { 79 factory.put(elementGrp, this); 80 81 82 List 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 100 public List getChildren() 101 { 102 return this.children; 103 } 104 105 106 109 public JRElementGroup getElementGroup() 110 { 111 return this.elementGroup; 112 } 113 114 115 118 public static JRElement[] getElements(List children) 119 { 120 JRElement[] elements = null; 121 122 if (children != null) 123 { 124 List allElements = new ArrayList (); 125 Object 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 161 public static JRElement getElementByKey(JRElement[] elements, String 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 key) 195 { 196 return getElementByKey(getElements(), key); 197 } 198 199 200 203 public JRChild getCopy(JRAbstractObjectFactory factory) 204 { 205 return factory.getElementGroup(this); 206 } 207 208 209 212 public void writeXml(JRXmlWriter xmlWriter) throws IOException 213 { 214 xmlWriter.writeElementGroup(this); 215 } 216 217 218 } 219 | Popular Tags |