home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / awt / vertlayout.java < prev   
Encoding:
Java Source  |  1996-08-14  |  4.9 KB  |  192 lines

  1. package como.awt;
  2.  
  3. /*
  4.  * @(#)VertLayout.java    1.0 96/03/06 Jan Kautz
  5.  *
  6.  */
  7.  
  8. import java.awt.*;
  9.  
  10. /**
  11.  * Vert layout is used to layout buttons in a panel. It will arrange
  12.  * buttons top down.
  13.  * Each line is aligned left/right/centered.
  14.  *
  15.  * @version     1.0
  16.  * @author     Jan Kautz
  17.  */
  18. public class VertLayout implements LayoutManager {
  19.     public static final int LEFT     = 0;
  20.     public static final int CENTER     = 1;
  21.     public static final int RIGHT     = 2;
  22.     public static final int STRETCH     = 3;
  23.  
  24.     int align;
  25.     int hgap;
  26.     int vgap;
  27.  
  28.     /**
  29.      * Constructs a new Vert Layout with a left alignment.
  30.      */
  31.     public VertLayout() {
  32.     this(LEFT, 5, 5);
  33.     }
  34.  
  35.     /**
  36.      * Constructs a new Vert Layout with the specified alignment.
  37.      * @param align the alignment value
  38.      */
  39.     public VertLayout(int align) {
  40.     this(align, 5, 5);
  41.     }
  42.  
  43.     /**
  44.      * Constructs a new Vert Layout with the specified alignment and gap
  45.      * values.
  46.      * @param align the alignment value
  47.      * @param hgap the horizontal gap variable
  48.      * @param vgap the vertical gap variable
  49.      */
  50.     public VertLayout(int align, int hgap, int vgap) {
  51.     this.align = align;
  52.     this.hgap = hgap;
  53.     this.vgap = vgap;
  54.     }
  55.  
  56.     /**
  57.      * Adds the specified component to the layout. Does not apply.
  58.      * @param name the name of the component
  59.      * @param comp the 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. Does not apply.
  66.      * @param comp the component to remove
  67.      */
  68.     public void removeLayoutComponent(Component comp) {
  69.     }
  70.  
  71.     /**
  72.      * Returns the preferred dimensions for this layout given the components
  73.      * in the specified target container.
  74.      * @param target the component which needs to be laid out
  75.      * @see Container
  76.      * @see #minimumSize
  77.      */
  78.     public Dimension preferredLayoutSize(Container target) {
  79.     Dimension dim = new Dimension(0, 0);
  80.     int nmembers = target.countComponents();
  81.  
  82.     for (int i = 0 ; i < nmembers ; i++) {
  83.         Component m = target.getComponent(i);
  84.         if( m.isVisible() ) {
  85.         Dimension d = m.preferredSize();
  86.         dim.height += d.height;
  87.                 dim.width = Math.max( dim.width, d.width );
  88.  
  89.         if (i > 0) {
  90.             dim.height += vgap;
  91.         }
  92.         }
  93.     }
  94.     Insets insets = target.insets();
  95.     dim.width += insets.left + insets.right + hgap*2;
  96.     dim.height += insets.top + insets.bottom + vgap*2;
  97.     return dim;
  98.     }
  99.  
  100.     /**
  101.      * Returns the minimum dimensions needed to layout the components
  102.      * contained in the specified target container.
  103.      * @param target the component which needs to be laid out 
  104.      * @see #preferredSize
  105.      */
  106.     public Dimension minimumLayoutSize(Container target) {
  107.     Dimension dim = new Dimension(0, 0);
  108.     int nmembers = target.countComponents();
  109.  
  110.     for (int i = 0 ; i < nmembers ; i++) {
  111.         Component m = target.getComponent(i);
  112.         if( m.isVisible() ) {
  113.         Dimension d = m.minimumSize();
  114.         dim.height += d.height;
  115.                 dim.width = Math.max( dim.width, d.width );
  116.  
  117.         if (i > 0) {
  118.             dim.height += vgap;
  119.         }
  120.         }
  121.     }
  122.     Insets insets = target.insets();
  123.     dim.width += insets.left + insets.right + hgap*2;
  124.     dim.height += insets.top + insets.bottom + vgap*2;
  125.     return dim;
  126.     }
  127.  
  128.     /**
  129.      * Lays out the container. This method will actually reshape the
  130.      * components in target in order to satisfy the constraints of
  131.      * the VertLayout object. 
  132.      * @param target the specified container being laid out.
  133.      * @see Container
  134.      */
  135.     public void layoutContainer(Container target) {
  136.     Insets insets = target.insets();
  137.     Dimension targetsize = target.size();
  138.     int maxwidth = targetsize.width - (insets.left + insets.right + hgap*2);
  139.     int nmembers = target.countComponents();
  140.     int x = hgap, y = insets.top + vgap;
  141.     int rowh = 0, start = 0;
  142.  
  143.     for (int i = 0 ; i < nmembers ; i++) {
  144.         Component m = target.getComponent(i);
  145.         if( m.isVisible() ) {
  146.         Dimension d = m.preferredSize();
  147.         Dimension willbe = new Dimension( d );
  148.  
  149.         if( d.width > maxwidth )     // zu breit!
  150.             willbe.width = maxwidth;
  151.  
  152.         if( align == STRETCH )     // muss so breit sein
  153.             willbe.width = maxwidth;
  154.  
  155.         m.resize( willbe.width, willbe.height );
  156.  
  157.         switch( align )
  158.         {
  159.            case CENTER:
  160.               m.move( hgap + (maxwidth - willbe.width)/2, y );
  161.               break;
  162.  
  163.            case RIGHT:
  164.               m.move( hgap + maxwidth - willbe.width, y );
  165.               break;
  166.  
  167.            case STRETCH:
  168.            case LEFT:
  169.               m.move( x, y );
  170.               break;
  171.         }
  172.  
  173.         y += willbe.height + vgap;
  174.         }
  175.     }
  176.     }
  177.     
  178.     /**
  179.      * Returns the String representation of this FlowLayout's values.
  180.      */
  181.     public String toString() {
  182.     String str = "";
  183.     switch (align) {
  184.       case LEFT:    str = ",align=left"; break;
  185.       case CENTER:  str = ",align=center"; break;
  186.       case RIGHT:   str = ",align=right"; break;
  187.       case STRETCH: str = ",align=stretched"; break;
  188.     }
  189.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
  190.     }
  191. }
  192.