protected void doLayout() {
// Determine the maximum label and flag message width
int maximumLabelWidth = 0;
int maximumFlagMessageWidth = 0;
BoxModel boxModel = WidgetHelper.getBoxModel(this);
for (int sectionIndex = 0, sectionCount = getWidgetCount(); sectionIndex < sectionCount; sectionIndex++) {
Form.Section section = (Form.Section) getWidget(sectionIndex);
for (int fieldIndex = 0, fieldCount = section.getWidgetCount(); fieldIndex < fieldCount; fieldIndex++) {
Form.Field fieldWidget = (Form.Field) section
.getWidget(fieldIndex);
// Widget field = fieldWidget.getWidget(0);
if (fieldWidget.isVisible()) {
InlineLabel label = fieldWidget.labelWidget;
maximumLabelWidth = Math.max(maximumLabelWidth,
WidgetHelper.getPreferredWidth(label));
// TODO if (showFlagMessagesInline) {}
}
}
}
// Determine the field width
int width = getElement().getClientWidth();
int fieldWidth = Math.max(0, width
- (maximumLabelWidth + horizontalSpacing));
// TODO if (showFlagIcons) {}
// TODO if (showFlagMessagesInline) {}
fieldWidth = Math.max(0,
fieldWidth - boxModel.getPaddingWidthContribution());
// Lay out the components
int rowY = boxModel.getPadding().top;
for (int sectionIndex = 0, sectionCount = getWidgetCount(); sectionIndex < sectionCount; sectionIndex++) {
Form.Section section = (Form.Section) getWidget(sectionIndex);
WidgetHelper.setSize(section, width, 1);
WidgetHelper.setLocation(section, 0, 0);
Separator separator = section.separator;
if (sectionIndex > 0 || section.getHeading() != null) {
int separatorWidth = Math.max(
width - boxModel.getPaddingWidthContribution(), 0);
separator.setVisible(true);
int separatorHeight = WidgetHelper.getPreferredHeight(
separator, separatorWidth);
WidgetHelper
.setSize(separator, separatorWidth, separatorHeight);
WidgetHelper.setLocation(separator, boxModel.getPadding().left,
rowY);
rowY += separatorHeight;
} else {
separator.setVisible(false);
}
for (int fieldIndex = 0, fieldCount = section.getWidgetCount(); fieldIndex < fieldCount; fieldIndex++) {
Form.Field fieldWidget = (Form.Field) section
.getWidget(fieldIndex);
Widget field = fieldWidget.getWidget(0);
InlineLabel label = fieldWidget.labelWidget;
WidgetHelper.setSize(fieldWidget, width, 1);
WidgetHelper.setLocation(fieldWidget, 0, 0);
if (fieldWidget.isVisible()) {
// Show the label
label.setVisible(true);
// Determine the label size and baseline
Dimensions labelSize = WidgetHelper.getPreferredSize(label);
WidgetHelper.setSize(label, labelSize.width,
labelSize.height);
int labelAscent = WidgetHelper.getBaseline(label,
labelSize.width, labelSize.height);
int labelDescent = labelSize.height - labelAscent;
// Determine the field size and baseline
Dimensions fieldSize;
if (fill) {
fieldSize = new Dimensions(fieldWidth,
WidgetHelper.getPreferredHeight(field,
fieldWidth));
} else {
fieldSize = WidgetHelper.getPreferredSize(field);
}
WidgetHelper.setSize(field, fieldSize.width,
fieldSize.height);
int fieldAscent = WidgetHelper.getBaseline(field,
fieldSize.width, fieldSize.height);
if (fieldAscent == -1) {
fieldAscent = labelAscent;
}
int fieldDescent = fieldSize.height - fieldAscent;
// Determine the baseline and row height
int maximumAscent = Math.max(labelAscent, fieldAscent);
int maximumDescent = Math.max(labelDescent, fieldDescent);
int baseline = maximumAscent;
int rowHeight = maximumAscent + maximumDescent;
// Position the label
int labelX = boxModel.getPadding().left;
if (!leftAlignLabels) {
labelX += maximumLabelWidth - labelSize.width;
}
// TODO if (showFlagIcons) {}
int labelY = rowY + (baseline - labelAscent);
WidgetHelper.setLocation(label, labelX, labelY);
// Position the field
int fieldX = boxModel.getPadding().left + maximumLabelWidth
+ horizontalSpacing;
// TODO if (showFlagIcons) {}
int fieldY = rowY + (baseline - fieldAscent);
WidgetHelper.setLocation(field, fieldX, fieldY);