for (int fieldIndex = 0, fieldCount = section.getLength();
fieldIndex < fieldCount; fieldIndex++) {
Component field = section.get(fieldIndex);
if (field.isDisplayable()) {
Label label = labels.get(sectionIndex).get(fieldIndex);
maximumLabelWidth = Math.max(maximumLabelWidth,
label.getPreferredWidth(-1));
maximumFieldWidth = Math.max(maximumFieldWidth,
field.getPreferredWidth(-1));
}
}
}
// Determine the maximum field width
int width = getWidth();
int availableFieldWidth = 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.isDisplayable()) {
// Show the row components
label.setVisible(true);
field.setVisible(true);
flagImageView.setVisible(true);
// Set the row component sizes
label.setSize(label.getPreferredSize());
Dimensions fieldSize = null;
if (fieldAlignment == HorizontalAlignment.JUSTIFY) {
fieldSize = new Dimensions(availableFieldWidth,
field.getPreferredHeight(availableFieldWidth));
} else {
fieldSize = field.getPreferredSize();
}
field.setSize(fieldSize);
flagImageView.setSize(flagImageView.getPreferredSize());
int rowHeight = Math.max(label.getHeight(),
Math.max(field.getHeight(), FLAG_IMAGE_SIZE));
// Set the row component locations
int labelX = rightAlignLabels ? maximumLabelWidth - label.getWidth() : 0;
label.setLocation(labelX, rowY);
int fieldX = 0;
switch(fieldAlignment) {
case LEFT:
case JUSTIFY: {
fieldX = maximumLabelWidth + horizontalSpacing;
break;
}
case RIGHT: {
fieldX = maximumLabelWidth + horizontalSpacing
+ Math.max(0, Math.max(availableFieldWidth, maximumFieldWidth)
- field.getWidth());
break;
}
case CENTER: {
fieldX = maximumLabelWidth + horizontalSpacing
+ Math.max(0, (Math.max(availableFieldWidth, maximumFieldWidth)
- field.getWidth()) / 2);
break;
}
}
field.setLocation(fieldX, rowY);
flagImageView.setLocation(fieldX + field.getWidth() + flagImageOffset,
rowY + (rowHeight - flagImageView.getHeight()) / 2);
// Update the row y-coordinate
rowY += rowHeight + verticalSpacing;
} else {
// Hide the row components
label.setVisible(false);
field.setVisible(false);
flagImageView.setVisible(false);
}
}
}