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.List ; 32 import java.util.LinkedList ; 33 import java.util.Iterator ; 34 import java.util.Collections ; 35 36 37 50 51 public class RtfExtraRowSet extends RtfContainer { 52 static final int DEFAULT_IDNUM = 0; 54 55 58 private ITableColumnsInfo parentITableColumnsInfo = null; 59 60 64 private final List cells = new LinkedList (); 65 private static class PositionedCell 66 implements Comparable { 67 private final RtfTableCell cell; 68 private final int xOffset; 69 private final int rowIndex; 70 71 PositionedCell(RtfTableCell c, int index, int offset) { 72 cell = c; 73 xOffset = offset; 74 rowIndex = index; 75 } 76 77 78 public String toString() { 79 return "PositionedCell: row " + rowIndex + ", offset " + xOffset; 80 } 81 82 83 public int compareTo(Object o) { 84 int result = 0; 85 if (o == null) { 86 result = 1; 87 } else if (!(o instanceof PositionedCell)) { 88 result = 1; 89 } else { 90 final PositionedCell pc = (PositionedCell)o; 91 if (this.rowIndex < pc.rowIndex) { 92 result = -1; 93 } else if (this.rowIndex > pc.rowIndex) { 94 result = 1; 95 } else if (this.xOffset < pc.xOffset) { 96 result = -1; 97 } else if (this.xOffset > pc.xOffset) { 98 result = 1; 99 } 100 } 101 102 return result; 103 } 104 105 public boolean equals(Object o) { 106 return o != null && this.compareTo(o) == 0; 107 } 108 } 109 110 111 private int maxRowIndex; 112 113 116 RtfExtraRowSet(Writer w) 117 throws IOException { 118 super(null, w); 119 } 120 121 126 int addTable(RtfTable tbl, int rowIndex, int xOffset) { 127 for (Iterator it = tbl.getChildren().iterator(); it.hasNext();) { 129 final RtfElement e = (RtfElement)it.next(); 130 if (e instanceof RtfTableRow) { 131 addRow((RtfTableRow)e, rowIndex, xOffset); 132 rowIndex++; 133 maxRowIndex = Math.max(rowIndex, maxRowIndex); 134 } 135 } 136 return rowIndex; 137 } 138 139 140 private void addRow(RtfTableRow row, int rowIndex, int xOffset) { 141 for (Iterator it = row.getChildren().iterator(); it.hasNext();) { 142 final RtfElement e = (RtfElement)it.next(); 143 if (e instanceof RtfTableCell) { 144 final RtfTableCell c = (RtfTableCell)e; 145 cells.add(new PositionedCell(c, rowIndex, xOffset)); 146 xOffset += c.getCellWidth(); 147 } 148 } 149 } 150 151 155 RtfTableCell createExtraCell(int rowIndex, int xOffset, int cellWidth, 156 RtfAttributes parentCellAttributes) 157 throws IOException { 158 final RtfTableCell c = new RtfTableCell(null, writer, cellWidth, 159 parentCellAttributes, DEFAULT_IDNUM); 160 cells.add(new PositionedCell(c, rowIndex, xOffset)); 161 return c; 162 } 163 164 169 protected void writeRtfContent() throws IOException { 170 Collections.sort(cells); 172 173 List rowCells = null; 175 int rowIndex = -1; 176 for (Iterator it = cells.iterator(); it.hasNext();) { 177 final PositionedCell pc = (PositionedCell)it.next(); 178 if (pc.rowIndex != rowIndex) { 179 if (rowCells != null) { 181 writeRow(rowCells); 182 } 183 rowIndex = pc.rowIndex; 184 rowCells = new LinkedList (); 185 } 186 rowCells.add(pc); 187 } 188 189 if (rowCells != null) { 191 writeRow(rowCells); 192 } 193 } 194 195 196 private void writeRow(List cells) 197 throws IOException { 198 if (allCellsEmpty(cells)) { 199 return; 200 } 201 202 final RtfTableRow row = new RtfTableRow(null, writer, DEFAULT_IDNUM); 203 int cellIndex = 0; 204 205 ITableColumnsInfo parentITableColumnsInfo = getParentITableColumnsInfo(); 207 parentITableColumnsInfo.selectFirstColumn(); 208 209 float xOffset = 0; 211 float xOffsetOfLastPositionedCell = 0; 212 213 for (Iterator it = cells.iterator(); it.hasNext();) { 214 final PositionedCell pc = (PositionedCell)it.next(); 215 216 if (cellIndex == 0 && pc.xOffset > 0) { 219 222 for (int i = 0; (xOffset < pc.xOffset) 225 && (i < parentITableColumnsInfo.getNumberOfColumns()); i++) { 226 xOffset += parentITableColumnsInfo.getColumnWidth(); 228 row.newTableCellMergedVertically((int)parentITableColumnsInfo.getColumnWidth(), 230 pc.cell.attrib); 231 parentITableColumnsInfo.selectNextColumn(); 233 } 234 } 235 236 row.addChild(pc.cell); 237 xOffsetOfLastPositionedCell = pc.xOffset + pc.cell.getCellWidth(); 239 cellIndex++; 240 } 241 242 245 if (parentITableColumnsInfo.getColumnIndex() 249 < (parentITableColumnsInfo.getNumberOfColumns() - 1)) { 250 parentITableColumnsInfo.selectNextColumn(); 251 252 while (parentITableColumnsInfo.getColumnIndex() 253 < parentITableColumnsInfo.getNumberOfColumns()) { 254 row.newTableCellMergedVertically((int)parentITableColumnsInfo.getColumnWidth(), 260 attrib); 261 parentITableColumnsInfo.selectNextColumn(); 263 } 264 } 265 266 row.writeRtf(); 267 } 268 269 272 private static boolean allCellsEmpty(List cells) { 273 boolean empty = true; 274 for (Iterator it = cells.iterator(); it.hasNext();) { 275 final PositionedCell pc = (PositionedCell)it.next(); 276 if (pc.cell.containsText()) { 277 empty = false; 278 break; 279 } 280 } 281 return empty; 282 } 283 284 290 public boolean isEmpty() { 291 return false; 292 } 293 294 298 public ITableColumnsInfo getParentITableColumnsInfo() { 299 return this.parentITableColumnsInfo; 300 } 301 302 306 public void setParentITableColumnsInfo (ITableColumnsInfo parentITableColumnsInfo) { 307 this.parentITableColumnsInfo = parentITableColumnsInfo; 308 } 309 310 } 311 | Popular Tags |