1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d.text;
|
12
|
|
13
|
import java.util.List;
|
14
|
|
15
|
/**
|
16
|
* The layout manager for {@link InlineFlow} figures.
|
17
|
*
|
18
|
* <P>
|
19
|
* WARNING: This class is not intended to be subclassed by clients.
|
20
|
*
|
21
|
* @author hudsonr
|
22
|
* @since 2.1
|
23
|
*/
|
24
|
public class InlineFlowLayout extends FlowContainerLayout {
|
25
|
|
26
|
/**
|
27
|
* Creates a new InlineFlowLayout with the given FlowFigure.
|
28
|
*
|
29
|
* @param flow
|
30
|
* The FlowFigure
|
31
|
*/
|
32
|
public InlineFlowLayout(FlowFigure flow) {
|
33
|
super(flow);
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* Adds the given box as a line below the current line.
|
38
|
*
|
39
|
* @param box
|
40
|
* the box to add
|
41
|
*/
|
42
|
public void addLine(CompositeBox box) {
|
43
|
endLine();
|
44
|
getContext().addLine(box);
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* @see FlowContainerLayout#createNewLine()
|
49
|
*/
|
50
|
protected void createNewLine() {
|
51
|
currentLine = new NestedLine((InlineFlow) getFlowFigure());
|
52
|
setupLine(currentLine);
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* @see FlowContext#endLine()
|
57
|
*/
|
58
|
public void endLine() {
|
59
|
flush();
|
60
|
getContext().endLine();
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* @see FlowContainerLayout#flush()
|
65
|
*/
|
66
|
protected void flush() {
|
67
|
if (currentLine != null && currentLine.isOccupied()) {
|
68
|
// We want to preserve the state when a linebox is being added
|
69
|
boolean sameLine = getContext().getContinueOnSameLine();
|
70
|
getContext().addToCurrentLine(currentLine);
|
71
|
((InlineFlow) getFlowFigure()).getFragments().add(currentLine);
|
72
|
currentLine = null;
|
73
|
getContext().setContinueOnSameLine(sameLine);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* InlineFlowLayout gets this information from its context.
|
79
|
*
|
80
|
* @see FlowContext#getContinueOnSameLine()
|
81
|
*/
|
82
|
public boolean getContinueOnSameLine() {
|
83
|
return getContext().getContinueOnSameLine();
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* @see FlowContext#getWidthLookahead(FlowFigure, int[])
|
88
|
*/
|
89
|
public void getWidthLookahead(FlowFigure child, int result[]) {
|
90
|
List children = getFlowFigure().getChildren();
|
91
|
int index = -1;
|
92
|
if (child != null)
|
93
|
index = children.indexOf(child);
|
94
|
|
95
|
for (int i = index + 1; i < children.size(); i++)
|
96
|
if (((FlowFigure) children.get(i))
|
97
|
.addLeadingWordRequirements(result))
|
98
|
return;
|
99
|
|
100
|
getContext().getWidthLookahead(getFlowFigure(), result);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* @see FlowContainerLayout#isCurrentLineOccupied()
|
105
|
*/
|
106
|
public boolean isCurrentLineOccupied() {
|
107
|
return (currentLine != null && !currentLine.getFragments().isEmpty())
|
108
|
|| getContext().isCurrentLineOccupied();
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Clears out all fragments prior to the call to layoutChildren().
|
113
|
*/
|
114
|
public void preLayout() {
|
115
|
((InlineFlow) getFlowFigure()).getFragments().clear();
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* InlineFlow passes this information to its context.
|
120
|
*
|
121
|
* @see FlowContext#setContinueOnSameLine(boolean)
|
122
|
*/
|
123
|
public void setContinueOnSameLine(boolean value) {
|
124
|
getContext().setContinueOnSameLine(value);
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Initializes the given LineBox. Called by createNewLine().
|
129
|
*
|
130
|
* @param line
|
131
|
* The LineBox to initialize.
|
132
|
*/
|
133
|
protected void setupLine(LineBox line) {
|
134
|
line.setX(0);
|
135
|
line.setRecommendedWidth(getContext().getRemainingLineWidth());
|
136
|
}
|
137
|
|
138
|
}
|