KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > testdocs > NestedTable


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: NestedTable.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20
21 /*
22  * This file is part of the RTF library of the FOP project, which was originally
23  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
24  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
25  * the FOP project.
26  */

27
28 package org.apache.fop.render.rtf.rtflib.testdocs;
29
30 import java.io.IOException JavaDoc;
31
32 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfDocumentArea;
33 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfSection;
34 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTable;
35 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfParagraph;
36 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableRow;
37 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableCell;
38
39 /** Generates an RTF document to test nested tables with the jfor rtflib package.
40  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
41  */

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