1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 28 29 import java.io.IOException ; 30 import java.io.Writer ; 31 import java.util.Iterator ; 32 33 38 39 public class RtfTableRow extends RtfContainer implements ITableAttributes { 40 private RtfTableCell cell; 41 private int id; 43 private int highestCell = 0; 44 45 46 47 RtfTableRow(RtfTable parent, Writer w, int idNum) throws IOException { 48 super(parent, w); 49 id = idNum; 50 } 51 52 53 RtfTableRow(RtfTable parent, Writer w, RtfAttributes attrs, int idNum) throws IOException { 54 super(parent, w, attrs); 55 id = idNum; 56 } 57 58 64 public RtfTableCell newTableCell(int cellWidth) throws IOException { 65 highestCell++; 66 cell = new RtfTableCell(this, writer, cellWidth, highestCell); 67 return cell; 68 } 69 70 77 public RtfTableCell newTableCell(int cellWidth, RtfAttributes attrs) throws IOException { 78 highestCell++; 79 cell = new RtfTableCell(this, writer, cellWidth, attrs, highestCell); 80 return cell; 81 } 82 83 92 public RtfTableCell newTableCellMergedVertically(int cellWidth, 93 RtfAttributes attrs) throws IOException { 94 highestCell++; 95 cell = new RtfTableCell (this, writer, cellWidth, attrs, highestCell); 96 cell.setVMerge(RtfTableCell.MERGE_WITH_PREVIOUS); 97 return cell; 98 } 99 100 108 public RtfTableCell newTableCellMergedHorizontally (int cellWidth, 109 RtfAttributes attrs) throws IOException { 110 highestCell++; 111 RtfAttributes wAttributes = null; 114 if (attrs != null) { 115 wAttributes = (RtfAttributes)attrs.clone(); 116 } 117 118 cell = new RtfTableCell(this, writer, cellWidth, wAttributes, highestCell); 119 cell.setHMerge(RtfTableCell.MERGE_WITH_PREVIOUS); 120 return cell; 121 } 122 123 126 protected void writeRtfPrefix() throws IOException { 127 newLine(); 128 writeGroupMark(true); 129 } 130 131 135 protected void writeRtfContent() throws IOException { 136 137 if (getTable().isNestedTable()) { 138 writeControlWord("intbl"); 140 writeControlWord("itap2"); 141 } else { 142 writeRowAndCellsDefintions(); 144 } 145 super.writeRtfContent(); 147 } 148 149 153 public void writeRowAndCellsDefintions() throws IOException { 154 writeControlWord("trowd"); 156 157 if (!getTable().isNestedTable()) { 158 writeControlWord("itap0"); 159 } 160 161 if (attrib != null && attrib.isSet(ITableAttributes.ROW_KEEP_TOGETHER)) { 163 writeControlWord(ROW_KEEP_TOGETHER); 164 } 165 166 writePaddingAttributes(); 167 168 final RtfTable parentTable = (RtfTable) parent; 169 adjustBorderProperties(parentTable); 170 171 writeAttributes(attrib, new String []{ITableAttributes.ATTR_HEADER}); 172 writeAttributes(attrib, ITableAttributes.ROW_BORDER); 173 writeAttributes(attrib, ITableAttributes.CELL_BORDER); 174 writeAttributes(attrib, IBorderAttributes.BORDERS); 175 176 if (attrib.isSet(ITableAttributes.ROW_HEIGHT)) { 177 writeOneAttribute( 178 ITableAttributes.ROW_HEIGHT, 179 attrib.getValue(ITableAttributes.ROW_HEIGHT)); 180 } 181 182 int xPos = 0; 184 185 final Object leftIndent = attrib.getValue(ITableAttributes.ATTR_ROW_LEFT_INDENT); 186 if (leftIndent != null) { 187 xPos = ((Integer )leftIndent).intValue(); 188 } 189 190 RtfAttributes tableBorderAttributes = getTable().getBorderAttributes(); 191 192 int index = 0; 193 for (Iterator it = getChildren().iterator(); it.hasNext();) { 194 final RtfElement e = (RtfElement)it.next(); 195 if (e instanceof RtfTableCell) { 196 197 RtfTableCell rtfcell = (RtfTableCell)e; 198 199 202 if (tableBorderAttributes != null) { 203 if (index == 0) { 205 String border = ITableAttributes.CELL_BORDER_LEFT; 206 if (!rtfcell.getRtfAttributes().isSet(border)) { 207 rtfcell.getRtfAttributes().set(border, 208 (RtfAttributes) tableBorderAttributes.getValue(border)); 209 } 210 } 211 212 if (index == this.getChildCount() - 1) { 213 String border = ITableAttributes.CELL_BORDER_RIGHT; 214 if (!rtfcell.getRtfAttributes().isSet(border)) { 215 rtfcell.getRtfAttributes().set(border, 216 (RtfAttributes) tableBorderAttributes.getValue(border)); 217 } 218 } 219 220 if (isFirstRow()) { 221 String border = ITableAttributes.CELL_BORDER_TOP; 222 if (!rtfcell.getRtfAttributes().isSet(border)) { 223 rtfcell.getRtfAttributes().set(border, 224 (RtfAttributes) tableBorderAttributes.getValue(border)); 225 } 226 } 227 228 if ((parentTable != null) && (parentTable.isHighestRow(id))) { 229 String border = ITableAttributes.CELL_BORDER_BOTTOM; 230 if (!rtfcell.getRtfAttributes().isSet(border)) { 231 rtfcell.getRtfAttributes().set(border, 232 (RtfAttributes) tableBorderAttributes.getValue(border)); 233 } 234 } 235 } 236 237 if (index == 0) { 239 if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_LEFT)) { 240 rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_LEFT, 241 (String ) attrib.getValue(ITableAttributes.ROW_BORDER_LEFT)); 242 } 243 } 244 245 if (index == this.getChildCount() - 1) { 246 if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_RIGHT)) { 247 rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_RIGHT, 248 (String ) attrib.getValue(ITableAttributes.ROW_BORDER_RIGHT)); 249 } 250 } 251 252 if (isFirstRow()) { 253 if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_TOP)) { 254 rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_TOP, 255 (String ) attrib.getValue(ITableAttributes.ROW_BORDER_TOP)); 256 } 257 } 258 259 if ((parentTable != null) && (parentTable.isHighestRow(id))) { 260 if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_BOTTOM)) { 261 rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_BOTTOM, 262 (String ) attrib.getValue(ITableAttributes.ROW_BORDER_BOTTOM)); 263 } 264 } 265 266 xPos = rtfcell.writeCellDef(xPos); 268 } 269 index++; } 271 272 newLine(); 273 } 274 275 private void adjustBorderProperties(RtfTable parentTable) { 276 if (attrib != null && parentTable != null) { 278 279 if (isFirstRow() && parentTable.isHighestRow(id)) { 281 attrib.unset(ITableAttributes.ROW_BORDER_HORIZONTAL); 282 } else if (isFirstRow()) { 284 attrib.unset(ITableAttributes.ROW_BORDER_BOTTOM); 285 } else if (parentTable.isHighestRow(id)) { 287 attrib.unset(ITableAttributes.ROW_BORDER_TOP); 288 } else { 290 attrib.unset(ITableAttributes.ROW_BORDER_BOTTOM); 291 attrib.unset(ITableAttributes.ROW_BORDER_TOP); 292 } 293 } 294 } 295 296 300 protected void writeRtfSuffix() throws IOException { 301 if (getTable().isNestedTable()) { 302 writeGroupMark(true); 304 writeStarControlWord("nesttableprops"); 305 writeRowAndCellsDefintions(); 306 writeControlWordNS("nestrow"); 307 writeGroupMark(false); 308 309 writeGroupMark(true); 310 writeControlWord("nonesttables"); 311 writeControlWord("par"); 312 writeGroupMark(false); 313 } else { 314 writeControlWord("row"); 315 } 316 317 writeGroupMark(false); 318 } 319 320 324 private void writePaddingAttributes() 325 throws IOException { 326 if (attrib != null && !attrib.isSet(ATTR_RTF_15_TRGAPH)) { 331 int gaph = -1; 332 try { 333 final Integer leftPadStr = (Integer )attrib.getValue(ATTR_ROW_PADDING_LEFT); 335 if (leftPadStr != null) { 336 gaph = leftPadStr.intValue(); 337 } 338 final Integer rightPadStr = (Integer )attrib.getValue(ATTR_ROW_PADDING_RIGHT); 339 if (rightPadStr != null) { 340 gaph = (gaph + rightPadStr.intValue()) / 2; 341 } 342 } catch (Exception e) { 343 final String msg = "RtfTableRow.writePaddingAttributes: " + e.toString(); 344 } 346 if (gaph >= 0) { 347 attrib.set(ATTR_RTF_15_TRGAPH, gaph); 348 } 349 } 350 351 writeAttributes(attrib, ATTRIB_ROW_PADDING); 353 } 354 355 358 public boolean isFirstRow() { 359 return (id == 1); 360 } 361 362 366 public boolean isHighestCell(int cellId) { 367 return (highestCell == cellId) ? true : false; 368 } 369 370 374 public RtfTable getTable() { 375 RtfElement e = this; 376 while (e.parent != null) { 377 if (e.parent instanceof RtfTable) { 378 return (RtfTable) e.parent; 379 } 380 381 e = e.parent; 382 } 383 384 return null; 385 } 386 } 387 | Popular Tags |