} else {
layoutData = new BoxLayoutData();
widget.setLayoutData(layoutData);
}
Margins cm = layoutData.getMargins();
if (cm == null) {
cm = new Margins(0);
layoutData.setMargins(cm);
}
}
if (findWidth || findHeight) {
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
if (!widget.isVisible()) {
continue;
}
BoxLayoutData layoutData = (BoxLayoutData) widget.getLayoutData();
Margins cm = layoutData.getMargins();
calculateHeight += widget.getOffsetHeight();
maxWidgetWidth = Math.max(maxWidgetWidth, widget.getOffsetWidth());
calculateHeight += (cm.getTop() + cm.getBottom());
maxMarginLeft = Math.max(maxMarginLeft, cm.getLeft());
maxMarginRight = Math.max(maxMarginRight, cm.getRight());
}
maxWidgetWidth += (maxMarginLeft + maxMarginRight);
if (findHeight) {
h = calculateHeight;
}
if (findWidth) {
w = maxWidgetWidth;
}
}
int pl = 0;
int pt = 0;
int pb = 0;
int pr = 0;
if (getPadding() != null) {
pl = getPadding().getLeft();
pt = getPadding().getTop();
pb = getPadding().getBottom();
pr = getPadding().getRight();
}
if (findHeight) {
h += pt + pb;
}
if (findWidth) {
w += pl + pr;
}
int stretchWidth = w - pl - pr;
int totalFlex = 0;
int totalHeight = 0;
int maxWidth = 0;
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
widget.addStyleName(CommonStyles.get().positionable());
widget.getElement().getStyle().setMargin(0, Unit.PX);
// callLayout(widget, false);
BoxLayoutData layoutData = (BoxLayoutData) widget.getLayoutData();
Margins cm = layoutData.getMargins();
totalFlex += layoutData.getFlex();
totalHeight += widget.getOffsetHeight() + cm.getTop() + cm.getBottom();
maxWidth = Math.max(maxWidth, widget.getOffsetWidth() + cm.getLeft() + cm.getRight());
}
int innerCtWidth = maxWidth + pl + pr;
if (vBoxLayoutAlign.equals(VBoxLayoutAlign.STRETCH)) {
getContainerTarget().setSize(w, h, true);
} else {
getContainerTarget().setSize(w = Math.max(w, innerCtWidth), h, true);
}
int extraHeight = h - totalHeight - pt - pb;
int allocated = 0;
int cw, ch, cl;
int availableWidth = w - pl - pr;
if (getPack().equals(BoxLayoutPack.CENTER)) {
pt += extraHeight / 2;
} else if (getPack().equals(BoxLayoutPack.END)) {
pt += extraHeight;
}
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
BoxLayoutData layoutData = (BoxLayoutData) widget.getLayoutData();
Margins cm = layoutData.getMargins();
cw = widget.getOffsetWidth();
ch = widget.getOffsetHeight();
pt += cm.getTop();
if (vBoxLayoutAlign.equals(VBoxLayoutAlign.CENTER)) {
int diff = availableWidth - (cw + cm.getLeft() + cm.getRight());
if (diff == 0) {
cl = pl + cm.getLeft();
} else {
cl = pl + cm.getLeft() + (diff / 2);
}
} else {
if (vBoxLayoutAlign.equals(VBoxLayoutAlign.RIGHT)) {
cl = w - (pr + cm.getRight() + cw);
} else {
cl = pl + cm.getLeft();
}
}
boolean component = widget instanceof Component;
Component c = null;
if (component) {
c = (Component) widget;
}
int height = -1;
if (component) {
c.setPosition(cl, pt);
} else {
XElement.as(widget.getElement()).setLeftTop(cl, pt);
}
if (getPack().equals(BoxLayoutPack.START) && layoutData.getFlex() > 0) {
int add = (int) Math.floor(extraHeight * (layoutData.getFlex() / totalFlex));
allocated += add;
if (isAdjustForFlexRemainder() && i == getWidgetCount() - 1) {
add += extraHeight - allocated;
}
ch += add;
height = ch;
}
if (vBoxLayoutAlign.equals(VBoxLayoutAlign.STRETCH)) {
applyLayout(
widget,
Util.constrain(stretchWidth - cm.getLeft() - cm.getRight(), layoutData.getMinSize(), layoutData.getMaxSize()),
height);
} else if (vBoxLayoutAlign.equals(VBoxLayoutAlign.STRETCHMAX)) {
applyLayout(widget,
Util.constrain(maxWidth - cm.getLeft() - cm.getRight(), layoutData.getMinSize(), layoutData.getMaxSize()),
height);
} else if (height > 0) {
applyLayout(widget, -1, height);
}
pt += ch + cm.getBottom();
}
}