1 50 51 package com.lowagie.text.rtf.headerfooter; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 57 import com.lowagie.text.HeaderFooter; 58 import com.lowagie.text.Phrase; 59 import com.lowagie.text.rtf.RtfBasicElement; 60 import com.lowagie.text.rtf.document.RtfDocument; 61 62 63 71 public class RtfHeaderFooterGroup extends HeaderFooter implements RtfBasicElement { 72 73 76 private static final int MODE_NONE = 0; 77 80 private static final int MODE_SINGLE = 1; 81 84 private static final int MODE_MULTIPLE = 2; 85 86 89 private int mode = MODE_NONE; 90 93 private int type = RtfHeaderFooter.TYPE_HEADER; 94 95 98 private RtfHeaderFooter headerAll = null; 99 102 private RtfHeaderFooter headerFirst = null; 103 106 private RtfHeaderFooter headerLeft = null; 107 110 private RtfHeaderFooter headerRight = null; 111 114 private RtfDocument document = null; 115 116 121 public RtfHeaderFooterGroup() { 122 super(new Phrase(""), false); 123 this.mode = MODE_NONE; 124 } 125 126 133 public RtfHeaderFooterGroup(RtfDocument doc, int type) { 134 super(new Phrase(""), false); 135 this.document = doc; 136 this.type = type; 137 } 138 139 147 public RtfHeaderFooterGroup(RtfDocument doc, RtfHeaderFooterGroup headerFooter, int type) { 148 super(new Phrase(""), false); 149 this.document = doc; 150 this.mode = headerFooter.getMode(); 151 this.type = type; 152 if(headerFooter.getHeaderAll() != null) { 153 this.headerAll = new RtfHeaderFooter(this.document, headerFooter.getHeaderAll(), RtfHeaderFooter.DISPLAY_ALL_PAGES); 154 } 155 if(headerFooter.getHeaderFirst() != null) { 156 this.headerFirst = new RtfHeaderFooter(this.document, headerFooter.getHeaderFirst(), RtfHeaderFooter.DISPLAY_FIRST_PAGE); 157 } 158 if(headerFooter.getHeaderLeft() != null) { 159 this.headerLeft = new RtfHeaderFooter(this.document, headerFooter.getHeaderLeft(), RtfHeaderFooter.DISPLAY_LEFT_PAGES); 160 } 161 if(headerFooter.getHeaderRight() != null) { 162 this.headerRight = new RtfHeaderFooter(this.document, headerFooter.getHeaderRight(), RtfHeaderFooter.DISPLAY_RIGHT_PAGES); 163 } 164 setType(this.type); 165 } 166 167 174 public RtfHeaderFooterGroup(RtfDocument doc, RtfHeaderFooter headerFooter, int type) { 175 super(new Phrase(""), false); 176 this.document = doc; 177 this.type = type; 178 this.mode = MODE_SINGLE; 179 headerAll = new RtfHeaderFooter(doc, headerFooter, RtfHeaderFooter.DISPLAY_ALL_PAGES); 180 headerAll.setType(this.type); 181 } 182 183 190 public RtfHeaderFooterGroup(RtfDocument doc, HeaderFooter headerFooter, int type) { 191 super(new Phrase(""), false); 192 this.document = doc; 193 this.type = type; 194 this.mode = MODE_SINGLE; 195 headerAll = new RtfHeaderFooter(doc, headerFooter, type, RtfHeaderFooter.DISPLAY_ALL_PAGES); 196 headerAll.setType(this.type); 197 } 198 199 204 public void setRtfDocument(RtfDocument doc) { 205 this.document = doc; 206 if(headerAll != null) { 207 headerAll.setRtfDocument(this.document); 208 } 209 if(headerFirst != null) { 210 headerFirst.setRtfDocument(this.document); 211 } 212 if(headerLeft != null) { 213 headerLeft.setRtfDocument(this.document); 214 } 215 if(headerRight != null) { 216 headerRight.setRtfDocument(this.document); 217 } 218 } 219 220 226 public byte[] write() 227 { 228 ByteArrayOutputStream result = new ByteArrayOutputStream (); 229 try { 230 writeContent(result); 231 } catch(IOException ioe) { 232 ioe.printStackTrace(); 233 } 234 return result.toByteArray(); 235 } 236 239 public void writeContent(final OutputStream result) throws IOException 240 { 241 if(this.mode == MODE_SINGLE) { 242 headerAll.writeContent(result); 244 } else if(this.mode == MODE_MULTIPLE) { 245 if(headerFirst != null) { 246 headerFirst.writeContent(result); 248 } 249 if(headerLeft != null) { 250 headerLeft.writeContent(result); 252 } 253 if(headerRight != null) { 254 headerRight.writeContent(result); 256 } 257 if(headerAll != null) { 258 headerAll.writeContent(result); 260 } 261 } 262 } 263 264 270 public void setHeaderFooter(RtfHeaderFooter headerFooter, int displayAt) { 271 this.mode = MODE_MULTIPLE; 272 headerFooter.setRtfDocument(this.document); 273 headerFooter.setType(this.type); 274 headerFooter.setDisplayAt(displayAt); 275 switch(displayAt) { 276 case RtfHeaderFooter.DISPLAY_ALL_PAGES: 277 headerAll = headerFooter; 278 break; 279 case RtfHeaderFooter.DISPLAY_FIRST_PAGE: 280 headerFirst = headerFooter; 281 break; 282 case RtfHeaderFooter.DISPLAY_LEFT_PAGES: 283 headerLeft = headerFooter; 284 break; 285 case RtfHeaderFooter.DISPLAY_RIGHT_PAGES: 286 headerRight = headerFooter; 287 break; 288 } 289 } 290 291 297 public void setHeaderFooter(HeaderFooter headerFooter, int displayAt) { 298 this.mode = MODE_MULTIPLE; 299 switch(displayAt) { 300 case RtfHeaderFooter.DISPLAY_ALL_PAGES: 301 headerAll = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt); 302 break; 303 case RtfHeaderFooter.DISPLAY_FIRST_PAGE: 304 headerFirst = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt); 305 break; 306 case RtfHeaderFooter.DISPLAY_LEFT_PAGES: 307 headerLeft = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt); 308 break; 309 case RtfHeaderFooter.DISPLAY_RIGHT_PAGES: 310 headerRight = new RtfHeaderFooter(this.document, headerFooter, this.type, displayAt); 311 break; 312 } 313 } 314 315 320 public void setHasTitlePage() { 321 if(this.mode == MODE_SINGLE) { 322 this.mode = MODE_MULTIPLE; 323 headerFirst = new RtfHeaderFooter(this.document, headerAll, RtfHeaderFooter.DISPLAY_FIRST_PAGE); 324 headerFirst.setType(this.type); 325 } 326 } 327 328 333 public void setHasFacingPages() { 334 if(this.mode == MODE_SINGLE) { 335 this.mode = MODE_MULTIPLE; 336 this.headerLeft = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES); 337 this.headerLeft.setType(this.type); 338 this.headerRight = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES); 339 this.headerRight.setType(this.type); 340 this.headerAll = null; 341 } else if(this.mode == MODE_MULTIPLE) { 342 if(this.headerLeft == null && this.headerAll != null) { 343 this.headerLeft = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_LEFT_PAGES); 344 this.headerLeft.setType(this.type); 345 } 346 if(this.headerRight == null && this.headerAll != null) { 347 this.headerRight = new RtfHeaderFooter(this.document, this.headerAll, RtfHeaderFooter.DISPLAY_RIGHT_PAGES); 348 this.headerRight.setType(this.type); 349 } 350 this.headerAll = null; 351 } 352 } 353 354 359 public boolean hasTitlePage() { 360 return (headerFirst != null); 361 } 362 363 368 public boolean hasFacingPages() { 369 return (headerLeft != null || headerRight != null); 370 } 371 372 376 public void setInTable(boolean inTable) { 377 } 378 379 383 public void setInHeader(boolean inHeader) { 384 } 385 386 393 public void setType(int type) { 394 this.type = type; 395 if(headerAll != null) { 396 headerAll.setType(this.type); 397 } 398 if(headerFirst != null) { 399 headerFirst.setType(this.type); 400 } 401 if(headerLeft != null) { 402 headerLeft.setType(this.type); 403 } 404 if(headerRight != null) { 405 headerRight.setType(this.type); 406 } 407 } 408 409 414 protected int getMode() { 415 return this.mode; 416 } 417 418 423 protected RtfHeaderFooter getHeaderAll() { 424 return headerAll; 425 } 426 427 432 protected RtfHeaderFooter getHeaderFirst() { 433 return headerFirst; 434 } 435 436 441 protected RtfHeaderFooter getHeaderLeft() { 442 return headerLeft; 443 } 444 445 450 protected RtfHeaderFooter getHeaderRight() { 451 return headerRight; 452 } 453 } 454 | Popular Tags |