public static ColumnCategoriesModel setupColumnCategories(
TableColumn[] columnProps) {
ColumnCategoriesModel model = new ColumnCategoriesModel();
Node rootNode = model.addRootCategory("Root");
Node all = model.addCategory(rootNode, "All");
Map<String, List<Integer>> columnCategoryToColumnIndexesMap = new LinkedHashMap<String, List<Integer>>();
List<Integer> indexesNotCategorized = new LinkedList<Integer>();
for (TableColumn tableColumn : columnProps) {
indexesNotCategorized.add(tableColumn.index);
}
for (int columnIndex = 0; columnIndex < columnProps.length; columnIndex++) {
String categoryName = columnProps[columnIndex].categoryName;
// Column if part of a category
if (categoryName != null) {
List<Integer> columnCategoryIndexes = columnCategoryToColumnIndexesMap
.get(categoryName);
// Create an entry in the map for the category
if (columnCategoryIndexes == null) {
columnCategoryIndexes = new LinkedList<Integer>();
columnCategoryToColumnIndexesMap.put(categoryName,
columnCategoryIndexes);
}
// Add to map
columnCategoryIndexes.add(columnIndex);
indexesNotCategorized.remove(Integer.valueOf(columnIndex));
}
}
// Transfer the map created to the category model
all.addChildColumnIndexes(ArrayUtil.asIntArray(indexesNotCategorized));
for (String columnGroupName : columnCategoryToColumnIndexesMap.keySet()) {
List<Integer> columnIndexes = columnCategoryToColumnIndexesMap
.get(columnGroupName);
int[] intColumnIndexes = new int[columnIndexes.size()];
int i = 0;
for (Integer columnIndex : columnIndexes) {
intColumnIndexes[i] = columnIndex;
i++;
}
Node node = model.addCategory(all, columnGroupName);
node.addChildColumnIndexes(ArrayUtil.asIntArray(columnIndexes));
}
return model;
}