1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.layouts;
17
18 import java.awt.Component;
19 import java.awt.Container;
20 import java.awt.Dimension;
21 import java.awt.Insets;
22 import java.awt.LayoutManager2;
23 import java.awt.Rectangle;
24 import java.io.Serializable;
25 import java.util.HashMap;
26
27 import javax.swing.JComponent;
28
29 import org.xulux.api.gui.IWidget;
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class XYLayout extends SwingLayoutAbstract implements LayoutManager2, Serializable {
44
45
46
47 protected HashMap map;
48
49
50
51
52 private boolean firstLayout = true;
53
54
55
56
57 public XYLayout() {
58 map = new HashMap();
59 }
60
61
62
63
64
65 public XYLayout(IWidget parent) {
66 map = new HashMap();
67 setParent(parent);
68 }
69
70
71
72
73
74 public void addLayoutComponent(Component comp, Object constraints) {
75 map.put(comp, constraints);
76 }
77
78
79
80
81 public float getLayoutAlignmentX(Container target) {
82 return 0.5F;
83 }
84
85
86
87
88 public float getLayoutAlignmentY(Container target) {
89 return 0.5F;
90 }
91
92
93
94
95 public void invalidateLayout(Container target) {
96 }
97
98
99
100
101 public Dimension maximumLayoutSize(Container target) {
102 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
103 }
104
105
106
107
108 public void addLayoutComponent(String name, Component comp) {
109 }
110
111
112
113
114 public void layoutContainer(Container parent) {
115 if (parent == null) {
116 return;
117 }
118 synchronized (parent.getTreeLock()) {
119 Insets insets = parent.getInsets();
120 int count = parent.getComponentCount();
121 for (int i = 0; i < count; i++) {
122 Component component = parent.getComponent(i);
123 if (!component.isVisible()) {
124 continue;
125 }
126 IWidget widget = (IWidget) map.get(component);
127 Rectangle r = null;
128 if (widget != null && widget.isVisible()) {
129 r = getRectangle(widget, component);
130 if (component instanceof JComponent) {
131 ((JComponent) component).setPreferredSize(new Dimension(r.width, r.height));
132 }
133 component.setSize(r.width, r.height);
134 } else {
135
136
137
138
139
140 r = getRectangle(component, insets);
141 }
142
143 component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height);
144 }
145 }
146 }
147
148
149
150
151
152
153
154
155
156 public Rectangle getRectangle(IWidget widget, Component component) {
157 if (widget == null) {
158 return null;
159 }
160 Rectangle r = widget.getRectangle().getRectangle();
161 if (r.width <= 0 && r.height <= 0) {
162 Dimension d = component.getPreferredSize();
163 if (d != null) {
164
165
166
167 r.width = d.width;
168 r.height = d.height;
169 widget.getRectangle().setSize(r.width, r.height);
170 }
171 }
172 return r;
173 }
174
175
176
177
178
179
180
181
182
183
184
185 public Rectangle getRectangle(Component component, Insets insets) {
186
187 if (component == null) {
188 return null;
189 }
190 Rectangle r = component.getBounds();
191
192
193 if (!isFirstLayout()) {
194 if (insets != null) {
195 r.x -= insets.left;
196 r.y -= insets.top;
197 component.setBounds(r);
198 }
199 } else {
200 setFirstLayout(false);
201 }
202 Dimension d = component.getPreferredSize();
203 r.width = d.width;
204 r.height = d.height;
205 return r;
206 }
207
208
209
210
211 public Dimension minimumLayoutSize(Container parent) {
212 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
213 }
214
215
216
217
218 public Dimension preferredLayoutSize(Container parent) {
219 Dimension dim = getLayoutSize(parent);
220 return dim;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 protected Dimension getLayoutSize(Container parent) {
238 Dimension dim = new Dimension(0, 0);
239 IWidget pWidget = (IWidget) map.get(parent);
240 int maxX = 0;
241 int maxY = 0;
242 for (int i = 0; i < parent.getComponentCount(); i++) {
243 Component component = parent.getComponent(i);
244 IWidget widget = (IWidget) map.get(component);
245 if (widget != null && widget.isVisible()) {
246 Rectangle r = getRectangle(widget, component);
247 int tmpWidth = r.x + r.width;
248 if (tmpWidth > maxX) {
249 maxX = tmpWidth;
250 }
251 int tmpHeight = r.y + r.height;
252 if (tmpHeight > maxY) {
253 maxY = tmpHeight;
254 }
255 } else if (widget == null) {
256
257 Dimension compDim = component.getPreferredSize();
258 maxX += compDim.width;
259 maxY += compDim.height;
260 }
261 }
262 dim.width = maxX;
263 dim.height = maxY;
264
265 Insets insets = parent.getInsets();
266 dim.width += insets.left + insets.right;
267 dim.height += insets.top + insets.bottom;
268 return dim;
269 }
270
271
272
273
274
275 protected void setFirstLayout(boolean firstLayout) {
276 this.firstLayout = firstLayout;
277 }
278
279
280
281
282 protected boolean isFirstLayout() {
283 return this.firstLayout;
284 }
285
286
287
288
289 public void removeLayoutComponent(Component comp) {
290 map.remove(comp);
291 }
292
293
294
295
296 public void destroy() {
297 }
298
299
300
301
302 public void addWidget(IWidget widget) {
303 Object nativeWidget = widget.getNativeWidget();
304 if (nativeWidget instanceof Component) {
305 addLayoutComponent((Component) nativeWidget, widget);
306 }
307 }
308
309
310
311
312 public void removeWidget(IWidget widget) {
313 Object nativeWidget = widget.getNativeWidget();
314 if (nativeWidget instanceof Component) {
315 removeLayoutComponent((Component) nativeWidget);
316 }
317 }
318 }