< prev index next >

src/java.desktop/share/classes/javax/swing/ViewportLayout.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.AWTError;
  29 import java.awt.LayoutManager;
  30 import java.awt.Component;
  31 import java.awt.Container;
  32 import java.awt.Rectangle;
  33 import java.awt.Point;
  34 import java.awt.Dimension;
  35 import java.awt.Insets;
  36 import java.io.Serializable;
  37 
  38 /**
  39  * The default layout manager for <code>JViewport</code>.
  40  * <code>ViewportLayout</code> defines
  41  * a policy for layout that should be useful for most applications.
  42  * The viewport makes its view the same size as the viewport,
  43  * however it will not make the view smaller than its minimum size.
  44  * As the viewport grows the view is kept bottom justified until
  45  * the entire view is visible, subsequently the view is kept top
  46  * justified.
  47  * <p>
  48  * <strong>Warning:</strong>
  49  * Serialized objects of this class will not be compatible with
  50  * future Swing releases. The current serialization support is
  51  * appropriate for short term storage or RMI between applications running
  52  * the same version of Swing.  As of 1.4, support for long term storage
  53  * of all JavaBeans&trade;
  54  * has been added to the <code>java.beans</code> package.
  55  * Please see {@link java.beans.XMLEncoder}.
  56  *
  57  * @author Hans Muller
  58  * @since 1.2
  59  */
  60 @SuppressWarnings("serial") // Same-version serialization only
  61 public class ViewportLayout implements LayoutManager, Serializable
  62 {
  63     // Single instance used by JViewport.
  64     static ViewportLayout SHARED_INSTANCE = new ViewportLayout();
  65 
  66     /**
  67      * Adds the specified component to the layout. Not used by this class.
  68      * @param name the name of the component
  69      * @param c the component to be added
  70      */
  71     public void addLayoutComponent(String name, Component c) { }
  72 
  73     /**
  74      * Removes the specified component from the layout. Not used by
  75      * this class.
  76      * @param c the component to remove
  77      */
  78     public void removeLayoutComponent(Component c) { }
  79 
  80 
  81     /**
  82      * Returns the preferred dimensions for this layout given the components
  83      * in the specified target container.
  84      * @param parent the component which needs to be laid out
  85      * @return a <code>Dimension</code> object containing the
  86      *          preferred dimensions
  87      * @see #minimumLayoutSize
  88      */
  89     public Dimension preferredLayoutSize(Container parent) {
  90         Component view = ((JViewport)parent).getView();
  91         if (view == null) {
  92             return new Dimension(0, 0);
  93         }
  94         else if (view instanceof Scrollable) {
  95             return ((Scrollable)view).getPreferredScrollableViewportSize();
  96         }
  97         else {
  98             return view.getPreferredSize();
  99         }
 100     }
 101 
 102 
 103     /**
 104      * Returns the minimum dimensions needed to layout the components
 105      * contained in the specified target container.
 106      *
 107      * @param parent the component which needs to be laid out
 108      * @return a <code>Dimension</code> object containing the minimum
 109      *          dimensions
 110      * @see #preferredLayoutSize
 111      */
 112     public Dimension minimumLayoutSize(Container parent) {
 113         return new Dimension(4, 4);
 114     }
 115 
 116 
 117     /**
 118      * Called by the AWT when the specified container needs to be laid out.
 119      *
 120      * @param parent  the container to lay out
 121      *
 122      * @throws AWTError if the target isn't the container specified to the
 123      *                      <code>BoxLayout</code> constructor
 124      */
 125     public void layoutContainer(Container parent)
 126     {
 127         JViewport vp = (JViewport)parent;
 128         Component view = vp.getView();
 129         Scrollable scrollableView = null;
 130 
 131         if (view == null) {
 132             return;
 133         }
 134         else if (view instanceof Scrollable) {
 135             scrollableView = (Scrollable) view;
 136         }
 137 
 138         /* All of the dimensions below are in view coordinates, except
 139          * vpSize which we're converting.
 140          */
 141 
 142         Insets insets = vp.getInsets();
 143         Dimension viewPrefSize = view.getPreferredSize();




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.AWTError;
  29 import java.awt.LayoutManager;
  30 import java.awt.Component;
  31 import java.awt.Container;
  32 import java.awt.Rectangle;
  33 import java.awt.Point;
  34 import java.awt.Dimension;
  35 import java.awt.Insets;
  36 import java.io.Serializable;
  37 
  38 /**
  39  * The default layout manager for {@code JViewport}.
  40  * {@code ViewportLayout} defines
  41  * a policy for layout that should be useful for most applications.
  42  * The viewport makes its view the same size as the viewport,
  43  * however it will not make the view smaller than its minimum size.
  44  * As the viewport grows the view is kept bottom justified until
  45  * the entire view is visible, subsequently the view is kept top
  46  * justified.
  47  * <p>
  48  * <strong>Warning:</strong>
  49  * Serialized objects of this class will not be compatible with
  50  * future Swing releases. The current serialization support is
  51  * appropriate for short term storage or RMI between applications running
  52  * the same version of Swing.  As of 1.4, support for long term storage
  53  * of all JavaBeans&trade;
  54  * has been added to the {@code java.beans} package.
  55  * Please see {@link java.beans.XMLEncoder}.
  56  *
  57  * @author Hans Muller
  58  * @since 1.2
  59  */
  60 @SuppressWarnings("serial") // Same-version serialization only
  61 public class ViewportLayout implements LayoutManager, Serializable
  62 {
  63     // Single instance used by JViewport.
  64     static ViewportLayout SHARED_INSTANCE = new ViewportLayout();
  65 
  66     /**
  67      * Adds the specified component to the layout. Not used by this class.
  68      * @param name the name of the component
  69      * @param c the component to be added
  70      */
  71     public void addLayoutComponent(String name, Component c) { }
  72 
  73     /**
  74      * Removes the specified component from the layout. Not used by
  75      * this class.
  76      * @param c the component to remove
  77      */
  78     public void removeLayoutComponent(Component c) { }
  79 
  80 
  81     /**
  82      * Returns the preferred dimensions for this layout given the components
  83      * in the specified target container.
  84      * @param parent the component which needs to be laid out
  85      * @return a {@code Dimension} object containing the
  86      *          preferred dimensions
  87      * @see #minimumLayoutSize
  88      */
  89     public Dimension preferredLayoutSize(Container parent) {
  90         Component view = ((JViewport)parent).getView();
  91         if (view == null) {
  92             return new Dimension(0, 0);
  93         }
  94         else if (view instanceof Scrollable) {
  95             return ((Scrollable)view).getPreferredScrollableViewportSize();
  96         }
  97         else {
  98             return view.getPreferredSize();
  99         }
 100     }
 101 
 102 
 103     /**
 104      * Returns the minimum dimensions needed to layout the components
 105      * contained in the specified target container.
 106      *
 107      * @param parent the component which needs to be laid out
 108      * @return a {@code Dimension} object containing the minimum
 109      *          dimensions
 110      * @see #preferredLayoutSize
 111      */
 112     public Dimension minimumLayoutSize(Container parent) {
 113         return new Dimension(4, 4);
 114     }
 115 
 116 
 117     /**
 118      * Called by the AWT when the specified container needs to be laid out.
 119      *
 120      * @param parent  the container to lay out
 121      *
 122      * @throws AWTError if the target isn't the container specified to the
 123      *                      {@code BoxLayout} constructor
 124      */
 125     public void layoutContainer(Container parent)
 126     {
 127         JViewport vp = (JViewport)parent;
 128         Component view = vp.getView();
 129         Scrollable scrollableView = null;
 130 
 131         if (view == null) {
 132             return;
 133         }
 134         else if (view instanceof Scrollable) {
 135             scrollableView = (Scrollable) view;
 136         }
 137 
 138         /* All of the dimensions below are in view coordinates, except
 139          * vpSize which we're converting.
 140          */
 141 
 142         Insets insets = vp.getInsets();
 143         Dimension viewPrefSize = view.getPreferredSize();


< prev index next >