comparison org.eclipse.draw2d/src/org/eclipse/draw2d/BorderLayout.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
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.BorderLayout;
14
15 import java.lang.all;
16
17 import org.eclipse.draw2d.geometry.Dimension;
18 import org.eclipse.draw2d.geometry.Insets;
19 import org.eclipse.draw2d.geometry.Rectangle;
20 import org.eclipse.draw2d.AbstractHintLayout;
21 import org.eclipse.draw2d.IFigure;
22 import org.eclipse.draw2d.PositionConstants;
23
24 /**
25 * @author Pratik Shah
26 */
27 public class BorderLayout
28 : AbstractHintLayout
29 {
30
31 /**
32 * Constant to be used as a constraint for child figures
33 */
34 private static Integer CENTER_;
35 public static Integer CENTER(){
36 if( !initStaticCtor_done ) initStaticCtor();
37 return CENTER_;
38 }
39 /**
40 * Constant to be used as a constraint for child figures
41 */
42 private static Integer TOP_;
43 public static Integer TOP(){
44 if( !initStaticCtor_done ) initStaticCtor();
45 return TOP_;
46 }
47 /**
48 * Constant to be used as a constraint for child figures
49 */
50 private static Integer BOTTOM_;
51 public static Integer BOTTOM(){
52 if( !initStaticCtor_done ) initStaticCtor();
53 return BOTTOM_;
54 }
55 /**
56 * Constant to be used as a constraint for child figures
57 */
58 private static Integer LEFT_;
59 public static Integer LEFT(){
60 if( !initStaticCtor_done ) initStaticCtor();
61 return LEFT_;
62 }
63 /**
64 * Constant to be used as a constraint for child figures
65 */
66 private static Integer RIGHT_;
67 public static Integer RIGHT(){
68 if( !initStaticCtor_done ) initStaticCtor();
69 return RIGHT_;
70 }
71
72 private static bool initStaticCtor_done = false;
73 private static void initStaticCtor(){
74 synchronized( BorderLayout.classinfo ){
75 if( !initStaticCtor_done ){
76 CENTER_ = new Integer(PositionConstants.CENTER);
77 TOP_ = new Integer(PositionConstants.TOP);
78 BOTTOM_ = new Integer(PositionConstants.BOTTOM);
79 LEFT_ = new Integer(PositionConstants.LEFT);
80 RIGHT_ = new Integer(PositionConstants.RIGHT);
81 initStaticCtor_done = true;
82 }
83 }
84 }
85
86 private IFigure center, left, top, bottom, right;
87 private int vGap = 0, hGap = 0;
88
89 /**
90 * @see org.eclipse.draw2d.AbstractHintLayout#calculateMinimumSize(IFigure, int, int)
91 */
92 protected Dimension calculateMinimumSize(IFigure container, int wHint, int hHint) {
93 int minWHint = 0, minHHint = 0;
94 if (wHint < 0) {
95 minWHint = -1;
96 }
97 if (hHint < 0) {
98 minHHint = -1;
99 }
100 Insets border = container.getInsets();
101 wHint = Math.max(minWHint, wHint - border.getWidth());
102 hHint = Math.max(minHHint, hHint - border.getHeight());
103 Dimension minSize = new Dimension();
104 int middleRowWidth = 0, middleRowHeight = 0;
105 int rows = 0, columns = 0;
106
107 if (top !is null && top.isVisible()) {
108 Dimension childSize = top.getMinimumSize(wHint, hHint);
109 hHint = Math.max(minHHint, hHint - (childSize.height + vGap));
110 minSize.setSize(childSize);
111 rows += 1;
112 }
113 if (bottom !is null && bottom.isVisible()) {
114 Dimension childSize = bottom.getMinimumSize(wHint, hHint);
115 hHint = Math.max(minHHint, hHint - (childSize.height + vGap));
116 minSize.width = Math.max(minSize.width, childSize.width);
117 minSize.height += childSize.height;
118 rows += 1;
119 }
120 if (left !is null && left.isVisible()) {
121 Dimension childSize = left.getMinimumSize(wHint, hHint);
122 middleRowWidth = childSize.width;
123 middleRowHeight = childSize.height;
124 wHint = Math.max(minWHint, wHint - (childSize.width + hGap));
125 columns += 1;
126 }
127 if (right !is null && right.isVisible()) {
128 Dimension childSize = right.getMinimumSize(wHint, hHint);
129 middleRowWidth += childSize.width;
130 middleRowHeight = Math.max(childSize.height, middleRowHeight);
131 wHint = Math.max(minWHint, wHint - (childSize.width + hGap));
132 columns += 1;
133 }
134 if (center !is null && center.isVisible()) {
135 Dimension childSize = center.getMinimumSize(wHint, hHint);
136 middleRowWidth += childSize.width;
137 middleRowHeight = Math.max(childSize.height, middleRowHeight);
138 columns += 1;
139 }
140
141 rows += columns > 0 ? 1 : 0;
142 // Add spacing, insets, and the size of the middle row
143 minSize.height += middleRowHeight + border.getHeight() + ((rows - 1) * vGap);
144 minSize.width = Math.max(minSize.width, middleRowWidth) + border.getWidth()
145 + ((columns - 1) * hGap);
146
147 return minSize;
148 }
149
150 /**
151 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
152 */
153 protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHint) {
154 int minWHint = 0, minHHint = 0;
155 if (wHint < 0)
156 minWHint = -1;
157
158 if (hHint < 0)
159 minHHint = -1;
160
161 Insets border = container.getInsets();
162 wHint = Math.max(minWHint, wHint - border.getWidth());
163 hHint = Math.max(minHHint, hHint - border.getHeight());
164 Dimension prefSize = new Dimension();
165 int middleRowWidth = 0, middleRowHeight = 0;
166 int rows = 0, columns = 0;
167
168 if (top !is null && top.isVisible()) {
169 Dimension childSize = top.getPreferredSize(wHint, hHint);
170 hHint = Math.max(minHHint, hHint - (childSize.height + vGap));
171 prefSize.setSize(childSize);
172 rows += 1;
173 }
174 if (bottom !is null && bottom.isVisible()) {
175 Dimension childSize = bottom.getPreferredSize(wHint, hHint);
176 hHint = Math.max(minHHint, hHint - (childSize.height + vGap));
177 prefSize.width = Math.max(prefSize.width, childSize.width);
178 prefSize.height += childSize.height;
179 rows += 1;
180 }
181 if (left !is null && left.isVisible()) {
182 Dimension childSize = left.getPreferredSize(wHint, hHint);
183 middleRowWidth = childSize.width;
184 middleRowHeight = childSize.height;
185 wHint = Math.max(minWHint, wHint - (childSize.width + hGap));
186 columns += 1;
187 }
188 if (right !is null && right.isVisible()) {
189 Dimension childSize = right.getPreferredSize(wHint, hHint);
190 middleRowWidth += childSize.width;
191 middleRowHeight = Math.max(childSize.height, middleRowHeight);
192 wHint = Math.max(minWHint, wHint - (childSize.width + hGap));
193 columns += 1;
194 }
195 if (center !is null && center.isVisible()) {
196 Dimension childSize = center.getPreferredSize(wHint, hHint);
197 middleRowWidth += childSize.width;
198 middleRowHeight = Math.max(childSize.height, middleRowHeight);
199 columns += 1;
200 }
201
202 rows += columns > 0 ? 1 : 0;
203 // Add spacing, insets, and the size of the middle row
204 prefSize.height += middleRowHeight + border.getHeight() + ((rows - 1) * vGap);
205 prefSize.width = Math.max(prefSize.width, middleRowWidth) + border.getWidth()
206 + ((columns - 1) * hGap);
207
208 return prefSize;
209 }
210
211 /**
212 * @see org.eclipse.draw2d.LayoutManager#layout(IFigure)
213 */
214 public void layout(IFigure container) {
215 Rectangle area = container.getClientArea();
216 Rectangle rect = new Rectangle();
217
218 Dimension childSize;
219
220 if (top !is null && top.isVisible()) {
221 childSize = top.getPreferredSize(area.width, -1);
222 rect.setLocation(area.x, area.y);
223 rect.setSize(childSize);
224 rect.width = area.width;
225 top.setBounds(rect);
226 area.y += rect.height + vGap;
227 area.height -= rect.height + vGap;
228 }
229 if (bottom !is null && bottom.isVisible()) {
230 childSize = bottom.getPreferredSize(Math.max(area.width, 0), -1);
231 rect.setSize(childSize);
232 rect.width = area.width;
233 rect.setLocation(area.x, area.y + area.height - rect.height);
234 bottom.setBounds(rect);
235 area.height -= rect.height + vGap;
236 }
237 if (left !is null && left.isVisible()) {
238 childSize = left.getPreferredSize(-1, Math.max(0, area.height));
239 rect.setLocation(area.x, area.y);
240 rect.width = childSize.width;
241 rect.height = Math.max(0, area.height);
242 left.setBounds(rect);
243 area.x += rect.width + hGap;
244 area.width -= rect.width + hGap;
245 }
246 if (right !is null && right.isVisible()) {
247 childSize = right.getPreferredSize(-1, Math.max(0, area.height));
248 rect.width = childSize.width;
249 rect.height = Math.max(0, area.height);
250 rect.setLocation(area.x + area.width - rect.width, area.y);
251 right.setBounds(rect);
252 area.width -= rect.width + hGap;
253 }
254 if (center !is null && center.isVisible()) {
255 if (area.width < 0)
256 area.width = 0;
257 if (area.height < 0)
258 area.height = 0;
259 center.setBounds(area);
260 }
261 }
262
263 /**
264 * @see org.eclipse.draw2d.AbstractLayout#remove(IFigure)
265 */
266 public void remove(IFigure child) {
267 if (center is child) {
268 center = null;
269 } else if (top is child) {
270 top = null;
271 } else if (bottom is child) {
272 bottom = null;
273 } else if (right is child) {
274 right = null;
275 } else if (left is child) {
276 left = null;
277 }
278 }
279
280 /**
281 * Sets the location of hte given child in this layout. Valid constraints:
282 * <UL>
283 * <LI>{@link #CENTER}</LI>
284 * <LI>{@link #TOP}</LI>
285 * <LI>{@link #BOTTOM}</LI>
286 * <LI>{@link #LEFT}</LI>
287 * <LI>{@link #RIGHT}</LI>
288 * <LI><code>null</code> (to remove a child's constraint)</LI>
289 * </UL>
290 *
291 * <p>
292 * Ensure that the given Figure is indeed a child of the Figure on which this layout has
293 * been set. Proper behaviour cannot be guaranteed if that is not the case. Also ensure
294 * that every child has a valid constraint.
295 * </p>
296 * <p>
297 * Passing a <code>null</code> constraint will invoke {@link #remove(IFigure)}.
298 * </p>
299 * <p>
300 * If the given child was assigned another constraint earlier, it will be re-assigned to
301 * the new constraint. If there is another child with the given constraint, it will be
302 * over-ridden so that the given child now has that constraint.
303 * </p>
304 *
305 * @see org.eclipse.draw2d.AbstractLayout#setConstraint(IFigure, Object)
306 */
307 public void setConstraint(IFigure child, Object constraint) {
308 remove(child);
309 super.setConstraint(child, constraint);
310 if (constraint is null) {
311 return;
312 }
313
314 switch ((cast(Integer) constraint).intValue()) {
315 case PositionConstants.CENTER :
316 center = child;
317 break;
318 case PositionConstants.TOP :
319 top = child;
320 break;
321 case PositionConstants.BOTTOM :
322 bottom = child;
323 break;
324 case PositionConstants.RIGHT :
325 right = child;
326 break;
327 case PositionConstants.LEFT :
328 left = child;
329 break;
330 default :
331 break;
332 }
333 }
334
335 /**
336 * Sets the horizontal spacing to be used between the children. Default is 0.
337 *
338 * @param gap The horizonal spacing
339 */
340 public void setHorizontalSpacing(int gap) {
341 hGap = gap;
342 }
343
344 /**
345 * Sets the vertical spacing ot be used between the children. Default is 0.
346 *
347 * @param gap The vertical spacing
348 */
349 public void setVerticalSpacing(int gap) {
350 vGap = gap;
351 }
352
353 }