Пример #1
0
  /**
   * Calculates the preferred size dimensions for the specified panel given the components in the
   * specified parent container.
   *
   * @see #minimumLayoutSize
   * @param target The component to be laid out.
   * @return A size deemed suitable for laying out the container.
   */
  public Dimension preferredLayoutSize(Container target) {
    int count;
    Container parent;
    Component component;
    Point point;
    Dimension dimension;
    Insets insets;
    Dimension ret;

    synchronized (target.getTreeLock()) {
      count = target.getComponentCount();
      if (0 == count) {
        // be the same size unless we have a parent
        ret = target.getSize();
        parent = target.getParent();
        if (null != parent) {
          insets = parent.getInsets();
          ret = parent.getSize();
          ret.setSize(
              ret.width - insets.left - insets.right, ret.height - insets.top - insets.bottom);
        }
      } else {
        ret = new Dimension(0, 0);
        for (int i = 0; i < count; i++) {
          component = target.getComponent(i);
          if (component.isVisible()) {
            point = component.getLocation();
            dimension = component.getPreferredSize();
            ret.width = Math.max(ret.width, point.x + dimension.width);
            ret.height = Math.max(ret.height, point.y + dimension.height);
          }
        }
        insets = target.getInsets();
        ret.width += insets.left + insets.right;
        ret.height += insets.top + insets.bottom;
      }
    }

    return (ret);
  }