comparison org.eclipse.draw2d/src/org/eclipse/draw2d/text/BlockFlowLayout.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.text.BlockFlowLayout;
14
15 import java.lang.all;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.draw2d.Figure;
19 import org.eclipse.draw2d.PositionConstants;
20 import org.eclipse.draw2d.geometry.Insets;
21 import org.eclipse.draw2d.text.FlowContainerLayout;
22 import org.eclipse.draw2d.text.BlockBox;
23 import org.eclipse.draw2d.text.BlockFlow;
24 import org.eclipse.draw2d.text.CompositeBox;
25 import org.eclipse.draw2d.text.FlowFigure;
26 import org.eclipse.draw2d.text.LineRoot;
27
28 /**
29 * The layout for {@link BlockFlow} figures.
30 *
31 * <P>WARNING: This class is not intended to be subclassed by clients.
32 * @author hudsonr
33 * @since 2.1
34 */
35 public class BlockFlowLayout
36 : FlowContainerLayout
37 {
38
39 BlockBox blockBox;
40 bool blockInvalid = false;
41 private bool continueOnSameLine = false;
42 private CompositeBox previousLine = null;
43
44 /**
45 * Creates a new BlockFlowLayout with the given BlockFlow.
46 * @param blockFlow the BlockFlow
47 */
48 public this(BlockFlow blockFlow) {
49 super(blockFlow);
50 }
51
52 private void addBelowPreviousLine(CompositeBox line) {
53 if (previousLine is null)
54 line.setLineTop(line.getTopMargin());
55 else
56 line.setLineTop(previousLine.getBaseline() + previousLine.getDescent()
57 + Math.max(previousLine.getBottomMargin(), line.getTopMargin()));
58
59 int alignment = getBlockFlow().getHorizontalAligment();
60 if (alignment is PositionConstants.LEFT || alignment is PositionConstants.RIGHT) {
61 int orientation = getBlockFlow().getOrientation();
62 if (alignment is PositionConstants.LEFT)
63 alignment = orientation is SWT.LEFT_TO_RIGHT
64 ? PositionConstants.ALWAYS_LEFT : PositionConstants.ALWAYS_RIGHT;
65 else
66 alignment = orientation is SWT.LEFT_TO_RIGHT
67 ? PositionConstants.ALWAYS_RIGHT : PositionConstants.ALWAYS_LEFT;
68 }
69 if (alignment !is PositionConstants.CENTER && getBlockFlow().isMirrored())
70 alignment = (PositionConstants.ALWAYS_LEFT | PositionConstants.ALWAYS_RIGHT)
71 & ~alignment;
72
73 switch (alignment) {
74 case PositionConstants.ALWAYS_RIGHT:
75 line.setX(blockBox.getRecommendedWidth() - line.getWidth());
76 break;
77 case PositionConstants.CENTER:
78 line.setX((blockBox.getRecommendedWidth() - line.getWidth()) / 2);
79 break;
80 case PositionConstants.ALWAYS_LEFT:
81 line.setX(0);
82 break;
83 default:
84 throw new RuntimeException("Unexpected state"); //$NON-NLS-1$
85 }
86 blockBox.add(line);
87 previousLine = line;
88 }
89
90 /**
91 * Align the line horizontally and then commit it.
92 */
93 protected void addCurrentLine() {
94 addBelowPreviousLine(currentLine);
95 (cast(LineRoot)currentLine).commit();
96 }
97
98 /**
99 * @see FlowContext#addLine(CompositeBox)
100 */
101 public void addLine(CompositeBox box) {
102 endLine();
103 addBelowPreviousLine(box);
104 }
105
106 /**
107 * Marks the blocks contents as changed. This means that children will be invalidated
108 * during validation.
109 * @since 3.1
110 */
111 public void blockContentsChanged() {
112 blockInvalid = true;
113 }
114
115 /**
116 * @see FlowContainerLayout#cleanup()
117 */
118 protected void cleanup() {
119 super.cleanup();
120 previousLine = null;
121 }
122
123 /**
124 * @see FlowContainerLayout#createNewLine()
125 */
126 protected void createNewLine() {
127 currentLine = new LineRoot(getBlockFlow().isMirrored());
128 currentLine.setRecommendedWidth(blockBox.getRecommendedWidth());
129 }
130
131 /**
132 * Called by flush(), adds the BlockBox associated with this BlockFlowLayout
133 * to the current line and then ends the line.
134 */
135 protected void endBlock() {
136 if (blockInvalid) {
137 Insets insets = getBlockFlow().getInsets();
138 blockBox.height += insets.getHeight();
139 blockBox.width += insets.getWidth();
140 }
141
142 if (getContext() !is null)
143 getContext().addLine(blockBox);
144
145 if (blockInvalid) {
146 blockInvalid = false;
147 List v = getFlowFigure().getChildren();
148 for (int i = 0; i < v.size(); i++)
149 (cast(FlowFigure)v.get(i)).postValidate();
150 }
151 }
152
153 /**
154 * @see FlowContext#endLine()
155 */
156 public void endLine() {
157 if (currentLine is null || !currentLine.isOccupied())
158 return;
159 addCurrentLine();
160 currentLine = null;
161 }
162
163 /**
164 * @see FlowContainerLayout#flush()
165 */
166 protected void flush() {
167 endLine();
168 endBlock();
169 }
170
171 bool forceChildInvalidation(Figure f) {
172 return blockInvalid;
173 }
174
175 /**
176 * Returns the BlockFlow associated with this BlockFlowLayout
177 * @return the BlockFlow
178 */
179 protected final BlockFlow getBlockFlow() {
180 return cast(BlockFlow)getFlowFigure();
181 }
182
183 int getContextWidth() {
184 return getContext().getRemainingLineWidth();
185 }
186
187 /**
188 * @see FlowContext#getContinueOnSameLine()
189 */
190 public bool getContinueOnSameLine() {
191 return continueOnSameLine;
192 }
193
194 /**
195 * @see FlowContext#getWidthLookahead(FlowFigure, int[])
196 */
197 public void getWidthLookahead(FlowFigure child, int result[]) {
198 List children = getFlowFigure().getChildren();
199 int index = -1;
200 if (child !is null)
201 index = children.indexOf(child);
202
203 for (int i = index + 1; i < children.size(); i++)
204 if ((cast(FlowFigure)children.get(i)).addLeadingWordRequirements(result))
205 return;
206 }
207
208 /**
209 * @see FlowContainerLayout#preLayout()
210 */
211 protected void preLayout() {
212 setContinueOnSameLine(false);
213 blockBox = getBlockFlow().getBlockBox_package();
214 setupBlock();
215 //Probably could setup current and previous line here, or just previous
216 }
217
218 /**
219 * @see org.eclipse.draw2d.text.FlowContext#setContinueOnSameLine(bool)
220 */
221 public void setContinueOnSameLine(bool value) {
222 continueOnSameLine = value;
223 }
224
225 /**
226 * sets up the single block that contains all of the lines.
227 */
228 protected void setupBlock() {
229 int recommended = getContextWidth();
230 if (recommended is Integer.MAX_VALUE)
231 recommended = -1;
232 BlockFlow bf = getBlockFlow();
233 if (recommended > 0) {
234 int borderCorrection = bf.getInsets().getWidth() + bf.getLeftMargin()
235 + bf.getRightMargin();
236 recommended = Math.max(0, recommended - borderCorrection);
237 }
238
239 if (recommended !is blockBox.recommendedWidth) {
240 blockInvalid = true;
241 blockBox.setRecommendedWidth(recommended);
242 }
243
244 if (blockInvalid) {
245 blockBox.height = 0;
246 blockBox.setWidth(Math.max(0, recommended));
247 }
248 }
249
250 }