home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / FlowLayout.java < prev    next >
Text File  |  1997-10-27  |  8KB  |  287 lines

  1. /*
  2.  * @(#)FlowLayout.java    1.23 97/01/27
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. /**
  25.  * Flow layout is used to layout buttons in a panel. It will arrange
  26.  * buttons left to right until no more buttons fit on the same line.
  27.  * Each line is centered.
  28.  *
  29.  * @version     1.23, 01/27/97
  30.  * @author     Arthur van Hoff
  31.  * @author     Sami Shaio
  32.  */
  33. public class FlowLayout implements LayoutManager, java.io.Serializable {
  34.  
  35.     /**
  36.      * The left alignment variable. 
  37.      */
  38.     public static final int LEFT     = 0;
  39.  
  40.     /**
  41.      * The right alignment variable. 
  42.      */
  43.     public static final int CENTER     = 1;
  44.  
  45.     /**
  46.      * The right alignment variable.
  47.      */
  48.     public static final int RIGHT     = 2;
  49.  
  50.     int align;
  51.     int hgap;
  52.     int vgap;
  53.  
  54.     /*
  55.      * JDK 1.1 serialVersionUID 
  56.      */
  57.      private static final long serialVersionUID = -7262534875583282631L;
  58.  
  59.     /**
  60.      * Constructs a new Flow Layout with a centered alignment and a
  61.      * default 5-unit horizontal and vertical gap.
  62.      */
  63.     public FlowLayout() {
  64.     this(CENTER, 5, 5);
  65.     }
  66.  
  67.     /**
  68.      * Constructs a new Flow Layout with the specified alignment and a
  69.      * default 5-unit horizontal and vertical gap.
  70.      * @param align the alignment value
  71.      */
  72.     public FlowLayout(int align) {
  73.     this(align, 5, 5);
  74.     }
  75.  
  76.     /**
  77.      * Constructs a new Flow Layout with the specified alignment and gap
  78.      * values.
  79.      * @param align the alignment value
  80.      * @param hgap the horizontal gap variable
  81.      * @param vgap the vertical gap variable
  82.      */
  83.     public FlowLayout(int align, int hgap, int vgap) {
  84.     this.align = align;
  85.     this.hgap = hgap;
  86.     this.vgap = vgap;
  87.     }
  88.  
  89.     /**
  90.      * Returns the alignment value for this layout, one of LEFT,
  91.      * CENTER, or RIGHT.
  92.      */
  93.     public int getAlignment() {
  94.     return align;
  95.     }
  96.     
  97.     /**
  98.      * Sets the alignment value for this layout.
  99.      * @param align the alignment value, one of LEFT, CENTER, or RIGHT.
  100.      */
  101.     public void setAlignment(int align) {
  102.     this.align = align;
  103.     }
  104.  
  105.     /**
  106.      * Returns the horizontal gap between components.
  107.      */
  108.     public int getHgap() {
  109.     return hgap;
  110.     }
  111.     
  112.     /**
  113.      * Sets the horizontal gap between components.
  114.      * @param hgap the horizontal gap between components
  115.      */
  116.     public void setHgap(int hgap) {
  117.     this.hgap = hgap;
  118.     }
  119.     
  120.     /**
  121.      * Returns the vertical gap between components.
  122.      */
  123.     public int getVgap() {
  124.     return vgap;
  125.     }
  126.     
  127.     /**
  128.      * Sets the vertical gap between components.
  129.      * @param vgap the vertical gap between components
  130.      */
  131.     public void setVgap(int vgap) {
  132.     this.vgap = vgap;
  133.     }
  134.  
  135.     /**
  136.      * Adds the specified component to the layout. Not used by this class.
  137.      * @param name the name of the component
  138.      * @param comp the the component to be added
  139.      */
  140.     public void addLayoutComponent(String name, Component comp) {
  141.     }
  142.  
  143.     /**
  144.      * Removes the specified component from the layout. Not used by
  145.      * this class.  
  146.      * @param comp the component to remove
  147.      */
  148.     public void removeLayoutComponent(Component comp) {
  149.     }
  150.  
  151.     /**
  152.      * Returns the preferred dimensions for this layout given the components
  153.      * in the specified target container.
  154.      * @param target the component which needs to be laid out
  155.      * @see Container
  156.      * @see #minimumLayoutSize
  157.      */
  158.     public Dimension preferredLayoutSize(Container target) {
  159.     Dimension dim = new Dimension(0, 0);
  160.     int nmembers = target.getComponentCount();
  161.  
  162.     for (int i = 0 ; i < nmembers ; i++) {
  163.         Component m = target.getComponent(i);
  164.         if (m.visible) {
  165.         Dimension d = m.getPreferredSize();
  166.         dim.height = Math.max(dim.height, d.height);
  167.         if (i > 0) {
  168.             dim.width += hgap;
  169.         }
  170.         dim.width += d.width;
  171.         }
  172.     }
  173.     Insets insets = target.getInsets();
  174.     dim.width += insets.left + insets.right + hgap*2;
  175.     dim.height += insets.top + insets.bottom + vgap*2;
  176.     return dim;
  177.     }
  178.  
  179.     /**
  180.      * Returns the minimum dimensions needed to layout the components
  181.      * contained in the specified target container.
  182.      * @param target the component which needs to be laid out 
  183.      * @see #preferredLayoutSize
  184.      */
  185.     public Dimension minimumLayoutSize(Container target) {
  186.     Dimension dim = new Dimension(0, 0);
  187.     int nmembers = target.getComponentCount();
  188.  
  189.     for (int i = 0 ; i < nmembers ; i++) {
  190.         Component m = target.getComponent(i);
  191.         if (m.visible) {
  192.         Dimension d = m.getMinimumSize();
  193.         dim.height = Math.max(dim.height, d.height);
  194.         if (i > 0) {
  195.             dim.width += hgap;
  196.         }
  197.         dim.width += d.width;
  198.         }
  199.     }
  200.     Insets insets = target.getInsets();
  201.     dim.width += insets.left + insets.right + hgap*2;
  202.     dim.height += insets.top + insets.bottom + vgap*2;
  203.     return dim;
  204.     }
  205.  
  206.     /** 
  207.      * Centers the elements in the specified row, if there is any slack.
  208.      * @param target the component which needs to be moved
  209.      * @param x the x coordinate
  210.      * @param y the y coordinate
  211.      * @param width the width dimensions
  212.      * @param height the height dimensions
  213.      * @param rowStart the beginning of the row
  214.      * @param rowEnd the the ending of the row
  215.      */
  216.     private void moveComponents(Container target, int x, int y, int width, int height, int rowStart, int rowEnd) {
  217.     switch (align) {
  218.     case LEFT:
  219.         break;
  220.     case CENTER:
  221.         x += width / 2;
  222.         break;
  223.     case RIGHT:
  224.         x += width;
  225.         break;
  226.     }
  227.     for (int i = rowStart ; i < rowEnd ; i++) {
  228.         Component m = target.getComponent(i);
  229.         if (m.visible) {
  230.         m.setLocation(x, y + (height - m.height) / 2);
  231.         x += hgap + m.width;
  232.         }
  233.     }
  234.     }
  235.  
  236.     /**
  237.      * Lays out the container. This method will actually reshape the
  238.      * components in the target in order to satisfy the constraints of
  239.      * the BorderLayout object. 
  240.      * @param target the specified component being laid out.
  241.      * @see Container
  242.      */
  243.     public void layoutContainer(Container target) {
  244.     Insets insets = target.getInsets();
  245.     int maxwidth = target.width - (insets.left + insets.right + hgap*2);
  246.     int nmembers = target.getComponentCount();
  247.     int x = 0, y = insets.top + vgap;
  248.     int rowh = 0, start = 0;
  249.  
  250.     for (int i = 0 ; i < nmembers ; i++) {
  251.         Component m = target.getComponent(i);
  252.         if (m.visible) {
  253.         Dimension d = m.getPreferredSize();
  254.         m.setSize(d.width, d.height);
  255.     
  256.         if ((x == 0) || ((x + d.width) <= maxwidth)) {
  257.             if (x > 0) {
  258.             x += hgap;
  259.             }
  260.             x += d.width;
  261.             rowh = Math.max(rowh, d.height);
  262.         } else {
  263.             moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, i);
  264.             x = d.width;
  265.             y += vgap + rowh;
  266.             rowh = d.height;
  267.             start = i;
  268.         }
  269.         }
  270.     }
  271.     moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, nmembers);
  272.     }
  273.     
  274.     /**
  275.      * Returns the String representation of this FlowLayout's values.
  276.      */
  277.     public String toString() {
  278.     String str = "";
  279.     switch (align) {
  280.       case LEFT:    str = ",align=left"; break;
  281.       case CENTER:  str = ",align=center"; break;
  282.       case RIGHT:   str = ",align=right"; break;
  283.     }
  284.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
  285.     }
  286. }
  287.