/**
   * This method is obsolete and supplied for backwards compatability only; new code should call
   * {@link #getLayoutInfo(Container, int) getLayoutInfo} instead.
   */
  protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
    synchronized (parent.getTreeLock()) {
      GridBagLayoutInfo r = new GridBagLayoutInfo();
      Component comp;
      GridBagConstraints constraints;
      Dimension d;
      Component components[] = parent.getComponents();

      int compindex, i, j, k, px, py, pixels_diff, nextSize;
      int curX, curY, curWidth, curHeight, curRow, curCol;
      double weight_diff, weight, start, size;
      int xMax[], yMax[];

      /*
       * Pass #1
       *
       * Figure out the dimensions of the layout grid (use a value of 1 for
       * zero or negative widths and heights).
       */

      r.width = r.height = 0;
      curRow = curCol = -1;
      xMax = new int[MAXGRIDSIZE];
      yMax = new int[MAXGRIDSIZE];

      for (compindex = 0; compindex < components.length; compindex++) {
        comp = components[compindex];
        if (!comp.isVisible()) continue;
        constraints = lookupConstraints(comp);

        curX = constraints.gridx;
        curY = constraints.gridy;
        curWidth = constraints.gridwidth;
        if (curWidth <= 0) curWidth = 1;
        curHeight = constraints.gridheight;
        if (curHeight <= 0) curHeight = 1;

        /* If x or y is negative, then use relative positioning: */
        if (curX < 0 && curY < 0) {
          if (curRow >= 0) curY = curRow;
          else if (curCol >= 0) curX = curCol;
          else curY = 0;
        }
        if (curX < 0) {
          px = 0;
          for (i = curY; i < (curY + curHeight); i++) px = Math.max(px, xMax[i]);

          curX = px - curX - 1;
          if (curX < 0) curX = 0;
        } else if (curY < 0) {
          py = 0;
          for (i = curX; i < (curX + curWidth); i++) py = Math.max(py, yMax[i]);

          curY = py - curY - 1;
          if (curY < 0) curY = 0;
        }

        /* Adjust the grid width and height */
        for (px = curX + curWidth; r.width < px; r.width++) ;
        for (py = curY + curHeight; r.height < py; r.height++) ;

        /* Adjust the xMax and yMax arrays */
        for (i = curX; i < (curX + curWidth); i++) {
          yMax[i] = py;
        }
        for (i = curY; i < (curY + curHeight); i++) {
          xMax[i] = px;
        }

        /* Cache the current slave's size. */
        if (sizeflag == PREFERREDSIZE) d = comp.getPreferredSize();
        else d = comp.getMinimumSize();
        constraints.minWidth = d.width;
        constraints.minHeight = d.height;

        /* Zero width and height must mean that this is the last item (or
         * else something is wrong). */
        if (constraints.gridheight == 0 && constraints.gridwidth == 0) curRow = curCol = -1;

        /* Zero width starts a new row */
        if (constraints.gridheight == 0 && curRow < 0) curCol = curX + curWidth;

        /* Zero height starts a new column */
        else if (constraints.gridwidth == 0 && curCol < 0) curRow = curY + curHeight;
      }

      /*
       * Apply minimum row/column dimensions
       */
      if (columnWidths != null && r.width < columnWidths.length) r.width = columnWidths.length;
      if (rowHeights != null && r.height < rowHeights.length) r.height = rowHeights.length;

      /*
       * Pass #2
       *
       * Negative values for gridX are filled in with the current x value.
       * Negative values for gridY are filled in with the current y value.
       * Negative or zero values for gridWidth and gridHeight end the current
       *  row or column, respectively.
       */

      curRow = curCol = -1;
      xMax = new int[MAXGRIDSIZE];
      yMax = new int[MAXGRIDSIZE];

      for (compindex = 0; compindex < components.length; compindex++) {
        comp = components[compindex];
        if (!comp.isVisible()) continue;
        constraints = lookupConstraints(comp);

        curX = constraints.gridx;
        curY = constraints.gridy;
        curWidth = constraints.gridwidth;
        curHeight = constraints.gridheight;

        /* If x or y is negative, then use relative positioning: */
        if (curX < 0 && curY < 0) {
          if (curRow >= 0) curY = curRow;
          else if (curCol >= 0) curX = curCol;
          else curY = 0;
        }

        if (curX < 0) {
          if (curHeight <= 0) {
            curHeight += r.height - curY;
            if (curHeight < 1) curHeight = 1;
          }

          px = 0;
          for (i = curY; i < (curY + curHeight); i++) px = Math.max(px, xMax[i]);

          curX = px - curX - 1;
          if (curX < 0) curX = 0;
        } else if (curY < 0) {
          if (curWidth <= 0) {
            curWidth += r.width - curX;
            if (curWidth < 1) curWidth = 1;
          }

          py = 0;
          for (i = curX; i < (curX + curWidth); i++) py = Math.max(py, yMax[i]);

          curY = py - curY - 1;
          if (curY < 0) curY = 0;
        }

        if (curWidth <= 0) {
          curWidth += r.width - curX;
          if (curWidth < 1) curWidth = 1;
        }

        if (curHeight <= 0) {
          curHeight += r.height - curY;
          if (curHeight < 1) curHeight = 1;
        }

        px = curX + curWidth;
        py = curY + curHeight;

        for (i = curX; i < (curX + curWidth); i++) {
          yMax[i] = py;
        }
        for (i = curY; i < (curY + curHeight); i++) {
          xMax[i] = px;
        }

        /* Make negative sizes start a new row/column */
        if (constraints.gridheight == 0 && constraints.gridwidth == 0) curRow = curCol = -1;
        if (constraints.gridheight == 0 && curRow < 0) curCol = curX + curWidth;
        else if (constraints.gridwidth == 0 && curCol < 0) curRow = curY + curHeight;

        /* Assign the new values to the gridbag slave */
        constraints.tempX = curX;
        constraints.tempY = curY;
        constraints.tempWidth = curWidth;
        constraints.tempHeight = curHeight;
      }

      /*
       * Apply minimum row/column dimensions and weights
       */
      if (columnWidths != null)
        System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length);
      if (rowHeights != null) System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length);
      if (columnWeights != null)
        System.arraycopy(columnWeights, 0, r.weightX, 0, columnWeights.length);
      if (rowWeights != null) System.arraycopy(rowWeights, 0, r.weightY, 0, rowWeights.length);

      /*
       * Pass #3
       *
       * Distribute the minimun widths and weights:
       */

      nextSize = Integer.MAX_VALUE;

      for (i = 1; i != Integer.MAX_VALUE; i = nextSize, nextSize = Integer.MAX_VALUE) {
        for (compindex = 0; compindex < components.length; compindex++) {
          comp = components[compindex];
          if (!comp.isVisible()) continue;
          constraints = lookupConstraints(comp);

          if (constraints.tempWidth == i) {
            px = constraints.tempX + constraints.tempWidth; /* right column */

            /*
             * Figure out if we should use this slave\'s weight.  If the weight
             * is less than the total weight spanned by the width of the cell,
             * then discard the weight.  Otherwise split the difference
             * according to the existing weights.
             */

            weight_diff = constraints.weightx;
            for (k = constraints.tempX; k < px; k++) weight_diff -= r.weightX[k];
            if (weight_diff > 0.0) {
              weight = 0.0;
              for (k = constraints.tempX; k < px; k++) weight += r.weightX[k];
              for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
                double wt = r.weightX[k];
                double dx = (wt * weight_diff) / weight;
                r.weightX[k] += dx;
                weight_diff -= dx;
                weight -= wt;
              }
              /* Assign the remainder to the rightmost cell */
              r.weightX[px - 1] += weight_diff;
            }

            /*
             * Calculate the minWidth array values.
             * First, figure out how wide the current slave needs to be.
             * Then, see if it will fit within the current minWidth values.
             * If it will not fit, add the difference according to the
             * weightX array.
             */

            pixels_diff =
                constraints.minWidth
                    + constraints.ipadx
                    + constraints.insets.left
                    + constraints.insets.right;

            for (k = constraints.tempX; k < px; k++) pixels_diff -= r.minWidth[k];
            if (pixels_diff > 0) {
              weight = 0.0;
              for (k = constraints.tempX; k < px; k++) weight += r.weightX[k];
              for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
                double wt = r.weightX[k];
                int dx = (int) ((wt * ((double) pixels_diff)) / weight);
                r.minWidth[k] += dx;
                pixels_diff -= dx;
                weight -= wt;
              }
              /* Any leftovers go into the rightmost cell */
              r.minWidth[px - 1] += pixels_diff;
            }
          } else if (constraints.tempWidth > i && constraints.tempWidth < nextSize)
            nextSize = constraints.tempWidth;

          if (constraints.tempHeight == i) {
            py = constraints.tempY + constraints.tempHeight; /* bottom row */

            /*
             * Figure out if we should use this slave's weight.  If the weight
             * is less than the total weight spanned by the height of the cell,
             * then discard the weight.  Otherwise split it the difference
             * according to the existing weights.
             */

            weight_diff = constraints.weighty;
            for (k = constraints.tempY; k < py; k++) weight_diff -= r.weightY[k];
            if (weight_diff > 0.0) {
              weight = 0.0;
              for (k = constraints.tempY; k < py; k++) weight += r.weightY[k];
              for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
                double wt = r.weightY[k];
                double dy = (wt * weight_diff) / weight;
                r.weightY[k] += dy;
                weight_diff -= dy;
                weight -= wt;
              }
              /* Assign the remainder to the bottom cell */
              r.weightY[py - 1] += weight_diff;
            }

            /*
             * Calculate the minHeight array values.
             * First, figure out how tall the current slave needs to be.
             * Then, see if it will fit within the current minHeight values.
             * If it will not fit, add the difference according to the
             * weightY array.
             */

            pixels_diff =
                constraints.minHeight
                    + constraints.ipady
                    + constraints.insets.top
                    + constraints.insets.bottom;
            for (k = constraints.tempY; k < py; k++) pixels_diff -= r.minHeight[k];
            if (pixels_diff > 0) {
              weight = 0.0;
              for (k = constraints.tempY; k < py; k++) weight += r.weightY[k];
              for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
                double wt = r.weightY[k];
                int dy = (int) ((wt * ((double) pixels_diff)) / weight);
                r.minHeight[k] += dy;
                pixels_diff -= dy;
                weight -= wt;
              }
              /* Any leftovers go into the bottom cell */
              r.minHeight[py - 1] += pixels_diff;
            }
          } else if (constraints.tempHeight > i && constraints.tempHeight < nextSize)
            nextSize = constraints.tempHeight;
        }
      }

      return r;
    }
  }
  /**
   * This method is obsolete and supplied for backwards compatability only; new code should call
   * {@link #arrangeGrid(Container) arrangeGrid} instead.
   */
  protected void ArrangeGrid(Container parent) {
    Component comp;
    int compindex;
    GridBagConstraints constraints;
    Insets insets = parent.getInsets();
    Component components[] = parent.getComponents();
    Dimension d;
    Rectangle r = new Rectangle();
    int i, diffw, diffh;
    double weight;
    GridBagLayoutInfo info;

    rightToLeft = !parent.getComponentOrientation().isLeftToRight();

    /*
     * If the parent has no slaves anymore, then don't do anything
     * at all:  just leave the parent's size as-is.
     */
    if (components.length == 0
        && (columnWidths == null || columnWidths.length == 0)
        && (rowHeights == null || rowHeights.length == 0)) {
      return;
    }

    /*
     * Pass #1: scan all the slaves to figure out the total amount
     * of space needed.
     */

    info = getLayoutInfo(parent, PREFERREDSIZE);
    d = getMinSize(parent, info);

    if (parent.width < d.width || parent.height < d.height) {
      info = getLayoutInfo(parent, MINSIZE);
      d = getMinSize(parent, info);
    }

    layoutInfo = info;
    r.width = d.width;
    r.height = d.height;

    /*
     * DEBUG
     *
     * DumpLayoutInfo(info);
     * for (compindex = 0 ; compindex < components.length ; compindex++) {
     * comp = components[compindex];
     * if (!comp.isVisible())
     *	continue;
     * constraints = lookupConstraints(comp);
     * DumpConstraints(constraints);
     * }
     * System.out.println("minSize " + r.width + " " + r.height);
     */

    /*
     * If the current dimensions of the window don't match the desired
     * dimensions, then adjust the minWidth and minHeight arrays
     * according to the weights.
     */

    diffw = parent.width - r.width;
    if (diffw != 0) {
      weight = 0.0;
      for (i = 0; i < info.width; i++) weight += info.weightX[i];
      if (weight > 0.0) {
        for (i = 0; i < info.width; i++) {
          int dx = (int) ((((double) diffw) * info.weightX[i]) / weight);
          info.minWidth[i] += dx;
          r.width += dx;
          if (info.minWidth[i] < 0) {
            r.width -= info.minWidth[i];
            info.minWidth[i] = 0;
          }
        }
      }
      diffw = parent.width - r.width;
    } else {
      diffw = 0;
    }

    diffh = parent.height - r.height;
    if (diffh != 0) {
      weight = 0.0;
      for (i = 0; i < info.height; i++) weight += info.weightY[i];
      if (weight > 0.0) {
        for (i = 0; i < info.height; i++) {
          int dy = (int) ((((double) diffh) * info.weightY[i]) / weight);
          info.minHeight[i] += dy;
          r.height += dy;
          if (info.minHeight[i] < 0) {
            r.height -= info.minHeight[i];
            info.minHeight[i] = 0;
          }
        }
      }
      diffh = parent.height - r.height;
    } else {
      diffh = 0;
    }

    /*
     * DEBUG
     *
     * System.out.println("Re-adjusted:");
     * DumpLayoutInfo(info);
     */

    /*
     * Now do the actual layout of the slaves using the layout information
     * that has been collected.
     */

    info.startx = diffw / 2 + insets.left;
    info.starty = diffh / 2 + insets.top;

    for (compindex = 0; compindex < components.length; compindex++) {
      comp = components[compindex];
      if (!comp.isVisible()) continue;
      constraints = lookupConstraints(comp);

      if (!rightToLeft) {
        r.x = info.startx;
        for (i = 0; i < constraints.tempX; i++) r.x += info.minWidth[i];
      } else {
        r.x = parent.width - (diffw / 2 + insets.right);
        for (i = 0; i < constraints.tempX; i++) r.x -= info.minWidth[i];
      }

      r.y = info.starty;
      for (i = 0; i < constraints.tempY; i++) r.y += info.minHeight[i];

      r.width = 0;
      for (i = constraints.tempX; i < (constraints.tempX + constraints.tempWidth); i++) {
        r.width += info.minWidth[i];
      }

      r.height = 0;
      for (i = constraints.tempY; i < (constraints.tempY + constraints.tempHeight); i++) {
        r.height += info.minHeight[i];
      }

      adjustForGravity(constraints, r);

      /* fix for 4408108 - components were being created outside of the container */
      if (r.x < 0) {
        r.width -= r.x;
        r.x = 0;
      }

      if (r.y < 0) {
        r.height -= r.y;
        r.y = 0;
      }

      /*
       * If the window is too small to be interesting then
       * unmap it.  Otherwise configure it and then make sure
       * it's mapped.
       */

      if ((r.width <= 0) || (r.height <= 0)) {
        comp.setBounds(0, 0, 0, 0);
      } else {
        if (comp.x != r.x || comp.y != r.y || comp.width != r.width || comp.height != r.height) {
          comp.setBounds(r.x, r.y, r.width, r.height);
        }
      }
    }
  }