KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > table > PrimaryGridUnit


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: PrimaryGridUnit.java 478928 2006-11-24 17:32:48Z vhennebert $ */
19
20 package org.apache.fop.layoutmgr.table;
21
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.fop.fo.flow.TableCell;
26 import org.apache.fop.fo.flow.TableColumn;
27
28 /**
29  * This class represents a primary grid unit of a spanned cell.
30  */

31 public class PrimaryGridUnit extends GridUnit {
32
33     /** Cell layout manager. */
34     private TableCellLayoutManager cellLM;
35     /** List of Knuth elements representing the contents of the cell. */
36     private LinkedList JavaDoc elements;
37     /** Index of row where this cell starts */
38     private int startRow;
39     /** Links to the spanned grid units. (List of GridUnit arrays, one array represents a row) */
40     private List JavaDoc rows;
41     /** The calculated size of the cell's content. (cached value) */
42     private int contentLength = -1;
43
44     /**
45      * Creates a new primary grid unit.
46      *
47      * @param cell table cell which occupies this grid unit
48      * @param column table column this grid unit belongs to
49      * @param startCol index of the column this grid unit belongs to, zero-based
50      * @param startRow index of the row this grid unit belongs to, zero-based
51      */

52     public PrimaryGridUnit(TableCell cell, TableColumn column, int startCol, int startRow) {
53         super(cell, column, startCol, 0);
54         this.startRow = startRow;
55         if (cell != null) {
56             cellLM = new TableCellLayoutManager(cell, this);
57         }
58     }
59
60     public TableCellLayoutManager getCellLM() {
61         return cellLM;
62     }
63
64     public boolean isPrimary() {
65         return true;
66     }
67
68     /**
69      * Sets the Knuth elements for the table cell containing this grid unit.
70      *
71      * @param elements a list of ListElement (?)
72      */

73     public void setElements(LinkedList JavaDoc elements) {
74         this.elements = elements;
75     }
76
77     public LinkedList JavaDoc getElements() {
78         return this.elements;
79     }
80
81     /**
82      * @return half the maximum before border width of this cell.
83      */

84     public int getHalfMaxBeforeBorderWidth() {
85         int value = 0;
86         if (getRows() != null) {
87             int before = 0;
88             //first row for before borders
89
GridUnit[] row = (GridUnit[])getRows().get(0);
90             for (int i = 0; i < row.length; i++) {
91                 if (row[i].hasBorders()) {
92                     before = Math.max(before,
93                             row[i].getBorders().getBorderBeforeWidth(false));
94                 }
95             }
96             value += before / 2;
97         } else {
98             if (hasBorders()) {
99                 value += getBorders().getBorderBeforeWidth(false) / 2;
100             }
101         }
102         return value;
103     }
104
105     /**
106      * @return half the maximum after border width of this cell.
107      */

108     public int getHalfMaxAfterBorderWidth() {
109         int value = 0;
110         if (getRows() != null) {
111             //Last row for after borders
112
int after = 0;
113             GridUnit[] row = (GridUnit[])getRows().get(getRows().size() - 1);
114             for (int i = 0; i < row.length; i++) {
115                 if (row[i].hasBorders()) {
116                     after = Math.max(after, row[i].getBorders().getBorderAfterWidth(false));
117                 }
118             }
119             value += after / 2;
120         } else {
121             if (hasBorders()) {
122                 value += getBorders().getBorderAfterWidth(false) / 2;
123             }
124         }
125         return value;
126     }
127
128     /**
129      * @return the sum of half the maximum before and after border
130      * widths of this cell.
131      */

132     public int getHalfMaxBorderWidth() {
133         return getHalfMaxBeforeBorderWidth() + getHalfMaxAfterBorderWidth();
134     }
135
136     /** @param value The length of the cell content to remember. */
137     public void setContentLength(int value) {
138         this.contentLength = value;
139     }
140
141     /** @return the length of the cell content. */
142     public int getContentLength() {
143         return contentLength;
144     }
145
146     /** @return true if cell/row has an explicit BPD/height */
147     public boolean hasBPD() {
148         if (!getCell().getBlockProgressionDimension().getOptimum(null).isAuto()) {
149             return true;
150         }
151         if (getRow() != null
152                 && !getRow().getBlockProgressionDimension().getOptimum(null).isAuto()) {
153             return true;
154         }
155         return false;
156     }
157
158     /**
159      * Returns the grid units belonging to the same span as this one.
160      *
161      * @return a list of GridUnit[], each array corresponds to a row
162      */

163     public List JavaDoc getRows() {
164         return this.rows;
165     }
166
167     public void addRow(GridUnit[] row) {
168         if (rows == null) {
169             rows = new java.util.ArrayList JavaDoc();
170         }
171         rows.add(row);
172     }
173
174     /**
175      * Returns the index of the row this grid unit belongs to.
176      *
177      * @return the index of the row this grid unit belongs to.
178      */

179     public int getStartRow() {
180         return this.startRow;
181     }
182
183     /**
184      * Returns the widths of the start- and end-borders of the span this grid unit belongs
185      * to.
186      *
187      * @return a two-element array containing the widths of the start-border then the
188      * end-border
189      */

190     public int[] getStartEndBorderWidths() {
191         int[] widths = new int[2];
192         if (rows == null) {
193             widths[0] = getBorders().getBorderStartWidth(false);
194             widths[1] = getBorders().getBorderEndWidth(false);
195         } else {
196             for (int i = 0; i < rows.size(); i++) {
197                 GridUnit[] gridUnits = (GridUnit[])rows.get(i);
198                 widths[0] = Math.max(widths[0],
199                         (gridUnits[0]).
200                             getBorders().getBorderStartWidth(false));
201                 widths[1] = Math.max(widths[1],
202                         (gridUnits[gridUnits.length - 1]).
203                             getBorders().getBorderEndWidth(false));
204             }
205         }
206         return widths;
207     }
208
209     /** @see java.lang.Object#toString() */
210     public String JavaDoc toString() {
211         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
212         sb.append(" startRow=").append(startRow);
213         return sb.toString();
214     }
215
216     /** @return true if this cell spans over more than one grid unit. */
217     public boolean hasSpanning() {
218         return (getCell().getNumberColumnsSpanned() > 1)
219             || (getCell().getNumberRowsSpanned() > 1);
220     }
221
222 }
223
Popular Tags