int numberOfWeightColumns = 0;
int totalWeight = 0;
// First calc space occupied by fixed columns
for (int i = 0; i < size; i++) {
ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
if (col instanceof ColumnPixelData) {
ColumnPixelData cpd = (ColumnPixelData) col;
int pixels = cpd.width;
if (cpd.addTrim) {
pixels += COLUMN_TRIM;
}
widths[i] = pixels;
fixedWidth += pixels;
} else if (col instanceof ColumnWeightData) {
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();
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.
int rest = width - fixedWidth;
int totalDistributed = 0;
for (int i = 0; i < size; ++i) {
ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
if (col instanceof ColumnWeightData) {
ColumnWeightData cw = (ColumnWeightData) col;
// calculate weight as above
// int weight = firstTime ? cw.weight :
// tableColumns[i].getWidth();
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;
ColumnLayoutData col = (ColumnLayoutData) columns.get(i);
if (col instanceof ColumnWeightData) {
++widths[i];
--diff;
}
}