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