home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / HorizBagLayout.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  4.4 KB  |  151 lines

  1. /*
  2.  * @(#)HorizBagLayout.java    1.1 95/10/06 Herb Jellinek
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.awt;
  21.  
  22. import java.awt.*;
  23. import java.util.Vector;
  24.  
  25. /**
  26.  * A horizontal 'bag' of Components.  Allocates space for each Component
  27.  * from left to right.
  28.  *
  29.  * @version     1.1 10/06/95
  30.  * @author     Herb Jellinek
  31.  */
  32. public class HorizBagLayout implements LayoutManager {
  33.  
  34.     int hgap;
  35.  
  36.     /**
  37.      * Constructs a new HorizBagLayout.
  38.      */
  39.     public HorizBagLayout() {
  40.     this(0);
  41.     }
  42.  
  43.     /**
  44.      * Constructs a HorizBagLayout with the specified gaps.
  45.      * @param hgap the horizontal gap
  46.      */
  47.     public HorizBagLayout(int hgap) {
  48.     this.hgap = hgap;
  49.     }
  50.  
  51.     /**
  52.      * Adds the specified named component to the layout.
  53.      * @param name the String name
  54.      * @param comp the component to be added
  55.      */
  56.     public void addLayoutComponent(String name, Component comp) {
  57.     }
  58.  
  59.     /**
  60.      * Removes the specified component from the layout.
  61.      * @param comp the component to be removed
  62.      */
  63.     public void removeLayoutComponent(Component comp) {
  64.     }
  65.  
  66.     /**
  67.      * Returns the minimum dimensions needed to lay out the components
  68.      * contained in the specified target container. 
  69.      * @param target the Container on which to do the layout
  70.      * @see Container
  71.      * @see #preferredLayoutSize
  72.      */
  73.     public Dimension minimumLayoutSize(Container target) {
  74.     Dimension dim = new Dimension();
  75.  
  76.     for (int i = 0; i < target.countComponents(); i++) {
  77.         Component comp = target.getComponent(i);
  78.         if (comp.isVisible()) {
  79.         Dimension d = comp.minimumSize();
  80.         dim.width += d.width + hgap;
  81.         dim.height = Math.max(d.height, dim.height);
  82.         }
  83.     }
  84.  
  85.     Insets insets = target.insets();
  86.     dim.width += insets.left + insets.right;
  87.     dim.height += insets.top + insets.bottom;
  88.  
  89.     return dim;
  90.     }
  91.     
  92.     /**
  93.      * Returns the preferred dimensions for this layout given the components
  94.      * in the specified target container.
  95.      * @param target the component which needs to be laid out
  96.      * @see Container
  97.      * @see #minimumLayoutSize
  98.      */
  99.     public Dimension preferredLayoutSize(Container target) {
  100.     Dimension dim = new Dimension();
  101.  
  102.     for (int i = 0; i < target.countComponents(); i++) {
  103.         Component comp = target.getComponent(i);
  104.         if (comp.isVisible()) {
  105.         Dimension d = comp.preferredSize();
  106.         dim.width += d.width + hgap;
  107.         dim.height = Math.max(dim.height, d.height);
  108.         }
  109.     }
  110.  
  111.     Insets insets = target.insets();
  112.     dim.width += insets.left + insets.right;
  113.     dim.height += insets.top + insets.bottom;
  114.  
  115.     return dim;
  116.     }
  117.  
  118.     /**
  119.      * Lays out the specified container. This method will actually reshape the
  120.      * components in the specified target container in order to satisfy the 
  121.      * constraints of the HorizBagLayout object. 
  122.      * @param target the component being laid out
  123.      * @see Container
  124.      */
  125.     public void layoutContainer(Container target) {
  126.     Insets insets = target.insets();
  127.     int top = insets.top;
  128.     int bottom = target.size().height - insets.bottom;
  129.     int left = insets.left;
  130.     int right = target.size().width - insets.right;
  131.  
  132.     for (int i = 0; i < target.countComponents(); i++) {
  133.         Component comp = target.getComponent(i);
  134.         if (comp.isVisible()) {
  135.         int compWidth = comp.size().height;
  136.         comp.resize(compWidth, bottom - top);
  137.         Dimension d = comp.preferredSize();
  138.         comp.reshape(left, top, d.width, bottom - top);
  139.         left += d.width + hgap;
  140.         }
  141.     }
  142.     }
  143.     
  144.     /**
  145.      * Returns the String representation of this HorizBagLayout's values.
  146.      */
  147.     public String toString() {
  148.     return getClass().getName() + "[hgap=" + hgap + "]";
  149.     }
  150. }
  151.