選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RtfExtraRowSet.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. /*
  52. * This file is part of the RTF library of the FOP project, which was originally
  53. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  54. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  55. * the FOP project.
  56. */
  57. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  58. import java.io.Writer;
  59. import java.io.IOException;
  60. import java.util.List;
  61. import java.util.LinkedList;
  62. import java.util.Iterator;
  63. import java.util.Collections;
  64. import org.apache.fop.render.rtf.rtflib.interfaces.ITableColumnsInfo;
  65. /**
  66. * Used to add extra table rows after a row that contains a nested table:
  67. * <li> created by RtfTableRow before generating RTF code
  68. * <li> an RtfTableCell that contains a nested table can ask this to put
  69. * some of its children in extra rows that after the current row
  70. * <li> once RtfTableRow is done rendering its children, it renders this,
  71. * causing extra rows to be generated, with content that can come
  72. * from several RtfTableCells
  73. *
  74. * See org.apache.fop.rtf.rtflib.testdocs.NestedTable for an example of
  75. * usage.
  76. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  77. */
  78. public class RtfExtraRowSet extends RtfContainer {
  79. // TODO what is idnum?
  80. static final int DEFAULT_IDNUM = 0;
  81. /** Parent table context
  82. * (added by Boris Poudérous on july 2002 in order to process nested tables)
  83. */
  84. private ITableColumnsInfo parentITableColumnsInfo = null;
  85. /** While a top-level RtfTableRow is being rendered, we build a list of
  86. * RtfTableCells that must be rendered in extra rows.
  87. * This holds a cell with positioning information
  88. */
  89. private final List cells = new LinkedList();
  90. private static class PositionedCell
  91. implements Comparable {
  92. private final RtfTableCell cell;
  93. private final int xOffset;
  94. private final int rowIndex;
  95. PositionedCell(RtfTableCell c, int index, int offset) {
  96. cell = c;
  97. xOffset = offset;
  98. rowIndex = index;
  99. }
  100. /** debugging dump */
  101. public String toString() {
  102. return "PositionedCell: row " + rowIndex + ", offset " + xOffset;
  103. }
  104. /** cells need to be sorted by row index and then by x offset */
  105. public int compareTo(Object o) {
  106. int result = 0;
  107. if (o == null) {
  108. result = 1;
  109. } else if (!(o instanceof PositionedCell)) {
  110. result = 1;
  111. } else {
  112. final PositionedCell pc = (PositionedCell)o;
  113. if (this.rowIndex < pc.rowIndex) {
  114. result = -1;
  115. } else if (this.rowIndex > pc.rowIndex) {
  116. result = 1;
  117. } else if (this.xOffset < pc.xOffset) {
  118. result = -1;
  119. } else if (this.xOffset > pc.xOffset) {
  120. result = 1;
  121. }
  122. }
  123. return result;
  124. }
  125. public boolean equals(Object o) {
  126. return o != null && this.compareTo(o) == 0;
  127. }
  128. }
  129. /** our maximum row index */
  130. private int maxRowIndex;
  131. /** an RtfExtraRowSet has no parent, it is only used temporary during
  132. * generation of RTF for an RtfTableRow
  133. */
  134. RtfExtraRowSet(Writer w)
  135. throws IOException {
  136. super(null, w);
  137. }
  138. /** Add all cells of given Table to this set for later rendering in extra rows
  139. * @return index of extra row to use for elements that follow this table in the same cell
  140. * @param rowIndex index of first extra row to create to hold cells of tbl
  141. * @param xOffset horizontal position of left edge of first column of tbl
  142. */
  143. int addTable(RtfTable tbl, int rowIndex, int xOffset) {
  144. // process all rows of the table
  145. for (Iterator it = tbl.getChildren().iterator(); it.hasNext();) {
  146. final RtfElement e = (RtfElement)it.next();
  147. if (e instanceof RtfTableRow) {
  148. addRow((RtfTableRow)e, rowIndex, xOffset);
  149. rowIndex++;
  150. maxRowIndex = Math.max(rowIndex, maxRowIndex);
  151. }
  152. }
  153. return rowIndex;
  154. }
  155. /** add all cells of given row to this set */
  156. private void addRow(RtfTableRow row, int rowIndex, int xOffset) {
  157. for (Iterator it = row.getChildren().iterator(); it.hasNext();) {
  158. final RtfElement e = (RtfElement)it.next();
  159. if (e instanceof RtfTableCell) {
  160. final RtfTableCell c = (RtfTableCell)e;
  161. cells.add(new PositionedCell(c, rowIndex, xOffset));
  162. xOffset += c.getCellWidth();
  163. }
  164. }
  165. }
  166. /** create an extra cell to hold content that comes after a nested table in a cell
  167. * Modified by Boris Poudérous in order to permit the extra cell to have
  168. * the attributes of its parent cell
  169. */
  170. RtfTableCell createExtraCell(int rowIndex, int xOffset, int cellWidth,
  171. RtfAttributes parentCellAttributes)
  172. throws IOException {
  173. final RtfTableCell c = new RtfTableCell(null, writer, cellWidth,
  174. parentCellAttributes, DEFAULT_IDNUM);
  175. cells.add(new PositionedCell(c, rowIndex, xOffset));
  176. return c;
  177. }
  178. /**
  179. * render extra RtfTableRows containing all the extra RtfTableCells that we
  180. * contain
  181. * @throws IOException for I/O problems
  182. */
  183. protected void writeRtfContent() throws IOException {
  184. // sort cells by rowIndex and xOffset
  185. Collections.sort(cells);
  186. // process all extra cells by rendering them into extra rows
  187. List rowCells = null;
  188. int rowIndex = -1;
  189. for (Iterator it = cells.iterator(); it.hasNext();) {
  190. final PositionedCell pc = (PositionedCell)it.next();
  191. if (pc.rowIndex != rowIndex) {
  192. // starting a new row, render previous one
  193. if (rowCells != null) {
  194. writeRow(rowCells);
  195. }
  196. rowIndex = pc.rowIndex;
  197. rowCells = new LinkedList();
  198. }
  199. rowCells.add(pc);
  200. }
  201. // render last row
  202. if (rowCells != null) {
  203. writeRow(rowCells);
  204. }
  205. }
  206. /** write one RtfTableRow containing given PositionedCells */
  207. private void writeRow(List cells)
  208. throws IOException {
  209. if (allCellsEmpty(cells)) {
  210. return;
  211. }
  212. final RtfTableRow row = new RtfTableRow(null, writer, DEFAULT_IDNUM);
  213. int cellIndex = 0;
  214. // Get the context of the table that holds the nested table
  215. ITableColumnsInfo parentITableColumnsInfo = getParentITableColumnsInfo();
  216. parentITableColumnsInfo.selectFirstColumn();
  217. // X offset of the current empty cell to add
  218. float xOffset = 0;
  219. float xOffsetOfLastPositionedCell = 0;
  220. for (Iterator it = cells.iterator(); it.hasNext();) {
  221. final PositionedCell pc = (PositionedCell)it.next();
  222. // if first cell is not at offset 0, add placeholder cell
  223. // TODO should be merged with the cell that is above it
  224. if (cellIndex == 0 && pc.xOffset > 0) {
  225. /**
  226. * Added by Boris Poudérous
  227. */
  228. // Add empty cells merged vertically with the cells above and with the same widths
  229. // (BEFORE the cell that contains the nested table)
  230. for (int i = 0; (xOffset < pc.xOffset)
  231. && (i < parentITableColumnsInfo.getNumberOfColumns()); i++) {
  232. // Get the width of the cell above
  233. xOffset += parentITableColumnsInfo.getColumnWidth();
  234. // Create the empty cell merged vertically
  235. row.newTableCellMergedVertically((int)parentITableColumnsInfo.getColumnWidth(),
  236. pc.cell.attrib);
  237. // Select next column in order to have its width
  238. parentITableColumnsInfo.selectNextColumn();
  239. }
  240. }
  241. row.addChild(pc.cell);
  242. // Line added by Boris Poudérous
  243. xOffsetOfLastPositionedCell = pc.xOffset + pc.cell.getCellWidth();
  244. cellIndex++;
  245. }
  246. /**
  247. * Added by Boris Poudérous
  248. */
  249. // Add empty cells merged vertically with the cells above AFTER the cell
  250. // that contains the nested table
  251. // The cells added have the same widths than the cells above.
  252. if (parentITableColumnsInfo.getColumnIndex()
  253. < (parentITableColumnsInfo.getNumberOfColumns() - 1)) {
  254. parentITableColumnsInfo.selectNextColumn();
  255. while (parentITableColumnsInfo.getColumnIndex()
  256. < parentITableColumnsInfo.getNumberOfColumns()) {
  257. // Create the empty cell merged vertically
  258. // TODO : the new cells after the extra cell don't have its
  259. // attributes as we did for the previous cells.
  260. // => in fact the m_attrib below (last argument) is
  261. // empty => should be the attributes of the above cells.
  262. row.newTableCellMergedVertically((int)parentITableColumnsInfo.getColumnWidth(),
  263. attrib);
  264. // Select next column in order to have its width
  265. parentITableColumnsInfo.selectNextColumn();
  266. }
  267. }
  268. row.writeRtf();
  269. }
  270. /** true if all cells of given list are empty
  271. * @param cells List of PositionedCell objects
  272. */
  273. private static boolean allCellsEmpty(List cells) {
  274. boolean empty = true;
  275. for (Iterator it = cells.iterator(); it.hasNext();) {
  276. final PositionedCell pc = (PositionedCell)it.next();
  277. if (pc.cell.containsText()) {
  278. empty = false;
  279. break;
  280. }
  281. }
  282. return empty;
  283. }
  284. /**
  285. * As this contains cells from several rows, we say that it's empty
  286. * only if we have no cells.
  287. * writeRow makes the decision about rendering specific rows
  288. * @return false (always)
  289. */
  290. public boolean isEmpty() {
  291. return false;
  292. }
  293. /**
  294. * @return The table context of the parent table
  295. * Added by Boris Poudérous on july 2002 in order to process nested tables
  296. */
  297. public ITableColumnsInfo getParentITableColumnsInfo() {
  298. return this.parentITableColumnsInfo;
  299. }
  300. /**
  301. *
  302. * @param parentITableColumnsInfo table context to set
  303. */
  304. public void setParentITableColumnsInfo (ITableColumnsInfo parentITableColumnsInfo) {
  305. this.parentITableColumnsInfo = parentITableColumnsInfo;
  306. }
  307. /** - end - */
  308. }