1. /*
  2. * @(#)ViewportLayout.java 1.38 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. 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.38 12/19/03
  36. * @author Hans Muller
  37. */
  38. public class ViewportLayout implements LayoutManager, Serializable
  39. {
  40. // Single instance used by JViewport.
  41. static ViewportLayout SHARED_INSTANCE = new ViewportLayout();
  42. /**
  43. * Adds the specified component to the layout. Not used by this class.
  44. * @param name the name of the component
  45. * @param c the the component to be added
  46. */
  47. public void addLayoutComponent(String name, Component c) { }
  48. /**
  49. * Removes the specified component from the layout. Not used by
  50. * this class.
  51. * @param c the component to remove
  52. */
  53. public void removeLayoutComponent(Component c) { }
  54. /**
  55. * Returns the preferred dimensions for this layout given the components
  56. * in the specified target container.
  57. * @param parent the component which needs to be laid out
  58. * @return a <code>Dimension</code> object containing the
  59. * preferred dimensions
  60. * @see #minimumLayoutSize
  61. */
  62. public Dimension preferredLayoutSize(Container parent) {
  63. Component view = ((JViewport)parent).getView();
  64. if (view == null) {
  65. return new Dimension(0, 0);
  66. }
  67. else if (view instanceof Scrollable) {
  68. return ((Scrollable)view).getPreferredScrollableViewportSize();
  69. }
  70. else {
  71. return view.getPreferredSize();
  72. }
  73. }
  74. /**
  75. * Returns the minimum dimensions needed to layout the components
  76. * contained in the specified target container.
  77. *
  78. * @param parent the component which needs to be laid out
  79. * @return a <code>Dimension</code> object containing the minimum
  80. * dimensions
  81. * @see #preferredLayoutSize
  82. */
  83. public Dimension minimumLayoutSize(Container parent) {
  84. return new Dimension(4, 4);
  85. }
  86. /**
  87. * Called by the AWT when the specified container needs to be laid out.
  88. *
  89. * @param parent the container to lay out
  90. *
  91. * @exception AWTError if the target isn't the container specified to the
  92. * <code>BoxLayout</code> constructor
  93. */
  94. public void layoutContainer(Container parent)
  95. {
  96. JViewport vp = (JViewport)parent;
  97. Component view = vp.getView();
  98. Scrollable scrollableView = null;
  99. if (view == null) {
  100. return;
  101. }
  102. else if (view instanceof Scrollable) {
  103. scrollableView = (Scrollable) view;
  104. }
  105. /* All of the dimensions below are in view coordinates, except
  106. * vpSize which we're converting.
  107. */
  108. Insets insets = vp.getInsets();
  109. Dimension viewPrefSize = view.getPreferredSize();
  110. Dimension vpSize = vp.getSize();
  111. Dimension extentSize = vp.toViewCoordinates(vpSize);
  112. Dimension viewSize = new Dimension(viewPrefSize);
  113. if (scrollableView != null) {
  114. if (scrollableView.getScrollableTracksViewportWidth()) {
  115. viewSize.width = vpSize.width;
  116. }
  117. if (scrollableView.getScrollableTracksViewportHeight()) {
  118. viewSize.height = vpSize.height;
  119. }
  120. }
  121. Point viewPosition = vp.getViewPosition();
  122. /* If the new viewport size would leave empty space to the
  123. * right of the view, right justify the view or left justify
  124. * the view when the width of the view is smaller than the
  125. * container.
  126. */
  127. if (scrollableView == null ||
  128. vp.getParent() == null ||
  129. vp.getParent().getComponentOrientation().isLeftToRight()) {
  130. if ((viewPosition.x + extentSize.width) > viewSize.width) {
  131. viewPosition.x = Math.max(0, viewSize.width - extentSize.width);
  132. }
  133. } else {
  134. if (extentSize.width > viewSize.width) {
  135. viewPosition.x = viewSize.width - extentSize.width;
  136. } else {
  137. viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x));
  138. }
  139. }
  140. /* If the new viewport size would leave empty space below the
  141. * view, bottom justify the view or top justify the view when
  142. * the height of the view is smaller than the container.
  143. */
  144. if ((viewPosition.y + extentSize.height) > viewSize.height) {
  145. viewPosition.y = Math.max(0, viewSize.height - extentSize.height);
  146. }
  147. /* If we haven't been advised about how the viewports size
  148. * should change wrt to the viewport, i.e. if the view isn't
  149. * an instance of Scrollable, then adjust the views size as follows.
  150. *
  151. * If the origin of the view is showing and the viewport is
  152. * bigger than the views preferred size, then make the view
  153. * the same size as the viewport.
  154. */
  155. if (scrollableView == null) {
  156. if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
  157. viewSize.width = vpSize.width;
  158. }
  159. if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
  160. viewSize.height = vpSize.height;
  161. }
  162. }
  163. vp.setViewPosition(viewPosition);
  164. vp.setViewSize(viewSize);
  165. }
  166. }