home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JAVA_UTL / HYPERPRO / SRC / PVS / UTILS / SEPARATO.JAV < prev    next >
Encoding:
Text File  |  1996-09-14  |  5.7 KB  |  229 lines

  1. package PVS.Utils;
  2. /**
  3.  * A layout manager for container splitted into two 
  4.  *   resizable component with separator
  5.  *   container should contain exactly 3 component
  6.  *   first and last are "real" component
  7.  *   middle component is separator
  8.  *
  9.  * @version 1.0, 12/14/95
  10.  * @author V.Bulatov
  11.  */
  12.  
  13. import java.awt.*;
  14.  
  15.  
  16. public class SeparatorLayout implements LayoutManager {
  17.   public static final int VERTICAL = 0; // separator is horizontal
  18.   public static final int HORIZONTAL = 1;// separator is vertical
  19.   
  20.   int direction;
  21.  
  22.   private double weight=0.5; // weight of first component
  23.  
  24.   public SeparatorLayout() {
  25.     this(HORIZONTAL);
  26.   }
  27.   
  28.   public SeparatorLayout(int direction) {
  29.     this(direction,0.5);
  30.   }
  31.  
  32.   public SeparatorLayout(int direction, double weight) {
  33.     this.direction = direction;    
  34.     if(weight >1.0 || weight < 0.0)
  35.       throw new 
  36.     IllegalArgumentException("wrong weight:" + weight);
  37.     this.weight = weight;
  38.   }
  39.   
  40.   /**
  41.    * required by interface
  42.    */
  43.   public void addLayoutComponent(String name, Component comp) {
  44.   }
  45.   
  46.   /**
  47.    * required by interface
  48.    */
  49.   public void removeLayoutComponent(Component comp) {
  50.   }
  51.  
  52.   /** 
  53.    * Returns the preferred dimensions for this layout given the components
  54.    * in the specified container.
  55.    * @param parent the component which needs to be laid out 
  56.    * @see #minimumLayoutSize
  57.    */
  58.   public Dimension preferredLayoutSize(Container parent) {
  59.     int ncomponents = parent.countComponents();
  60.     if(ncomponents != 3 ){
  61.       throw new 
  62.     IllegalArgumentException("wrong number of component ["+
  63.                  ncomponents+"] for SeparatorLayout");
  64.     }
  65.  
  66.     return getSize(parent,
  67.            parent.getComponent(0).preferredSize(),
  68.            parent.getComponent(1).preferredSize(),
  69.            parent.getComponent(2).preferredSize());
  70.   }
  71.  
  72.   /** 
  73.    * Returns the minimum dimensions for this layout given the components
  74.    * in the specified container.
  75.    * @param parent the component which needs to be laid out 
  76.    * @see #minimumLayoutSize
  77.    */
  78.   public Dimension minimumLayoutSize(Container parent) {
  79.     int ncomponents = parent.countComponents();
  80.     if(ncomponents != 3 ){
  81.       throw new 
  82.     IllegalArgumentException("wrong number of component for SeparatorLayout");
  83.     }
  84.  
  85.     return getSize(parent,
  86.            parent.getComponent(0).minimumSize(),
  87.            parent.getComponent(1).minimumSize(),
  88.            parent.getComponent(2).minimumSize());
  89.   }
  90.   
  91.   /**
  92.     just calculates width and height of columns
  93.    */
  94.  
  95.   Dimension getSize(Container parent,Dimension d0, Dimension d1, Dimension d2){
  96.     int width, height;
  97.     switch(direction){
  98.     default:
  99.     case HORIZONTAL:
  100.       width = d0.width;
  101.       if(width < d1.width)
  102.     width = d1.width;
  103.       if(width < d2.width)
  104.     width = d2.width;
  105.       height = d0.height+d1.height+d2.height;
  106.       break;
  107.     case VERTICAL:
  108.       height = d0.height;
  109.       if(height < d1.height)
  110.     height = d1.height;
  111.       if(height < d2.height)
  112.     height = d2.height;
  113.       width= d0.width+d1.width+d2.width;
  114.       break;
  115.     }
  116.     
  117.     Insets insets = parent.insets();
  118.     return new Dimension(insets.left + insets.right+width,
  119.              insets.top + insets.bottom + height);
  120.   }
  121.   
  122.   /** 
  123.    * Lays out the container in the specified panel.  
  124.    * @param parent the specified component being laid out
  125.    * @see Container
  126.    */
  127.   public void layoutContainer(Container parent) {
  128.     Insets insets = parent.insets();
  129.  
  130.     int ncomponents = parent.countComponents();
  131.     if(ncomponents != 3 )
  132.       return;
  133.  
  134.     Component comp0 = parent.getComponent(0);
  135.     Component sep = parent.getComponent(1);
  136.     Component comp1 = parent.getComponent(2);
  137.  
  138.     int w = parent.size().width - (insets.left + insets.right);
  139.     int h = parent.size().height - (insets.top + insets.bottom);
  140.  
  141.     
  142.     switch(direction){
  143.     default:
  144.     case HORIZONTAL:
  145.       int hs = sep.minimumSize().height;
  146.       int h1 = (int)((h-hs)*weight);
  147.       comp0.reshape(0, 0, w, h1);
  148.       sep.reshape(0, h1, w, hs);
  149.       comp1.reshape(0, h1+hs, w, h - h1 - hs);
  150.       break;
  151.     case VERTICAL:
  152.       int ws = sep.minimumSize().width;
  153.       int w1 = (int)((w-ws)*weight);
  154.       comp0.reshape(0, 0, w1, h);
  155.       sep.reshape(w1, 0, ws, h);
  156.       comp1.reshape(w1+ws, 0, w - w1 - ws,h );
  157.       break;      
  158.     }
  159.  
  160.     comp0.validate();
  161.     comp1.validate();
  162.   }
  163.  
  164.   /** 
  165.    * moves separator of in this Container and setup 
  166.    * current proportion of division of the Container
  167.    */
  168.   public void moveSeparator(Container parent, int x, int y){
  169.     if(parent.getLayout() != this){
  170.       throw new 
  171.     IllegalArgumentException
  172.       ("layout of container is not this SeparatorLayout");
  173.     }
  174.  
  175.     Insets insets = parent.insets();
  176.  
  177.     int ncomponents = parent.countComponents();
  178.     if(ncomponents != 3 )
  179.       return;
  180.  
  181.     Component comp0 = parent.getComponent(0);
  182.     Component sep = parent.getComponent(1);
  183.     Component comp1 = parent.getComponent(2);
  184.  
  185.     int w = parent.size().width - (insets.left + insets.right);
  186.     int h = parent.size().height - (insets.top + insets.bottom);
  187.  
  188.     switch(direction){
  189.     default:
  190.     case HORIZONTAL:
  191.       int hs = sep.minimumSize().height;
  192.       if(y < 0)
  193.     y = 0;
  194.       else if (y > h - hs)
  195.     y = h - hs;
  196.       weight = (double)y/(h-hs);
  197.       break;
  198.     case VERTICAL:
  199.       int ws = sep.minimumSize().width;
  200.       if(x < 0)
  201.     x = 0;
  202.       else if(x > w - ws)
  203.     x = w-ws;
  204.       weight = (double)x/(w-ws);
  205.       break;      
  206.     }   
  207.     parent.invalidate();
  208.  
  209.     parent.validate();
  210.   }
  211.  
  212.   public void setWeight(double weight){
  213.     if(weight >1.0 || weight < 0.0)
  214.       throw new 
  215.     IllegalArgumentException("wrong weight:" + weight);
  216.     this.weight = weight;
  217.   }
  218.   
  219.   /**
  220.    * Returns the String representation of this SeparatorLayout's values.
  221.    */
  222.   public String toString() {
  223.     return getClass().getName() + "[direction = "
  224.       +((direction == HORIZONTAL)?"HORIZONTAL":"VERTICAL")+
  225.     ", weight = "+weight+"]";
  226.   }
  227.  
  228. }
  229.