Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 938532f8a52062136b52fd61ec050736237c06cb (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
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
 * Copyright (c) 2016 Florian Thienel 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:
 * 		Florian Thienel - initial API and implementation
 *******************************************************************************/
package org.eclipse.vex.core.internal.boxes;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.vex.core.internal.core.Graphics;
import org.eclipse.vex.core.internal.core.Rectangle;

/**
 * @author Florian Thienel
 */
public class TableCell extends BaseBox implements IStructuralBox, IDecoratorBox<IHeightAdjustableBox> {

	private IBox parent;
	private int top;
	private int left;
	private int width;

	private IHeightAdjustableBox component;

	private String columnName;
	private String startColumnName;
	private String endColumnName;
	private int verticalSpan = 1;

	private GridArea gridArea;
	private TableLayoutGrid layoutGrid;

	private int naturalHeight;
	private int usedHeight;

	@Override
	public void setParent(final IBox parent) {
		this.parent = parent;
	}

	@Override
	public IBox getParent() {
		return parent;
	}

	@Override
	public int getAbsoluteTop() {
		if (parent == null) {
			return top;
		}
		return parent.getAbsoluteTop() + top;
	}

	@Override
	public int getAbsoluteLeft() {
		if (parent == null) {
			return left;
		}
		return parent.getAbsoluteLeft() + left;
	}

	public int getTop() {
		return top;
	}

	public int getLeft() {
		return left;
	}

	public void setPosition(final int top, final int left) {
		this.top = top;
		this.left = left;
	}

	@Override
	public int getWidth() {
		return width;
	}

	public void setWidth(final int width) {
		this.width = Math.max(0, width);
	}

	@Override
	public int getHeight() {
		return usedHeight;
	}

	public void useHeight(final int height) {
		usedHeight += height;
	}

	public int getNaturalHeight() {
		return naturalHeight;
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle(left, top, width, usedHeight);
	}

	@Override
	public void accept(final IBoxVisitor visitor) {
		visitor.visit(this);
	}

	@Override
	public <T> T accept(final IBoxVisitorWithResult<T> visitor) {
		return visitor.visit(this);
	}

	public void setComponent(final IHeightAdjustableBox component) {
		this.component = component;
		component.setParent(this);
	}

	@Override
	public IHeightAdjustableBox getComponent() {
		return component;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(final String columnName) {
		this.columnName = columnName;
	}

	public String getStartColumnName() {
		return startColumnName;
	}

	public void setStartColumnName(final String startColumnName) {
		this.startColumnName = startColumnName;
	}

	public String getEndColumnName() {
		return endColumnName;
	}

	public void setEndColumnName(final String endColumnName) {
		this.endColumnName = endColumnName;
	}

	public int getVerticalSpan() {
		return verticalSpan;
	}

	public void setVerticalSpan(final int verticalSpan) {
		this.verticalSpan = verticalSpan;
	}

	public GridArea getGridArea() {
		return gridArea;
	}

	public void setGridArea(final GridArea gridArea) {
		this.gridArea = gridArea;
	}

	public TableLayoutGrid getLayoutGrid() {
		return layoutGrid;
	}

	public void setLayoutGrid(final TableLayoutGrid layoutGrid) {
		this.layoutGrid = layoutGrid;
	}

	public int calculateNaturalHeight(final Graphics graphics, final int width) {
		naturalHeight = 0;
		if (component == null) {
			return 0;
		}

		component.setWidth(width);
		component.layout(graphics);
		naturalHeight = component.getHeight();
		usedHeight = 0;

		return naturalHeight;
	}

	public void layout(final Graphics graphics) {
		if (naturalHeight == 0) {
			naturalHeight = calculateNaturalHeight(graphics, width);
		}

		component.setPosition(0, 0);
		adjustComponentHeight();
	}

	private void adjustComponentHeight() {
		component.setHeight(usedHeight);
	}

	@Override
	public Collection<IBox> reconcileLayout(final Graphics graphics) {
		final int oldHeight = naturalHeight;
		naturalHeight = calculateNaturalHeight(graphics, width);

		if (oldHeight == naturalHeight) {
			return NOTHING_INVALIDATED;
		}

		component.setPosition(0, 0);
		adjustComponentHeight();

		final ArrayList<IBox> invalidatedRows = new ArrayList<IBox>();
		for (int row = gridArea.startRow; row <= gridArea.endRow; row += 1) {
			invalidatedRows.add(layoutGrid.getRow(row));
		}

		return invalidatedRows;
	}

	@Override
	public void paint(final Graphics graphics) {
		ChildBoxPainter.paint(component, graphics);
	}
}

Back to the top