home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / FlowLayout.java < prev    next >
Text File  |  1996-05-03  |  7KB  |  231 lines

  1. /*
  2.  * @(#)FlowLayout.java    1.18 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 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. package java.awt;
  20.  
  21. /**
  22.  * Flow layout is used to layout buttons in a panel. It will arrange
  23.  * buttons left to right until no more buttons fit on the same line.
  24.  * Each line is centered.
  25.  *
  26.  * @version     1.18, 14 Dec 1995
  27.  * @author     Arthur van Hoff
  28.  * @author     Sami Shaio
  29.  */
  30. public class FlowLayout implements LayoutManager {
  31.  
  32.     /**
  33.      * The left alignment variable. 
  34.      */
  35.     public static final int LEFT     = 0;
  36.  
  37.     /**
  38.      * The right alignment variable. 
  39.      */
  40.     public static final int CENTER     = 1;
  41.  
  42.     /**
  43.      * The right alignment variable.
  44.      */
  45.     public static final int RIGHT     = 2;
  46.  
  47.     int align;
  48.     int hgap;
  49.     int vgap;
  50.  
  51.     /**
  52.      * Constructs a new Flow Layout with a centered alignment.
  53.      */
  54.     public FlowLayout() {
  55.     this(CENTER, 5, 5);
  56.     }
  57.  
  58.     /**
  59.      * Constructs a new Flow Layout with the specified alignment.
  60.      * @param align the alignment value
  61.      */
  62.     public FlowLayout(int align) {
  63.     this(align, 5, 5);
  64.     }
  65.  
  66.     /**
  67.      * Constructs a new Flow Layout with the specified alignment and gap
  68.      * values.
  69.      * @param align the alignment value
  70.      * @param hgap the horizontal gap variable
  71.      * @param vgap the vertical gap variable
  72.      */
  73.     public FlowLayout(int align, int hgap, int vgap) {
  74.     this.align = align;
  75.     this.hgap = hgap;
  76.     this.vgap = vgap;
  77.     }
  78.  
  79.     /**
  80.      * Adds the specified component to the layout. Not used by this class.
  81.      * @param name the name of the component
  82.      * @param comp the the component to be added
  83.      */
  84.     public void addLayoutComponent(String name, Component comp) {
  85.     }
  86.  
  87.     /**
  88.      * Removes the specified component from the layout. Not used by
  89.      * this class.  
  90.      * @param comp the component to remove
  91.      */
  92.     public void removeLayoutComponent(Component comp) {
  93.     }
  94.  
  95.     /**
  96.      * Returns the preferred dimensions for this layout given the components
  97.      * in the specified target container.
  98.      * @param target the component which needs to be laid out
  99.      * @see Container
  100.      * @see #minimumLayoutSize
  101.      */
  102.     public Dimension preferredLayoutSize(Container target) {
  103.     Dimension dim = new Dimension(0, 0);
  104.     int nmembers = target.countComponents();
  105.  
  106.     for (int i = 0 ; i < nmembers ; i++) {
  107.         Component m = target.getComponent(i);
  108.         if (m.visible) {
  109.         Dimension d = m.preferredSize();
  110.         dim.height = Math.max(dim.height, d.height);
  111.         if (i > 0) {
  112.             dim.width += hgap;
  113.         }
  114.         dim.width += d.width;
  115.         }
  116.     }
  117.     Insets insets = target.insets();
  118.     dim.width += insets.left + insets.right + hgap*2;
  119.     dim.height += insets.top + insets.bottom + vgap*2;
  120.     return dim;
  121.     }
  122.  
  123.     /**
  124.      * Returns the minimum dimensions needed to layout the components
  125.      * contained in the specified target container.
  126.      * @param target the component which needs to be laid out 
  127.      * @see #preferredLayoutSize
  128.      */
  129.     public Dimension minimumLayoutSize(Container target) {
  130.     Dimension dim = new Dimension(0, 0);
  131.     int nmembers = target.countComponents();
  132.  
  133.     for (int i = 0 ; i < nmembers ; i++) {
  134.         Component m = target.getComponent(i);
  135.         if (m.visible) {
  136.         Dimension d = m.minimumSize();
  137.         dim.height = Math.max(dim.height, d.height);
  138.         if (i > 0) {
  139.             dim.width += hgap;
  140.         }
  141.         dim.width += d.width;
  142.         }
  143.     }
  144.     Insets insets = target.insets();
  145.     dim.width += insets.left + insets.right + hgap*2;
  146.     dim.height += insets.top + insets.bottom + vgap*2;
  147.     return dim;
  148.     }
  149.  
  150.     /** 
  151.      * Centers the elements in the specified row, if there is any slack.
  152.      * @param target the component which needs to be moved
  153.      * @param x the x coordinate
  154.      * @param y the y coordinate
  155.      * @param width the width dimensions
  156.      * @param height the height dimensions
  157.      * @param rowStart the beginning of the row
  158.      * @param rowEnd the the ending of the row
  159.      */
  160.     private void moveComponents(Container target, int x, int y, int width, int height, int rowStart, int rowEnd) {
  161.     switch (align) {
  162.     case LEFT:
  163.         break;
  164.     case CENTER:
  165.         x += width / 2;
  166.         break;
  167.     case RIGHT:
  168.         x += width;
  169.         break;
  170.     }
  171.     for (int i = rowStart ; i < rowEnd ; i++) {
  172.         Component m = target.getComponent(i);
  173.         if (m.visible) {
  174.         m.move(x, y + (height - m.height) / 2);
  175.         x += hgap + m.width;
  176.         }
  177.     }
  178.     }
  179.  
  180.     /**
  181.      * Lays out the container. This method will actually reshape the
  182.      * components in the target in order to satisfy the constraints of
  183.      * the BorderLayout object. 
  184.      * @param target the specified component being laid out.
  185.      * @see Container
  186.      */
  187.     public void layoutContainer(Container target) {
  188.     Insets insets = target.insets();
  189.     int maxwidth = target.width - (insets.left + insets.right + hgap*2);
  190.     int nmembers = target.countComponents();
  191.     int x = 0, y = insets.top + vgap;
  192.     int rowh = 0, start = 0;
  193.  
  194.     for (int i = 0 ; i < nmembers ; i++) {
  195.         Component m = target.getComponent(i);
  196.         if (m.visible) {
  197.         Dimension d = m.preferredSize();
  198.         m.resize(d.width, d.height);
  199.     
  200.         if ((x == 0) || ((x + d.width) <= maxwidth)) {
  201.             if (x > 0) {
  202.             x += hgap;
  203.             }
  204.             x += d.width;
  205.             rowh = Math.max(rowh, d.height);
  206.         } else {
  207.             moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, i);
  208.             x = d.width;
  209.             y += vgap + rowh;
  210.             rowh = d.height;
  211.             start = i;
  212.         }
  213.         }
  214.     }
  215.     moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, nmembers);
  216.     }
  217.     
  218.     /**
  219.      * Returns the String representation of this FlowLayout's values.
  220.      */
  221.     public String toString() {
  222.     String str = "";
  223.     switch (align) {
  224.       case LEFT:    str = ",align=left"; break;
  225.       case CENTER:  str = ",align=center"; break;
  226.       case RIGHT:   str = ",align=right"; break;
  227.     }
  228.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
  229.     }
  230. }
  231.