Skip to content

Instantly share code, notes, and snippets.

@ksiwonia
Created March 29, 2013 13:24
Show Gist options
  • Save ksiwonia/5270818 to your computer and use it in GitHub Desktop.
Save ksiwonia/5270818 to your computer and use it in GitHub Desktop.
public int[] getReportColumnWidths(Integer availableWidth, Map<String, Integer> fixedColumns, List<String> allColumns) {
int[] reportColumnWidths = new int[allColumns.size()];
Integer remainedAvailableWidth = availableWidth;
Map<Integer, Integer> columnWithFixedWidth = new HashMap<Integer, Integer>();
int i = 0;
for (String entryColumn : allColumns) {
for (Map.Entry<String, Integer> entryFixedColumn : fixedColumns.entrySet()) {
if (entryColumn.toLowerCase().contains(entryFixedColumn.getKey().toLowerCase())) {
remainedAvailableWidth = remainedAvailableWidth - entryFixedColumn.getValue();
columnWithFixedWidth.put(i, entryFixedColumn.getValue());
}
}
i++;
}
Integer columnWithoutFixedWidth = allColumns.size() - columnWithFixedWidth.size();
if (allColumns.size() == columnWithFixedWidth.size() && remainedAvailableWidth >= 0) {
for (Map.Entry<Integer, Integer> entry : columnWithFixedWidth.entrySet()) {
reportColumnWidths[entry.getKey()] = entry.getValue();
}
} else if (remainedAvailableWidth >= 0
&& remainedAvailableWidth > columnWithoutFixedWidth * MINIMUM_ALLOWABLE_SIZE_COLUMN_IN_PIXEL) {
Integer columnSize = remainedAvailableWidth / columnWithoutFixedWidth;
Arrays.fill(reportColumnWidths, columnSize);
for (Map.Entry<Integer, Integer> entry : columnWithFixedWidth.entrySet()) {
reportColumnWidths[entry.getKey()] = entry.getValue();
}
} else {
Integer columnSize = availableWidth / allColumns.size();
Arrays.fill(reportColumnWidths, columnSize);
}
return reportColumnWidths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment