int height = getHeight();
boolean constrainWidth = (horizontalPolicy == ScrollBarPolicy.FILL);
boolean constrainHeight = (verticalPolicy == ScrollBarPolicy.FILL);
Component view = scrollPane.getView();
Component columnHeader = scrollPane.getColumnHeader();
Component rowHeader = scrollPane.getRowHeader();
Component corner = scrollPane.getCorner();
int rowHeaderWidth = 0;
if (rowHeader != null) {
rowHeaderWidth = rowHeader.getPreferredWidth(-1);
}
int columnHeaderHeight = 0;
if (columnHeader != null) {
columnHeaderHeight = columnHeader.getPreferredHeight(-1);
}
int previousViewWidth, viewWidth = 0;
int previousViewHeight, viewHeight = 0;
int previousHorizontalScrollBarHeight, horizontalScrollBarHeight = cachedHorizontalScrollBarHeight;
int previousVerticalScrollBarWidth, verticalScrollBarWidth = cachedVerticalScrollBarWidth;
int i = 0;
do {
previousViewWidth = viewWidth;
previousViewHeight = viewHeight;
previousHorizontalScrollBarHeight = horizontalScrollBarHeight;
previousVerticalScrollBarWidth = verticalScrollBarWidth;
if (view != null) {
if (constrainWidth && constrainHeight) {
viewWidth = Math.max
(width - rowHeaderWidth - verticalScrollBarWidth, 0);
viewHeight = Math.max
(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
} else if (constrainWidth) {
viewWidth = Math.max
(width - rowHeaderWidth - verticalScrollBarWidth, 0);
viewHeight = view.getPreferredHeight(viewWidth);
} else if (constrainHeight) {
viewHeight = Math.max
(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
viewWidth = view.getPreferredWidth(viewHeight);
} else {
Dimensions viewPreferredSize = view.getPreferredSize();
viewWidth = viewPreferredSize.width;
viewHeight = viewPreferredSize.height;
}
}
if (horizontalPolicy == ScrollBarPolicy.ALWAYS
|| (horizontalPolicy == ScrollBarPolicy.AUTO
&& viewWidth > width - rowHeaderWidth - verticalScrollBarWidth)) {
horizontalScrollBarHeight = horizontalScrollBar.getPreferredHeight(-1);
} else {
horizontalScrollBarHeight = 0;
}
if (verticalPolicy == ScrollBarPolicy.ALWAYS
|| (verticalPolicy == ScrollBarPolicy.AUTO
&& viewHeight > height - columnHeaderHeight - horizontalScrollBarHeight)) {
verticalScrollBarWidth = verticalScrollBar.getPreferredWidth(-1);
} else {
verticalScrollBarWidth = 0;
}
if (++i > 4) {
// Infinite loop protection
System.err.println("Breaking out of potential infinite loop");
break;
}
} while (viewWidth != previousViewWidth
|| viewHeight != previousViewHeight
|| horizontalScrollBarHeight != previousHorizontalScrollBarHeight
|| verticalScrollBarWidth != previousVerticalScrollBarWidth);
int scrollTop = scrollPane.getScrollTop();
int scrollLeft = scrollPane.getScrollLeft();
if (view != null) {
view.setSize(viewWidth, viewHeight);
view.setLocation(rowHeaderWidth - scrollLeft, columnHeaderHeight - scrollTop);
}
if (columnHeader != null) {
columnHeader.setSize(viewWidth, columnHeaderHeight);
columnHeader.setLocation(rowHeaderWidth - scrollLeft, 0);
}
if (rowHeader != null) {
rowHeader.setSize(rowHeaderWidth, viewHeight);
rowHeader.setLocation(0, columnHeaderHeight - scrollTop);
}
if (horizontalScrollBarHeight > 0) {
horizontalScrollBar.setVisible(true);
int horizontalScrollBarWidth = Math.max
(width - rowHeaderWidth - verticalScrollBarWidth, 0);
horizontalScrollBar.setSize(horizontalScrollBarWidth,
horizontalScrollBarHeight);
horizontalScrollBar.setLocation(rowHeaderWidth,
height - horizontalScrollBarHeight);
} else {
horizontalScrollBar.setVisible(false);
}
if (verticalScrollBarWidth > 0) {
verticalScrollBar.setVisible(true);
int verticalScrollBarHeight = Math.max
(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
verticalScrollBar.setSize(verticalScrollBarWidth,
verticalScrollBarHeight);
verticalScrollBar.setLocation(width - verticalScrollBarWidth,
columnHeaderHeight);
} else {
verticalScrollBar.setVisible(false);
}
// Handle corner components
if (columnHeaderHeight > 0
&& rowHeaderWidth > 0) {
if (corner != null) {
corner.setVisible(true);
corner.setSize(rowHeaderWidth, columnHeaderHeight);
corner.setLocation(0, 0);
topLeftCorner.setVisible(false);
} else {
topLeftCorner.setVisible(true);
topLeftCorner.setSize(rowHeaderWidth, columnHeaderHeight);
topLeftCorner.setLocation(0, 0);
}
} else {
if (corner != null) {
corner.setVisible(false);
}
topLeftCorner.setVisible(false);
}