001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PrimaryGridUnit.java 519975 2007-03-19 15:40:31Z vhennebert $ */
019:
020: package org.apache.fop.layoutmgr.table;
021:
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.fop.fo.flow.TableCell;
028: import org.apache.fop.fo.flow.TableColumn;
029:
030: /**
031: * This class represents a primary grid unit of a spanned cell. This is the "before-start"
032: * (top-left, usually) grid unit of the span.
033: */
034: public class PrimaryGridUnit extends GridUnit {
035:
036: private static Log log = LogFactory.getLog(PrimaryGridUnit.class);
037:
038: /** Cell layout manager. */
039: private TableCellLayoutManager cellLM;
040: /** List of Knuth elements representing the contents of the cell. */
041: private LinkedList elements;
042: /** Index of row where this cell starts */
043: private int startRow;
044: /** Links to the spanned grid units. (List of GridUnit arrays, one array represents a row) */
045: private List rows;
046: /** The calculated size of the cell's content. (cached value) */
047: private int contentLength = -1;
048:
049: /**
050: * Creates a new primary grid unit.
051: *
052: * @param cell table cell which occupies this grid unit
053: * @param column table column this grid unit belongs to
054: * @param startCol index of the column this grid unit belongs to, zero-based
055: * @param startRow index of the row this grid unit belongs to, zero-based
056: */
057: public PrimaryGridUnit(TableCell cell, TableColumn column,
058: int startCol, int startRow) {
059: super (cell, column, startCol, 0);
060: this .startRow = startRow;
061: log.trace("PrimaryGridUnit created, row " + startRow + " col "
062: + startCol);
063: if (cell != null) {
064: cellLM = new TableCellLayoutManager(cell, this );
065: }
066: }
067:
068: public TableCellLayoutManager getCellLM() {
069: return cellLM;
070: }
071:
072: public boolean isPrimary() {
073: return true;
074: }
075:
076: /**
077: * Sets the Knuth elements for the table cell containing this grid unit.
078: *
079: * @param elements a list of ListElement (?)
080: */
081: public void setElements(LinkedList elements) {
082: this .elements = elements;
083: }
084:
085: public LinkedList getElements() {
086: return this .elements;
087: }
088:
089: /**
090: * @return half the maximum before border width of this cell.
091: */
092: public int getHalfMaxBeforeBorderWidth() {
093: int value = 0;
094: if (getRows() != null) {
095: int before = 0;
096: //first row for before borders
097: GridUnit[] row = (GridUnit[]) getRows().get(0);
098: for (int i = 0; i < row.length; i++) {
099: if (row[i].hasBorders()) {
100: before = Math.max(before, row[i].getBorders()
101: .getBorderBeforeWidth(false));
102: }
103: }
104: value += before / 2;
105: } else {
106: if (hasBorders()) {
107: value += getBorders().getBorderBeforeWidth(false) / 2;
108: }
109: }
110: return value;
111: }
112:
113: /**
114: * @return half the maximum after border width of this cell.
115: */
116: public int getHalfMaxAfterBorderWidth() {
117: int value = 0;
118: if (getRows() != null) {
119: //Last row for after borders
120: int after = 0;
121: GridUnit[] row = (GridUnit[]) getRows().get(
122: getRows().size() - 1);
123: for (int i = 0; i < row.length; i++) {
124: if (row[i].hasBorders()) {
125: after = Math.max(after, row[i].getBorders()
126: .getBorderAfterWidth(false));
127: }
128: }
129: value += after / 2;
130: } else {
131: if (hasBorders()) {
132: value += getBorders().getBorderAfterWidth(false) / 2;
133: }
134: }
135: return value;
136: }
137:
138: /**
139: * @return the sum of half the maximum before and after border
140: * widths of this cell.
141: */
142: public int getHalfMaxBorderWidth() {
143: return getHalfMaxBeforeBorderWidth()
144: + getHalfMaxAfterBorderWidth();
145: }
146:
147: /** @param value The length of the cell content to remember. */
148: public void setContentLength(int value) {
149: this .contentLength = value;
150: }
151:
152: /** @return the length of the cell content. */
153: public int getContentLength() {
154: return contentLength;
155: }
156:
157: /** @return true if cell/row has an explicit BPD/height */
158: public boolean hasBPD() {
159: if (!getCell().getBlockProgressionDimension().getOptimum(null)
160: .isAuto()) {
161: return true;
162: }
163: if (getRow() != null
164: && !getRow().getBlockProgressionDimension().getOptimum(
165: null).isAuto()) {
166: return true;
167: }
168: return false;
169: }
170:
171: /**
172: * Returns the grid units belonging to the same span as this one.
173: *
174: * @return a list of GridUnit[], each array corresponds to a row
175: */
176: public List getRows() {
177: return this .rows;
178: }
179:
180: public void addRow(GridUnit[] row) {
181: if (rows == null) {
182: rows = new java.util.ArrayList();
183: }
184: rows.add(row);
185: }
186:
187: /**
188: * Returns the index of the row this grid unit belongs to.
189: *
190: * @return the index of the row this grid unit belongs to.
191: */
192: public int getStartRow() {
193: return this .startRow;
194: }
195:
196: /**
197: * Returns the widths of the start- and end-borders of the span this grid unit belongs
198: * to.
199: *
200: * @return a two-element array containing the widths of the start-border then the
201: * end-border
202: */
203: public int[] getStartEndBorderWidths() {
204: int[] widths = new int[2];
205: if (rows == null) {
206: widths[0] = getBorders().getBorderStartWidth(false);
207: widths[1] = getBorders().getBorderEndWidth(false);
208: } else {
209: for (int i = 0; i < rows.size(); i++) {
210: GridUnit[] gridUnits = (GridUnit[]) rows.get(i);
211: widths[0] = Math.max(widths[0], (gridUnits[0])
212: .getBorders().getBorderStartWidth(false));
213: widths[1] = Math.max(widths[1],
214: (gridUnits[gridUnits.length - 1]).getBorders()
215: .getBorderEndWidth(false));
216: }
217: }
218: return widths;
219: }
220:
221: /** @see java.lang.Object#toString() */
222: public String toString() {
223: StringBuffer sb = new StringBuffer(super .toString());
224: sb.append(" startRow=").append(startRow);
225: return sb.toString();
226: }
227:
228: /** @return true if this cell spans over more than one grid unit. */
229: public boolean hasSpanning() {
230: return (getCell().getNumberColumnsSpanned() > 1)
231: || (getCell().getNumberRowsSpanned() > 1);
232: }
233:
234: }
|