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 58 import com.lowagie.text.Cell; 59 import com.lowagie.text.Element; 60 import com.lowagie.text.Row; 61 import com.lowagie.text.rtf.RtfElement; 62 import com.lowagie.text.rtf.document.RtfDocument; 63 64 65 75 public class RtfRow extends RtfElement { 76 77 80 private static final byte[] ROW_BEGIN = "\\trowd".getBytes(); 81 84 private static final byte[] ROW_WIDTH_STYLE = "\\trftsWidth3".getBytes(); 85 88 private static final byte[] ROW_WIDTH = "\\trwWidth".getBytes(); 89 92 private static final byte[] ROW_KEEP_TOGETHER = "\\trkeep".getBytes(); 93 96 private static final byte[] ROW_HEADER_ROW = "\\trhdr".getBytes(); 97 100 private static final byte[] ROW_ALIGN_LEFT = "\\trql".getBytes(); 101 104 private static final byte[] ROW_ALIGN_RIGHT = "\\trqr".getBytes(); 105 108 private static final byte[] ROW_ALIGN_CENTER = "\\trqc".getBytes(); 109 112 private static final byte[] ROW_ALIGN_JUSTIFIED = "\\trqj".getBytes(); 113 116 private static final byte[] ROW_GRAPH = "\\trgaph10".getBytes(); 117 120 private static final byte[] ROW_CELL_SPACING_LEFT = "\\trspdl".getBytes(); 121 124 private static final byte[] ROW_CELL_SPACING_TOP = "\\trspdt".getBytes(); 125 128 private static final byte[] ROW_CELL_SPACING_RIGHT = "\\trspdr".getBytes(); 129 132 private static final byte[] ROW_CELL_SPACING_BOTTOM = "\\trspdb".getBytes(); 133 136 private static final byte[] ROW_CELL_SPACING_LEFT_STYLE = "\\trspdfl3".getBytes(); 137 140 private static final byte[] ROW_CELL_SPACING_TOP_STYLE = "\\trspdft3".getBytes(); 141 144 private static final byte[] ROW_CELL_SPACING_RIGHT_STYLE = "\\trspdfr3".getBytes(); 145 148 private static final byte[] ROW_CELL_SPACING_BOTTOM_STYLE = "\\trspdfb3".getBytes(); 149 152 private static final byte[] ROW_CELL_PADDING_LEFT = "\\trpaddl".getBytes(); 153 156 private static final byte[] ROW_CELL_PADDING_RIGHT = "\\trpaddr".getBytes(); 157 160 private static final byte[] ROW_CELL_PADDING_LEFT_STYLE = "\\trpaddfl3".getBytes(); 161 164 private static final byte[] ROW_CELL_PADDING_RIGHT_STYLE = "\\trpaddfr3".getBytes(); 165 168 private static final byte[] ROW_END = "\\row".getBytes(); 169 170 173 private RtfTable parentTable = null; 174 177 private ArrayList cells = null; 178 181 private int width = 0; 182 185 private int rowNumber = 0; 186 187 195 protected RtfRow(RtfDocument doc, RtfTable rtfTable, Row row, int rowNumber) { 196 super(doc); 197 this.parentTable = rtfTable; 198 this.rowNumber = rowNumber; 199 importRow(row); 200 } 201 202 207 private void importRow(Row row) { 208 this.cells = new ArrayList (); 209 this.width = this.document.getDocumentHeader().getPageSetting().getPageWidth() - this.document.getDocumentHeader().getPageSetting().getMarginLeft() - this.document.getDocumentHeader().getPageSetting().getMarginRight(); 210 this.width = (int) (this.width * this.parentTable.getTableWidthPercent() / 100); 211 212 int cellRight = 0; 213 int cellWidth = 0; 214 for(int i = 0; i < row.getColumns(); i++) { 215 cellWidth = (int) (this.width * this.parentTable.getProportionalWidths()[i] / 100); 216 cellRight = cellRight + cellWidth; 217 218 Cell cell = (Cell) row.getCell(i); 219 RtfCell rtfCell = new RtfCell(this.document, this, cell); 220 rtfCell.setCellRight(cellRight); 221 rtfCell.setCellWidth(cellWidth); 222 this.cells.add(rtfCell); 223 } 224 } 225 226 229 protected void handleCellSpanning() { 230 RtfCell deletedCell = new RtfCell(true); 231 for(int i = 0; i < this.cells.size(); i++) { 232 RtfCell rtfCell = (RtfCell) this.cells.get(i); 233 if(rtfCell.getColspan() > 1) { 234 int cSpan = rtfCell.getColspan(); 235 for(int j = i + 1; j < i + cSpan; j++) { 236 if(j < this.cells.size()) { 237 RtfCell rtfCellMerge = (RtfCell) this.cells.get(j); 238 rtfCell.setCellRight(rtfCell.getCellRight() + rtfCellMerge.getCellWidth()); 239 rtfCell.setCellWidth(rtfCell.getCellWidth() + rtfCellMerge.getCellWidth()); 240 this.cells.set(j, deletedCell); 241 } 242 } 243 } 244 if(rtfCell.getRowspan() > 1) { 245 ArrayList rows = this.parentTable.getRows(); 246 for(int j = 1; j < rtfCell.getRowspan(); j++) { 247 RtfRow mergeRow = (RtfRow) rows.get(this.rowNumber + j); 248 if(this.rowNumber + j < rows.size()) { 249 RtfCell rtfCellMerge = (RtfCell) mergeRow.getCells().get(i); 250 rtfCellMerge.setCellMergeChild(rtfCell); 251 } 252 if(rtfCell.getColspan() > 1) { 253 int cSpan = rtfCell.getColspan(); 254 for(int k = i + 1; k < i + cSpan; k++) { 255 if(k < mergeRow.getCells().size()) { 256 mergeRow.getCells().set(k, deletedCell); 257 } 258 } 259 } 260 } 261 } 262 } 263 } 264 265 268 protected void cleanRow() { 269 int i = 0; 270 while(i < this.cells.size()) { 271 if(((RtfCell) this.cells.get(i)).isDeleted()) { 272 this.cells.remove(i); 273 } else { 274 i++; 275 } 276 } 277 } 278 279 285 private byte[] writeRowDefinitions() { 286 ByteArrayOutputStream result = new ByteArrayOutputStream (); 287 try { 288 writeRowDefinitions(result); 289 } catch(IOException ioe) { 290 ioe.printStackTrace(); 291 } 292 return result.toByteArray(); 293 } 294 297 private void writeRowDefinitions(final OutputStream result) throws IOException 298 { 299 result.write(ROW_BEGIN); 300 result.write('\n'); 301 result.write(ROW_WIDTH_STYLE); 302 result.write(ROW_WIDTH); 303 result.write(intToByteArray(this.width)); 304 if(this.parentTable.getCellsFitToPage()) { 305 result.write(ROW_KEEP_TOGETHER); 306 } 307 if(this.rowNumber <= this.parentTable.getHeaderRows()) { 308 result.write(ROW_HEADER_ROW); 309 } 310 switch (this.parentTable.getAlignment()) { 311 case Element.ALIGN_LEFT: 312 result.write(ROW_ALIGN_LEFT); 313 break; 314 case Element.ALIGN_RIGHT: 315 result.write(ROW_ALIGN_RIGHT); 316 break; 317 case Element.ALIGN_CENTER: 318 result.write(ROW_ALIGN_CENTER); 319 break; 320 case Element.ALIGN_JUSTIFIED: 321 case Element.ALIGN_JUSTIFIED_ALL: 322 result.write(ROW_ALIGN_JUSTIFIED); 323 break; 324 } 325 result.write(ROW_GRAPH); 326 327 this.parentTable.getBorders().writeContent(result); 329 330 if(this.parentTable.getCellSpacing() > 0) { 331 result.write(ROW_CELL_SPACING_LEFT); 332 result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); 333 result.write(ROW_CELL_SPACING_LEFT_STYLE); 334 result.write(ROW_CELL_SPACING_TOP); 335 result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); 336 result.write(ROW_CELL_SPACING_TOP_STYLE); 337 result.write(ROW_CELL_SPACING_RIGHT); 338 result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); 339 result.write(ROW_CELL_SPACING_RIGHT_STYLE); 340 result.write(ROW_CELL_SPACING_BOTTOM); 341 result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); 342 result.write(ROW_CELL_SPACING_BOTTOM_STYLE); 343 } 344 345 result.write(ROW_CELL_PADDING_LEFT); 346 result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2))); 347 result.write(ROW_CELL_PADDING_RIGHT); 348 result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2))); 349 result.write(ROW_CELL_PADDING_LEFT_STYLE); 350 result.write(ROW_CELL_PADDING_RIGHT_STYLE); 351 352 result.write('\n'); 353 354 for(int i = 0; i < this.cells.size(); i++) { 355 RtfCell rtfCell = (RtfCell) this.cells.get(i); 356 rtfCell.writeDefinition(result); 358 } 359 } 360 361 367 public byte[] write() 368 { 369 ByteArrayOutputStream result = new ByteArrayOutputStream (); 370 try { 371 writeContent(result); 372 } catch(IOException ioe) { 373 ioe.printStackTrace(); 374 } 375 return result.toByteArray(); 376 } 377 380 public void writeContent(final OutputStream result) throws IOException 381 { 382 writeRowDefinitions(result); 384 385 for(int i = 0; i < this.cells.size(); i++) { 386 RtfCell rtfCell = (RtfCell) this.cells.get(i); 387 rtfCell.writeContent(result); 389 } 390 391 result.write(DELIMITER); 392 393 if(this.document.getDocumentSettings().isOutputTableRowDefinitionAfter()) { 394 writeRowDefinitions(result); 396 } 397 398 result.write(ROW_END); 399 result.write("\n".getBytes()); 400 } 401 402 407 protected RtfTable getParentTable() { 408 return this.parentTable; 409 } 410 411 416 protected ArrayList getCells() { 417 return this.cells; 418 } 419 } 420 | Popular Tags |