KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > flow > RowSpanMgr


1 /*
2  * $Id: RowSpanMgr.java,v 1.2.2.4 2003/02/25 12:57:01 jeremias Exp $
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 package org.apache.fop.fo.flow;
52
53 public class RowSpanMgr {
54     class SpanInfo {
55         int cellHeight;
56         int totalRowHeight;
57         int rowsRemaining;
58         // int numCols; // both V and H span
59
TableCell cell;
60
61         SpanInfo(TableCell cell, int cellHeight, int rowsSpanned) {
62             this.cell = cell;
63             this.cellHeight = cellHeight;
64             this.totalRowHeight = 0;
65             this.rowsRemaining = rowsSpanned;
66         }
67
68         /**
69          * Return the height remaining in the span.
70          */

71         int heightRemaining() {
72             int hl = cellHeight - totalRowHeight;
73             return (hl > 0) ? hl : 0;
74         }
75
76         boolean isInLastRow() {
77             return (rowsRemaining == 1);
78         }
79
80         boolean finishRow(int rowHeight) {
81             totalRowHeight += rowHeight;
82             if (--rowsRemaining == 0) {
83                 if (cell != null) {
84                     cell.setRowHeight(totalRowHeight);
85                 }
86                 return true;
87             } else
88                 return false;
89         }
90
91     }
92
93     private SpanInfo spanInfo[];
94
95     private boolean ignoreKeeps = false;
96
97     public RowSpanMgr(int numCols) {
98         this.spanInfo = new SpanInfo[numCols];
99     }
100
101     public void addRowSpan(TableCell cell, int firstCol, int numCols,
102                            int cellHeight, int rowsSpanned) {
103         spanInfo[firstCol - 1] = new SpanInfo(cell, cellHeight, rowsSpanned);
104         for (int i = 0; i < numCols - 1; i++) {
105             spanInfo[firstCol + i] = new SpanInfo(null, cellHeight,
106                                                   rowsSpanned); // copy!
107
}
108     }
109
110     public boolean isSpanned(int colNum) {
111         return (spanInfo[colNum - 1] != null);
112     }
113
114
115     public TableCell getSpanningCell(int colNum) {
116         if (spanInfo[colNum - 1] != null) {
117             return spanInfo[colNum - 1].cell;
118         } else
119             return null;
120     }
121
122
123     /**
124      * Return true if any column has an unfinished vertical span.
125      */

126     public boolean hasUnfinishedSpans() {
127         for (int i = 0; i < spanInfo.length; i++) {
128             if (spanInfo[i] != null)
129                 return true;
130         }
131         return false;
132     }
133
134     /**
135      * Done with a row.
136      * Any spans with only one row left are done
137      * This means that we can now set the total height for this cell box
138      * Loop over all cells with spans and find number of rows remaining
139      * if rows remaining = 1, set the height on the cell area and
140      * then remove the cell from the list of spanned cells. For other
141      * spans, add the rowHeight to the spanHeight.
142      */

143     public void finishRow(int rowHeight) {
144         for (int i = 0; i < spanInfo.length; i++) {
145             if (spanInfo[i] != null && spanInfo[i].finishRow(rowHeight))
146                 spanInfo[i] = null;
147         }
148     }
149
150     /**
151      * If the cell in this column is in the last row of its vertical
152      * span, return the height left. If it's not in the last row, or if
153      * the content height <= the content height of the previous rows
154      * of the span, return 0.
155      */

156     public int getRemainingHeight(int colNum) {
157         if (spanInfo[colNum - 1] != null) {
158             return spanInfo[colNum - 1].heightRemaining();
159         } else
160             return 0;
161     }
162
163     public boolean isInLastRow(int colNum) {
164         if (spanInfo[colNum - 1] != null) {
165             return spanInfo[colNum - 1].isInLastRow();
166         } else
167             return false;
168     }
169
170     /**
171      * helper method to prevent infinite loops if
172      * keeps or spans are not fitting on a page
173      * @param <code>true</code> if keeps and spans should be ignored
174      */

175     public void setIgnoreKeeps(boolean ignoreKeeps) {
176         this.ignoreKeeps = ignoreKeeps;
177     }
178
179     /**
180      * helper method (i.e. hack ;-) to prevent infinite loops if
181      * keeps or spans are not fitting on a page
182      * @return true if keeps or spans should be ignored
183      */

184     public boolean ignoreKeeps() {
185         return ignoreKeeps;
186     }
187
188 }
189
Popular Tags