Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / OverlayLayout.java
blob56b8c8bb67a62b318d44bbdebd6d64a41887f045
1 /* OverlayLayout.java -- A layout manager
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 import java.awt.AWTError;
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.Insets;
45 import java.awt.LayoutManager2;
46 import java.io.Serializable;
48 /**
49 * A layout manager that lays out the components of a container one over
50 * another.
52 * The components take as much space as is available in the container, but not
53 * more than specified by their maximum size.
55 * The overall layout is mainly affected by the components
56 * <code>alignmentX</code> and <code>alignmentY</code> properties. All
57 * components are aligned, so that their alignment points (for either
58 * direction) are placed in one line (the baseline for this direction).
60 * For example: An X alignment of 0.0 means that the component's alignment
61 * point is at it's left edge, an X alignment of 0.5 means that the alignment
62 * point is in the middle, an X alignment of 1.0 means, the aligment point is
63 * at the right edge. So if you have three components, the first with 0.0, the
64 * second with 0.5 and the third with 1.0, then they are laid out like this:
66 * <pre>
67 * +-------+
68 * | 1 |
69 * +-------+
70 * +-------+
71 * | 2 |
72 * +-------+
73 * +---------+
74 * | 3 +
75 * +---------+
76 * </pre>
77 * The above picture shows the X alignment between the components. An Y
78 * alignment like shown above cannot be achieved with this layout manager. The
79 * components are place on top of each other, with the X alignment shown above.
81 * @author Roman Kennke (kennke@aicas.com)
82 * @author Andrew Selkirk
84 public class OverlayLayout implements LayoutManager2, Serializable
86 private static final long serialVersionUID = 18082829169631543L;
88 /**
89 * The container to be laid out.
91 private Container target;
93 /**
94 * The size requirements of the containers children for the X direction.
96 private SizeRequirements[] xChildren;
98 /**
99 * The size requirements of the containers children for the Y direction.
101 private SizeRequirements[] yChildren;
104 * The size requirements of the container to be laid out for the X direction.
106 private SizeRequirements xTotal;
109 * The size requirements of the container to be laid out for the Y direction.
111 private SizeRequirements yTotal;
114 * The offsets of the child components in the X direction.
116 private int[] offsetsX;
119 * The offsets of the child components in the Y direction.
121 private int[] offsetsY;
124 * The spans of the child components in the X direction.
126 private int[] spansX;
129 * The spans of the child components in the Y direction.
131 private int[] spansY;
134 * Creates a new OverlayLayout for the specified container.
136 * @param target the container to be laid out
138 public OverlayLayout(Container target)
140 this.target = target;
144 * Notifies the layout manager that the layout has become invalid. It throws
145 * away cached layout information and recomputes it the next time it is
146 * requested.
148 * @param target not used here
150 public void invalidateLayout(Container target)
152 xChildren = null;
153 yChildren = null;
154 xTotal = null;
155 yTotal = null;
156 offsetsX = null;
157 offsetsY = null;
158 spansX = null;
159 spansY = null;
163 * This method is not used in this layout manager.
165 * @param string not used here
166 * @param component not used here
168 public void addLayoutComponent(String string, Component component)
170 // Nothing to do here.
174 * This method is not used in this layout manager.
176 * @param component not used here
177 * @param constraints not used here
179 public void addLayoutComponent(Component component, Object constraints)
181 // Nothing to do here.
185 * This method is not used in this layout manager.
187 * @param component not used here
189 public void removeLayoutComponent(Component component)
191 // Nothing to do here.
195 * Returns the preferred size of the container that is laid out. This is
196 * computed by the children's preferred sizes, taking their alignments into
197 * account.
199 * @param target not used here
201 * @returns the preferred size of the container that is laid out
203 public Dimension preferredLayoutSize(Container target)
205 if (target != this.target)
206 throw new AWTError("OverlayLayout can't be shared");
208 checkTotalRequirements();
209 return new Dimension(xTotal.preferred, yTotal.preferred);
213 * Returns the minimum size of the container that is laid out. This is
214 * computed by the children's minimum sizes, taking their alignments into
215 * account.
217 * @param target not used here
219 * @returns the minimum size of the container that is laid out
221 public Dimension minimumLayoutSize(Container target)
223 if (target != this.target)
224 throw new AWTError("OverlayLayout can't be shared");
226 checkTotalRequirements();
227 return new Dimension(xTotal.minimum, yTotal.minimum);
231 * Returns the maximum size of the container that is laid out. This is
232 * computed by the children's maximum sizes, taking their alignments into
233 * account.
235 * @param target not used here
237 * @returns the maximum size of the container that is laid out
239 public Dimension maximumLayoutSize(Container target)
241 if (target != this.target)
242 throw new AWTError("OverlayLayout can't be shared");
244 checkTotalRequirements();
245 return new Dimension(xTotal.maximum, yTotal.maximum);
249 * Returns the X alignment of the container that is laid out. This is
250 * computed by the children's preferred sizes, taking their alignments into
251 * account.
253 * @param target not used here
255 * @returns the X alignment of the container that is laid out
257 public float getLayoutAlignmentX(Container target)
259 if (target != this.target)
260 throw new AWTError("OverlayLayout can't be shared");
262 checkTotalRequirements();
263 return xTotal.alignment;
267 * Returns the Y alignment of the container that is laid out. This is
268 * computed by the children's preferred sizes, taking their alignments into
269 * account.
271 * @param target not used here
273 * @returns the X alignment of the container that is laid out
275 public float getLayoutAlignmentY(Container target)
277 if (target != this.target)
278 throw new AWTError("OverlayLayout can't be shared");
280 checkTotalRequirements();
281 return yTotal.alignment;
285 * Lays out the container and it's children.
287 * The children are laid out one over another.
289 * The components take as much space as is available in the container, but
290 * not more than specified by their maximum size.
292 * The overall layout is mainly affected by the components
293 * <code>alignmentX</code> and <code>alignmentY</code> properties. All
294 * components are aligned, so that their alignment points (for either
295 * direction) are placed in one line (the baseline for this direction).
297 * For example: An X alignment of 0.0 means that the component's alignment
298 * point is at it's left edge, an X alignment of 0.5 means that the alignment
299 * point is in the middle, an X alignment of 1.0 means, the aligment point is
300 * at the right edge. So if you have three components, the first with 0.0,
301 * the second with 0.5 and the third with 1.0, then they are laid out like
302 * this:
304 * <pre>
305 * +-------+
306 * | 1 |
307 * +-------+
308 * +-------+
309 * | 2 |
310 * +-------+
311 * +---------+
312 * | 3 +
313 * +---------+
314 * </pre>
315 * The above picture shows the X alignment between the components. An Y
316 * alignment like shown above cannot be achieved with this layout manager.
317 * The components are place on top of each other, with the X alignment shown
318 * above.
320 * @param target not used here
322 public void layoutContainer(Container target)
324 if (target != this.target)
325 throw new AWTError("OverlayLayout can't be shared");
327 checkLayout();
328 Component[] children = target.getComponents();
329 for (int i = 0; i < children.length; i++)
330 children[i].setBounds(offsetsX[i], offsetsY[i], spansX[i], spansY[i]);
334 * Makes sure that the xChildren and yChildren fields are correctly set up.
335 * A call to {@link #invalidateLayout(Container)} sets these fields to null,
336 * so they have to be set up again.
338 private void checkRequirements()
340 if (xChildren == null || yChildren == null)
342 Component[] children = target.getComponents();
343 xChildren = new SizeRequirements[children.length];
344 yChildren = new SizeRequirements[children.length];
345 for (int i = 0; i < children.length; i++)
347 if (! children[i].isVisible())
349 xChildren[i] = new SizeRequirements();
350 yChildren[i] = new SizeRequirements();
352 else
354 xChildren[i] =
355 new SizeRequirements(children[i].getMinimumSize().width,
356 children[i].getPreferredSize().width,
357 children[i].getMaximumSize().width,
358 children[i].getAlignmentX());
359 yChildren[i] =
360 new SizeRequirements(children[i].getMinimumSize().height,
361 children[i].getPreferredSize().height,
362 children[i].getMaximumSize().height,
363 children[i].getAlignmentY());
370 * Makes sure that the xTotal and yTotal fields are set up correctly. A call
371 * to {@link #invalidateLayout} sets these fields to null and they have to be
372 * recomputed.
374 private void checkTotalRequirements()
376 if (xTotal == null || yTotal == null)
378 checkRequirements();
379 xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
380 yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
385 * Makes sure that the offsetsX, offsetsY, spansX and spansY fields are set
386 * up correctly. A call to {@link #invalidateLayout} sets these fields
387 * to null and they have to be recomputed.
389 private void checkLayout()
391 if (offsetsX == null || offsetsY == null || spansX == null
392 || spansY == null)
394 checkRequirements();
395 checkTotalRequirements();
396 int len = target.getComponents().length;
397 offsetsX = new int[len];
398 offsetsY = new int[len];
399 spansX = new int[len];
400 spansY = new int[len];
402 Insets in = target.getInsets();
403 int width = target.getWidth() - in.left - in.right;
404 int height = target.getHeight() - in.top - in.bottom;
406 SizeRequirements.calculateAlignedPositions(width, xTotal,
407 xChildren, offsetsX, spansX);
408 SizeRequirements.calculateAlignedPositions(height, yTotal,
409 yChildren, offsetsY, spansY);