use of com.haulmont.cuba.gui.data.HierarchicalDatasource in project cuba by cuba-platform.
the class DesktopTreeTable method expandUpTo.
@Override
public void expandUpTo(int level) {
if (getDatasource() == null) {
return;
}
HierarchicalDatasource ds = getDatasource();
java.util.List<Object> currentLevelItemIds = new ArrayList<>(ds.getRootItemIds());
int i = 0;
while (i < level && !currentLevelItemIds.isEmpty()) {
for (Object itemId : new ArrayList<>(currentLevelItemIds)) {
Entity<Object> item = datasource.getItem(itemId);
impl.expandPath(((TreeTableModelAdapter) tableModel).getTreePath(item));
currentLevelItemIds.remove(itemId);
currentLevelItemIds.addAll(ds.getChildren(itemId));
}
i++;
}
}
use of com.haulmont.cuba.gui.data.HierarchicalDatasource in project cuba by cuba-platform.
the class ExcelExporter method exportTable.
public void exportTable(Table<Entity> table, List<Table.Column> columns, Boolean exportExpanded, ExportDisplay display, List<String> filterDescription, String fileName, ExportMode exportMode) {
if (display == null) {
throw new IllegalArgumentException("ExportDisplay is null");
}
createWorkbookWithSheet();
createFonts();
createFormats();
int r = 0;
if (filterDescription != null) {
for (r = 0; r < filterDescription.size(); r++) {
String line = filterDescription.get(r);
HSSFRow row = sheet.createRow(r);
if (r == 0) {
HSSFRichTextString richTextFilterName = new HSSFRichTextString(line);
richTextFilterName.applyFont(boldFont);
row.createCell(0).setCellValue(richTextFilterName);
} else {
row.createCell(0).setCellValue(line);
}
}
r++;
}
HSSFRow row = sheet.createRow(r);
createAutoColumnSizers(columns.size());
float maxHeight = sheet.getDefaultRowHeightInPoints();
CellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
for (Table.Column column : columns) {
String caption = column.getCaption();
int countOfReturnSymbols = StringUtils.countMatches(caption, "\n");
if (countOfReturnSymbols > 0) {
maxHeight = Math.max(maxHeight, (countOfReturnSymbols + 1) * sheet.getDefaultRowHeightInPoints());
headerCellStyle.setWrapText(true);
}
}
row.setHeightInPoints(maxHeight);
for (int c = 0; c < columns.size(); c++) {
Table.Column column = columns.get(c);
String caption = column.getCaption();
HSSFCell cell = row.createCell(c);
HSSFRichTextString richTextString = new HSSFRichTextString(caption);
richTextString.applyFont(boldFont);
cell.setCellValue(richTextString);
ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
sizer.notifyCellValue(caption, boldFont);
sizers[c] = sizer;
cell.setCellStyle(headerCellStyle);
}
CollectionDatasource datasource = table.getDatasource();
if (exportMode == ExportMode.SELECTED_ROWS && table.getSelected().size() > 0) {
Set<Entity> selected = table.getSelected();
List<Entity> ordered = ((Collection<Entity>) datasource.getItems()).stream().filter(selected::contains).collect(Collectors.toList());
for (Entity item : ordered) {
createRow(table, columns, 0, ++r, item.getId());
}
} else {
if (table instanceof TreeTable) {
TreeTable treeTable = (TreeTable) table;
HierarchicalDatasource ds = treeTable.getDatasource();
if (table.isAggregatable()) {
r = createAggregatableRow(table, columns, ++r, 1, datasource);
}
for (Object itemId : ds.getRootItemIds()) {
r = createHierarhicalRow(treeTable, columns, exportExpanded, r, itemId);
}
} else if (table instanceof GroupTable && datasource instanceof GroupDatasource && ((GroupDatasource) datasource).hasGroups()) {
GroupDatasource ds = (GroupDatasource) datasource;
if (table.isAggregatable()) {
r = createAggregatableRow(table, columns, ++r, 1, datasource);
}
for (Object item : ds.rootGroups()) {
r = createGroupRow((GroupTable) table, columns, ++r, (GroupInfo) item, 0);
}
} else {
if (table.isAggregatable()) {
r = createAggregatableRow(table, columns, ++r, 1, datasource);
}
for (Object itemId : datasource.getItemIds()) {
createRow(table, columns, 0, ++r, itemId);
}
}
}
for (int c = 0; c < columns.size(); c++) {
sheet.setColumnWidth(c, sizers[c].getWidth() * COL_WIDTH_MAGIC);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
wb.write(out);
} catch (IOException e) {
throw new RuntimeException("Unable to write document", e);
}
if (fileName == null) {
fileName = messages.getTools().getEntityCaption(datasource.getMetaClass());
}
display.show(new ByteArrayDataProvider(out.toByteArray()), fileName + ".xls", ExportFormat.XLS);
}
use of com.haulmont.cuba.gui.data.HierarchicalDatasource in project cuba by cuba-platform.
the class ExcelExporter method createHierarhicalRow.
protected int createHierarhicalRow(TreeTable table, List<Table.Column> columns, Boolean exportExpanded, int rowNumber, Object itemId) {
HierarchicalDatasource hd = table.getDatasource();
createRow(table, columns, 0, ++rowNumber, itemId);
if (BooleanUtils.isTrue(exportExpanded) && !table.isExpanded(itemId) && !hd.getChildren(itemId).isEmpty()) {
return rowNumber;
} else {
final Collection children = hd.getChildren(itemId);
if (children != null && !children.isEmpty()) {
for (Object id : children) {
if (BooleanUtils.isTrue(exportExpanded) && !table.isExpanded(id) && !hd.getChildren(id).isEmpty()) {
createRow(table, columns, 0, ++rowNumber, id);
continue;
}
rowNumber = createHierarhicalRow(table, columns, exportExpanded, rowNumber, id);
}
}
}
return rowNumber;
}
use of com.haulmont.cuba.gui.data.HierarchicalDatasource in project cuba by cuba-platform.
the class TreeLoader method loadTreeChildren.
protected void loadTreeChildren() {
Element itemsElem = element.element("treechildren");
if (itemsElem == null)
return;
String datasource = itemsElem.attributeValue("datasource");
if (!StringUtils.isBlank(datasource)) {
HierarchicalDatasource ds = (HierarchicalDatasource) context.getDsContext().get(datasource);
resultComponent.setDatasource(ds);
String captionProperty = itemsElem.attributeValue("captionProperty");
if (!StringUtils.isEmpty(captionProperty)) {
resultComponent.setCaptionProperty(captionProperty);
resultComponent.setCaptionMode(CaptionMode.PROPERTY);
}
}
}
Aggregations