int numberOfWeightColumns = 0;
int totalWeight = 0;
// First calc space occupied by fixed columns
for (int i = 0; i < size; i++) {
final ColumnLayoutData col = columns.get(i);
if (col instanceof ColumnPixelData) {
final int pixels = ((ColumnPixelData) col).width;
widths[i] = pixels;
fixedWidth += pixels;
} else if (col instanceof ColumnWeightData) {
final ColumnWeightData cw = (ColumnWeightData) col;
numberOfWeightColumns++;
// first time, use the weight specified by the column data,
// otherwise use
// the actual width as the weight
// int weight = firstTime ? cw.weight :
// tableColumns[i].getWidth();
final int weight = cw.weight;
totalWeight += weight;
} else {
Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
}
}
// Do we have columns that have a weight
if (numberOfWeightColumns > 0) {
// Now distribute the rest to the columns with weight.
final int rest = width - fixedWidth;
int totalDistributed = 0;
for (int i = 0; i < size; ++i) {
final ColumnLayoutData col = columns.get(i);
if (col instanceof ColumnWeightData) {
final ColumnWeightData cw = (ColumnWeightData) col;
// calculate weight as above
// int weight = firstTime ? cw.weight :
// tableColumns[i].getWidth();
final int weight = cw.weight;
int pixels = totalWeight == 0 ? 0 : weight * rest / totalWeight;
if (pixels < cw.minimumWidth) {
pixels = cw.minimumWidth;
}
totalDistributed += pixels;
widths[i] = pixels;
}
}
// Distribute any remaining pixels to columns with weight.
int diff = rest - totalDistributed;
for (int i = 0; diff > 0; ++i) {
if (i == size) {
i = 0;
}
final ColumnLayoutData col = columns.get(i);
if (col instanceof ColumnWeightData) {
++widths[i];
--diff;
}
}