// Ideally we shouldn't have to calculate this when laying out every widget, only when
// some widget has changed, c.f. java.awt.LayoutManager2.invalidateLayout()
updateAllSeparators();
// Fit the current widget into the appropriate cell
Rectangle rect = (mFather.getChildsRectangle() == null) ? mFather.getSize() : mFather.getChildsRectangle();
BorderLayoutConstraint cstr = (BorderLayoutConstraint)constraint;
Rectangle prefSize = widget.getPreferredSize();
int prefWidth = prefSize.getWidth();
int prefHeight = prefSize.getHeight();
int maxWidth = rect.getWidth(); // Cell width
int maxHeight = rect.getHeight();// Cell height
int x = 0; // Cell x offset
int y = 0; // Cell y offset
switch (cstr.mPosition) {
case NORTH:
x = 0; y = 0;
maxHeight = mYSep[0];
break;
case SOUTH:
x = 0; y = mYSep[1];
maxHeight -= mYSep[1];
break;
case CENTER:
x = mXSep[0]; y = mYSep[0];
maxWidth = mXSep[1] - mXSep[0];
maxHeight = mYSep[1] - mYSep[0];
break;
case WEST:
x = 0; y = mYSep[0];
maxWidth = mXSep[0];
maxHeight = mYSep[1] - mYSep[0];
break;
case EAST:
x = mXSep[1]; y = mYSep[0];
maxWidth -= mXSep[1];
maxHeight = mYSep[1] - mYSep[0];
break;
default:
throw new IllegalStateException("Unknown position for widget: " + cstr.mPosition);
}
if (prefWidth <= 0) {
prefWidth = maxWidth;
}
if (prefHeight <= 0) {
prefHeight = maxHeight;
}
/*
Protocol.debug("Widget prelayout for cell " + cstr.mPosition + " is offset(" + x + "," + y + ")" +
" maxsize(" + maxWidth + "," + maxHeight + ")");
*/
int width = 0;
int height = 0;
if (prefWidth < maxWidth) {
widget.setX(getAlignedCoordinate(prefWidth, maxWidth, x, cstr.mHorizontalConstraint));
width = prefWidth;
} else {
widget.setX(x);
width = maxWidth;
}
if (prefHeight < maxHeight) {
widget.setY(getAlignedCoordinate(prefHeight, maxHeight, y, cstr.mVerticalConstraint));
height = prefHeight;
} else {
widget.setY(y);
height = maxHeight;
}
/*
Protocol.debug("Widget layout for cell " + cstr.mPosition + " is offset(" + widget.getX() + "," + widget.getY() + ")" +
" maxsize(" + width + "," + height + ")");
*/
widget.setSize(new Rectangle(width, height));
}