You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NestedTable.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.testdocs;
  58. import java.io.IOException;
  59. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfDocumentArea;
  60. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfSection;
  61. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTable;
  62. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfParagraph;
  63. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableRow;
  64. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableCell;
  65. /** Generates an RTF document to test nested tables with the jfor rtflib package.
  66. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  67. */
  68. class NestedTable extends TestDocument {
  69. private static final int MM_TO_TWIPS = (int)(1440f / 25.4f);
  70. /** generate the body of the test document */
  71. protected void generateDocument(RtfDocumentArea rda, RtfSection sect)
  72. throws IOException {
  73. sect.newParagraph().newText("This document demonstrates pseudo-nested "
  74. + "tables created using merged table cells");
  75. firstTestTable(sect);
  76. RtfParagraph p = sect.newParagraph();
  77. p.newText("Test continues on next page.");
  78. p.newPageBreak();
  79. secondTestTable(sect);
  80. p = sect.newParagraph();
  81. p.newText("Test continues on next page.");
  82. p.newPageBreak();
  83. thirdTestTable(sect);
  84. sect.newParagraph().newText("End of nested tables test document");
  85. }
  86. private void firstTestTable(RtfSection sect)
  87. throws IOException {
  88. sect.newParagraph().newText("First test: table with one nested table in cell 1,1");
  89. final RtfTable tbl = sect.newTable(new DummyTableColumnsInfo());
  90. // first row, normal
  91. {
  92. RtfTableRow r = tbl.newTableRow();
  93. RtfTableCell c = r.newTableCell(160 * MM_TO_TWIPS);
  94. c.newParagraph().newText("cell 0,0, width 160mm, only cell in this row.");
  95. }
  96. // second row contains nested table
  97. {
  98. RtfTableRow r = tbl.newTableRow();
  99. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText
  100. ("cell 1,0, width 40mm, to the left of nested table.");
  101. final RtfTableCell c = r.newTableCell(80 * MM_TO_TWIPS);
  102. c.newParagraph().newText("cell 1,1, width 80mm, this text is "
  103. + "followed by a nested table in the same cell, followed "
  104. + "by text that says 'AFTER NESTED TABLE'.");
  105. fillNestedTable(c.newTable(new DummyTableColumnsInfo()), 1);
  106. c.newParagraph().newText("AFTER NESTED TABLE");
  107. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText
  108. ("cell 1,2, width 40mm, to the right of nested table.");
  109. }
  110. // third row, normal
  111. {
  112. RtfTableRow r = tbl.newTableRow();
  113. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText
  114. ("cell 2,0, width 80mm, this row has two cells.");
  115. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText
  116. ("cell 2,1, width 80mm, last cell.");
  117. }
  118. }
  119. private void secondTestTable(RtfSection sect)
  120. throws IOException {
  121. sect.newParagraph().newText("Second test: table with two nested tables in cell 1,1");
  122. final RtfTable tbl = sect.newTable(new DummyTableColumnsInfo());
  123. // first row, normal
  124. {
  125. RtfTableRow r = tbl.newTableRow();
  126. RtfTableCell c = r.newTableCell(160 * MM_TO_TWIPS);
  127. c.newParagraph().newText("second test table: cell 0,0, width 160mm, "
  128. + "only cell in this row.");
  129. }
  130. // second row contains nested table
  131. {
  132. RtfTableRow r = tbl.newTableRow();
  133. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText
  134. ("cell 1,0, width 40mm, to the left of nested tables.");
  135. final RtfTableCell c = r.newTableCell(80 * MM_TO_TWIPS);
  136. c.newParagraph().newText("cell 1,1, width 80mm, this text is "
  137. + "followed by a nested table in the same cell, followed "
  138. + "by text that says 'BETWEEN', then another table, then 'AFTER'.");
  139. fillNestedTable(c.newTable(new DummyTableColumnsInfo()), 2);
  140. c.newParagraph().newText("BETWEEN");
  141. fillNestedTable(c.newTable(new DummyTableColumnsInfo()), 3);
  142. c.newParagraph().newText("AFTER");
  143. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText
  144. ("cell 1,2, width 40mm, to the right of nested table.");
  145. }
  146. // third row, normal
  147. {
  148. RtfTableRow r = tbl.newTableRow();
  149. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText
  150. ("cell 2,0, width 80mm, this row has two cells.");
  151. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText
  152. ("cell 2,1, width 80mm, last cell.");
  153. }
  154. }
  155. private void thirdTestTable(RtfSection sect)
  156. throws IOException {
  157. sect.newParagraph().newText("Third test: table with two nested tables "
  158. + "in cell 1,1 and one nested table in cell 0,1");
  159. final RtfTable tbl = sect.newTable(new DummyTableColumnsInfo());
  160. // first row, normal
  161. {
  162. RtfTableRow r = tbl.newTableRow();
  163. RtfTableCell c = r.newTableCell(80 * MM_TO_TWIPS);
  164. c.newParagraph().newText("third test table: cell 0,0, width 40mm, "
  165. + "the cell to its right contains a nested table with no other text.");
  166. c = r.newTableCell(80 * MM_TO_TWIPS);
  167. fillNestedTable(c.newTable(new DummyTableColumnsInfo()), 4);
  168. }
  169. // second row contains nested table
  170. {
  171. RtfTableRow r = tbl.newTableRow();
  172. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText
  173. ("cell 1,0, width 40mm, to the left of nested tables.");
  174. final RtfTableCell c = r.newTableCell(80 * MM_TO_TWIPS);
  175. c.newParagraph().newText("cell 1,1, width 80mm, this text is "
  176. + "followed by a nested table in the same cell, followed "
  177. + "by text that says 'BETWEEN', then another table, then 'AFTER'.");
  178. fillNestedTable(c.newTable(new DummyTableColumnsInfo()), 5);
  179. c.newParagraph().newText("BETWEEN");
  180. fillNestedTable(c.newTable(new DummyTableColumnsInfo()), 6);
  181. c.newParagraph().newText("AFTER");
  182. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText
  183. ("cell 1,2, width 40mm, to the right of nested table.");
  184. }
  185. // third row, normal
  186. {
  187. RtfTableRow r = tbl.newTableRow();
  188. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText
  189. ("cell 2,0, width 80mm, this row has two cells.");
  190. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText
  191. ("cell 2,1, width 80mm, last cell.");
  192. }
  193. }
  194. /** fill the nested table */
  195. private void fillNestedTable(RtfTable tbl, int index)
  196. throws IOException {
  197. final String id = "TABLE " + index;
  198. {
  199. RtfTableRow r = tbl.newTableRow();
  200. r.newTableCell(80 * MM_TO_TWIPS).newParagraph().newText(
  201. id + ":nested cell 0,0. Nested table contains 3 rows with 1,2 and 3 cells respectively"
  202. );
  203. }
  204. {
  205. RtfTableRow r = tbl.newTableRow();
  206. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText(id + ":nested cell 1,0, 40mm.");
  207. r.newTableCell(40 * MM_TO_TWIPS).newParagraph().newText(id + ":nested cell 1,1, 40mm.");
  208. }
  209. {
  210. RtfTableRow r = tbl.newTableRow();
  211. r.newTableCell(30 * MM_TO_TWIPS).newParagraph().newText(id + ":nested cell 2,0, 30mm.");
  212. r.newTableCell(30 * MM_TO_TWIPS).newParagraph().newText(id + ":nested cell 2,1, 30mm.");
  213. r.newTableCell(20 * MM_TO_TWIPS).newParagraph().newText(id + ":nested cell 2,2, 20mm.");
  214. }
  215. }
  216. }