1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.io.IOException ; 31 import java.util.ArrayList ; 32 import java.util.Arrays ; 33 import java.util.List ; 34 35 import net.sf.jasperreports.engine.JRAbstractObjectFactory; 36 import net.sf.jasperreports.engine.JRChild; 37 import net.sf.jasperreports.engine.JRElement; 38 import net.sf.jasperreports.engine.JRElementGroup; 39 import net.sf.jasperreports.engine.xml.JRXmlWriter; 40 41 42 46 public class JRFillElementGroup implements JRElementGroup, JRCloneable 47 { 48 49 50 53 protected List children = new ArrayList (); 54 protected JRElementGroup elementGroup = null; 55 56 59 protected JRFillElement[] elements = null; 60 61 64 private JRElement topElementInGroup = null; 65 private JRElement bottomElementInGroup = null; 66 private int stretchHeightDiff = 0; 67 68 69 72 protected JRFillElementGroup( 73 JRElementGroup elementGrp, 74 JRFillObjectFactory factory 75 ) 76 { 77 factory.put(elementGrp, this); 78 79 if (elementGrp != null) 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 94 this.getElements(); 95 96 this.elementGroup = factory.getElementGroup(elementGrp.getElementGroup()); 97 } 98 } 99 100 101 protected JRFillElementGroup(JRFillElementGroup elementGrp, JRFillCloneFactory factory) 102 { 103 factory.put(elementGrp, this); 104 105 List list = elementGrp.getChildren(); 106 if (list != null) 107 { 108 for (int i = 0; i < list.size(); i++) 109 { 110 JRCloneable child = (JRCloneable) list.get(i); 111 JRCloneable clone = child.createClone(factory); 112 children.add(clone); 113 } 114 } 115 116 getElements(); 117 118 elementGroup = (JRFillElementGroup) factory.getClone((JRFillElementGroup) elementGrp.getElementGroup()); 119 } 120 121 122 125 public List getChildren() 126 { 127 return this.children; 128 } 129 130 131 134 public JRElementGroup getElementGroup() 135 { 136 return this.elementGroup; 137 } 138 139 140 143 public JRElement[] getElements() 144 { 145 if (this.elements == null) 146 { 147 if (this.children != null) 148 { 149 List allElements = new ArrayList (); 150 Object child = null; 151 JRElement[] childElementArray = null; 152 for(int i = 0; i < this.children.size(); i++) 153 { 154 child = this.children.get(i); 155 if (child instanceof JRFillElement) 156 { 157 allElements.add(child); 158 } 159 else if (child instanceof JRFillElementGroup) 160 { 161 childElementArray = ((JRFillElementGroup)child).getElements(); 162 if (childElementArray != null) 163 { 164 allElements.addAll( Arrays.asList(childElementArray) ); 165 } 166 } 167 } 168 169 this.elements = new JRFillElement[allElements.size()]; 170 allElements.toArray(this.elements); 171 } 172 } 173 174 return this.elements; 175 } 176 177 178 181 public JRElement getElementByKey(String key) 182 { 183 return null; 184 } 185 186 187 190 protected void reset() 191 { 192 topElementInGroup = null; 193 } 194 195 196 199 protected int getStretchHeightDiff() 200 { 201 if (topElementInGroup == null) 202 { 203 stretchHeightDiff = 0; 204 205 setTopBottomElements(); 206 207 JRElement[] allElements = getElements(); 208 209 if (allElements != null && allElements.length > 0) 210 { 211 JRFillElement topElem = null; 212 JRFillElement bottomElem = null; 213 214 for(int i = 0; i < allElements.length; i++) 215 { 216 JRFillElement element = (JRFillElement)allElements[i]; 217 if (element.isToPrint()) 219 { 220 if ( 221 topElem == null || 222 (topElem != null && 223 element.getRelativeY() + element.getStretchHeight() < 224 topElem.getRelativeY() + topElem.getStretchHeight()) 225 ) 226 { 227 topElem = element; 228 } 229 230 if ( 231 bottomElem == null || 232 (bottomElem != null && 233 element.getRelativeY() + element.getStretchHeight() > 234 bottomElem.getRelativeY() + bottomElem.getStretchHeight()) 235 ) 236 { 237 bottomElem = element; 238 } 239 } 240 } 241 242 if (topElem != null) 243 { 244 stretchHeightDiff = 245 bottomElem.getRelativeY() + bottomElem.getStretchHeight() - topElem.getRelativeY() - 246 (bottomElementInGroup.getY() + bottomElementInGroup.getHeight() - topElementInGroup.getY()); 247 } 248 249 if (stretchHeightDiff < 0) 250 { 251 stretchHeightDiff = 0; 252 } 253 } 254 } 255 256 return stretchHeightDiff; 257 } 258 259 260 263 private void setTopBottomElements() 264 { 265 JRElement[] allElements = getElements(); 266 267 if (allElements != null && allElements.length > 0) 268 { 269 for(int i = 0; i < allElements.length; i++) 270 { 271 if ( 272 topElementInGroup == null || 273 (topElementInGroup != null && 274 allElements[i].getY() + allElements[i].getHeight() < 275 topElementInGroup.getY() + topElementInGroup.getHeight()) 276 ) 277 { 278 topElementInGroup = allElements[i]; 279 } 280 281 if ( 282 bottomElementInGroup == null || 283 (bottomElementInGroup != null && 284 allElements[i].getY() + allElements[i].getHeight() > 285 bottomElementInGroup.getY() + bottomElementInGroup.getHeight()) 286 ) 287 { 288 bottomElementInGroup = allElements[i]; 289 } 290 } 291 } 292 } 293 294 295 298 public JRChild getCopy(JRAbstractObjectFactory factory) 299 { 300 return factory.getElementGroup(this); 301 } 302 303 304 307 public void writeXml(JRXmlWriter xmlWriter) throws IOException 308 { 309 xmlWriter.writeElementGroup(this); 310 } 311 312 313 public JRCloneable createClone(JRFillCloneFactory factory) 314 { 315 return new JRFillElementGroup(this, factory); 316 } 317 } 318 | Popular Tags |