Skip to main content
summaryrefslogtreecommitdiffstats
blob: ca43353651ab62d927facc5d260389286f769961 (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
/*******************************************************************************
 * 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.List;

import org.eclipse.draw2d.Figure;

/**
 * A layout for FlowFigures with children.
 * <P>
 * WARNING: This class is not intended to be subclassed by clients.
 * 
 * @author mengbo
 * @since 2.1
 */
public abstract class FlowContainerLayout extends FlowFigureLayout implements
		FlowContext {
//	private static Logger _log = PDPlugin.getLogger(FlowContainerLayout.class);

	/**
	 * the current line
	 */
	protected LineBox _currentLine;

	private boolean _calculatingMaxWidth;

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowFigureLayout#FlowFigureLayout(FlowFigure)
	 */
	protected FlowContainerLayout(FlowFigure flowFigure) {
		super(flowFigure);
	}

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#addToCurrentLine(FlowBox)
	 */
	public void addToCurrentLine(FlowBox block) {
		getCurrentLine().add(block);
	}

	/**
	 * Used by getCurrentLine().
	 */
	protected abstract void createNewLine();

	/**
	 * Used by getCurrentLine(int topmargin)
	 * 
	 * @param topMargin
	 */
	protected void createNewLine(int topMargin) {
		createNewLine();
	}

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#getCurrentLine()
	 */
	public LineBox getCurrentLine() {
		if (_currentLine == null)
			createNewLine();
		return _currentLine;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#getCurrentLine(int)
	 */
	public LineBox getCurrentLine(int topMargin) {
		if (_currentLine == null) {
			createNewLine(topMargin);
		}
		// if the current line only contains an empty string, reset the current
		// line using the given margin.
		else if (_currentLine.isEmptyStringLine()) {
			List list = _currentLine.getFragments();
			createNewLine(topMargin);
			_currentLine._fragments.addAll(list);
		}
		return _currentLine;
	}

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#isCurrentLineOccupied
	 */
	public boolean isCurrentLineOccupied() {
		return _currentLine != null && _currentLine.isOccupied();
	}

	/**
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowFigureLayout#layout()
	 */
	protected void layout() {
		preLayout();
		layoutChildren();
		flush();
		cleanup();
	}

	/**
	 * Layout all children.
	 */
	protected void layoutChildren() {
		List children = getFlowFigure().getChildren();
		for (int i = 0; i < children.size(); i++) {
			Figure f = (Figure) children.get(i);
			f.invalidate();
			f.validate();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#getLastMarginRight()
	 */
	public int getLastMarginRight() {
		if (_currentLine == null || !_currentLine.isOccupied()) {
			return 0;
		}
		FlowBox box = (FlowBox) _currentLine.getFragments().get(
				_currentLine.getFragments().size() - 1);
		if (box != null) {
			return box.getMarginInsets().right;
		}
        return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowContext#isCalculatingMaxWidth()
	 */
	public boolean isCalculatingMaxWidth() {
		if (_calculatingMaxWidth) {
			return true;
		} else if (this.getFlowContext() == null) {
			return false;
		} else {
			return this.getFlowContext().isCalculatingMaxWidth();
		}
	}

	/**
	 * Called before layoutChildren() to setup any necessary state.
	 */
	protected abstract void preLayout();

	/**
	 * Called after {@link #layoutChildren()}when all children have been laid
	 * out. This method exists to flush the last line.
	 */
	protected abstract void flush();

	/**
	 * Flush anything pending and free all temporary data used during layout.
	 */
	protected abstract void cleanup();
}

Back to the top