Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97f4571c040a0c622d0e4ccf9bb09a0e44dfd4a7 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
 * 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;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.css2.property.ICSSPropertyID;
import org.eclipse.jst.pagedesigner.css2.style.ITagEditInfo;

/**
 * The layout manager for {@link CSSFigure}figures. This class is based on
 * InlineFlowLayout of draw2d.
 * 
 * @author mengbo
 */
public class CSSInlineFlowLayout extends CSSLayout {
	List _fragments = new ArrayList();

	/**
	 * Creates a new InlineFlowLayout with the given FlowFigure.
	 * 
	 * @param flow
	 *            The FlowFigure
	 */
	public CSSInlineFlowLayout(CSSFigure flow) {
		super(flow);
	}

	/**
	 * Clears out all fragments prior to the call to layoutChildren().
	 */
	public void preLayout() {
		super.preLayout();
		_fragments.clear();
		// force creating of the first line. avoid empty element don't have
		// fragments.
		// createFirstLine();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContainerLayout#layoutChildren()
	 */
	protected void layoutChildren() {
		// For designer, to make it to have some size. otherwise can't
		// be found on screen.
		// List children = getCSSFigure().getChildren();
		// if (children.size() == 0)
		// {
		// FlowBox box = new FlowBox();
		// box._height = getCSSStyle().getCSSFont().getFontSize();
		// box._width = 2;
		// addToCurrentLine(box);
		//
		// }
		super.layoutChildren();
	}

	/**
	 * Adds the given FlowBox to the current line of this InlineFlowLayout.
	 * 
	 * @param block
	 *            the FlowBox to add to the current line
	 */
	public void addToCurrentLine(FlowBox block) {
		getCurrentLine().add(block);
		// XXX: ???: will currentLine be added multiple times to fragments?
		// (yang)
		// _fragments.add(_currentLine);
	}

	private void createFirstLine() {
		_currentLine = new LineBox();
		setupLine(_currentLine, true);
		_fragments.add(_currentLine);
	}

	/**
	 * @see FlowContainerLayout#createNewLine()
	 */
	protected void createNewLine() {
		_currentLine = new LineBox();
		setupLine(_currentLine, false);
		_fragments.add(_currentLine);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContainerLayout#createNewLine(int)
	 */
	protected void createNewLine(int topMargin) {
		// inline flow don't support vertical margin.
		createNewLine();
	}

	/**
	 * @see FlowContainerLayout#cleanup()
	 */
	protected void cleanup() {
		_currentLine = null;
	}

	/**
	 * @see FlowContainerLayout#flush()
	 */
	protected void flush() {
		if (_fragments.isEmpty()) {
			createFirstLine();
		} else if (_fragments.size() == 1) {

			ICSSStyle style = getCSSStyle();
			int minWidth = 0, minHeight = 0;
			// try to see whether there is any designer specified min size
			ITagEditInfo info = (ITagEditInfo) style
					.getAdapter(ITagEditInfo.class);
			if (info != null) {
				minWidth = info.getMinWidth();
				minHeight = info.getMinHeight();
			}
			FlowBox box = (FlowBox) _fragments.get(0);
			if (minWidth > box._width) {
				box._width = minWidth;
			}
			if (minHeight > box._height) {
				box._height = minHeight;
			}
		}

		if (_currentLine != null /* && _currentLine.isOccupied() */) {
			_currentLine._marginInsets.right = getCSSStyle().getMarginInsets().right;
			getFlowContext().addToCurrentLine(_currentLine);
		}

	}

	/**
	 * @see FlowContext#endLine()
	 */
	public void endLine() {
		if (_currentLine == null) {
			getFlowContext().endLine();
			return;
		}
		// If nothing was ever placed in the line, ignore it. and if the line is
		// the first line, just remove it.
		if (_currentLine.isOccupied()) {
			getFlowContext().addToCurrentLine(_currentLine);
		} else if (_fragments.size() == 1) {
			_fragments.remove(0);
		}
		getFlowContext().endLine();
		_currentLine = null;
	}

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#getCurrentY()
	 */
	public int getCurrentY() {
		return getCurrentLine()._y;
	}

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContainerLayout#isCurrentLineOccupied()
	 */
	public boolean isCurrentLineOccupied() {
		if (_currentLine == null) {
			return getFlowContext().isCurrentLineOccupied();
		} else if (_currentLine.getFragments().isEmpty()) {
			return getFlowContext().isCurrentLineOccupied();
		} else {
			return true;
		}
	}

	/**
	 * Initializes the given LineBox. Called by createNewLine().
	 * 
	 * @param line
	 *            The LineBox to initialize.
	 */
	protected void setupLine(LineBox line, boolean firstline) {
		LineBox parent = getFlowContext().getCurrentLine();
		line._x = 0;
		line._y = getFlowContext().getCurrentY();

		line.setRecommendedWidth(parent.getAvailableWidth());

		setLineVerticalAlign(line);
		setFontinfoForLine(line);

		if (firstline && getCSSStyle() != null) {
			ICSSStyle style = getCSSStyle();
			int minWidth = 0, minHeight = 0;
			// try to see whether there is any designer specified min size
			ITagEditInfo info = (ITagEditInfo) style
					.getAdapter(ITagEditInfo.class);
			if (info != null) {
				minWidth = info.getMinWidth();
				minHeight = info.getMinHeight();
			}

			// // CSS also has the min-width/min-height property. We should also
			// get that,
			// // and using the max of the "min-width" css property and the
			// designer specified min size.
			// int height =
			// getLengthValue(style,ICSSPropertyID.ATTR_MIN_HEIGHT);
			// if(height > minHeight)
			// {
			// minHeight = height;
			// }
			// int width = getLengthValue(style,ICSSPropertyID.ATTR_MIN_WIDTH);
			// if(width > minWidth)
			// {
			// minWidth = width;
			// }
			if (minWidth > 0) {
				line.setWidth(minWidth);
			}
			int fontHeight = this.getCSSStyle().getCSSFont().getXHeight();
			if (minHeight > 0 && minHeight > fontHeight) {
				line.setHeight(minHeight);
			} else {
				line.setHeight(fontHeight);
			}
		}
	}

	private void setLineVerticalAlign(LineBox box) {
		ICSSStyle style = getCSSStyle();
		if (style != null) {
			box.setVerticalAlignData(style
					.getStyleProperty(ICSSPropertyID.ATTR_VERTICAL_ALIGN));
		}
	}

	private void setFontinfoForLine(LineBox line) {

		ICSSStyle style = getCSSStyle();
		if (style != null) {
			line.setFontMetrics(FigureUtilities.getFontMetrics(style
					.getCSSFont().getSwtFont()));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowFigureLayout#dispose()
	 */
	public void dispose() {
		// 

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.ICSSLayout#getFragmentsForRead()
	 */
	public List getFragmentsForRead() {
		return _fragments;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.ICSSLayout#postValidate()
	 */
	public void postValidate() {
		List list = _fragments;

		FlowBox box;
		int left = Integer.MAX_VALUE, top = left;
		int right = Integer.MIN_VALUE, bottom = right;
		for (int i = 0; i < list.size(); i++) {
			box = (FlowBox) list.get(i);
			// if (box instanceof LineBox && !((LineBox) box).isOccupied())
			// {
			// continue; // skip unoccupied line
			// }
			left = Math.min(left, box._x);
			right = Math.max(right, box._x + box._width);
			top = Math.min(top, box._y);
			bottom = Math.max(bottom, box._y + box._height);
		}
		getCSSFigure().setBounds(
				new Rectangle(left, top, right - left, bottom - top));
		list = getCSSFigure().getChildren();
		for (int i = 0; i < list.size(); i++) {
			((FlowFigure) list.get(i)).postValidate();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#getContainerWidth()
	 */
	public int getContainerWidth() {
		// FIXME: don't really understand what means for inline
		return this.getFlowContext().getContainerWidth();
	}
}

Back to the top