Skip to main content
summaryrefslogtreecommitdiffstats
blob: c74963a58b1d1d5837f226896b3d303cfc8a2388 (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
/*******************************************************************************
 * 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.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * This layout if for those thigns that it's parent will decide its size. Such
 * as table cell.
 * 
 * @author mengbo
 * @version 1.5
 */
public class CSSPageFlowLayout extends CSSBlockFlowLayout {
	private Dimension _pageSize = new Dimension();

	private int _recommendedWidth;

	private int _pageSizeCacheKeys[] = new int[4];

	private Dimension _pageSizeCacheValues[] = new Dimension[4];

	private Dimension _cacheMaxWidthSize = null;

	/**
	 * @param cssfigure
	 */
	public CSSPageFlowLayout(CSSFigure cssfigure) {
		super(cssfigure);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.FlowFigureLayout#invalidate()
	 */
	public void invalidate() {
		super.invalidate();
		_pageSizeCacheKeys = new int[4];
		_pageSizeCacheValues = new Dimension[4];
		_pageSize = new Dimension();
		_recommendedWidth = 0;
		_cacheMaxWidthSize = null;
	}

	protected void endBlock() {
		layoutLines();
	}

	/**
	 * TODO: This method is not being called.
	 */
	public void postValidate() {
		Rectangle r = new Rectangle(_blockBox._x, _blockBox._y, _blockBox
				.getWidth(), _blockBox.getHeight());
		r = r.expand(getCSSFigure().getInsets());
		_pageSize.width = r.width;
		_pageSize.height = r.height;

		List list = getCSSFigure().getChildren();
		for (int i = 0; i < list.size(); i++) {
			((FlowFigure) list.get(i)).postValidate();
		}

	}

	/**
	 * Setup blockBox to the initial bounds of the Page
	 */
	protected void setupBlock() {
		// Remove all current Fragments
		_blockBox.clear();

		// Setup the one fragment for this Block with the correct X and
		// available width
		int recommendedWidth = getRecommendedWidth();
		_blockBox.setRecommendedWidth(recommendedWidth);

		if (recommendedWidth > 0 && recommendedWidth != Integer.MAX_VALUE) {
			_blockBox.setWidth(recommendedWidth);
		}

		_blockBox._x = 0;
	}

	public int getRecommendedWidth() {
		return _recommendedWidth;
	}

	private void setRecommendedWidth(int width) {
		if (_recommendedWidth == width) {
			return;
		}
		_recommendedWidth = width;
		getCSSFigure().invalidate2();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.layout.CSSLayout#setBoundsCalled(org.eclipse.jst.pagedesigner.css2.layout.CSSFigure,
	 *      org.eclipse.draw2d.geometry.Rectangle, boolean)
	 */
	public void setBoundsCalled(Rectangle r, boolean invalidate) {
		super.setBoundsCalled(r, invalidate);
		CSSFigure figure = getCSSFigure();
		int newWidth = r.width - figure.getInsets().getWidth();
		if (invalidate || getRecommendedWidth() != newWidth) {
			setRecommendedWidth(newWidth);
			figure.getUpdateManager().addInvalidFigure(figure);
		}
	}

	/**
	 * @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
	 */
	public Dimension getPreferredSize(IFigure container, int width, int h) {
		if (width >= 0) {
			width = Math.max(0, width - container.getInsets().getWidth());
		}

		for (int i = 0; i < 4; i++) {
			if (_pageSizeCacheKeys[i] == width
					&& _pageSizeCacheValues[i] != null) {
				return _pageSizeCacheValues[i];
			}
		}

		_pageSizeCacheKeys[3] = _pageSizeCacheKeys[2];
		_pageSizeCacheKeys[2] = _pageSizeCacheKeys[1];
		_pageSizeCacheKeys[1] = _pageSizeCacheKeys[0];
		_pageSizeCacheKeys[0] = width;

		_pageSizeCacheValues[3] = _pageSizeCacheValues[2];
		_pageSizeCacheValues[2] = _pageSizeCacheValues[1];
		_pageSizeCacheValues[1] = _pageSizeCacheValues[0];

		// Flowpage must temporarily layout to determine its preferred size
		int oldWidth = getRecommendedWidth();
		setRecommendedWidth(width);
		container.validate();
		_pageSizeCacheValues[0] = _pageSize.getExpanded(container.getInsets()
				.getWidth(), container.getInsets().getHeight());

		if (width != oldWidth) {
			setRecommendedWidth(oldWidth);
			container.getUpdateManager().addInvalidFigure(container);
		}
		return _pageSizeCacheValues[0];
	}

	public Dimension getMaxContentWidthSize(IFigure container) {
		if (this._cacheMaxWidthSize == null) {
			boolean b = getCalcuatingMaxWidth();
			setCalculatingMaxWidth(true);

			// Flowpage must temporarily layout to determine its preferred size
			int oldWidth = getRecommendedWidth();
			setRecommendedWidth(Integer.MAX_VALUE);
			container.validate();
			_cacheMaxWidthSize = _pageSize.getExpanded(container.getInsets()
					.getWidth(), container.getInsets().getHeight());

			if (0 != oldWidth) {
				setRecommendedWidth(oldWidth);
				container.getUpdateManager().addInvalidFigure(container);
			}

			setCalculatingMaxWidth(b);
		}
		return _cacheMaxWidthSize;
	}
}

Back to the top