1 /*
   2  * Copyright (c) 1995, 2007, 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  * A horizontal 'bag' of Components.  Allocates space for each Component
  32  * from left to right.
  33  *
  34  * @author      Herb Jellinek
  35  */
  36 public class HorizBagLayout implements LayoutManager {
  37 
  38     int hgap;
  39 
  40     /**
  41      * Constructs a new HorizBagLayout.
  42      */
  43     public HorizBagLayout() {
  44         this(0);
  45     }
  46 
  47     /**
  48      * Constructs a HorizBagLayout with the specified gaps.
  49      * @param hgap the horizontal gap
  50      */
  51     public HorizBagLayout(int hgap) {
  52         this.hgap = hgap;
  53     }
  54 
  55     /**
  56      * Adds the specified named component to the layout.
  57      * @param name the String name
  58      * @param comp the component to be added
  59      */
  60     public void addLayoutComponent(String name, Component comp) {
  61     }
  62 
  63     /**
  64      * Removes the specified component from the layout.
  65      * @param comp the component to be removed
  66      */
  67     public void removeLayoutComponent(Component comp) {
  68     }
  69 
  70     /**
  71      * Returns the minimum dimensions needed to lay out the components
  72      * contained in the specified target container.
  73      * @param target the Container on which to do the layout
  74      * @see Container
  75      * @see #preferredLayoutSize
  76      */
  77     public Dimension minimumLayoutSize(Container target) {
  78         Dimension dim = new Dimension();
  79 
  80         for (int i = 0; i < target.countComponents(); i++) {
  81             Component comp = target.getComponent(i);
  82             if (comp.isVisible()) {
  83                 Dimension d = comp.minimumSize();
  84                 dim.width += d.width + hgap;
  85                 dim.height = Math.max(d.height, dim.height);
  86             }
  87         }
  88 
  89         Insets insets = target.insets();
  90         dim.width += insets.left + insets.right;
  91         dim.height += insets.top + insets.bottom;
  92 
  93         return dim;
  94     }
  95 
  96     /**
  97      * Returns the preferred dimensions for this layout given the components
  98      * in the specified target container.
  99      * @param target the component which needs to be laid out
 100      * @see Container
 101      * @see #minimumLayoutSize
 102      */
 103     public Dimension preferredLayoutSize(Container target) {
 104         Dimension dim = new Dimension();
 105 
 106         for (int i = 0; i < target.countComponents(); i++) {
 107             Component comp = target.getComponent(i);
 108             if (comp.isVisible()) {
 109                 Dimension d = comp.preferredSize();
 110                 dim.width += d.width + hgap;
 111                 dim.height = Math.max(dim.height, d.height);
 112             }
 113         }
 114 
 115         Insets insets = target.insets();
 116         dim.width += insets.left + insets.right;
 117         dim.height += insets.top + insets.bottom;
 118 
 119         return dim;
 120     }
 121 
 122     /**
 123      * Lays out the specified container. This method will actually reshape the
 124      * components in the specified target container in order to satisfy the
 125      * constraints of the HorizBagLayout object.
 126      * @param target the component being laid out
 127      * @see Container
 128      */
 129     public void layoutContainer(Container target) {
 130         Insets insets = target.insets();
 131         int top = insets.top;
 132         int bottom = target.size().height - insets.bottom;
 133         int left = insets.left;
 134         int right = target.size().width - insets.right;
 135 
 136         for (int i = 0; i < target.countComponents(); i++) {
 137             Component comp = target.getComponent(i);
 138             if (comp.isVisible()) {
 139                 int compWidth = comp.size().width;
 140                 comp.resize(compWidth, bottom - top);
 141                 Dimension d = comp.preferredSize();
 142                 comp.reshape(left, top, d.width, bottom - top);
 143                 left += d.width + hgap;
 144             }
 145         }
 146     }
 147 
 148     /**
 149      * Returns the String representation of this HorizBagLayout's values.
 150      */
 151     public String toString() {
 152         return getClass().getName() + "[hgap=" + hgap + "]";
 153     }
 154 }