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

  1. /*
  2.  * @(#)GridBagLayout.java    1.5 95/11/16 Doug Stein
  3.  *
  4.  * Copyright (c) 1995 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. import java.util.Hashtable;
  22. import java.util.Vector;
  23.  
  24. class GridBagLayoutInfo {
  25.   int width, height;        /* number of cells horizontally, vertically */
  26.   int startx, starty;        /* starting point for layout */
  27.   int minWidth[];        /* largest minWidth in each column */
  28.   int minHeight[];        /* largest minHeight in each row */
  29.   double weightX[];        /* largest weight in each column */
  30.   double weightY[];        /* largest weight in each row */
  31.  
  32.   GridBagLayoutInfo () {
  33.     minWidth = new int[GridBagLayout.MAXGRIDSIZE];
  34.     minHeight = new int[GridBagLayout.MAXGRIDSIZE];
  35.     weightX = new double[GridBagLayout.MAXGRIDSIZE];
  36.     weightY = new double[GridBagLayout.MAXGRIDSIZE];
  37.   }
  38. }
  39.  
  40. /**
  41.     GridBagLayout is a flexible layout manager
  42.     that aligns components vertically and horizontally,
  43.     without requiring that the components be the same size.
  44.     Each GridBagLayout uses a rectangular grid of cells,
  45.     with each component occupying one or more cells
  46.     (called its <em>display area</em>).
  47.     Each component managed by a GridBagLayout 
  48.     is associated with a
  49.     <a href=java.awt.GridBagConstraints.html>GridBagConstraints</a> instance
  50.     that specifies how the component is laid out
  51.     within its display area.
  52.     How a GridBagLayout places a set of components
  53.     depends on each component's GridBagConstraints and minimum size,
  54.     as well as the preferred size of the components' container.
  55.     <p>
  56.  
  57.     To use a GridBagLayout effectively,
  58.     you must customize one or more of its components' GridBagConstraints.
  59.     You customize a GridBagConstraints object by setting one or more
  60.     of its instance variables:
  61.     <dl>
  62.     <dt> <a href=java.awt.GridBagConstraints.html#gridx>gridx</a>,
  63.          <a href=java.awt.GridBagConstraints.html#gridy>gridy</a>
  64.     <dd> Specifies the cell at the upper left of the component's display area,
  65.      where the upper-left-most cell has address gridx=0, gridy=0.
  66.      Use GridBagConstraints.RELATIVE (the default value)
  67.      to specify that the component be just placed
  68.      just to the right of (for gridx)
  69.      or just below (for gridy)
  70.      the component that was added to the container
  71.      just before this component was added.
  72.     <dt> <a href=java.awt.GridBagConstraints.html#gridwidth>gridwidth</a>,
  73.          <a href=java.awt.GridBagConstraints.html#gridheight>gridheight</a>
  74.     <dd> Specifies the number of cells in a row (for gridwidth)
  75.      or column (for gridheight)
  76.      in the component's display area.
  77.      The default value is 1.
  78.      Use GridBagConstraints.REMAINDER to specify 
  79.      that the component be the last one in its row (for gridwidth)
  80.      or column (for gridheight).
  81.      Use GridBagConstraints.RELATIVE to specify 
  82.      that the component be the next to last one
  83.      in its row (for gridwidth) or column (for gridheight).
  84.     <dt> <a href=java.awt.GridBagConstraints.html#fill>fill</a>
  85.     <dd> Used when the component's display area
  86.         is larger than the component's requested size
  87.      to determine whether (and how) to resize the component.
  88.      Valid values are
  89.           GridBagConstraint.NONE
  90.           (the default),
  91.           GridBagConstraint.HORIZONTAL
  92.           (make the component wide enough to fill its display area
  93.           horizontally, but don't change its height),
  94.           GridBagConstraint.VERTICAL
  95.           (make the component tall enough to fill its display area
  96.           vertically, but don't change its width),
  97.           and 
  98.           GridBagConstraint.BOTH
  99.           (make the component fill its display area entirely).
  100.     <dt> <a href=java.awt.GridBagConstraints.html#ipadx>ipadx</a>,
  101.          <a href=java.awt.GridBagConstraints.html#ipady>ipady</a>
  102.     <dd> Specifies the internal padding: 
  103.      how much to add to the minimum size of the component.
  104.      The width of the component will be at least
  105.      its minimum width plus ipadx*2 pixels
  106.      (since the padding applies to both sides of the component).
  107.      Similarly, the height of the component will be at least
  108.      the minimum height plus ipady*2 pixels.
  109.     <dt> <a href=java.awt.GridBagConstraints.html#insets>insets</a>
  110.     <dd> Specifies the external padding of the component --
  111.      the minimum amount of space between the component 
  112.      and the edges of its display area.
  113.     <dt> <a href=java.awt.GridBagConstraints.html#anchor>anchor</a>
  114.     <dd> Used when the component is smaller than its display area
  115.      to determine where (within the area) to place the component.
  116.      Valid values are
  117.      GridBagConstraints.CENTER (the default),
  118.      GridBagConstraints.NORTH,
  119.      GridBagConstraints.NORTHEAST,
  120.      GridBagConstraints.EAST,
  121.      GridBagConstraints.SOUTHEAST,
  122.      GridBagConstraints.SOUTH,
  123.      GridBagConstraints.SOUTHWEST,
  124.      GridBagConstraints.WEST, and
  125.      GridBagConstraints.NORTHWEST.
  126.     <dt> <a href=java.awt.GridBagConstraints.html#weightx>weightx</a>,
  127.          <a href=java.awt.GridBagConstraints.html#weighty>weighty</a>
  128.     <dd> Used to determine how to distribute space;
  129.      this is important for specifying resizing behavior.
  130.      Unless you specify a weight
  131.      for at least one component in a row (weightx)
  132.      and column (weighty),
  133.      all the components clump together in the center of
  134.      their container.
  135.      This is because when the weight is zero (the default),
  136.      the GridBagLayout puts any extra space 
  137.      between its grid of cells and the edges of the container.
  138.     </dl>
  139.  
  140.     The following figure shows ten components (all buttons)
  141.     managed by a GridBagLayout:
  142.     <blockquote>
  143.     <img src=images/java.awt/GridBagEx.gif width=262 height=155>
  144.     </blockquote>
  145.  
  146.     All the components have fill=GridBagConstraints.BOTH.
  147.     In addition, the components have the following non-default constraints:
  148.     <ul>
  149.     <li>Button1, Button2, Button3:
  150.         weightx=1.0
  151.     <li>Button4:
  152.         weightx=1.0,
  153.         gridwidth=GridBagConstraints.REMAINDER
  154.     <li>Button5:
  155.         gridwidth=GridBagConstraints.REMAINDER
  156.     <li>Button6:
  157.         gridwidth=GridBagConstraints.RELATIVE
  158.     <li>Button7:
  159.         gridwidth=GridBagConstraints.REMAINDER
  160.     <li>Button8:
  161.         gridheight=2, weighty=1.0,
  162.     <li>Button9, Button 10:
  163.         gridwidth=GridBagConstraints.REMAINDER
  164.     </ul>
  165.  
  166.     Here is the code that implements the example shown above:
  167.     <blockquote>
  168.     <pre>
  169. import java.awt.*;
  170. import java.util.*;
  171. import java.applet.Applet;
  172.  
  173. public class GridBagEx1 extends Applet {
  174.  
  175.     protected void makebutton(String name,
  176.                               GridBagLayout gridbag,
  177.                               GridBagConstraints c) {
  178.         Button button = new Button(name);
  179.         gridbag.setConstraints(button, c);
  180.         add(button);
  181.     }
  182.  
  183.     public void init() {
  184.         GridBagLayout gridbag = new GridBagLayout();
  185.         GridBagConstraints c = new GridBagConstraints();
  186.  
  187.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  188.         setLayout(gridbag);
  189.    
  190.         c.fill = GridBagConstraints.BOTH;
  191.         c.weightx = 1.0;
  192.         makebutton("Button1", gridbag, c);
  193.         makebutton("Button2", gridbag, c);
  194.         makebutton("Button3", gridbag, c);
  195.     
  196.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  197.         makebutton("Button4", gridbag, c);
  198.     
  199.         c.weightx = 0.0;           //reset to the default
  200.         makebutton("Button5", gridbag, c); //another row
  201.     
  202.     c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
  203.         makebutton("Button6", gridbag, c);
  204.     
  205.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  206.         makebutton("Button7", gridbag, c);
  207.     
  208.     c.gridwidth = 1;              //reset to the default
  209.     c.gridheight = 2;
  210.         c.weighty = 1.0;
  211.         makebutton("Button8", gridbag, c);
  212.     
  213.         c.weighty = 0.0;           //reset to the default
  214.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  215.     c.gridheight = 1;           //reset to the default
  216.         makebutton("Button9", gridbag, c);
  217.         makebutton("Button10", gridbag, c);
  218.     
  219.         resize(300, 100);
  220.     }
  221.     
  222.     public static void main(String args[]) {
  223.     Frame f = new Frame("GridBag Layout Example");
  224.     GridBagEx1 ex1 = new GridBagEx1();
  225.     
  226.     ex1.init();
  227.     
  228.     f.add("Center", ex1);
  229.     f.pack();
  230.     f.resize(f.preferredSize());
  231.     f.show();
  232.     }
  233. }
  234.     </pre>
  235.     </blockquote>
  236.  *
  237.  * @version 1.5, 16 Nov 1995
  238.  * @author Doug Stein
  239.  */
  240. public class GridBagLayout implements LayoutManager {
  241.  
  242.   protected static final int MAXGRIDSIZE = 128;
  243.   protected static final int MINSIZE = 1;
  244.   protected static final int PREFERREDSIZE = 2;
  245.  
  246.   protected Hashtable comptable;
  247.   protected GridBagConstraints defaultConstraints;
  248.   protected GridBagLayoutInfo layoutInfo;
  249.  
  250.   public int columnWidths[];
  251.   public int rowHeights[];
  252.   public double columnWeights[];
  253.   public double rowWeights[];
  254.  
  255.   /**
  256.    * Creates a gridbag layout.
  257.    */
  258.   public GridBagLayout () {
  259.     comptable = new Hashtable();
  260.     defaultConstraints = new GridBagConstraints();
  261.   }
  262.  
  263.   /**
  264.    * Sets the constraints for the specified component.
  265.    * @param comp the component to be modified
  266.    * @param constraints the constraints to be applied
  267.    */
  268.   public void setConstraints(Component comp, GridBagConstraints constraints) {
  269.     comptable.put(comp, constraints.clone());
  270.   }
  271.  
  272.   /**
  273.    * Retrieves the constraints for the specified component.  A copy of
  274.    * the constraints is returned.
  275.    * @param comp the component to be queried
  276.    */
  277.   public GridBagConstraints getConstraints(Component comp) {
  278.     GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
  279.     if (constraints == null) {
  280.       setConstraints(comp, defaultConstraints);
  281.       constraints = (GridBagConstraints)comptable.get(comp);
  282.     }
  283.     return (GridBagConstraints)constraints.clone();
  284.   }
  285.  
  286.   /**
  287.    * Retrieves the constraints for the specified component.  The return
  288.    * value is not a copy, but is the actual constraints class used by the
  289.    * layout mechanism.
  290.    * @param comp the component to be queried
  291.    */
  292.   protected GridBagConstraints lookupConstraints(Component comp) {
  293.     GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
  294.     if (constraints == null) {
  295.       setConstraints(comp, defaultConstraints);
  296.       constraints = (GridBagConstraints)comptable.get(comp);
  297.     }
  298.     return constraints;
  299.   }
  300.  
  301.   public Point getLayoutOrigin () {
  302.     Point origin = new Point(0,0);
  303.     if (layoutInfo != null) {
  304.       origin.x = layoutInfo.startx;
  305.       origin.y = layoutInfo.starty;
  306.     }
  307.     return origin;
  308.   }
  309.  
  310.   public int [][] getLayoutDimensions () {
  311.     if (layoutInfo == null)
  312.       return new int[2][0];
  313.  
  314.     int dim[][] = new int [2][];
  315.     dim[0] = new int[layoutInfo.width];
  316.     dim[1] = new int[layoutInfo.height];
  317.  
  318.     System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
  319.     System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
  320.  
  321.     return dim;
  322.   }
  323.  
  324.   public double [][] getLayoutWeights () {
  325.     if (layoutInfo == null)
  326.       return new double[2][0];
  327.  
  328.     double weights[][] = new double [2][];
  329.     weights[0] = new double[layoutInfo.width];
  330.     weights[1] = new double[layoutInfo.height];
  331.  
  332.     System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
  333.     System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
  334.  
  335.     return weights;
  336.   }
  337.  
  338.   public Point location(int x, int y) {
  339.     Point loc = new Point(0,0);
  340.     int i, d;
  341.  
  342.     if (layoutInfo == null)
  343.       return loc;
  344.  
  345.     d = layoutInfo.startx;
  346.     for (i=0; i<layoutInfo.width; i++) {
  347.       d += layoutInfo.minWidth[i];
  348.       if (d > x)
  349.     break;
  350.     }
  351.     loc.x = i;
  352.  
  353.     d = layoutInfo.starty;
  354.     for (i=0; i<layoutInfo.height; i++) {
  355.       d += layoutInfo.minHeight[i];
  356.       if (d > y)
  357.     break;
  358.     }
  359.     loc.y = i;
  360.  
  361.     return loc;
  362.   }
  363.  
  364.   /**
  365.    * Adds the specified component with the specified name to the layout.
  366.    * @param name the name of the component
  367.    * @param comp the component to be added
  368.    */
  369.   public void addLayoutComponent(String name, Component comp) {
  370.   }
  371.  
  372.   /**
  373.    * Removes the specified component from the layout. Does not apply.
  374.    * @param comp the component to be removed
  375.    */
  376.   public void removeLayoutComponent(Component comp) {
  377.   }
  378.  
  379.   /** 
  380.    * Returns the preferred dimensions for this layout given the components
  381.    * in the specified panel.
  382.    * @param parent the component which needs to be laid out 
  383.    * @see #minimumLayoutSize
  384.    */
  385.   public Dimension preferredLayoutSize(Container parent) {
  386.     GridBagLayoutInfo info = GetLayoutInfo(parent, PREFERREDSIZE);
  387.     return GetMinSize(parent, info);
  388.   }
  389.  
  390.   /**
  391.    * Returns the minimum dimensions needed to layout the components 
  392.    * contained in the specified panel.
  393.    * @param parent the component which needs to be laid out 
  394.    * @see #preferredLayoutSize
  395.    */
  396.   public Dimension minimumLayoutSize(Container parent) {
  397.     GridBagLayoutInfo info = GetLayoutInfo(parent, MINSIZE);
  398.     return GetMinSize(parent, info);
  399.   }
  400.  
  401.   /** 
  402.    * Lays out the container in the specified panel.  
  403.    * @param parent the specified component being laid out
  404.    * @see Container
  405.    */
  406.   public void layoutContainer(Container parent) {
  407.     ArrangeGrid(parent);
  408.   }
  409.  
  410.   /**
  411.    * Returns the String representation of this GridLayout's values.
  412.    */
  413.   public String toString() {
  414.     return getClass().getName();
  415.   }
  416.  
  417.   /**
  418.    * Print the layout information.  Useful for debugging.
  419.    */
  420.  
  421.   /* DEBUG
  422.    *
  423.    *  protected void DumpLayoutInfo(GridBagLayoutInfo s) {
  424.    *    int x;
  425.    *
  426.    *    System.out.println("Col\tWidth\tWeight");
  427.    *    for (x=0; x<s.width; x++) {
  428.    *      System.out.println(x + "\t" +
  429.    *             s.minWidth[x] + "\t" +
  430.    *             s.weightX[x]);
  431.    *    }
  432.    *    System.out.println("Row\tHeight\tWeight");
  433.    *    for (x=0; x<s.height; x++) {
  434.    *      System.out.println(x + "\t" +
  435.    *             s.minHeight[x] + "\t" +
  436.    *             s.weightY[x]);
  437.    *    }
  438.    *  }
  439.    */
  440.  
  441.   /**
  442.    * Print the layout constraints.  Useful for debugging.
  443.    */
  444.  
  445.   /* DEBUG
  446.    *
  447.    *  protected void DumpConstraints(GridBagConstraints constraints) {
  448.    *    System.out.println(
  449.    *               "wt " +
  450.    *               constraints.weightx +
  451.    *               " " +
  452.    *               constraints.weighty +
  453.    *               ", " +
  454.    *
  455.    *               "box " +
  456.    *               constraints.gridx +
  457.    *               " " +
  458.    *               constraints.gridy +
  459.    *               " " +
  460.    *               constraints.gridwidth +
  461.    *               " " +
  462.    *               constraints.gridheight +
  463.    *               ", " +
  464.    *
  465.    *               "min " +
  466.    *               constraints.minWidth +
  467.    *               " " +
  468.    *               constraints.minHeight +
  469.    *               ", " +
  470.    *
  471.    *               "pad " +
  472.    *               constraints.insets.bottom +
  473.    *               " " +
  474.    *               constraints.insets.left +
  475.    *               " " +
  476.    *               constraints.insets.right +
  477.    *               " " +
  478.    *               constraints.insets.top +
  479.    *               " " +
  480.    *               constraints.ipadx +
  481.    *               " " +
  482.    *               constraints.ipady);
  483.    *  }
  484.    */
  485.  
  486.   /*
  487.    * Fill in an instance of the above structure for the current set
  488.    * of managed children.  This requires three passes through the
  489.    * set of children:
  490.    *
  491.    * 1) Figure out the dimensions of the layout grid
  492.    * 2) Determine which cells the components occupy
  493.    * 3) Distribute the weights and min sizes amoung the rows/columns.
  494.    *
  495.    * This also caches the minsizes for all the children when they are
  496.    * first encountered (so subsequent loops don't need to ask again).
  497.    */
  498.   
  499.   protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
  500.     GridBagLayoutInfo r = new GridBagLayoutInfo();
  501.     Component comp;
  502.     GridBagConstraints constraints;
  503.     Dimension d;
  504.     Component components[] = parent.getComponents();
  505.  
  506.     int compindex, i, j, k, px, py, pixels_diff, nextSize;
  507.     int curX, curY, curWidth, curHeight, curRow, curCol;
  508.     double weight_diff, weight, start, size;
  509.     int xMax[], yMax[];
  510.  
  511.     /*
  512.      * Pass #1
  513.      *
  514.      * Figure out the dimensions of the layout grid (use a value of 1 for
  515.      * zero or negative widths and heights).
  516.      */
  517.     
  518.     r.width = r.height = 0;
  519.     curRow = curCol = -1;
  520.     xMax = new int[MAXGRIDSIZE];
  521.     yMax = new int[MAXGRIDSIZE];
  522.  
  523.     for (compindex = 0 ; compindex < components.length ; compindex++) {
  524.       comp = components[compindex];
  525.       if (!comp.isVisible())
  526.     continue;
  527.       constraints = lookupConstraints(comp);
  528.       
  529.       curX = constraints.gridx;
  530.       curY = constraints.gridy;
  531.       curWidth = constraints.gridwidth;
  532.       if (curWidth <= 0)
  533.     curWidth = 1;
  534.       curHeight = constraints.gridheight;
  535.       if (curHeight <= 0)
  536.     curHeight = 1;
  537.       
  538.       /* If x or y is negative, then use relative positioning: */
  539.       if (curX < 0 && curY < 0) {
  540.     if (curRow >= 0)
  541.       curY = curRow;
  542.     else if (curCol >= 0)
  543.       curX = curCol;
  544.     else
  545.       curY = 0;
  546.       }
  547.       if (curX < 0) {
  548.     px = 0;
  549.     for (i = curY; i < (curY + curHeight); i++)
  550.       px = Math.max(px, xMax[i]);
  551.     
  552.     curX = px - curX - 1;
  553.     if(curX < 0)
  554.       curX = 0;
  555.       }
  556.       else if (curY < 0) {
  557.     py = 0;
  558.     for (i = curX; i < (curX + curWidth); i++)
  559.       py = Math.max(py, yMax[i]);
  560.     
  561.     curY = py - curY - 1;
  562.     if(curY < 0)
  563.       curY = 0;
  564.       }
  565.       
  566.       /* Adjust the grid width and height */
  567.       for (px = curX + curWidth; r.width < px; r.width++);
  568.       for (py = curY + curHeight; r.height < py; r.height++);
  569.       
  570.       /* Adjust the xMax and yMax arrays */
  571.       for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
  572.       for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
  573.       
  574.       /* Cache the current slave's size. */
  575.       if (sizeflag == PREFERREDSIZE)
  576.     d = comp.preferredSize();
  577.       else
  578.     d = comp.minimumSize();
  579.       constraints.minWidth = d.width;
  580.       constraints.minHeight = d.height;
  581.       
  582.       /* Zero width and height must mean that this is the last item (or
  583.        * else something is wrong). */
  584.       if (constraints.gridheight == 0 && constraints.gridwidth == 0)
  585.     curRow = curCol = -1;
  586.       
  587.       /* Zero width starts a new row */
  588.       if (constraints.gridheight == 0 && curRow < 0)
  589.     curCol = curX + curWidth;
  590.       
  591.       /* Zero height starts a new column */
  592.       else if (constraints.gridwidth == 0 && curCol < 0)
  593.     curRow = curY + curHeight;
  594.     }
  595.     
  596.     /*
  597.      * Apply minimum row/column dimensions
  598.      */
  599.     if (columnWidths != null && r.width < columnWidths.length)
  600.       r.width = columnWidths.length;
  601.     if (rowHeights != null && r.height < rowHeights.length)
  602.       r.height = rowHeights.length;
  603.  
  604.     /*
  605.      * Pass #2
  606.      *
  607.      * Negative values for gridX are filled in with the current x value.
  608.      * Negative values for gridY are filled in with the current y value.
  609.      * Negative or zero values for gridWidth and gridHeight end the current
  610.      *  row or column, respectively.
  611.      */
  612.     
  613.     curRow = curCol = -1;
  614.     xMax = new int[MAXGRIDSIZE];
  615.     yMax = new int[MAXGRIDSIZE];
  616.     
  617.     for (compindex = 0 ; compindex < components.length ; compindex++) {
  618.       comp = components[compindex];
  619.       if (!comp.isVisible())
  620.     continue;
  621.       constraints = lookupConstraints(comp);
  622.       
  623.       curX = constraints.gridx;
  624.       curY = constraints.gridy;
  625.       curWidth = constraints.gridwidth;
  626.       curHeight = constraints.gridheight;
  627.       
  628.       /* If x or y is negative, then use relative positioning: */
  629.       if (curX < 0 && curY < 0) {
  630.     if(curRow >= 0)
  631.       curY = curRow;
  632.     else if(curCol >= 0)
  633.       curX = curCol;
  634.     else
  635.       curY = 0;
  636.       }
  637.       
  638.       if (curX < 0) {
  639.     if (curHeight <= 0) {
  640.       curHeight += r.height - curY;
  641.       if (curHeight < 1)
  642.         curHeight = 1;
  643.     }
  644.     
  645.     px = 0;
  646.     for (i = curY; i < (curY + curHeight); i++)
  647.       px = Math.max(px, xMax[i]);
  648.     
  649.     curX = px - curX - 1;
  650.     if(curX < 0)
  651.       curX = 0;
  652.       }
  653.       else if (curY < 0) {
  654.     if (curWidth <= 0) {
  655.       curWidth += r.width - curX;
  656.       if (curWidth < 1)
  657.         curWidth = 1;
  658.     }
  659.     
  660.     py = 0;
  661.     for (i = curX; i < (curX + curWidth); i++)
  662.       py = Math.max(py, yMax[i]);
  663.     
  664.     curY = py - curY - 1;
  665.     if(curY < 0)
  666.       curY = 0;
  667.       }
  668.       
  669.       if (curWidth <= 0) {
  670.     curWidth += r.width - curX;
  671.     if (curWidth < 1)
  672.       curWidth = 1;
  673.       }
  674.       
  675.       if (curHeight <= 0) {
  676.     curHeight += r.height - curY;
  677.     if (curHeight < 1)
  678.       curHeight = 1;
  679.       }
  680.       
  681.       px = curX + curWidth;
  682.       py = curY + curHeight;
  683.       
  684.       for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
  685.       for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
  686.       
  687.       /* Make negative sizes start a new row/column */
  688.       if (constraints.gridheight == 0 && constraints.gridwidth == 0)
  689.     curRow = curCol = -1;
  690.       if (constraints.gridheight == 0 && curRow < 0)
  691.     curCol = curX + curWidth;
  692.       else if (constraints.gridwidth == 0 && curCol < 0)
  693.         curRow = curY + curHeight;
  694.       
  695.       /* Assign the new values to the gridbag slave */
  696.       constraints.tempX = curX;
  697.       constraints.tempY = curY;
  698.       constraints.tempWidth = curWidth;
  699.       constraints.tempHeight = curHeight;
  700.     }
  701.     
  702.     /*
  703.      * Apply minimum row/column dimensions and weights
  704.      */
  705.     if (columnWidths != null)
  706.       System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length);
  707.     if (rowHeights != null)
  708.       System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length);
  709.     if (columnWeights != null)
  710.       System.arraycopy(columnWeights, 0, r.weightX, 0, columnWeights.length);
  711.     if (rowWeights != null)
  712.       System.arraycopy(rowWeights, 0, r.weightY, 0, rowWeights.length);
  713.  
  714.     /*
  715.      * Pass #3
  716.      *
  717.      * Distribute the minimun widths and weights:
  718.      */
  719.     
  720.     nextSize = Integer.MAX_VALUE;
  721.     
  722.     for (i = 1;
  723.      i != Integer.MAX_VALUE;
  724.      i = nextSize, nextSize = Integer.MAX_VALUE) {
  725.       for (compindex = 0 ; compindex < components.length ; compindex++) {
  726.     comp = components[compindex];
  727.     if (!comp.isVisible())
  728.       continue;
  729.     constraints = lookupConstraints(comp);
  730.       
  731.     if (constraints.tempWidth == i) {
  732.       px = constraints.tempX + constraints.tempWidth; /* right column */
  733.       
  734.       /* 
  735.        * Figure out if we should use this slave\'s weight.  If the weight
  736.        * is less than the total weight spanned by the width of the cell,
  737.        * then discard the weight.  Otherwise split the difference
  738.        * according to the existing weights.
  739.        */
  740.       
  741.       weight_diff = constraints.weightx;
  742.       for (k = constraints.tempX; k < px; k++)
  743.         weight_diff -= r.weightX[k];
  744.       if (weight_diff > 0.0) {
  745.         weight = 0.0;
  746.         for (k = constraints.tempX; k < px; k++)
  747.           weight += r.weightX[k];
  748.         for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
  749.           double wt = r.weightX[k];
  750.           double dx = (wt * weight_diff) / weight;
  751.           r.weightX[k] += dx;
  752.           weight_diff -= dx;
  753.           weight -= wt;
  754.         }
  755.         /* Assign the remainder to the rightmost cell */
  756.         r.weightX[px-1] += weight_diff;
  757.       }
  758.       
  759.       /*
  760.        * Calculate the minWidth array values.
  761.        * First, figure out how wide the current slave needs to be.
  762.        * Then, see if it will fit within the current minWidth values.
  763.        * If it will not fit, add the difference according to the
  764.        * weightX array.
  765.        */
  766.       
  767.       pixels_diff =
  768.         constraints.minWidth + constraints.ipadx +
  769.         constraints.insets.left + constraints.insets.right;
  770.  
  771.       for (k = constraints.tempX; k < px; k++)
  772.         pixels_diff -= r.minWidth[k];
  773.       if (pixels_diff > 0) {
  774.         weight = 0.0;
  775.         for (k = constraints.tempX; k < px; k++)
  776.           weight += r.weightX[k];
  777.         for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
  778.           double wt = r.weightX[k];
  779.           int dx = (int)((wt * ((double)pixels_diff)) / weight);
  780.           r.minWidth[k] += dx;
  781.           pixels_diff -= dx;
  782.           weight -= wt;
  783.         }
  784.         /* Any leftovers go into the rightmost cell */
  785.         r.minWidth[px-1] += pixels_diff;
  786.       }
  787.     }
  788.     else if (constraints.tempWidth > i && constraints.tempWidth < nextSize)
  789.       nextSize = constraints.tempWidth;
  790.     
  791.     
  792.     if (constraints.tempHeight == i) {
  793.       py = constraints.tempY + constraints.tempHeight; /* bottom row */
  794.       
  795.       /* 
  796.        * Figure out if we should use this slave\'s weight.  If the weight
  797.        * is less than the total weight spanned by the height of the cell,
  798.        * then discard the weight.  Otherwise split it the difference
  799.        * according to the existing weights.
  800.        */
  801.       
  802.       weight_diff = constraints.weighty;
  803.       for (k = constraints.tempY; k < py; k++)
  804.         weight_diff -= r.weightY[k];
  805.       if (weight_diff > 0.0) {
  806.         weight = 0.0;
  807.         for (k = constraints.tempY; k < py; k++)
  808.           weight += r.weightY[k];
  809.         for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
  810.           double wt = r.weightY[k];
  811.           double dy = (wt * weight_diff) / weight;
  812.           r.weightY[k] += dy;
  813.           weight_diff -= dy;
  814.           weight -= wt;
  815.         }
  816.         /* Assign the remainder to the bottom cell */
  817.         r.weightY[py-1] += weight_diff;
  818.       }
  819.       
  820.       /*
  821.        * Calculate the minHeight array values.
  822.        * First, figure out how tall the current slave needs to be.
  823.        * Then, see if it will fit within the current minHeight values.
  824.        * If it will not fit, add the difference according to the
  825.        * weightY array.
  826.        */
  827.       
  828.       pixels_diff =
  829.         constraints.minHeight + constraints.ipady +
  830.         constraints.insets.top + constraints.insets.bottom;
  831.       for (k = constraints.tempY; k < py; k++)
  832.         pixels_diff -= r.minHeight[k];
  833.       if (pixels_diff > 0) {
  834.         weight = 0.0;
  835.         for (k = constraints.tempY; k < py; k++)
  836.           weight += r.weightY[k];
  837.         for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
  838.           double wt = r.weightY[k];
  839.           int dy = (int)((wt * ((double)pixels_diff)) / weight);
  840.           r.minHeight[k] += dy;
  841.           pixels_diff -= dy;
  842.           weight -= wt;
  843.         }
  844.         /* Any leftovers go into the bottom cell */
  845.         r.minHeight[py-1] += pixels_diff;
  846.       }
  847.     }
  848.     else if (constraints.tempHeight > i &&
  849.          constraints.tempHeight < nextSize)
  850.       nextSize = constraints.tempHeight;
  851.       }
  852.     }
  853.  
  854.     return r;
  855.   }
  856.   
  857.   /*
  858.    * Adjusts the x, y, width, and height fields to the correct
  859.    * values depending on the constraint geometry and pads.
  860.    */
  861.   protected void AdjustForGravity(GridBagConstraints constraints,
  862.                   Rectangle r) {
  863.     int diffx, diffy;
  864.  
  865.     r.x += constraints.insets.left;
  866.     r.width -= (constraints.insets.left + constraints.insets.right);
  867.     r.y += constraints.insets.top;
  868.     r.height -= (constraints.insets.top + constraints.insets.bottom);
  869.     
  870.     diffx = 0;
  871.     if ((constraints.fill != GridBagConstraints.HORIZONTAL &&
  872.      constraints.fill != GridBagConstraints.BOTH)
  873.     && (r.width > (constraints.minWidth + constraints.ipadx))) {
  874.       diffx = r.width - (constraints.minWidth + constraints.ipadx);
  875.       r.width = constraints.minWidth + constraints.ipadx;
  876.     }
  877.     
  878.     diffy = 0;
  879.     if ((constraints.fill != GridBagConstraints.VERTICAL &&
  880.      constraints.fill != GridBagConstraints.BOTH)
  881.     && (r.height > (constraints.minHeight + constraints.ipady))) {
  882.       diffy = r.height - (constraints.minHeight + constraints.ipady);
  883.       r.height = constraints.minHeight + constraints.ipady;
  884.     }
  885.     
  886.     switch (constraints.anchor) {
  887.     case GridBagConstraints.CENTER:
  888.       r.x += diffx/2;
  889.       r.y += diffy/2;
  890.       break;
  891.     case GridBagConstraints.NORTH:
  892.       r.x += diffx/2;
  893.       break;
  894.     case GridBagConstraints.NORTHEAST:
  895.       r.x += diffx;
  896.       break;
  897.     case GridBagConstraints.EAST:
  898.       r.x += diffx;
  899.       r.y += diffy/2;
  900.       break;
  901.     case GridBagConstraints.SOUTHEAST:
  902.       r.x += diffx;
  903.       r.y += diffy;
  904.       break;
  905.     case GridBagConstraints.SOUTH:
  906.       r.x += diffx/2;
  907.       r.y += diffy;
  908.       break;
  909.     case GridBagConstraints.SOUTHWEST:
  910.       r.y += diffy;
  911.       break;
  912.     case GridBagConstraints.WEST:
  913.       r.y += diffy/2;
  914.       break;
  915.     case GridBagConstraints.NORTHWEST:
  916.       break;
  917.     default:
  918.       throw new IllegalArgumentException("illegal anchor value");
  919.     }
  920.   }
  921.  
  922.   /*
  923.    * Figure out the minimum size of the
  924.    * master based on the information from GetLayoutInfo()
  925.    */
  926.   protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info) {
  927.     Dimension d = new Dimension();
  928.     int i, t;
  929.     Insets insets = parent.insets();
  930.  
  931.     t = 0;
  932.     for(i = 0; i < info.width; i++)
  933.       t += info.minWidth[i];
  934.     d.width = t + insets.left + insets.right;
  935.  
  936.     t = 0;
  937.     for(i = 0; i < info.height; i++)
  938.       t += info.minHeight[i];
  939.     d.height = t + insets.top + insets.bottom;
  940.  
  941.     return d;
  942.   }
  943.  
  944.   /*
  945.    * Lay out the grid
  946.    */
  947.   protected void ArrangeGrid(Container parent) {
  948.     Component comp;
  949.     int compindex;
  950.     GridBagConstraints constraints;
  951.     Insets insets = parent.insets();
  952.     Component components[] = parent.getComponents();
  953.     Dimension d;
  954.     Rectangle r = new Rectangle();
  955.     int i, diffw, diffh;
  956.     double weight;
  957.     GridBagLayoutInfo info;
  958.     
  959.     /*
  960.      * If the parent has no slaves anymore, then don't do anything
  961.      * at all:  just leave the parent's size as-is.
  962.      */
  963.     if (components.length == 0 &&
  964.     (columnWidths == null || columnWidths.length == 0) &&
  965.     (rowHeights == null || rowHeights.length == 0)) {
  966.       return;
  967.     }
  968.     
  969.     /*
  970.      * Pass #1: scan all the slaves to figure out the total amount
  971.      * of space needed.
  972.      */
  973.     
  974.     info = GetLayoutInfo(parent, PREFERREDSIZE);
  975.     d = GetMinSize(parent, info);
  976.  
  977.     if (d.width < parent.width || d.height < parent.height) {
  978.       info = GetLayoutInfo(parent, MINSIZE);
  979.       d = GetMinSize(parent, info);
  980.     }
  981.  
  982.     layoutInfo = info;
  983.     r.width = d.width;
  984.     r.height = d.height;
  985.  
  986.     /*
  987.      * DEBUG
  988.      *
  989.      * DumpLayoutInfo(info);
  990.      * for (compindex = 0 ; compindex < components.length ; compindex++) {
  991.      * comp = components[compindex];
  992.      * if (!comp.isVisible())
  993.      *    continue;
  994.      * constraints = lookupConstraints(comp);
  995.      * DumpConstraints(constraints);
  996.      * }
  997.      * System.out.println("minSize " + r.width + " " + r.height);
  998.      */
  999.     
  1000.     /*
  1001.      * If the current dimensions of the window don't match the desired
  1002.      * dimensions, then adjust the minWidth and minHeight arrays
  1003.      * according to the weights.
  1004.      */
  1005.     
  1006.     diffw = parent.width - r.width;
  1007.     if (diffw != 0) {
  1008.       weight = 0.0;
  1009.       for (i = 0; i < info.width; i++)
  1010.     weight += info.weightX[i];
  1011.       if (weight > 0.0) {
  1012.     for (i = 0; i < info.width; i++) {
  1013.       int dx = (int)(( ((double)diffw) * info.weightX[i]) / weight);
  1014.       info.minWidth[i] += dx;
  1015.       r.width += dx;
  1016.       if (info.minWidth[i] < 0) {
  1017.         r.width -= info.minWidth[i];
  1018.         info.minWidth[i] = 0;
  1019.       }
  1020.     }
  1021.       }
  1022.       diffw = parent.width - r.width;
  1023.     }
  1024.     else {
  1025.       diffw = 0;
  1026.     }
  1027.     
  1028.     diffh = parent.height - r.height;
  1029.     if (diffh != 0) {
  1030.       weight = 0.0;
  1031.       for (i = 0; i < info.height; i++)
  1032.     weight += info.weightY[i];
  1033.       if (weight > 0.0) {
  1034.     for (i = 0; i < info.height; i++) {
  1035.       int dy = (int)(( ((double)diffh) * info.weightY[i]) / weight);
  1036.       info.minHeight[i] += dy;
  1037.       r.height += dy;
  1038.       if (info.minHeight[i] < 0) {
  1039.         r.height -= info.minHeight[i];
  1040.         info.minHeight[i] = 0;
  1041.       }
  1042.     }
  1043.       }
  1044.       diffh = parent.height - r.height;
  1045.     }
  1046.     else {
  1047.       diffh = 0;
  1048.     }
  1049.  
  1050.     /*
  1051.      * DEBUG
  1052.      *
  1053.      * System.out.println("Re-adjusted:");
  1054.      * DumpLayoutInfo(info);
  1055.      */
  1056.     
  1057.     /*
  1058.      * Now do the actual layout of the slaves using the layout information
  1059.      * that has been collected.
  1060.      */
  1061.     
  1062.     info.startx = diffw/2 + insets.left;
  1063.     info.starty = diffh/2 + insets.top;
  1064.  
  1065.     for (compindex = 0 ; compindex < components.length ; compindex++) {
  1066.       comp = components[compindex];
  1067.       if (!comp.isVisible())
  1068.     continue;
  1069.       constraints = lookupConstraints(comp);
  1070.  
  1071.       r.x = info.startx;
  1072.       for(i = 0; i < constraints.tempX; i++)
  1073.     r.x += info.minWidth[i];
  1074.       
  1075.       r.y = info.starty;
  1076.       for(i = 0; i < constraints.tempY; i++)
  1077.     r.y += info.minHeight[i];
  1078.       
  1079.       r.width = 0;
  1080.       for(i = constraints.tempX;
  1081.       i < (constraints.tempX + constraints.tempWidth);
  1082.       i++) {
  1083.     r.width += info.minWidth[i];
  1084.       }
  1085.       
  1086.       r.height = 0;
  1087.       for(i = constraints.tempY;
  1088.       i < (constraints.tempY + constraints.tempHeight);
  1089.       i++) {
  1090.     r.height += info.minHeight[i];
  1091.       }
  1092.       
  1093.       AdjustForGravity(constraints, r);
  1094.       
  1095.       /*
  1096.        * If the window is too small to be interesting then
  1097.        * unmap it.  Otherwise configure it and then make sure
  1098.        * it's mapped.
  1099.        */
  1100.       
  1101.       if ((r.width <= 0) || (r.height <= 0)) {
  1102.     comp.reshape(0, 0, 0, 0);
  1103.       }
  1104.       else {
  1105.     if (comp.x != r.x || comp.y != r.y ||
  1106.         comp.width != r.width || comp.height != r.height) {
  1107.       comp.reshape(r.x, r.y, r.width, r.height);
  1108.     }
  1109.       }
  1110.     }
  1111.   }
  1112. }
  1113.