KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > tools > TableContext


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: TableContext.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.tools;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.commons.logging.impl.SimpleLog;
25 import org.apache.commons.logging.Log;
26 import org.apache.fop.render.rtf.rtflib.rtfdoc.ITableColumnsInfo;
27 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
28
29
30 /** Used when handling fo:table to hold information to build the table.
31  *
32  * Contributor(s):
33  * @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
34  * @author Trembicki-Guy, Ed <GuyE@DNB.com>
35  * @author Boris Poudérous <boris.pouderous@eads-telecom.com>
36  * @author Peter Herweg <pherweg@web.de>
37  *
38  * This class was originally developed for the JFOR project and
39  * is now integrated into FOP.
40  */

41
42 public class TableContext implements ITableColumnsInfo {
43     private final Log log = new SimpleLog("FOP/RTF");
44     private final BuilderContext context;
45     private final List JavaDoc colWidths = new java.util.ArrayList JavaDoc();
46     private int colIndex;
47
48     /**
49      * This ArrayList contains one element for each column in the table.
50      * value == 0 means there is no row-spanning
51      * value > 0 means there is row-spanning
52      * Each value in the list is decreased by 1 after each finished table-row
53      */

54     private final List JavaDoc colRowSpanningNumber = new java.util.ArrayList JavaDoc();
55
56     /**
57      * If there has a vertical merged cell to be created, its attributes are
58      * inherited from the corresponding MERGE_START-cell.
59      * For this purpose the attributes of a cell are stored in this array, as soon
60      * as a number-rows-spanned attribute has been found.
61      */

62     private final List JavaDoc colRowSpanningAttrs = new java.util.ArrayList JavaDoc();
63     
64     /**
65      * This ArrayList contains one element for each column in the table.
66      * value == true means, it's the first of multiple spanned columns
67      * value == false meanst, it's NOT the first of multiple spanned columns
68      */

69     private final List JavaDoc colFirstSpanningCol = new java.util.ArrayList JavaDoc();
70
71     private boolean bNextRowBelongsToHeader = false;
72
73     /**
74      *
75      * @param value Specifies, if next row belongs to header
76      */

77     public void setNextRowBelongsToHeader(boolean value) {
78         this.bNextRowBelongsToHeader = value;
79     }
80
81     /**
82      *
83      * @return true, if next row belongs to header
84      */

85     public boolean getNextRowBelongsToHeader() {
86         return bNextRowBelongsToHeader;
87     }
88
89     /**
90      *
91      * @param ctx BuilderContext
92      */

93     public TableContext(BuilderContext ctx) {
94         context = ctx;
95     }
96
97     /**
98      * Adds a column and sets its width.
99      * @param width Width of next column
100      */

101     public void setNextColumnWidth(Float JavaDoc width) {
102         colWidths.add(width);
103     }
104
105     /**
106      *
107      * @return RtfAttributes of current row-spanning cell
108      */

109     public RtfAttributes getColumnRowSpanningAttrs() {
110         return (RtfAttributes)colRowSpanningAttrs.get(colIndex);
111     }
112
113     /**
114      *
115      * @return Number of currently spanned rows
116      */

117     public Integer JavaDoc getColumnRowSpanningNumber() {
118         return (Integer JavaDoc)colRowSpanningNumber.get(colIndex);
119     }
120     
121     /**
122      *
123      * @return true, if it's the first of multiple spanning columns
124      */

125     public boolean getFirstSpanningCol() {
126         Boolean JavaDoc b = (Boolean JavaDoc) colFirstSpanningCol.get(colIndex);
127         return b.booleanValue();
128     }
129
130     /**
131      *
132      * @param iRowSpanning number of rows to span
133      * @param attrs RtfAttributes of row-spanning cell
134      */

135     public void setCurrentColumnRowSpanning(
136             Integer JavaDoc iRowSpanning, RtfAttributes attrs) {
137
138         if (colIndex < colRowSpanningNumber.size()) {
139             colRowSpanningNumber.set(colIndex, iRowSpanning);
140             colRowSpanningAttrs.set(colIndex, attrs);
141         } else {
142             colRowSpanningNumber.add(iRowSpanning);
143             colRowSpanningAttrs.add(colIndex, attrs);
144         }
145     }
146
147     /**
148      *
149      * @param iRowSpanning number of rows to span in next column
150      * @param attrs RtfAttributes of row-spanning cell
151      */

152     public void setNextColumnRowSpanning(Integer JavaDoc iRowSpanning,
153             RtfAttributes attrs) {
154         colRowSpanningNumber.add(iRowSpanning);
155         colRowSpanningAttrs.add(colIndex, attrs);
156     }
157     
158     /**
159      *
160      * @param bFirstSpanningCol specifies, if it's the first of
161      * multiple spanned columns
162      */

163     public void setCurrentFirstSpanningCol(
164             boolean bFirstSpanningCol) {
165
166         if (colIndex < colRowSpanningNumber.size()) {
167             while (colIndex >= colFirstSpanningCol.size()) {
168                 setNextFirstSpanningCol(false);
169             }
170             colFirstSpanningCol.set(colIndex, new Boolean JavaDoc(bFirstSpanningCol));
171         } else {
172             colFirstSpanningCol.add(new Boolean JavaDoc(bFirstSpanningCol));
173         }
174     }
175
176     /**
177      *
178      * @param bFirstSpanningCol specifies, if it's the first of
179      * multiple spanned columns
180      */

181     public void setNextFirstSpanningCol(
182             boolean bFirstSpanningCol) {
183         colFirstSpanningCol.add(new Boolean JavaDoc(bFirstSpanningCol));
184     }
185
186     /**
187      * Added by Peter Herweg on 2002-06-29
188      * This function is called after each finished table-row.
189      * It decreases all values in colRowSpanningNumber by 1. If a value
190      * reaches 0 row-spanning is finished, and the value won't be decreased anymore.
191      */

192     public void decreaseRowSpannings() {
193         for (int z = 0; z < colRowSpanningNumber.size(); ++z) {
194             Integer JavaDoc i = (Integer JavaDoc)colRowSpanningNumber.get(z);
195
196             if (i.intValue() > 0) {
197                 i = new Integer JavaDoc(i.intValue() - 1);
198             }
199
200             colRowSpanningNumber.set(z, i);
201
202             if (i.intValue() == 0) {
203                 colRowSpanningAttrs.set(z, null);
204                 colFirstSpanningCol.set(z, new Boolean JavaDoc(false));
205             }
206         }
207     }
208
209     /**
210      * Reset the column iteration index, meant to be called when creating a new row
211      * The 'public' modifier has been added by Boris Poudérous for
212      * 'number-columns-spanned' processing
213      */

214     public void selectFirstColumn() {
215         colIndex = 0;
216     }
217
218     /**
219      * Increment the column iteration index
220      * The 'public' modifier has been added by Boris Poudérous for
221      * 'number-columns-spanned' processing
222      */

223     public void selectNextColumn() {
224         colIndex++;
225     }
226
227     /**
228      * Get current column width according to column iteration index
229      * @return INVALID_COLUMN_WIDTH if we cannot find the value
230      * The 'public' modifier has been added by Boris Poudérous for
231      * 'number-columns-spanned' processing
232      */

233     public float getColumnWidth() {
234         if (colIndex < 0) {
235             throw new IllegalStateException JavaDoc("colIndex must not be negative!");
236         } else if (colIndex >= getNumberOfColumns()) {
237             log.warn("Column width for column " + (colIndex + 1) + " is not defined, using "
238                     + INVALID_COLUMN_WIDTH);
239             while (colIndex >= getNumberOfColumns()) {
240                 setNextColumnWidth(new Float JavaDoc(INVALID_COLUMN_WIDTH));
241             }
242         }
243         return ((Float JavaDoc)colWidths.get(colIndex)).floatValue();
244     }
245
246     /**
247      * Set current column index.
248      * @param index New column index
249      */

250     public void setColumnIndex(int index) {
251         colIndex = index;
252     }
253     
254     /**
255      * @return Index of current column
256      */

257     public int getColumnIndex() {
258         return colIndex;
259     }
260     /** - end - */
261
262     /**
263      * @return Number of columns
264      */

265     public int getNumberOfColumns() {
266         return colWidths.size();
267     }
268     /** - end - */
269 }
270
271
Popular Tags