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