1 /*
   2  * Copyright (c) 1995, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  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 sun.awt;
  27 
  28 import java.awt.*;
  29 
  30 
  31 /**
  32  * A vertical 'bag' of Components.  Allocates space for each Component from
  33  * top to bottom.
  34  *
  35  * @author      Herb Jellinek
  36  */
  37 public class VerticalBagLayout implements LayoutManager {
  38 
  39     int vgap;
  40 
  41     /**
  42      * Constructs a new VerticalBagLayout.
  43      */
  44     public VerticalBagLayout() {
  45         this(0);
  46     }
  47 
  48     /**
  49      * Constructs a VerticalBagLayout with the specified gaps.
  50      * @param vgap the vertical gap
  51      */
  52     public VerticalBagLayout(int vgap) {
  53         this.vgap = vgap;
  54     }
  55 
  56     /**
  57      * Adds the specified named component to the layout.
  58      * @param name the String name
  59      * @param comp the component to be added
  60      */
  61     public void addLayoutComponent(String name, Component comp) {
  62     }
  63 
  64     /**
  65      * Removes the specified component from the layout.
  66      * @param comp the component to be removed
  67      */
  68     public void removeLayoutComponent(Component comp) {
  69     }
  70 
  71     /**
  72      * Returns the minimum dimensions needed to lay out the components
  73      * contained in the specified target container.
  74      * @param target the Container on which to do the layout
  75      * @see Container
  76      * @see #preferredLayoutSize
  77      */
  78     public Dimension minimumLayoutSize(Container target) {
  79         Dimension dim = new Dimension();
  80         int nmembers = target.countComponents();
  81 
  82         for (int i = 0; i < nmembers; i++) {
  83             Component comp = target.getComponent(i);
  84             if (comp.isVisible()) {
  85                 Dimension d = comp.minimumSize();
  86                 dim.width = Math.max(d.width, dim.width);
  87                 dim.height += d.height + vgap;
  88             }
  89         }
  90 
  91         Insets insets = target.insets();
  92         dim.width += insets.left + insets.right;
  93         dim.height += insets.top + insets.bottom;
  94 
  95         return dim;
  96     }
  97 
  98     /**
  99      * Returns the preferred dimensions for this layout given the components
 100      * in the specified target container.
 101      * @param target the component which needs to be laid out
 102      * @see Container
 103      * @see #minimumLayoutSize
 104      */
 105     public Dimension preferredLayoutSize(Container target) {
 106         Dimension dim = new Dimension();
 107         int nmembers = target.countComponents();
 108 
 109         for (int i = 0; i < nmembers; i++) {
 110             Component comp = target.getComponent(i);
 111             if (true || comp.isVisible()) {
 112                 Dimension d = comp.preferredSize();
 113                 dim.width = Math.max(d.width, dim.width);
 114                 dim.height += d.height + vgap;
 115             }
 116         }
 117 
 118         Insets insets = target.insets();
 119         dim.width += insets.left + insets.right;
 120         dim.height += insets.top + insets.bottom;
 121 
 122         return dim;
 123     }
 124 
 125     /**
 126      * Lays out the specified container. This method will actually reshape the
 127      * components in the specified target container in order to satisfy the
 128      * constraints of the VerticalBagLayout object.
 129      * @param target the component being laid out
 130      * @see Container
 131      */
 132     public void layoutContainer(Container target) {
 133         Insets insets = target.insets();
 134         int top = insets.top;
 135         int bottom = target.size().height - insets.bottom;
 136         int left = insets.left;
 137         int right = target.size().width - insets.right;
 138         int nmembers = target.countComponents();
 139 
 140         for (int i = 0; i < nmembers; i++) {
 141             Component comp = target.getComponent(i);
 142             if (comp.isVisible()) {
 143                 int compHeight = comp.size().height;
 144                 comp.resize(right - left, compHeight);
 145                 Dimension d = comp.preferredSize();
 146                 comp.reshape(left, top, right - left, d.height);
 147                 top += d.height + vgap;
 148             }
 149         }
 150     }
 151 
 152     /**
 153      * Returns the String representation of this VerticalBagLayout's values.
 154      */
 155     public String toString() {
 156         return getClass().getName() + "[vgap=" + vgap + "]";
 157     }
 158 }