1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/* * @(#)OverlayLayout.java 1.29 06/04/10 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing; import java.awt.*; import java.io.Serializable; /** * A layout manager to arrange components over the top * of each other. The requested size of the container * will be the largest requested size of the children, * taking alignment needs into consideration. * * The alignment is based upon what is needed to properly * fit the children in the allocation area. The children * will be placed such that their alignment points are all * on top of each other. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @version 1.29 04/10/06 * @author Timothy Prinzing */ public class OverlayLayout implements LayoutManager2,Serializable { /** * Constructs a layout manager that performs overlay * arrangement of the children. The layout manager * created is dedicated to the given container. * * @param target the container to do layout against */ public OverlayLayout(Container target) { this.target = target; } /** * Returns the container that uses this layout manager. * * @return the container that uses this layout manager * * @since 1.6 */ public final Container getTarget() { return this.target; } /** * Indicates a child has changed its layout related information, * which causes any cached calculations to be flushed. * * @param target the container */ public void invalidateLayout(Container target) { checkContainer(target); xChildren = null; yChildren = null; xTotal = null; yTotal = null; } /** * Adds the specified component to the layout. Used by * this class to know when to invalidate layout. * * @param name the name of the component * @param comp the the component to be added */ public void addLayoutComponent(String name, Component comp) { invalidateLayout(comp.getParent()); } /** * Removes the specified component from the layout. Used by * this class to know when to invalidate layout. * * @param comp the component to remove */ public void removeLayoutComponent(Component comp) { invalidateLayout(comp.getParent()); } /** * Adds the specified component to the layout, using the specified * constraint object. Used by this class to know when to invalidate * layout. * * @param comp the component to be added * @param constraints where/how the component is added to the layout. */ public void addLayoutComponent(Component comp, Object constraints) { invalidateLayout(comp.getParent()); } /** * Returns the preferred dimensions for this layout given the components * in the specified target container. Recomputes the layout if it * has been invalidated. Factors in the current inset setting returned * by getInsets(). * * @param target the component which needs to be laid out * @return a Dimension object containing the preferred dimensions * @see #minimumLayoutSize */ public Dimension preferredLayoutSize(Container target) { checkContainer(target); checkRequests(); Dimension size = new Dimension(xTotal.preferred, yTotal.preferred); Insets insets = target.getInsets(); size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; return size; } /** * Returns the minimum dimensions needed to lay out the components * contained in the specified target container. Recomputes the layout * if it has been invalidated, and factors in the current inset setting. * * @param target the component which needs to be laid out * @return a Dimension object containing the minimum dimensions * @see #preferredLayoutSize */ public Dimension minimumLayoutSize(Container target) { checkContainer(target); checkRequests(); Dimension size = new Dimension(xTotal.minimum, yTotal.minimum); Insets insets = target.getInsets(); size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; return size; } /** * Returns the maximum dimensions needed to lay out the components * contained in the specified target container. Recomputes the * layout if it has been invalidated, and factors in the inset setting * returned by <code>getInset</code>. * * @param target the component that needs to be laid out * @return a <code>Dimension</code> object containing the maximum * dimensions * @see #preferredLayoutSize */ public Dimension maximumLayoutSize(Container target) { checkContainer(target); checkRequests(); Dimension size = new Dimension(xTotal.maximum, yTotal.maximum); Insets insets = target.getInsets(); size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; return size; } /** * Returns the alignment along the x axis for the container. * * @param target the container * @return the alignment >= 0.0f && <= 1.0f */ public float getLayoutAlignmentX(Container target) { checkContainer(target); checkRequests(); return xTotal.alignment; } /** * Returns the alignment along the y axis for the container. * * @param target the container * @return the alignment >= 0.0f && <= 1.0f */ public float getLayoutAlignmentY(Container target) { checkContainer(target); checkRequests(); return yTotal.alignment; } /** * Called by the AWT when the specified container needs to be laid out. * * @param target the container to lay out * * @exception AWTError if the target isn't the container specified to the * constructor */ public void layoutContainer(Container target) { checkContainer(target); checkRequests(); int nChildren = target.getComponentCount(); int[] xOffsets = new int[nChildren]; int[] xSpans = new int[nChildren]; int[] yOffsets = new int[nChildren]; int[] ySpans = new int[nChildren]; // determine the child placements Dimension alloc = target.getSize(); Insets in = target.getInsets(); alloc.width -= in.left + in.right; alloc.height -= in.top + in.bottom; SizeRequirements.calculateAlignedPositions(alloc.width, xTotal, xChildren, xOffsets, xSpans); SizeRequirements.calculateAlignedPositions(alloc.height, yTotal, yChildren, yOffsets, ySpans); // flush changes to the container for (int i = 0; i < nChildren; i++) { Component c = target.getComponent(i); c.setBounds(in.left + xOffsets[i], in.top + yOffsets[i], xSpans[i], ySpans[i]); } } void checkContainer(Container target) { if (this.target != target) { throw new AWTError("OverlayLayout can't be shared"); } } void checkRequests() { if (xChildren == null || yChildren == null) { // The requests have been invalidated... recalculate // the request information. int n = target.getComponentCount(); xChildren = new SizeRequirements[n]; yChildren = new SizeRequirements[n]; for (int i = 0; i < n; i++) { Component c = target.getComponent(i); Dimension min = c.getMinimumSize(); Dimension typ = c.getPreferredSize(); Dimension max = c.getMaximumSize(); xChildren[i] = new SizeRequirements(min.width, typ.width, max.width, c.getAlignmentX()); yChildren[i] = new SizeRequirements(min.height, typ.height, max.height, c.getAlignmentY()); } xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren); yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren); } } private Container target; private SizeRequirements[] xChildren; private SizeRequirements[] yChildren; private SizeRequirements xTotal; private SizeRequirements yTotal; }