sectionIndex < sectionCount; sectionIndex++) {
Form.Section section = sections.get(sectionIndex);
for (int fieldIndex = 0, fieldCount = section.getLength();
fieldIndex < fieldCount; fieldIndex++) {
Component field = section.get(fieldIndex);
if (field.isVisible()) {
Label label = labels.get(sectionIndex).get(fieldIndex);
maximumLabelWidth = Math.max(maximumLabelWidth,
label.getPreferredWidth(-1));
}
}
}
// Determine the field width
int width = getWidth();
int fieldWidth = Math.max(0, width - (maximumLabelWidth + horizontalSpacing
+ flagImageOffset + FLAG_IMAGE_SIZE));
// Lay out the components
int rowY = 0;
for (int sectionIndex = 0, sectionCount = sections.getLength();
sectionIndex < sectionCount; sectionIndex++) {
Form.Section section = sections.get(sectionIndex);
Separator separator = separators.get(sectionIndex);
if (sectionIndex == 0
&& !showFirstSectionHeading) {
separator.setVisible(false);
} else {
separator.setVisible(true);
separator.setSize(width, separator.getPreferredHeight(width));
separator.setLocation(0, rowY);
rowY += separator.getHeight();
}
for (int fieldIndex = 0, fieldCount = section.getLength();
fieldIndex < fieldCount; fieldIndex++) {
Component field = section.get(fieldIndex);
Label label = labels.get(sectionIndex).get(fieldIndex);
ImageView flagImageView = flagImageViews.get(sectionIndex).get(fieldIndex);
if (field.isVisible()) {
// Show the row components
label.setVisible(true);
flagImageView.setVisible(true);
// Determine the label size and baseline
Dimensions labelSize = label.getPreferredSize();
label.setSize(labelSize);
int labelAscent = label.getBaseline(labelSize.width, labelSize.height);
if (labelAscent == -1) {
labelAscent = labelSize.height;
}
int labelDescent = labelSize.height - labelAscent;
// Determine the field size and baseline
Dimensions fieldSize;
if (fill) {
fieldSize = new Dimensions(fieldWidth, field.getPreferredHeight(fieldWidth));
} else {
fieldSize = field.getPreferredSize();
}
field.setSize(fieldSize);
int fieldAscent = field.getBaseline(fieldSize.width, fieldSize.height);
if (fieldAscent == -1) {
fieldAscent = fieldSize.height;
}
int fieldDescent = fieldSize.height - fieldAscent;
// Determine the baseline and row height
int baseline = Math.max(labelAscent, fieldAscent);
int rowHeight = Math.max(baseline + Math.max(labelDescent, fieldDescent), FLAG_IMAGE_SIZE);
// Align the label and field to baseline
int labelX = rightAlignLabels ? maximumLabelWidth - label.getWidth() : 0;
int labelY = rowY + (baseline - labelAscent);
label.setLocation(labelX, labelY);
int fieldX = maximumLabelWidth + horizontalSpacing;
int fieldY = rowY + (baseline - fieldAscent);
field.setLocation(fieldX, fieldY);
// Vertically center the flag on the label
flagImageView.setSize(flagImageView.getPreferredSize());
int flagImageY = labelY + (labelSize.height - flagImageView.getHeight()) / 2;
flagImageView.setLocation(fieldX + field.getWidth() + flagImageOffset, flagImageY);
// Update the row y-coordinate
rowY += rowHeight + verticalSpacing;
} else {
// Hide the row components