1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 28 29 import java.io.Writer ; 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 33 36 37 public class RtfTableCell 38 extends RtfContainer 39 implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer, 40 IRtfExternalGraphicContainer, IRtfTextrunContainer { 41 private RtfParagraph paragraph; 42 private RtfList list; 43 private RtfTable table; 44 private RtfExternalGraphic externalGraphic; 45 private final RtfTableRow parentRow; 46 private boolean setCenter; 47 private boolean setRight; 48 private int id; 49 50 51 public static final int DEFAULT_CELL_WIDTH = 2000; 52 53 54 private int cellWidth; 55 private int widthOffset; 56 57 58 private int vMerge = NO_MERGE; 59 private int hMerge = NO_MERGE; 60 61 62 public static final int NO_MERGE = 0; 63 64 65 public static final int MERGE_START = 1; 66 67 68 public static final int MERGE_WITH_PREVIOUS = 2; 69 70 71 RtfTableCell(RtfTableRow parent, Writer w, int cellWidth, int idNum) throws IOException { 72 super(parent, w); 73 id = idNum; 74 parentRow = parent; 75 this.cellWidth = cellWidth; 76 setCenter = false; 77 setRight = false; 78 79 } 80 81 82 RtfTableCell(RtfTableRow parent, Writer w, int cellWidth, RtfAttributes attrs, 83 int idNum) throws IOException { 84 super(parent, w, attrs); 85 id = idNum; 86 parentRow = parent; 87 this.cellWidth = cellWidth; 88 } 89 90 96 public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException { 97 closeAll(); 98 99 if (attrs == null) { 101 attrs = new RtfAttributes(); 102 } 103 104 attrs.set("intbl"); 105 106 paragraph = new RtfParagraph(this, writer, attrs); 107 108 if (paragraph.attrib.isSet("qc")) { 109 setCenter = true; 110 attrs.set("qc"); 111 } else if (paragraph.attrib.isSet("qr")) { 112 setRight = true; 113 attrs.set("qr"); 114 } else { 115 attrs.set("ql"); 116 } 117 attrs.set("intbl"); 118 119 120 return paragraph; 122 } 123 124 129 public RtfExternalGraphic newImage() throws IOException { 130 closeAll(); 131 externalGraphic = new RtfExternalGraphic(this, writer); 132 return externalGraphic; 133 } 134 135 141 public RtfParagraph newParagraph() throws IOException { 142 return newParagraph(null); 143 } 144 145 151 public RtfList newList(RtfAttributes attrib) throws IOException { 152 closeAll(); 153 list = new RtfList(this, writer, attrib); 154 return list; 155 } 156 157 163 public RtfTable newTable(ITableColumnsInfo tc) throws IOException { 164 closeAll(); 165 table = new RtfTable(this, writer, tc); 166 return table; 167 } 168 169 176 public RtfTable newTable(RtfAttributes attrs, ITableColumnsInfo tc) throws IOException { 178 closeAll(); 179 table = new RtfTable(this, writer, attrs, tc); return table; 181 } 182 183 187 int writeCellDef(int offset) throws IOException { 188 197 if (hMerge == MERGE_WITH_PREVIOUS) { 199 return offset; 200 } 201 202 newLine(); 203 this.widthOffset = offset; 204 205 if (vMerge == MERGE_START) { 207 writeControlWord("clvmgf"); 208 } else if (vMerge == MERGE_WITH_PREVIOUS) { 209 writeControlWord("clvmrg"); 210 } 211 212 215 writeAttributes (attrib, ITableAttributes.CELL_COLOR); 217 218 219 writeAttributes (attrib, ITableAttributes.ATTRIB_CELL_PADDING); 220 writeAttributes (attrib, ITableAttributes.CELL_BORDER); 221 writeAttributes (attrib, IBorderAttributes.BORDERS); 222 223 int iCurrentWidth = this.cellWidth; 225 if (attrib.getValue("number-columns-spanned") != null) { 226 int nbMergedCells = ((Integer )attrib.getValue("number-columns-spanned")).intValue(); 228 229 RtfTable tab = getRow().getTable(); 230 231 ITableColumnsInfo tableColumnsInfo 233 = tab.getITableColumnsInfo(); 234 235 tableColumnsInfo.selectFirstColumn(); 236 237 while ((this.id - 1) != tableColumnsInfo.getColumnIndex()) { 242 tableColumnsInfo.selectNextColumn(); 243 } 244 245 int i = nbMergedCells - 1; 248 while (i > 0) { 249 tableColumnsInfo.selectNextColumn(); 250 iCurrentWidth += (int)tableColumnsInfo.getColumnWidth(); 251 252 i--; 253 } 254 } 255 final int xPos = offset + iCurrentWidth; 256 257 if (setCenter) { 260 writeControlWord("qc"); 261 } else if (setRight) { 262 writeControlWord("qr"); 263 } else { 264 writeControlWord("ql"); 265 } 266 267 writeControlWord("cellx" + xPos); 268 269 writeControlWord("ql"); 271 272 return xPos; 273 274 } 275 276 280 protected void writeRtfContent() throws IOException { 281 if (hMerge == MERGE_WITH_PREVIOUS) { 283 return; 284 } 285 286 super.writeRtfContent(); 287 } 288 289 294 protected void writeRtfPrefix() throws IOException { 295 if (hMerge == MERGE_WITH_PREVIOUS) { 297 return; 298 } 299 300 super.writeRtfPrefix(); 301 } 302 303 307 protected void writeRtfSuffix() throws IOException { 308 if (hMerge == MERGE_WITH_PREVIOUS) { 310 return; 311 } 312 313 if (getRow().getTable().isNestedTable()) { 314 writeControlWordNS("nestcell"); 316 writeGroupMark(true); 317 writeControlWord("nonesttables"); 318 writeControlWord("par"); 319 writeGroupMark(false); 320 } else { 321 326 if (setCenter) { 327 writeControlWord("qc"); 328 } else if (setRight) { 329 writeControlWord("qr"); 330 } else { 331 RtfElement lastChild = null; 332 333 if (getChildren().size() > 0) { 334 lastChild = (RtfElement) getChildren().get(getChildren().size() - 1); 335 } 336 337 338 if (lastChild != null 339 && lastChild instanceof RtfTextrun) { 340 } else { 343 writeControlWord("ql"); 344 } 345 } 346 347 if (!containsText()) { 348 writeControlWord("intbl"); 349 350 } 354 355 writeControlWord("cell"); 356 } 357 } 358 359 360 private void closeCurrentParagraph() throws IOException { 362 if (paragraph != null) { 363 paragraph.close(); 364 } 365 } 366 367 private void closeCurrentList() throws IOException { 368 if (list != null) { 369 list.close(); 370 } 371 } 372 373 private void closeCurrentTable() throws IOException { 374 if (table != null) { 375 table.close(); 376 } 377 } 378 379 private void closeCurrentExternalGraphic() throws IOException { 380 if (externalGraphic != null) { 381 externalGraphic.close(); 382 } 383 } 384 385 private void closeAll() 386 throws IOException { 387 closeCurrentTable(); 388 closeCurrentParagraph(); 389 closeCurrentList(); 390 closeCurrentExternalGraphic(); 391 } 392 393 396 public void setVMerge(int mergeStatus) { this.vMerge = mergeStatus; } 397 398 401 public int getVMerge() { return this.vMerge; } 402 403 407 public void setHMerge(int mergeStatus) { 408 this.hMerge = mergeStatus; 409 } 410 411 414 public int getHMerge() { 415 return this.hMerge; 416 } 417 418 419 int getCellWidth() { return this.cellWidth; } 420 421 427 460 461 468 public boolean isEmpty() { 469 return false; 470 } 471 472 475 boolean paragraphNeedsPar(RtfParagraph p) { 476 boolean pFound = false; 478 boolean result = false; 479 for (Iterator it = getChildren().iterator(); it.hasNext();) { 480 final Object o = it.next(); 481 if (!pFound) { 482 pFound = (o == p); 484 } else { 485 if (o instanceof RtfParagraph) { 486 final RtfParagraph p2 = (RtfParagraph)o; 487 if (!p2.isEmpty()) { 488 result = true; 490 break; 491 } 492 } else if (o instanceof RtfTable) { 493 break; 494 } 495 } 496 } 497 return result; 498 } 499 500 506 public RtfTextrun getTextrun() throws IOException { 507 RtfAttributes attrs = new RtfAttributes(); 508 509 if (!getRow().getTable().isNestedTable()) { 510 attrs.set("intbl"); 511 } 512 513 RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, attrs); 514 515 textrun.setSuppressLastPar(true); 518 519 return textrun; 520 } 521 522 526 public RtfTableRow getRow() { 527 RtfElement e = this; 528 while (e.parent != null) { 529 if (e.parent instanceof RtfTableRow) { 530 return (RtfTableRow) e.parent; 531 } 532 533 e = e.parent; 534 } 535 536 return null; 537 } 538 } 539 | Popular Tags |