KickJava   Java API By Example, From Geeks To Geeks.

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


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: GridUnit.java 478928 2006-11-24 17:32:48Z vhennebert $ */
19
20 package org.apache.fop.layoutmgr.table;
21
22 import org.apache.fop.fo.FONode;
23 import org.apache.fop.fo.flow.Table;
24 import org.apache.fop.fo.flow.TableBody;
25 import org.apache.fop.fo.flow.TableCell;
26 import org.apache.fop.fo.flow.TableColumn;
27 import org.apache.fop.fo.flow.TableRow;
28 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
29 import org.apache.fop.fo.properties.CommonBorderPaddingBackground.BorderInfo;
30
31 /**
32  * This class represents one grid unit inside a table.
33  */

34 public class GridUnit {
35
36     /** Indicates that the grid unit is in the first column. */
37     public static final int IN_FIRST_COLUMN = 0;
38     /** Indicates that the grid unit is in the last column. */
39     public static final int IN_LAST_COLUMN = 1;
40     /** Indicates that the grid unit is in the first row of the table. */
41     public static final int FIRST_IN_TABLE = 2;
42     /** Indicates that the grid unit is in the first row of the table part (header, footer, body). */
43     public static final int FIRST_IN_PART = 3;
44     /** Indicates that the grid unit is in the last row of the table part (header, footer, body). */
45     public static final int LAST_IN_PART = 4;
46     /** Indicates that the grid unit is in the last row of the table. */
47     public static final int LAST_IN_TABLE = 5;
48     /** Indicates that the primary grid unit has a pending keep-with-next. */
49     public static final int KEEP_WITH_NEXT_PENDING = 6;
50     /** Indicates that the primary grid unit has a pending keep-with-previous. */
51     public static final int KEEP_WITH_PREVIOUS_PENDING = 7;
52
53     /** Primary grid unit */
54     private PrimaryGridUnit primary;
55     /** Table cell which occupies this grid unit */
56     private TableCell cell;
57     /** Table row which occupies this grid unit (may be null) */
58     private TableRow row;
59     /** Table column that this grid unit belongs to */
60     private TableColumn column;
61
62     /** start index of grid unit within row in column direction */
63     private int startCol;
64     /** index of grid unit within cell in column direction */
65     private int colSpanIndex;
66     /** index of grid unit within cell in row direction */
67     private int rowSpanIndex;
68     /** effective borders for a cell slot */
69     private CommonBorderPaddingBackground effectiveBorders;
70     /** flags for the grid unit */
71     private byte flags = 0;
72
73
74     /**
75      * Creates a new grid unit.
76      *
77      * @param cell table cell which occupies this grid unit
78      * @param column table column this grid unit belongs to
79      * @param startCol index of the column this grid unit belongs to
80      * @param colSpanIndex index of this grid unit in the span, in column direction
81      */

82     public GridUnit(TableCell cell, TableColumn column, int startCol, int colSpanIndex) {
83         this(null, cell, column, startCol, colSpanIndex);
84     }
85
86     /**
87      * Creates a new grid unit.
88      *
89      * @param primary ??
90      * @param column table column this grid unit belongs to
91      * @param startCol index of the column this grid unit belongs to
92      * @param colSpanIndex index of this grid unit in the span, in column direction
93      */

94     public GridUnit(PrimaryGridUnit primary, TableColumn column, int startCol, int colSpanIndex) {
95         this(primary, primary.getCell(), column, startCol, colSpanIndex);
96     }
97
98     /**
99      * Creates a new grid unit.
100      *
101      * @param primary ??
102      * @param cell table cell which occupies this grid unit
103      * @param column table column this grid unit belongs to
104      * @param startCol index of the column this grid unit belongs to
105      * @param colSpanIndex index of this grid unit in the span, in column direction
106      */

107     protected GridUnit(PrimaryGridUnit primary, TableCell cell, TableColumn column, int startCol, int colSpanIndex) {
108         this.primary = primary;
109         this.cell = cell;
110         this.column = column;
111         this.startCol = startCol;
112         this.colSpanIndex = colSpanIndex;
113     }
114
115     public TableCell getCell() {
116         return cell;
117     }
118
119     public TableColumn getColumn() {
120         return column;
121     }
122
123     public TableRow getRow() {
124         if (row != null) {
125             return row;
126         } else if (getCell().getParent() instanceof TableRow) {
127             return (TableRow)getCell().getParent();
128         } else {
129             return null;
130         }
131     }
132
133     /**
134      * Sets the table-row FO, if applicable.
135      * @param row the table-row FO
136      */

137     public void setRow(TableRow row) {
138         this.row = row;
139     }
140
141     public TableBody getBody() {
142         FONode node = getCell();
143         while (node != null && !(node instanceof TableBody)) {
144             node = node.getParent();
145         }
146         return (TableBody)node;
147     }
148
149     public Table getTable() {
150         FONode node = getBody();
151         while (node != null && !(node instanceof Table)) {
152             node = node.getParent();
153         }
154         if (node == null && getColumn() != null) {
155             node = getColumn().getParent();
156         }
157         return (Table)node;
158     }
159
160     /**
161      * @return the primary grid unit if this is a spanned grid unit
162      */

163     public PrimaryGridUnit getPrimary() {
164         return (isPrimary() ? (PrimaryGridUnit)this : primary);
165     }
166
167     public boolean isPrimary() {
168         return false;
169     }
170
171     public boolean isEmpty() {
172         return cell == null;
173     }
174
175     public int getStartCol() {
176         return startCol;
177     }
178
179     /** @return true if the grid unit is the last in column spanning direction */
180     public boolean isLastGridUnitColSpan() {
181         if (cell != null) {
182             return (colSpanIndex == cell.getNumberColumnsSpanned() - 1);
183         } else {
184             return true;
185         }
186     }
187
188     /** @return true if the grid unit is the last in row spanning direction */
189     public boolean isLastGridUnitRowSpan() {
190         if (cell != null) {
191             return (rowSpanIndex == cell.getNumberRowsSpanned() - 1);
192         } else {
193             return true;
194         }
195     }
196
197     /**
198      * @return the index of the grid unit inside a cell in row direction
199      */

200     public int getRowSpanIndex() {
201         return rowSpanIndex;
202     }
203
204     /**
205      * @return the index of the grid unit inside a cell in column direction
206      */

207     public int getColSpanIndex() {
208         return colSpanIndex;
209     }
210
211     /**
212      * Returns a BorderInfo instance for a side of the currently applicable cell before border
213      * resolution (i.e. the value from the FO). A return value of null indicates an empty cell.
214      * See CollapsingBorderModel(EyeCatching) where this method is used.
215      * @param side for which side to return the BorderInfo
216      * @return the requested BorderInfo instance or null if the grid unit is an empty cell
217      */

218     public BorderInfo getOriginalBorderInfoForCell(int side) {
219         if (cell != null) {
220             return cell.getCommonBorderPaddingBackground().getBorderInfo(side);
221         } else {
222             return null;
223         }
224     }
225
226     /**
227      * @return the resolved normal borders for this grid unit
228      */

229     public CommonBorderPaddingBackground getBorders() {
230         return effectiveBorders;
231     }
232
233     /**
234      * @return true if the grid unit has any borders.
235      */

236     public boolean hasBorders() {
237         return (getBorders() != null) && getBorders().hasBorder();
238     }
239
240     /**
241      * Assigns the borders from the given cell to this cell info. Used in
242      * case of separate border model.
243      */

244     public void assignBorderForSeparateBorderModel() {
245         if (cell != null) {
246             effectiveBorders = cell.getCommonBorderPaddingBackground();
247         }
248     }
249
250     /**
251      * Resolve collapsing borders for the given cell. Used in case of the collapsing border model.
252      * @param other neighbouring grid unit if any
253      * @param side the side to resolve (one of CommonBorderPaddingBackground.BEFORE|AFTER|START|END)
254      */

255     public void resolveBorder(GridUnit other, int side) {
256         resolveBorder(other, side, 0);
257     }
258
259     /**
260      * Resolve collapsing borders for the given cell. Used in case of the collapsing border model.
261      * @param other neighbouring grid unit if any
262      * @param side the side to resolve (one of CommonBorderPaddingBackground.BEFORE|AFTER|START|END)
263      * @param resFlags flags for the border resolution
264      */

265     public void resolveBorder(GridUnit other, int side, int resFlags) {
266         CollapsingBorderModel borderModel = CollapsingBorderModel.getBorderModelFor(
267                 getTable().getBorderCollapse());
268         if (effectiveBorders == null) {
269             effectiveBorders = new CommonBorderPaddingBackground();
270         }
271         effectiveBorders.setBorderInfo(borderModel.determineWinner(this, other,
272                         side, resFlags), side);
273         if (cell != null) {
274             effectiveBorders.setPadding(cell.getCommonBorderPaddingBackground());
275         }
276     }
277
278     /**
279      * Returns a flag for this GridUnit.
280      * @param which the requested flag
281      * @return the value of the flag
282      */

283     public boolean getFlag(int which) {
284         return (flags & (1 << which)) != 0;
285     }
286
287     /**
288      * Sets a flag on a GridUnit.
289      * @param which the flag to set
290      * @param value the new value for the flag
291      */

292     public void setFlag(int which, boolean value) {
293         if (value) {
294             flags |= (1 << which); //set flag
295
} else {
296             flags &= ~(1 << which); //clear flag
297
}
298     }
299
300     /**
301      * @return the grid unit just below this grid unit if the cell is spanning.
302      */

303     public GridUnit createNextRowSpanningGridUnit() {
304         if (isLastGridUnitRowSpan()) {
305             return null;
306         } else {
307             //cloning the current GridUnit with adjustments
308
GridUnit gu = new GridUnit(getPrimary(), getColumn(), startCol, colSpanIndex);
309             gu.rowSpanIndex = rowSpanIndex + 1;
310             return gu;
311         }
312     }
313
314     /** @see java.lang.Object#toString() */
315     public String JavaDoc toString() {
316         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
317         if (isEmpty()) {
318             buffer.append("EMPTY");
319         } else if (isPrimary()) {
320             buffer.append("Primary");
321         }
322         buffer.append("GridUnit:");
323         if (colSpanIndex > 0) {
324             buffer.append(" colSpan=").append(colSpanIndex);
325         }
326         if (rowSpanIndex > 0) {
327             buffer.append(" rowSpan=").append(rowSpanIndex);
328         }
329         buffer.append(" startCol=").append(startCol);
330         buffer.append(" flags=").append(Integer.toBinaryString(flags));
331         return buffer.toString();
332     }
333
334 }
335
Popular Tags