KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > OverlayLayout


1 /*
2  * @(#)OverlayLayout.java 1.27 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9
10 import java.awt.*;
11 import java.io.Serializable JavaDoc;
12
13 /**
14  * A layout manager to arrange components over the top
15  * of each other. The requested size of the container
16  * will be the largest requested size of the children,
17  * taking alignment needs into consideration.
18  *
19  * The alignment is based upon what is needed to properly
20  * fit the children in the allocation area. The children
21  * will be placed such that their alignment points are all
22  * on top of each other.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @version 1.27 12/19/03
34  * @author Timothy Prinzing
35  */

36 public class OverlayLayout implements LayoutManager2,Serializable JavaDoc {
37
38     /**
39      * Constructs a layout manager that performs overlay
40      * arrangement of the children. The layout manager
41      * created is dedicated to the given container.
42      *
43      * @param target the container to do layout against
44      */

45     public OverlayLayout(Container target) {
46     this.target = target;
47     }
48
49     /**
50      * Indicates a child has changed its layout related information,
51      * which causes any cached calculations to be flushed.
52      *
53      * @param target the container
54      */

55     public void invalidateLayout(Container target) {
56     checkContainer(target);
57     xChildren = null;
58     yChildren = null;
59     xTotal = null;
60     yTotal = null;
61     }
62
63     /**
64      * Adds the specified component to the layout. Used by
65      * this class to know when to invalidate layout.
66      *
67      * @param name the name of the component
68      * @param comp the the component to be added
69      */

70     public void addLayoutComponent(String JavaDoc name, Component comp) {
71         invalidateLayout(comp.getParent());
72     }
73
74     /**
75      * Removes the specified component from the layout. Used by
76      * this class to know when to invalidate layout.
77      *
78      * @param comp the component to remove
79      */

80     public void removeLayoutComponent(Component comp) {
81         invalidateLayout(comp.getParent());
82     }
83
84     /**
85      * Adds the specified component to the layout, using the specified
86      * constraint object. Used by this class to know when to invalidate
87      * layout.
88      *
89      * @param comp the component to be added
90      * @param constraints where/how the component is added to the layout.
91      */

92     public void addLayoutComponent(Component comp, Object JavaDoc constraints) {
93         invalidateLayout(comp.getParent());
94     }
95
96     /**
97      * Returns the preferred dimensions for this layout given the components
98      * in the specified target container. Recomputes the layout if it
99      * has been invalidated. Factors in the current inset setting returned
100      * by getInsets().
101      *
102      * @param target the component which needs to be laid out
103      * @return a Dimension object containing the preferred dimensions
104      * @see #minimumLayoutSize
105      */

106     public Dimension preferredLayoutSize(Container target) {
107     checkContainer(target);
108     checkRequests();
109
110     Dimension size = new Dimension(xTotal.preferred, yTotal.preferred);
111     Insets insets = target.getInsets();
112     size.width += insets.left + insets.right;
113     size.height += insets.top + insets.bottom;
114     return size;
115     }
116
117     /**
118      * Returns the minimum dimensions needed to lay out the components
119      * contained in the specified target container. Recomputes the layout
120      * if it has been invalidated, and factors in the current inset setting.
121      *
122      * @param target the component which needs to be laid out
123      * @return a Dimension object containing the minimum dimensions
124      * @see #preferredLayoutSize
125      */

126     public Dimension minimumLayoutSize(Container target) {
127     checkContainer(target);
128     checkRequests();
129
130     Dimension size = new Dimension(xTotal.minimum, yTotal.minimum);
131     Insets insets = target.getInsets();
132     size.width += insets.left + insets.right;
133     size.height += insets.top + insets.bottom;
134     return size;
135     }
136
137     /**
138      * Returns the maximum dimensions needed to lay out the components
139      * contained in the specified target container. Recomputes the
140      * layout if it has been invalidated, and factors in the inset setting
141      * returned by <code>getInset</code>.
142      *
143      * @param target the component that needs to be laid out
144      * @return a <code>Dimension</code> object containing the maximum
145      * dimensions
146      * @see #preferredLayoutSize
147      */

148     public Dimension maximumLayoutSize(Container target) {
149     checkContainer(target);
150     checkRequests();
151
152     Dimension size = new Dimension(xTotal.maximum, yTotal.maximum);
153     Insets insets = target.getInsets();
154     size.width += insets.left + insets.right;
155     size.height += insets.top + insets.bottom;
156     return size;
157     }
158
159     /**
160      * Returns the alignment along the x axis for the container.
161      *
162      * @param target the container
163      * @return the alignment >= 0.0f && <= 1.0f
164      */

165     public float getLayoutAlignmentX(Container target) {
166     checkContainer(target);
167     checkRequests();
168     return xTotal.alignment;
169     }
170
171     /**
172      * Returns the alignment along the y axis for the container.
173      *
174      * @param target the container
175      * @return the alignment >= 0.0f && <= 1.0f
176      */

177     public float getLayoutAlignmentY(Container target) {
178     checkContainer(target);
179     checkRequests();
180     return yTotal.alignment;
181     }
182
183     /**
184      * Called by the AWT when the specified container needs to be laid out.
185      *
186      * @param target the container to lay out
187      *
188      * @exception AWTError if the target isn't the container specified to the
189      * constructor
190      */

191     public void layoutContainer(Container target) {
192     checkContainer(target);
193     checkRequests();
194     
195     int nChildren = target.getComponentCount();
196     int[] xOffsets = new int[nChildren];
197     int[] xSpans = new int[nChildren];
198     int[] yOffsets = new int[nChildren];
199     int[] ySpans = new int[nChildren];
200
201     // determine the child placements
202
Dimension alloc = target.getSize();
203     Insets in = target.getInsets();
204     alloc.width -= in.left + in.right;
205     alloc.height -= in.top + in.bottom;
206     SizeRequirements.calculateAlignedPositions(alloc.width, xTotal,
207                            xChildren, xOffsets,
208                            xSpans);
209     SizeRequirements.calculateAlignedPositions(alloc.height, yTotal,
210                            yChildren, yOffsets,
211                            ySpans);
212
213     // flush changes to the container
214
for (int i = 0; i < nChildren; i++) {
215         Component c = target.getComponent(i);
216         c.setBounds(in.left + xOffsets[i], in.top + yOffsets[i],
217             xSpans[i], ySpans[i]);
218     }
219     }
220
221     void checkContainer(Container target) {
222     if (this.target != target) {
223         throw new AWTError("OverlayLayout can't be shared");
224     }
225     }
226     
227     void checkRequests() {
228     if (xChildren == null || yChildren == null) {
229         // The requests have been invalidated... recalculate
230
// the request information.
231
int n = target.getComponentCount();
232         xChildren = new SizeRequirements JavaDoc[n];
233         yChildren = new SizeRequirements JavaDoc[n];
234         for (int i = 0; i < n; i++) {
235         Component c = target.getComponent(i);
236         Dimension min = c.getMinimumSize();
237         Dimension typ = c.getPreferredSize();
238         Dimension max = c.getMaximumSize();
239         xChildren[i] = new SizeRequirements JavaDoc(min.width, typ.width,
240                             max.width,
241                             c.getAlignmentX());
242         yChildren[i] = new SizeRequirements JavaDoc(min.height, typ.height,
243                             max.height,
244                             c.getAlignmentY());
245         }
246         
247         xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
248         yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
249     }
250     }
251         
252     private Container target;
253     private SizeRequirements JavaDoc[] xChildren;
254     private SizeRequirements JavaDoc[] yChildren;
255     private SizeRequirements JavaDoc xTotal;
256     private SizeRequirements JavaDoc yTotal;
257     
258 }
259
260
Popular Tags