1 50 51 package com.lowagie.text.rtf.table; 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 import java.util.Properties ; 59 60 import com.lowagie.text.BadElementException; 61 import com.lowagie.text.Cell; 62 import com.lowagie.text.DocumentException; 63 import com.lowagie.text.Element; 64 import com.lowagie.text.List; 65 import com.lowagie.text.Paragraph; 66 import com.lowagie.text.rtf.RtfBasicElement; 67 import com.lowagie.text.rtf.RtfExtendedElement; 68 import com.lowagie.text.rtf.document.RtfDocument; 69 import com.lowagie.text.rtf.style.RtfColor; 70 import com.lowagie.text.rtf.style.RtfParagraphStyle; 71 import com.lowagie.text.rtf.text.RtfParagraph; 72 73 74 86 public class RtfCell extends Cell implements RtfExtendedElement { 87 88 91 private static final int MERGE_NONE = 0; 92 95 private static final int MERGE_VERT_PARENT = 1; 96 99 private static final int MERGE_VERT_CHILD = 2; 100 101 104 private RtfRow parentRow = null; 105 108 private ArrayList content = null; 109 112 private int cellRight = 0; 113 116 private int cellWidth = 0; 117 120 private RtfBorderGroup borders = null; 121 122 125 private RtfColor backgroundColor = null; 126 129 private int cellPadding = 0; 130 133 private int mergeType = MERGE_NONE; 134 137 private RtfDocument document = null; 138 141 private boolean inHeader = false; 142 145 private boolean deleted = false; 146 147 150 public RtfCell() { 151 super(); 152 this.borders = new RtfBorderGroup(); 153 verticalAlignment = ALIGN_MIDDLE; 154 } 155 156 161 public RtfCell(String content) { 162 super(content); 163 this.borders = new RtfBorderGroup(); 164 verticalAlignment = ALIGN_MIDDLE; 165 } 166 167 173 public RtfCell(Element element) throws BadElementException { 174 super(element); 175 this.borders = new RtfBorderGroup(); 176 verticalAlignment = ALIGN_MIDDLE; 177 } 178 179 184 public RtfCell(Properties properties) { 185 super(properties); 186 this.borders = new RtfBorderGroup(); 187 verticalAlignment = ALIGN_MIDDLE; 188 } 189 190 195 protected RtfCell(boolean deleted) { 196 super(); 197 this.deleted = deleted; 198 verticalAlignment = ALIGN_MIDDLE; 199 } 200 201 208 protected RtfCell(RtfDocument doc, RtfRow row, Cell cell) { 209 this.document = doc; 210 this.parentRow = row; 211 importCell(cell); 212 } 213 214 219 private void importCell(Cell cell) { 220 this.content = new ArrayList (); 221 222 if(cell == null) { 223 this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, this.parentRow.getParentTable().getBorders()); 224 return; 225 } 226 227 this.colspan = cell.getColspan(); 228 this.rowspan = cell.getRowspan(); 229 if(cell.getRowspan() > 1) { 230 this.mergeType = MERGE_VERT_PARENT; 231 } 232 if(cell instanceof RtfCell) { 233 this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, ((RtfCell) cell).getBorders()); 234 } else { 235 this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, cell.getBorder(), cell.getBorderWidth(), cell.getBorderColor()); 236 } 237 this.verticalAlignment = cell.getVerticalAlignment(); 238 if(cell.getBackgroundColor() == null) { 239 this.backgroundColor = new RtfColor(this.document, 255, 255, 255); 240 } else { 241 this.backgroundColor = new RtfColor(this.document, cell.getBackgroundColor()); 242 } 243 244 this.cellPadding = (int) this.parentRow.getParentTable().getCellPadding(); 245 246 Iterator cellIterator = cell.getElements(); 247 Paragraph container = null; 248 while(cellIterator.hasNext()) { 249 try { 250 Element element = (Element) cellIterator.next(); 251 if(!(element instanceof Paragraph) && !(element instanceof List )) { 253 if(container != null) { 254 container.add(element); 255 } else { 256 container = new Paragraph(); 257 container.setAlignment(cell.getHorizontalAlignment()); 258 container.add(element); 259 } 260 } else { 261 if(container != null) { 262 RtfBasicElement rtfElement = this.document.getMapper().mapElement(container); 263 rtfElement.setInTable(true); 264 this.content.add(rtfElement); 265 container = null; 266 } 267 if (element instanceof Paragraph && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) { 270 ((Paragraph) element).setAlignment(cell.getHorizontalAlignment()); 271 } 272 273 RtfBasicElement rtfElement = this.document.getMapper().mapElement(element); 274 rtfElement.setInTable(true); 275 this.content.add(rtfElement); 276 } 277 } catch(DocumentException de) { 278 de.printStackTrace(); 279 } 280 } 281 if(container != null) { 282 try { 283 RtfBasicElement rtfElement = this.document.getMapper().mapElement(container); 284 rtfElement.setInTable(true); 285 this.content.add(rtfElement); 286 } catch(DocumentException de) { 287 de.printStackTrace(); 288 } 289 } 290 } 291 292 298 public byte[] writeDefinition() { 299 ByteArrayOutputStream result = new ByteArrayOutputStream (); 300 try { 301 writeDefinition(result); 302 } catch(IOException ioe) { 303 ioe.printStackTrace(); 304 } 305 306 return result.toByteArray(); 307 } 308 309 312 public void writeDefinition(final OutputStream result) throws IOException 313 { 314 if(this.mergeType == MERGE_VERT_PARENT) { 315 result.write("\\clvmgf".getBytes()); 316 } else if(this.mergeType == MERGE_VERT_CHILD) { 317 result.write("\\clvmrg".getBytes()); 318 } 319 switch (verticalAlignment) { 320 case Element.ALIGN_BOTTOM: 321 result.write("\\clvertalb".getBytes()); 322 break; 323 case Element.ALIGN_CENTER: 324 case Element.ALIGN_MIDDLE: 325 result.write("\\clvertalc".getBytes()); 326 break; 327 case Element.ALIGN_TOP: 328 result.write("\\clvertalt".getBytes()); 329 break; 330 } 331 this.borders.writeContent(result); 333 334 if(this.backgroundColor != null) { 335 result.write("\\clcbpat".getBytes()); 336 result.write(intToByteArray(this.backgroundColor.getColorNumber())); 337 } 338 result.write('\n'); 339 340 result.write("\\clftsWidth3".getBytes()); 341 result.write('\n'); 342 343 result.write("\\clwWidth".getBytes()); 344 result.write(intToByteArray(this.cellWidth)); 345 result.write('\n'); 346 347 if(this.cellPadding > 0) { 348 result.write("\\clpadl".getBytes()); 349 result.write(intToByteArray(this.cellPadding / 2)); 350 result.write("\\clpadt".getBytes()); 351 result.write(intToByteArray(this.cellPadding / 2)); 352 result.write("\\clpadr".getBytes()); 353 result.write(intToByteArray(this.cellPadding / 2)); 354 result.write("\\clpadb".getBytes()); 355 result.write(intToByteArray(this.cellPadding / 2)); 356 result.write("\\clpadfl3".getBytes()); 357 result.write("\\clpadft3".getBytes()); 358 result.write("\\clpadfr3".getBytes()); 359 result.write("\\clpadfb3".getBytes()); 360 } 361 result.write("\\cellx".getBytes()); 362 result.write(intToByteArray(this.cellRight)); 363 } 364 365 366 372 public byte[] write() 373 { 374 ByteArrayOutputStream result = new ByteArrayOutputStream (); 375 try { 376 writeContent(result); 377 } catch(IOException ioe) { 378 ioe.printStackTrace(); 379 } 380 381 return result.toByteArray(); 382 } 383 386 public void writeContent(final OutputStream result) throws IOException 387 { 388 if(this.content.size() == 0) { 389 result.write(RtfParagraph.PARAGRAPH_DEFAULTS); 390 if(this.parentRow.getParentTable().getTableFitToPage()) { 391 result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT); 392 } 393 result.write(RtfParagraph.IN_TABLE); 394 } else { 395 for(int i = 0; i < this.content.size(); i++) { 396 RtfBasicElement rtfElement = (RtfBasicElement) this.content.get(i); 397 if(rtfElement instanceof RtfParagraph) { 398 ((RtfParagraph) rtfElement).setKeepTogetherWithNext(this.parentRow.getParentTable().getTableFitToPage()); 399 } 400 rtfElement.writeContent(result); 402 if(rtfElement instanceof RtfParagraph && i < (this.content.size() - 1)) { 403 result.write(RtfParagraph.PARAGRAPH); 404 } 405 } 406 } 407 result.write("\\cell".getBytes()); 408 } 409 410 415 protected void setCellRight(int cellRight) { 416 this.cellRight = cellRight; 417 } 418 419 424 protected int getCellRight() { 425 return this.cellRight; 426 } 427 428 433 protected void setCellWidth(int cellWidth) { 434 this.cellWidth = cellWidth; 435 } 436 437 442 protected int getCellWidth() { 443 return this.cellWidth; 444 } 445 446 451 protected int getCellpadding() { 452 return this.cellPadding; 453 } 454 455 460 protected RtfBorderGroup getBorders() { 461 return this.borders; 462 } 463 464 469 public void setBorders(RtfBorderGroup borderGroup) { 470 this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, borderGroup); 471 } 472 473 478 protected RtfColor getRtfBackgroundColor() { 479 return this.backgroundColor; 480 } 481 482 487 protected void setCellMergeChild(RtfCell mergeParent) { 488 this.mergeType = MERGE_VERT_CHILD; 489 this.cellWidth = mergeParent.getCellWidth(); 490 this.cellRight = mergeParent.getCellRight(); 491 this.cellPadding = mergeParent.getCellpadding(); 492 this.borders = mergeParent.getBorders(); 493 this.verticalAlignment = mergeParent.getVerticalAlignment(); 494 this.backgroundColor = mergeParent.getRtfBackgroundColor(); 495 } 496 497 502 public void setRtfDocument(RtfDocument doc) { 503 this.document = doc; 504 } 505 506 510 public void setInTable(boolean inTable) { 511 } 512 513 518 public void setInHeader(boolean inHeader) { 519 this.inHeader = inHeader; 520 for(int i = 0; i < this.content.size(); i++) { 521 ((RtfBasicElement) this.content.get(i)).setInHeader(inHeader); 522 } 523 } 524 525 532 private byte[] intToByteArray(int i) { 533 return Integer.toString(i).getBytes(); 534 } 535 536 542 public boolean isDeleted() { 543 return this.deleted; 544 } 545 } 546 | Popular Tags |