Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 59d4ca542f26cb1c186903b030cde4179a5fb882 (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
228
229
/*******************************************************************************
 * Copyright (c) 2014 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.Collection;
import java.util.LinkedList;
import java.util.ListIterator;

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

/**
 * @author Florian Thienel
 */
public class Paragraph extends BaseBox implements IStructuralBox, IParentBox<IInlineBox> {

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

	private TextAlign textAlign = TextAlign.LEFT;

	private final LinkedList<IInlineBox> children = new LinkedList<IInlineBox>();
	private final LineArrangement lines = new LineArrangement();
	private IVisualDecorator<IStructuralBox> visualDecorator;

	@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;
	}

	@Override
	public int getTop() {
		return top;
	}

	@Override
	public int getLeft() {
		return left;
	}

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

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

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

	@Override
	public int getHeight() {
		return lines.getHeight();
	}

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

	public TextAlign getTextAlign() {
		return textAlign;
	}

	public void setTextAlign(final TextAlign textAlign) {
		this.textAlign = textAlign;
	}

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

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

	public boolean hasChildren() {
		return !children.isEmpty();
	}

	public void prependChild(final IInlineBox child) {
		if (child == null) {
			return;
		}
		if (!joinWithFirstChild(child)) {
			child.setParent(this);
			children.addFirst(child);
		}
	}

	private boolean joinWithFirstChild(final IInlineBox box) {
		if (!hasChildren()) {
			return false;
		}
		final IInlineBox firstChild = children.getFirst();
		final boolean joined = firstChild.join(box);
		if (joined) {
			children.removeFirst();
			children.addFirst(box);
		}
		return joined;
	}

	public void appendChild(final IInlineBox child) {
		if (child == null) {
			return;
		}
		if (!joinWithLastChild(child)) {
			child.setParent(this);
			children.add(child);
		}
	}

	private boolean joinWithLastChild(final IInlineBox box) {
		if (!hasChildren()) {
			return false;
		}
		final IInlineBox lastChild = children.getLast();
		final boolean joined = lastChild.join(box);
		return joined;
	}

	@Override
	public void replaceChildren(final Collection<? extends IBox> oldChildren, final IInlineBox newChild) {
		boolean newChildInserted = false;

		for (final ListIterator<IInlineBox> iter = children.listIterator(); iter.hasNext();) {
			final IInlineBox child = iter.next();
			if (oldChildren.contains(child)) {
				iter.remove();
				if (!newChildInserted) {
					iter.add(newChild);
					newChild.setParent(this);
					newChildInserted = true;
				}
			}
		}
	}

	public Iterable<IInlineBox> getChildren() {
		return children;
	}

	@Override
	public void layout(final Graphics graphics) {
		arrangeChildrenOnLines(graphics);
	}

	private void arrangeChildrenOnLines(final Graphics graphics) {
		lines.arrangeBoxes(graphics, children.listIterator(), width, textAlign);
	}

	@Override
	public boolean reconcileLayout(final Graphics graphics) {
		final int oldHeight = lines.getHeight();
		arrangeChildrenOnLines(graphics);
		return oldHeight != lines.getHeight();
	}

	@Override
	public void paint(final Graphics graphics) {
		for (final Line line : lines.getLines()) {
			/*
			 * Line takes care of moving the origin for each child box. The coordinates of the child boxes are relative
			 * to the Paragraph, not relative to the Line, because Paragraph is the children's parent. The Line is a
			 * transparent utility with regards to the box structure, which is used internally by Paragraph.
			 */
			line.paint(graphics);
		}
	}

	@Override
	public void setVisualDecorator(final IVisualDecorator<IStructuralBox> visualDecorator) {
		this.visualDecorator = visualDecorator;

	}

	@Override
	public void resetVisualDecorator() {
		visualDecorator = null;
	}

	@Override
	public void applyVisualDecorator() {
		if (visualDecorator != null) {
			visualDecorator.decorate(this);
		}
	}
}

Back to the top