int left = bInsets.left;
int right = bInsets.right;
int y = bInsets.top;
JCommandButtonPanel panel = (JCommandButtonPanel) parent;
boolean ltr = panel.getComponentOrientation().isLeftToRight();
// compute max width of buttons
int maxButtonWidth = 0;
int maxButtonHeight = 0;
int groupCount = panel.getGroupCount();
for (int i = 0; i < groupCount; i++) {
for (AbstractCommandButton button : panel.getGroupButtons(i)) {
maxButtonWidth = Math.max(maxButtonWidth, button
.getPreferredSize().width);
maxButtonHeight = Math.max(maxButtonHeight, button
.getPreferredSize().height);
}
}
groupRects = new Rectangle[groupCount];
int gap = getLayoutGap();
int maxWidth = parent.getWidth() - bInsets.left - bInsets.right
- groupInsets.left - groupInsets.right;
// for N buttons, there are N-1 gaps. Add the gap to the
// available width and divide by the max button width + gap.
int buttonsInRow = (maxButtonWidth == 0) ? 0 : (maxWidth + gap)
/ (maxButtonWidth + gap);
int maxButtonColumnsToUse = panel.getMaxButtonColumns();
if (maxButtonColumnsToUse > 0) {
buttonsInRow = Math.min(buttonsInRow, maxButtonColumnsToUse);
}
// System.out.println("Layout : " + buttonsInRow);
for (int i = 0; i < groupCount; i++) {
int topGroupY = y;
y += groupInsets.top;
JLabel groupLabel = groupLabels[i];
if (buttonPanel.isToShowGroupLabels()) {
int labelWidth = groupLabel.getPreferredSize().width;
int labelHeight = getGroupTitleHeight(i);
if (groupLabel.getComponentOrientation().isLeftToRight()) {
groupLabel.setBounds(left + groupInsets.left, y,
labelWidth, labelHeight);
} else {
groupLabel.setBounds(parent.getWidth() - right
- groupInsets.right - labelWidth, y,
labelWidth, labelHeight);
}
y += labelHeight + gap;
}
int buttonRows = (buttonsInRow == 0) ? 0 : (int) (Math
.ceil((double) panel.getGroupButtons(i).size()
/ buttonsInRow));
if (maxButtonColumnsToUse > 0) {
buttonsInRow = Math
.min(buttonsInRow, maxButtonColumnsToUse);
}
// spread the buttons so that we don't have extra space
// on the right
int actualButtonWidth = (buttonRows > 1) ? (maxWidth - (buttonsInRow - 1)
* gap)
/ buttonsInRow
: maxButtonWidth;
if (maxButtonColumnsToUse == 1)
actualButtonWidth = maxWidth;
if (ltr) {
int currX = left + groupInsets.left;
for (AbstractCommandButton button : panel
.getGroupButtons(i)) {
int endX = currX + actualButtonWidth;
if (endX > (parent.getWidth() - right - groupInsets.right)) {
currX = left + groupInsets.left;
y += maxButtonHeight;
y += gap;
}
button.setBounds(currX, y, actualButtonWidth,
maxButtonHeight);
// System.out.println(button.getText() + ":"
// + button.isVisible() + ":" + button.getBounds());
currX += actualButtonWidth;
currX += gap;
}
} else {
int currX = parent.getWidth() - right - groupInsets.right;
for (AbstractCommandButton button : panel
.getGroupButtons(i)) {
int startX = currX - actualButtonWidth;
if (startX < (left + groupInsets.left)) {
currX = parent.getWidth() - right
- groupInsets.right;