Mercurial > projects > dwt-addons
comparison dwtx/draw2d/DelegatingLayout.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
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 dwtx.draw2d.DelegatingLayout; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 import dwtx.dwtxhelper.Collection; | |
17 | |
18 import dwtx.draw2d.geometry.Dimension; | |
19 import dwtx.draw2d.IFigure; | |
20 import dwtx.draw2d.AbstractLayout; | |
21 import dwtx.draw2d.Locator; | |
22 | |
23 /** | |
24 * Figures using a DelegatingLayout as their layout manager give | |
25 * location responsibilities to their children. The children | |
26 * of a Figure using a DelegatingLayout should have a | |
27 * {@link Locator Locator} as a constraint whose | |
28 * {@link Locator#relocate(IFigure target) relocate} method is | |
29 * responsible for placing the child. | |
30 */ | |
31 public class DelegatingLayout | |
32 : AbstractLayout | |
33 { | |
34 | |
35 private Map constraints; | |
36 | |
37 this(){ | |
38 constraints = new HashMap(); | |
39 } | |
40 /** | |
41 * Calculates the preferred size of the given Figure. | |
42 * For the DelegatingLayout, this is the largest width and height | |
43 * values of the passed Figure's children. | |
44 * | |
45 * @param parent the figure whose preferred size is being calculated | |
46 * @param wHint the width hint | |
47 * @param hHint the height hint | |
48 * @return the preferred size | |
49 * @since 2.0 | |
50 */ | |
51 protected Dimension calculatePreferredSize(IFigure parent, int wHint, int hHint) { | |
52 List children = parent.getChildren(); | |
53 Dimension d = new Dimension(); | |
54 for (int i = 0; i < children.size(); i++) { | |
55 IFigure child = cast(IFigure)children.get(i); | |
56 d.union_(child.getPreferredSize()); | |
57 } | |
58 return d; | |
59 } | |
60 | |
61 /** | |
62 * @see dwtx.draw2d.LayoutManager#getConstraint(dwtx.draw2d.IFigure) | |
63 */ | |
64 public Object getConstraint(IFigure child) { | |
65 return constraints.get(cast(Object)child); | |
66 } | |
67 | |
68 /** | |
69 * Lays out the given figure's children based on their {@link Locator} constraint. | |
70 * @param parent the figure whose children should be layed out | |
71 */ | |
72 public void layout(IFigure parent) { | |
73 List children = parent.getChildren(); | |
74 for (int i = 0; i < children.size(); i++) { | |
75 IFigure child = cast(IFigure)children.get(i); | |
76 Locator locator = cast(Locator)constraints.get(cast(Object)child); | |
77 if (locator !is null) { | |
78 locator.relocate(child); | |
79 } | |
80 } | |
81 } | |
82 | |
83 /** | |
84 * Removes the locator for the given figure. | |
85 * @param child the child being removed | |
86 */ | |
87 public void remove(IFigure child) { | |
88 constraints.remove(cast(Object)child); | |
89 } | |
90 | |
91 /** | |
92 * Sets the constraint for the given figure. | |
93 * @param figure the figure whose contraint is being set | |
94 * @param constraint the new constraint | |
95 */ | |
96 public void setConstraint(IFigure figure, Object constraint) { | |
97 super.setConstraint(figure, constraint); | |
98 if (constraint !is null) | |
99 constraints.put(cast(Object)figure, constraint); | |
100 } | |
101 | |
102 } |