@Override
    public Dimension preferredLayoutSize(Container parent) {
      Dimension result = new Dimension();

      JScrollPane pane = (JScrollPane)parent;
      JBInsets.addTo(result, pane.getInsets());

      Border border = pane.getViewportBorder();
      if (border != null) JBInsets.addTo(result, border.getBorderInsets(parent));

      int vsbPolicy = pane.getVerticalScrollBarPolicy();
      int hsbPolicy = pane.getHorizontalScrollBarPolicy();
      if (viewport != null) {
        Component view = viewport.getView();
        if (view != null) {
          Point viewLocation = view.getLocation();
          Dimension viewportExtentSize = viewport.getPreferredSize();
          if (viewportExtentSize == null) viewportExtentSize = new Dimension();
          Dimension viewPreferredSize = view.getPreferredSize();
          if (viewPreferredSize == null) viewPreferredSize = new Dimension();
          if (view instanceof JComponent && !view.isPreferredSizeSet()) {
            JBInsets.removeFrom(viewPreferredSize, JBViewport.getViewInsets((JComponent)view));
          }
          result.width += viewportExtentSize.width;
          result.height += viewportExtentSize.height;
          if (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED) {
            if (viewportExtentSize.height < viewPreferredSize.height &&
                isVerticalScrollBarNeeded(view, viewLocation, viewPreferredSize, viewportExtentSize)) {
              vsbPolicy = VERTICAL_SCROLLBAR_ALWAYS;
            }
          }
          if (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED) {
            if (viewportExtentSize.width < viewPreferredSize.width &&
                isHorizontalScrollBarNeeded(view, viewLocation, viewPreferredSize, viewportExtentSize)) {
              hsbPolicy = HORIZONTAL_SCROLLBAR_ALWAYS;
            }
          }
        }
      }

      boolean isOverlappingScrollBar = isOverlappingScrollBar(pane);

      // disabled scroll bars should be minimized (see #adjustForVSB and #adjustForHSB)
      if (vsb != null && vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS && vsb.isEnabled() && ! isOverlappingScrollBar) result.width += vsb.getPreferredSize().width;
      if (hsb != null && hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS && hsb.isEnabled() && !isOverlappingScrollBar) result.height += hsb.getPreferredSize().height;

      if (rowHead != null && rowHead.isVisible()) result.width += rowHead.getPreferredSize().width;
      if (colHead != null && colHead.isVisible()) result.height += colHead.getPreferredSize().height;

      return result;
    }
@Override
    public Dimension preferredLayoutSize(Container parent)