comparison dwtx/draw2d/XYLayout.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.XYLayout;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17
18 import dwtx.draw2d.geometry.Dimension;
19 import dwtx.draw2d.geometry.Insets;
20 import dwtx.draw2d.geometry.Point;
21 import dwtx.draw2d.geometry.Rectangle;
22
23 import dwtx.draw2d.AbstractLayout;
24 import dwtx.draw2d.IFigure;
25
26 /**
27 * This class implements the {@link dwtx.draw2d.LayoutManager} interface using the
28 * XY Layout algorithm. This lays out the components using the layout constraints as
29 * defined by each component.
30 */
31 public class XYLayout
32 : AbstractLayout
33 {
34
35 /** The layout contraints */
36 protected Map constraints;
37
38 this(){
39 constraints = new HashMap();
40 }
41
42 /**
43 * Calculates and returns the preferred size of the input figure. Since in XYLayout the
44 * location of the child should be preserved, the preferred size would be a region which
45 * would hold all the children of the input figure. If no constraint is set, that child
46 * is ignored for calculation. If width and height are not positive, the preferred
47 * dimensions of the child are taken.
48 *
49 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
50 * @since 2.0
51 */
52 protected Dimension calculatePreferredSize(IFigure f, int wHint, int hHint) {
53 Rectangle rect = new Rectangle();
54 ListIterator children = f.getChildren().listIterator();
55 while (children.hasNext()) {
56 IFigure child = cast(IFigure)children.next();
57 Rectangle r = cast(Rectangle)constraints.get(cast(Object)child);
58 if (r is null)
59 continue;
60
61 if (r.width is -1 || r.height is -1) {
62 Dimension preferredSize = child.getPreferredSize(r.width, r.height);
63 r = r.getCopy();
64 if (r.width is -1)
65 r.width = preferredSize.width;
66 if (r.height is -1)
67 r.height = preferredSize.height;
68 }
69 rect.union_(r);
70 }
71 Dimension d = rect.getSize();
72 Insets insets = f.getInsets();
73 return (new Dimension(d.width + insets.getWidth(), d.height + insets.getHeight())).
74 union_(getBorderPreferredSize(f));
75 }
76
77 /**
78 * @see LayoutManager#getConstraint(IFigure)
79 */
80 public Object getConstraint(IFigure figure) {
81 return constraints.get(cast(Object)figure);
82 }
83
84 /**
85 * Returns the origin for the given figure.
86 * @param parent the figure whose origin is requested
87 * @return the origin
88 */
89 public Point getOrigin(IFigure parent) {
90 return parent.getClientArea().getLocation();
91 }
92
93 /**
94 * Implements the algorithm to layout the components of the given container figure.
95 * Each component is laid out using its own layout constraint specifying its size
96 * and position.
97 *
98 * @see LayoutManager#layout(IFigure)
99 */
100 public void layout(IFigure parent) {
101 Iterator children = parent.getChildren().iterator();
102 Point offset = getOrigin(parent);
103 IFigure f;
104 while (children.hasNext()) {
105 f = cast(IFigure)children.next();
106 Rectangle bounds = cast(Rectangle)getConstraint(f);
107 if (bounds is null) continue;
108
109 if (bounds.width is -1 || bounds.height is -1) {
110 Dimension preferredSize = f.getPreferredSize(bounds.width, bounds.height);
111 bounds = bounds.getCopy();
112 if (bounds.width is -1)
113 bounds.width = preferredSize.width;
114 if (bounds.height is -1)
115 bounds.height = preferredSize.height;
116 }
117 bounds = bounds.getTranslated(offset);
118 f.setBounds(bounds);
119 }
120 }
121
122 /**
123 * @see LayoutManager#remove(IFigure)
124 */
125 public void remove(IFigure figure) {
126 super.remove(figure);
127 constraints.remove(cast(Object)figure);
128 }
129
130 /**
131 * Sets the layout constraint of the given figure. The constraints can only be of type
132 * {@link Rectangle}.
133 *
134 * @see LayoutManager#setConstraint(IFigure, Object)
135 * @since 2.0
136 */
137 public void setConstraint(IFigure figure, Object newConstraint) {
138 super.setConstraint(figure, newConstraint);
139 if (newConstraint !is null)
140 constraints.put(cast(Object)figure, newConstraint);
141 }
142
143 }