1. /*
  2. * @(#)ViewportLayout.java 1.36 03/01/23
  3. *
  4. * Copyright 2003 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.LayoutManager;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.Rectangle;
  12. import java.awt.Point;
  13. import java.awt.Dimension;
  14. import java.awt.Insets;
  15. import java.io.Serializable;
  16. /**
  17. * The default layout manager for <code>JViewport</code>.
  18. * <code>ViewportLayout</code> defines
  19. * a policy for layout that should be useful for most applications.
  20. * The viewport makes its view the same size as the viewport,
  21. * however it will not make the view smaller than its minimum size.
  22. * As the viewport grows the view is kept bottom justified until
  23. * the entire view is visible, subsequently the view is kept top
  24. * justified.
  25. * <p>
  26. * <strong>Warning:</strong>
  27. * Serialized objects of this class will not be compatible with
  28. * future Swing releases. The current serialization support is
  29. * appropriate for short term storage or RMI between applications running
  30. * the same version of Swing. As of 1.4, support for long term storage
  31. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  32. * has been added to the <code>java.beans</code> package.
  33. * Please see {@link java.beans.XMLEncoder}.
  34. *
  35. * @version 1.36 01/23/03
  36. * @author Hans Muller
  37. */
  38. public class ViewportLayout implements LayoutManager, Serializable
  39. {
  40. /**
  41. * Adds the specified component to the layout. Not used by this class.
  42. * @param name the name of the component
  43. * @param c the the component to be added
  44. */
  45. public void addLayoutComponent(String name, Component c) { }
  46. /**
  47. * Removes the specified component from the layout. Not used by
  48. * this class.
  49. * @param c the component to remove
  50. */
  51. public void removeLayoutComponent(Component c) { }
  52. /**
  53. * Returns the preferred dimensions for this layout given the components
  54. * in the specified target container.
  55. * @param parent the component which needs to be laid out
  56. * @return a <code>Dimension</code> object containing the
  57. * preferred dimensions
  58. * @see #minimumLayoutSize
  59. */
  60. public Dimension preferredLayoutSize(Container parent) {
  61. Component view = ((JViewport)parent).getView();
  62. if (view == null) {
  63. return new Dimension(0, 0);
  64. }
  65. else if (view instanceof Scrollable) {
  66. return ((Scrollable)view).getPreferredScrollableViewportSize();
  67. }
  68. else {
  69. return view.getPreferredSize();
  70. }
  71. }
  72. /**
  73. * Returns the minimum dimensions needed to layout the components
  74. * contained in the specified target container.
  75. *
  76. * @param parent the component which needs to be laid out
  77. * @return a <code>Dimension</code> object containing the minimum
  78. * dimensions
  79. * @see #preferredLayoutSize
  80. */
  81. public Dimension minimumLayoutSize(Container parent) {
  82. return new Dimension(4, 4);
  83. }
  84. /**
  85. * Called by the AWT when the specified container needs to be laid out.
  86. *
  87. * @param parent the container to lay out
  88. *
  89. * @exception AWTError if the target isn't the container specified to the
  90. * <code>BoxLayout</code> constructor
  91. */
  92. public void layoutContainer(Container parent)
  93. {
  94. JViewport vp = (JViewport)parent;
  95. Component view = vp.getView();
  96. Scrollable scrollableView = null;
  97. if (view == null) {
  98. return;
  99. }
  100. else if (view instanceof Scrollable) {
  101. scrollableView = (Scrollable) view;
  102. }
  103. /* All of the dimensions below are in view coordinates, except
  104. * vpSize which we're converting.
  105. */
  106. Insets insets = vp.getInsets();
  107. Dimension viewPrefSize = view.getPreferredSize();
  108. Dimension vpSize = vp.getSize();
  109. Dimension extentSize = vp.toViewCoordinates(vpSize);
  110. Dimension viewSize = new Dimension(viewPrefSize);
  111. if (scrollableView != null) {
  112. if (scrollableView.getScrollableTracksViewportWidth()) {
  113. viewSize.width = vpSize.width;
  114. }
  115. if (scrollableView.getScrollableTracksViewportHeight()) {
  116. viewSize.height = vpSize.height;
  117. }
  118. }
  119. Point viewPosition = vp.getViewPosition();
  120. /* If the new viewport size would leave empty space to the
  121. * right of the view, right justify the view or left justify
  122. * the view when the width of the view is smaller than the
  123. * container.
  124. */
  125. if (scrollableView == null ||
  126. vp.getParent() == null ||
  127. vp.getParent().getComponentOrientation().isLeftToRight()) {
  128. if ((viewPosition.x + extentSize.width) > viewSize.width) {
  129. viewPosition.x = Math.max(0, viewSize.width - extentSize.width);
  130. }
  131. } else {
  132. if (extentSize.width > viewSize.width) {
  133. viewPosition.x = viewSize.width - extentSize.width;
  134. } else {
  135. viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x));
  136. }
  137. }
  138. /* If the new viewport size would leave empty space below the
  139. * view, bottom justify the view or top justify the view when
  140. * the height of the view is smaller than the container.
  141. */
  142. if ((viewPosition.y + extentSize.height) > viewSize.height) {
  143. viewPosition.y = Math.max(0, viewSize.height - extentSize.height);
  144. }
  145. /* If we haven't been advised about how the viewports size
  146. * should change wrt to the viewport, i.e. if the view isn't
  147. * an instance of Scrollable, then adjust the views size as follows.
  148. *
  149. * If the origin of the view is showing and the viewport is
  150. * bigger than the views preferred size, then make the view
  151. * the same size as the viewport.
  152. */
  153. if (scrollableView == null) {
  154. if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
  155. viewSize.width = vpSize.width;
  156. }
  157. if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
  158. viewSize.height = vpSize.height;
  159. }
  160. }
  161. vp.setViewPosition(viewPosition);
  162. vp.setViewSize(viewSize);
  163. }
  164. }