home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / prmjatgt / flowlayoutcust.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.0 KB  |  188 lines

  1. import java.awt.*;
  2.  
  3. /**
  4.  * A class that implements multi line FlowLayout
  5.  *
  6.  * @version     1.0 03/24/96
  7.  * @author         Kang, Dae Woong (namu@star.elim.net)
  8.  */
  9.  
  10. public class FlowLayoutCust implements LayoutManager 
  11. {
  12.  
  13.     static final int HORIZONTAL = 1;
  14.     static final int VERTICAL = 2;
  15.  
  16.     int direction;
  17.     int hgap = 0;
  18.     int vgap = 0;
  19.  
  20.     public FlowLayoutCust(int direction) 
  21.     {
  22.         this(direction, 0, 0);
  23.     }
  24.  
  25.     public FlowLayoutCust(int direction, int hgap, int vgab) 
  26.     {
  27.         this.direction = direction;
  28.         this.hgap = hgap;
  29.         this.vgap = vgap;
  30.     }
  31.  
  32.     public void addLayoutComponent(String name, Component comp)    {}
  33.     public void removeLayoutComponent(Component comp) {}
  34.  
  35.     public Dimension preferredLayoutSize(Container target) 
  36.     {
  37.         Dimension dm = target.size();
  38.         Insets insets = target.insets();
  39.         int nmembers = target.countComponents();
  40.  
  41.         if (direction == HORIZONTAL)
  42.         {
  43.     
  44.             int sumWidth =  0;
  45.             int sumHeight = insets.top + insets.bottom + vgap*2;
  46.             int rowHeight = 0;
  47.             int maxWidth = dm.width - (insets.left + insets.right + hgap*2);
  48.     
  49.             for (int i = 0 ; i < nmembers ; i++)
  50.             {
  51.                 Component m = target.getComponent(i);
  52.                 if (m.isVisible())
  53.                 {
  54.                     Dimension d = m.preferredSize();
  55.                     sumWidth += d.width;
  56.                     rowHeight = Math.max(rowHeight, d.height);
  57.                     
  58.                     if (sumWidth > maxWidth && i != 0)
  59.                     {
  60.                         sumHeight += rowHeight + vgap;
  61.                         sumWidth = d.width;
  62.                         rowHeight = d.height;
  63.                     }
  64.                     else
  65.                     {
  66.                         sumWidth += hgap;
  67.                     }
  68.                 }
  69.             }
  70.             sumHeight += rowHeight;
  71.             return new Dimension(dm.width, sumHeight);
  72.         }
  73.         else
  74.         {
  75.             int sumWidth =  insets.left + insets.right + hgap*2;
  76.             int sumHeight = 0;
  77.             int colWidth = 0;
  78.             int maxHeight = dm.height - (insets.top + insets.bottom + vgap*2);
  79.             for (int i = 0 ; i < nmembers ; i++)
  80.             {
  81.                 Component m = target.getComponent(i);
  82.                 if (m.isVisible())
  83.                 {
  84.                     Dimension d = m.preferredSize();
  85.                     sumHeight += d.height;
  86.                     colWidth = Math.max(colWidth, d.width);
  87.                     
  88.                     if (sumHeight > maxHeight && i != 0)
  89.                     {
  90.                         sumWidth += colWidth + hgap;
  91.                         sumHeight = d.height;
  92.                         colWidth = d.width;
  93.                     }
  94.                     else
  95.                     {
  96.                         sumHeight += vgap;
  97.                     }
  98.                 }
  99.             }
  100.             
  101.             sumWidth += colWidth;
  102.             return new Dimension(sumWidth, dm.height);
  103.         }
  104.     }
  105.  
  106.     public Dimension minimumLayoutSize(Container target) 
  107.     {
  108.         return preferredLayoutSize(target);
  109.     }
  110.  
  111.     public void layoutContainer(Container target) 
  112.     {
  113.         Dimension dm = target.size();
  114.  
  115.         Insets insets = target.insets();
  116.         int nmembers = target.countComponents();
  117.  
  118.         int posX =  insets.left;
  119.         int posY = insets.top;
  120.  
  121.         if (direction == HORIZONTAL)
  122.         {
  123.             int rowHeight = 0;
  124.             for (int i = 0 ; i < nmembers; i++)
  125.             {
  126.                 Component m = target.getComponent(i);
  127.                 if (m.isVisible())
  128.                 {
  129.                     Dimension d = m.preferredSize();
  130.                     m.resize(d.width, d.height);
  131.                     rowHeight = Math.max(rowHeight, d.height);
  132.                     if (posX + d.width > dm.width && i != 0)
  133.                     {
  134.                         posY += rowHeight + vgap;
  135.                         posX = insets.left + d.width;
  136.                         rowHeight = d.height;
  137.  
  138.                         m.move(insets.left, posY);
  139.                     }
  140.                     else
  141.                     {
  142.                         m.move(posX, posY);
  143.                         posX += d.width + hgap;
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.         else
  149.         {
  150.             int colWidth = 0;
  151.             for (int i = 0 ; i < nmembers; i++)
  152.             {
  153.                 Component m = target.getComponent(i);
  154.                 if (m.isVisible())
  155.                 {
  156.                     Dimension d = m.preferredSize();
  157.                     m.resize(d.width, d.height);
  158.                     colWidth = Math.max(colWidth, d.width);
  159.                     if (posY + d.height > dm.height && i != 0)
  160.                     {
  161.                         posX += colWidth + hgap;
  162.                         posY = insets.top + d.height;
  163.                         colWidth = d.width;
  164.  
  165.                         m.move(posX, insets.top);
  166.                     }
  167.                     else
  168.                     {
  169.                         m.move(posX, posY);
  170.                         posY += d.height + vgap;
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.     }
  176.  
  177.     public String toString() 
  178.     {
  179.         String str = "";
  180.         switch (direction) 
  181.         {
  182.         case HORIZONTAL:  str = ",direction=Horizontal"; break;
  183.         case VERTICAL:    str = ",direction=Vertical"; break;
  184.         }
  185.         return getClass().getName() + str ;
  186.     }
  187. }
  188.