Skip to main content
summaryrefslogtreecommitdiffstats
blob: cc41b1f92f0158438804a097c996da2e404a3934 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.css2.layout.table;

import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.css2.layout.ICSSFigure;
import org.eclipse.jst.pagedesigner.css2.property.ICSSPropertyID;
import org.eclipse.jst.pagedesigner.css2.value.Length;
import org.eclipse.jst.pagedesigner.utils.IntFlexArray;

/**
 * @author mengbo
 * @version 1.5
 */
public class TableCellInfo extends TableItemInfo {
	int _rowSpan = 1;

	int _colSpan = 1;

	int _colIndex;

	int _rowIndex;

	int _cellWidth = 0;

	int _cellHeight = 0;

	/**
	 * @param childfigure
	 */
	public TableCellInfo(ICSSFigure childfigure) {
		super(childfigure);
	}

	/**
	 * @return
	 */
	public int getColSpan() {
		return _colSpan;
	}

	/**
	 * @return
	 */
	public int getRowSpan() {
		return _rowSpan;
	}

	/**
	 * @return
	 */
	public int getRowIndex() {
		return _rowIndex;
	}

	/**
	 * @return
	 */
	public int getColumnIndex() {
		return _colIndex;
	}

	/**
	 * @return
	 */
	public Dimension getMinCWDimension() {
		return getFigure().getPreferredSize(_cellWidth, _cellHeight);
	}

	/**
	 * @return
	 */
	public Dimension getMaxCWDimension() {
		ICSSFigure figure = getFigure();
		LayoutManager layout = figure.getLayoutManager();
		if (layout instanceof CSSTableCellLayout) {
			Dimension d = ((CSSTableCellLayout) layout).getMaxContentWidthSize(
					figure, _cellWidth, _cellHeight);
			return d;
		} else {
			// should not happen
			return getMinCWDimension();
		}
	}

	/**
	 * @param context
	 */
	public void calculateCellInfo(TableInfoContext context) {
		ICSSStyle style = this.getStyle();
		_rowSpan = style.getRowSpan();
		_colSpan = style.getColSpan();

		// FIXME: we don't support rowspan and colspan to be 0.
		// by spec, 0 means span from current col/row to end.
		if (_rowSpan <= 0) {
			_rowSpan = 1;
		}
		if (_colSpan <= 0) {
			_colSpan = 1;
		}

		_rowIndex = context.getCurrentRow();

		IntFlexArray array = context.getIntFlexArray();
		int currentCol = context.getCurrentCol();

		// find a cell that is not occupied by cells in previous rows.
		while (array.getAt(currentCol) > 0) {
			currentCol++;
		}

		// ok, now array.getAt(currentCol) == 0
		_colIndex = currentCol;

		for (int i = 0; i < _colSpan; i++, currentCol++) {
			array.setAt(currentCol, _rowSpan);
		}
		context.setCurrentCol(currentCol);
	}

	/**
	 * @param tablewidth
	 *            table width
	 */
	public void calculateWidth(TableInfo tableInfo, int tablewidth) {
		ICSSStyle style = this.getFigure().getCSSStyle();
		if (style == null) {
			_cellWidth = -1;
		} else {
			Object width = style.getStyleProperty(ICSSPropertyID.ATTR_WIDTH);
			Length recommendedWidth = (width instanceof Length) ? (Length) width
					: null;

			int rw = 0;
			if (recommendedWidth == null || recommendedWidth.getValue() <= 0) {
				rw = 0;
			} else {
				if (recommendedWidth.isPercentage()) {
					// percentage width is used for remaining width
					// distribution, so not used here.
					int colspan = this.getColSpan();
					for (int i = 0; i < colspan; i++) {
						tableInfo.setWidthPercentage(this.getColumnIndex() + i,
								recommendedWidth.getValue() / colspan);
					}
				} else {
					rw = recommendedWidth.getValue();
					if (!style.isSizeIncludeBorderPadding()) {
						rw += style.getBorderInsets().getWidth()
								+ style.getPaddingInsets().getWidth();
					}
					if (this.getColSpan() == 1) {
						tableInfo._widthSpecified[this.getColumnIndex()] = true;
					}
				}

			}
			_cellWidth = rw;
		}

	}

	/**
	 * @param height
	 */
	public void calculateHeight(TableInfo tableInfo, int tableheight) {
		ICSSStyle style = this.getFigure().getCSSStyle();
		if (style == null) {
			_cellHeight = -1;
		} else {
			Object height = style.getStyleProperty(ICSSPropertyID.ATTR_HEIGHT);
			Length recommendedHeight = (height instanceof Length) ? (Length) height
					: null;

			int rh = 0;
			if (recommendedHeight == null || recommendedHeight.getValue() <= 0) {
				rh = 0;
			} else {
				if (recommendedHeight.isPercentage()) {
					int rowspan = this.getRowSpan();
					for (int i = 0; i < rowspan; i++) {
						tableInfo.setHeightPercentage(this.getRowIndex() + i,
								recommendedHeight.getValue() / rowspan);
					}
				} else {
					rh = recommendedHeight.getValue();
				}
				if (!style.isSizeIncludeBorderPadding()) {
					rh += style.getBorderInsets().getHeight()
							+ style.getPaddingInsets().getHeight();
				}
			}
			_cellHeight = rh;
		}

	}

	/**
	 * @return
	 */
	public int getHeight() {
		return _cellHeight;
	}
}

Back to the top