home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / Mje / VGridLayout.java < prev   
Encoding:
Java Source  |  1996-04-08  |  4.5 KB  |  136 lines

  1. /********************************************************************************
  2.  ** VHGridLayout is a layout manager which arranges components in a 
  3.  ** single column, and provides many options for doing so. The usage is:
  4.  **
  5.  ** vgrid = new VGridLayout(Insets insets, int interval, int notUsed);
  6.  ** container.setLayout(vgrid);
  7.  ** vgrid.setConstraints(comp1, percent); container.add(comp1);
  8.  ** vgrid.setConstraints(comp2, percent); container.add(comp2);
  9.  **                             :
  10.  **                             :
  11.  **
  12.  ** 'insets' is the top, bottom, left and right margins in pixels.
  13.  ** 'interval' is the spacing between components in pixels.
  14.  ** 'notUsed' is the percentage of space to be left unused from the top.
  15.  ** 'percent' is the percentage of space that the component will occupy.
  16.  **      0 means the component will occupy its preferred size.
  17.  **
  18.  ** VGridLayout combined with HGridLayout will provide a powerful way of
  19.  ** laying out components.
  20.  **
  21.  ********************************************************************************
  22.  ** Author: Chew Wei Yih, Victor (c) 1996.
  23.  ********************************************************************************/
  24.  
  25. import java.awt.*;
  26. import java.util.*;
  27. import java.lang.*;
  28.  
  29. public class VGridLayout implements LayoutManager
  30. {
  31.     Insets insets = null;
  32.     int interval, notUsed;
  33.     Hashtable constraints = new Hashtable();
  34.     
  35.     public VGridLayout(Insets insets, int interval, int notUsed)
  36.     {
  37.         this.insets = insets;
  38.         this.interval = interval;
  39.         this.notUsed = notUsed;
  40.     }
  41.     
  42.     public void addLayoutComponent(String name, Component comp)
  43.     {
  44.     }
  45.  
  46.     public void removeLayoutComponent(Component comp)
  47.     {
  48.     }
  49.  
  50.     public Dimension preferredLayoutSize(Container target)
  51.     {
  52.         return minimumLayoutSize(target);
  53.     }
  54.  
  55.     public Dimension minimumLayoutSize(Container target)
  56.     {
  57.         int width=0, height=0, numComp = target.countComponents();
  58.         int[] compHeight = new int[numComp];
  59.         int[] compPercent = new int[numComp];
  60.         double unitSize = 0.0;
  61.         Component comp = null;
  62.         Dimension dim = null;
  63.         Insets bounds = target.insets();
  64.  
  65.         for (int i=0; i<numComp; i++)
  66.         {
  67.             comp = (Component)target.getComponent(i);
  68.             dim = comp.preferredSize();
  69.             compHeight[i] = dim.height;
  70.             compPercent[i] = ((Integer)constraints.get(comp)).intValue();
  71.  
  72.             if (compPercent[i] != 0)
  73.             {
  74.                 double tmp = (double)dim.height/(double)compPercent[i];
  75.                 if (unitSize < tmp) unitSize = tmp;
  76.             }
  77.  
  78.             if (width < dim.width) width = dim.width;
  79.         }            
  80.  
  81.         for (int i=0; i<numComp; i++)
  82.         {
  83.             if (compPercent[i] == 0) height += compHeight[i];
  84.             else height += (int)(compPercent[i] * unitSize);
  85.         }
  86.  
  87.         width += bounds.left + bounds.right + insets.left + insets.right;
  88.         height += bounds.top + insets.top + interval*(numComp-1) + insets.bottom + bounds.bottom;
  89.  
  90.         dim = new Dimension(width, height);
  91.         return dim;
  92.     }
  93.  
  94.     public void setConstraints(Component comp, int percent)
  95.     {
  96.         constraints.put(comp, new Integer(percent));
  97.     }
  98.  
  99.     public void layoutContainer(Container target)
  100.     {
  101.         int numComp = target.countComponents();
  102.         int x, y;
  103.         Component[] comp = new Component[numComp];
  104.         int[] percent = new int[numComp];
  105.         int[] height = new int[numComp];
  106.  
  107.         Insets tbounds = target.insets();
  108.         Dimension dim = target.size();
  109.         int twidth = dim.width - tbounds.left - tbounds.right - insets.left - insets.right;
  110.         int theight = dim.height;
  111.  
  112.         int heightAvail = theight - tbounds.top - tbounds.bottom - insets.top - insets.bottom - (numComp-1)*interval;
  113.         int heightUnavail = notUsed * heightAvail / 100;
  114.         heightAvail -= heightUnavail;
  115.  
  116.         int heightFixed = 0;
  117.         for (int i=0; i<numComp; i++)
  118.         {
  119.             comp[i] = target.getComponent(i);
  120.             percent[i] = ((Integer)constraints.get(comp[i])).intValue();
  121.             height[i] = comp[i].preferredSize().height;
  122.             if (percent[i] == 0) heightFixed += height[i];
  123.         }
  124.         heightAvail -= heightFixed;
  125.  
  126.         x = tbounds.left + insets.left;
  127.         y = tbounds.top + insets.top + heightUnavail;
  128.         for (int i=0; i<numComp; i++)
  129.         {
  130.             if (percent[i] != 0) height[i] = (heightAvail * percent[i]) / 100;
  131.             comp[i].reshape(x, y, twidth, height[i]);
  132.             y += height[i] + interval;
  133.         }
  134.     }
  135. }
  136.