1 50 51 package com.lowagie.text; 52 53 import java.util.ArrayList ; 54 import java.util.Collection ; 55 import java.util.Iterator ; 56 57 82 83 public class Section extends ArrayList implements TextElementArray { 84 85 private static final long serialVersionUID = 3324172577544748043L; 87 88 90 91 protected Paragraph title; 92 93 94 protected String bookmarkTitle; 95 96 97 protected int numberDepth; 98 99 100 protected float indentationLeft; 101 102 103 protected float indentationRight; 104 105 106 protected float indentation; 107 108 109 protected boolean bookmarkOpen = true; 110 111 112 protected boolean triggerNewPage = false; 113 114 115 protected int subsections = 0; 116 117 118 protected ArrayList numbers = null; 119 120 122 125 protected Section() { 126 title = new Paragraph(); 127 numberDepth = 1; 128 } 129 130 136 protected Section(Paragraph title, int numberDepth) { 137 this.numberDepth = numberDepth; 138 this.title = title; 139 } 140 141 143 150 public boolean process(ElementListener listener) { 151 try { 152 Element element; 153 for (Iterator i = iterator(); i.hasNext(); ) { 154 element = (Element)i.next(); 155 listener.add(element); 156 } 157 return true; 158 } 159 catch(DocumentException de) { 160 return false; 161 } 162 } 163 164 169 public int type() { 170 return Element.SECTION; 171 } 172 173 179 public boolean isChapter() { 180 return type() == Element.CHAPTER; 181 } 182 183 189 public boolean isSection() { 190 return type() == Element.SECTION; 191 } 192 193 198 public ArrayList getChunks() { 199 ArrayList tmp = new ArrayList (); 200 for (Iterator i = iterator(); i.hasNext(); ) { 201 tmp.addAll(((Element) i.next()).getChunks()); 202 } 203 return tmp; 204 } 205 206 208 216 public void add(int index, Object o) { 217 try { 218 Element element = (Element) o; 219 if (element.type() == Element.PARAGRAPH || 220 element.type() == Element.LIST || 221 element.type() == Element.CHUNK || 222 element.type() == Element.PHRASE || 223 element.type() == Element.ANCHOR || 224 element.type() == Element.ANNOTATION || 225 element.type() == Element.TABLE || 226 element.type() == Element.PTABLE || 227 element.type() == Element.IMGTEMPLATE || 228 element.type() == Element.JPEG || 229 element.type() == Element.IMGRAW) { 230 super.add(index, element); 231 } 232 else { 233 throw new ClassCastException ("You can add a " + element.getClass().getName() + " to a Section."); 234 } 235 } 236 catch(ClassCastException cce) { 237 throw new ClassCastException ("Insertion of illegal Element: " + cce.getMessage()); 238 } 239 } 240 241 249 public boolean add(Object o) { 250 try { 251 Element element = (Element) o; 252 if (element.type() == Element.PARAGRAPH || 253 element.type() == Element.LIST || 254 element.type() == Element.CHUNK || 255 element.type() == Element.PHRASE || 256 element.type() == Element.ANCHOR || 257 element.type() == Element.ANNOTATION || 258 element.type() == Element.TABLE || 259 element.type() == Element.IMGTEMPLATE || 260 element.type() == Element.PTABLE || 261 element.type() == Element.JPEG || 262 element.type() == Element.IMGRAW) { 263 return super.add(o); 264 } 265 else if (element.type() == Element.SECTION) { 266 Section section = (Section) o; 267 section.setNumbers(++subsections, numbers); 268 return super.add(section); 269 } 270 else if (o instanceof MarkedSection && ((MarkedObject)o).element.type() == Element.SECTION) { 271 MarkedSection mo = (MarkedSection)o; 272 Section section = (Section)mo.element; 273 section.setNumbers(++subsections, numbers); 274 return super.add(mo); 275 } 276 else if (element instanceof MarkedObject) { 277 return super.add(o); 278 } 279 else { 280 throw new ClassCastException ("You can add a " + element.getClass().getName() + " to a Section."); 281 } 282 } 283 catch(ClassCastException cce) { 284 throw new ClassCastException ("Insertion of illegal Element: " + cce.getMessage()); 285 } 286 } 287 288 296 public boolean addAll(Collection collection) { 297 for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) { 298 this.add(iterator.next()); 299 } 300 return true; 301 } 302 303 305 313 public Section addSection(float indentation, Paragraph title, int numberDepth) { 314 Section section = new Section(title, numberDepth); 315 section.setIndentation(indentation); 316 add(section); 317 return section; 318 } 319 320 327 public Section addSection(float indentation, Paragraph title) { 328 return addSection(indentation, title, numberDepth + 1); 329 } 330 331 338 public Section addSection(Paragraph title, int numberDepth) { 339 return addSection(0, title, numberDepth); 340 } 341 342 345 public MarkedSection addMarkedSection() { 346 MarkedSection section = new MarkedSection(new Section(null, numberDepth + 1)); 347 add(section); 348 return section; 349 } 350 351 357 public Section addSection(Paragraph title) { 358 return addSection(0, title, numberDepth + 1); 359 } 360 361 369 public Section addSection(float indentation, String title, int numberDepth) { 370 return addSection(indentation, new Paragraph(title), numberDepth); 371 } 372 373 380 public Section addSection(String title, int numberDepth) { 381 return addSection(new Paragraph(title), numberDepth); 382 } 383 384 391 public Section addSection(float indentation, String title) { 392 return addSection(indentation, new Paragraph(title)); 393 } 394 395 401 public Section addSection(String title) { 402 return addSection(new Paragraph(title)); 403 } 404 405 407 412 public void setTitle(Paragraph title) { 413 this.title = title; 414 } 415 416 421 public Paragraph getTitle() { 422 if (title == null) { 423 return null; 424 } 425 int depth = Math.min(numbers.size(), numberDepth); 426 if (depth < 1) { 427 return title; 428 } 429 StringBuffer buf = new StringBuffer (" "); 430 for (int i = 0; i < depth; i++) { 431 buf.insert(0, "."); 432 buf.insert(0, ((Integer ) numbers.get(i)).intValue()); 433 } 434 Paragraph result = new Paragraph(title); 435 result.add(0, new Chunk(buf.toString(), title.getFont())); 436 return result; 437 } 438 439 448 public void setNumberDepth(int numberDepth) { 449 this.numberDepth = numberDepth; 450 } 451 452 457 public int getNumberDepth() { 458 return numberDepth; 459 } 460 461 466 public void setIndentationLeft(float indentation) { 467 indentationLeft = indentation; 468 } 469 470 475 public float getIndentationLeft() { 476 return indentationLeft; 477 } 478 479 484 public void setIndentationRight(float indentation) { 485 indentationRight = indentation; 486 } 487 488 493 public float getIndentationRight() { 494 return indentationRight; 495 } 496 497 502 public void setIndentation(float indentation) { 503 this.indentation = indentation; 504 } 505 506 511 public float getIndentation() { 512 return indentation; 513 } 514 515 519 public void setBookmarkOpen(boolean bookmarkOpen) { 520 this.bookmarkOpen = bookmarkOpen; 521 } 522 523 527 public boolean isBookmarkOpen() { 528 return bookmarkOpen; 529 } 530 531 535 public void setTriggerNewPage(boolean triggerNewPage) { 536 this.triggerNewPage = triggerNewPage; 537 } 538 539 543 public boolean isTriggerNewPage() { 544 return triggerNewPage; 545 } 546 547 552 public void setBookmarkTitle(String bookmarkTitle) { 553 this.bookmarkTitle = bookmarkTitle; 554 } 555 556 560 public Paragraph getBookmarkTitle() { 561 if (bookmarkTitle == null) 562 return getTitle(); 563 else 564 return new Paragraph(bookmarkTitle); 565 } 566 567 570 public void setChapterNumber(int number) { 571 numbers.set(numbers.size() - 1, new Integer (number)); 572 Object s; 573 for (Iterator i = iterator(); i.hasNext(); ) { 574 s = i.next(); 575 if (s instanceof Section) { 576 ((Section)s).setChapterNumber(number); 577 } 578 } 579 } 580 581 586 public int getDepth() { 587 return numbers.size(); 588 } 589 590 592 598 private void setNumbers(int number, ArrayList numbers) { 599 this.numbers = new ArrayList (); 600 this.numbers.add(new Integer (number)); 601 this.numbers.addAll(numbers); 602 } 603 604 606 612 public Paragraph title() { 613 return getTitle(); 614 } 615 616 622 public int numberDepth() { 623 return getNumberDepth(); 624 } 625 626 632 public float indentationLeft() { 633 return getIndentationLeft(); 634 } 635 636 642 public float indentationRight() { 643 return getIndentationRight(); 644 } 645 646 652 public float indentation() { 653 return getIndentation(); 654 } 655 656 662 public int depth() { 663 return getDepth(); 664 } 665 666 673 public Section addSection(java.util.Properties attributes) { 674 return com.lowagie.text.factories.ElementFactory.getSection(this, attributes); 675 } 676 } | Popular Tags |