1 50 51 package com.lowagie.text.rtf.text; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 59 import com.lowagie.text.Chunk; 60 import com.lowagie.text.DocumentException; 61 import com.lowagie.text.Element; 62 import com.lowagie.text.Section; 63 import com.lowagie.text.rtf.RtfBasicElement; 64 import com.lowagie.text.rtf.RtfElement; 65 import com.lowagie.text.rtf.document.RtfDocument; 66 import com.lowagie.text.rtf.field.RtfTOCEntry; 67 68 69 77 public class RtfSection extends RtfElement { 78 79 82 protected RtfParagraph title = null; 83 86 protected ArrayList items = null; 87 88 96 public RtfSection(RtfDocument doc, Section section) { 97 super(doc); 98 items = new ArrayList (); 99 try { 100 if(section.getTitle() != null) { 101 this.title = (RtfParagraph) doc.getMapper().mapElement(section.getTitle()); 102 } 103 if(document.getAutogenerateTOCEntries()) { 104 StringBuffer titleText = new StringBuffer (); 105 Iterator it = section.getTitle().iterator(); 106 while(it.hasNext()) { 107 Element element = (Element) it.next(); 108 if(element.type() == Element.CHUNK) { 109 titleText.append(((Chunk) element).getContent()); 110 } 111 } 112 if(titleText.toString().trim().length() > 0) { 113 RtfTOCEntry tocEntry = new RtfTOCEntry(titleText.toString()); 114 tocEntry.setRtfDocument(this.document); 115 this.items.add(tocEntry); 116 } 117 } 118 Iterator iterator = section.iterator(); 119 while(iterator.hasNext()) { 120 Element element = (Element) iterator.next(); 121 RtfBasicElement rtfElement = doc.getMapper().mapElement(element); 122 if(rtfElement != null) { 123 items.add(rtfElement); 124 } 125 } 126 127 updateIndentation(section.getIndentationLeft(), section.getIndentationRight(), section.getIndentation()); 128 } catch(DocumentException de) { 129 de.printStackTrace(); 130 } 131 } 132 133 139 public byte[] write() 140 { 141 ByteArrayOutputStream result = new ByteArrayOutputStream (); 142 try { 143 writeContent(result); 144 } catch(IOException ioe) { 145 ioe.printStackTrace(); 146 } 147 return result.toByteArray(); 148 } 149 152 public void writeContent(final OutputStream result) throws IOException 153 { 154 result.write(RtfParagraph.PARAGRAPH); 155 if(this.title != null) { 156 this.title.writeContent(result); 158 } 159 for(int i = 0; i < items.size(); i++) { 160 RtfBasicElement rbe = (RtfBasicElement) items.get(i); 161 rbe.writeContent(result); 163 } 164 } 165 166 167 173 public void setInTable(boolean inTable) { 174 super.setInTable(inTable); 175 for(int i = 0; i < this.items.size(); i++) { 176 ((RtfBasicElement) this.items.get(i)).setInTable(inTable); 177 } 178 } 179 180 186 public void setInHeader(boolean inHeader) { 187 super.setInHeader(inHeader); 188 for(int i = 0; i < this.items.size(); i++) { 189 ((RtfBasicElement) this.items.get(i)).setInHeader(inHeader); 190 } 191 } 192 193 201 private void updateIndentation(float indentLeft, float indentRight, float indentContent) { 202 if(this.title != null) { 203 this.title.setIndentLeft((int) (this.title.getIndentLeft() + indentLeft * RtfElement.TWIPS_FACTOR)); 204 this.title.setIndentRight((int) (this.title.getIndentRight() + indentRight * RtfElement.TWIPS_FACTOR)); 205 } 206 for(int i = 0; i < this.items.size(); i++) { 207 RtfBasicElement rtfElement = (RtfBasicElement) this.items.get(i); 208 if(rtfElement instanceof RtfSection) { 209 ((RtfSection) rtfElement).updateIndentation(indentLeft + indentContent, indentRight, 0); 210 } else if(rtfElement instanceof RtfParagraph) { 211 ((RtfParagraph) rtfElement).setIndentLeft((int) (((RtfParagraph) rtfElement).getIndentLeft() + (indentLeft + indentContent) * RtfElement.TWIPS_FACTOR)); 212 ((RtfParagraph) rtfElement).setIndentRight((int) (((RtfParagraph) rtfElement).getIndentRight() + indentRight * RtfElement.TWIPS_FACTOR)); 213 } 214 } 215 } 216 } 217 | Popular Tags |