19 package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
 
   21 import java.util.Collections;
 
   22 import java.util.List;
 
   23 import java.util.Optional;
 
   24 import org.apache.poi.ss.usermodel.Row;
 
   25 import org.apache.poi.ss.usermodel.Sheet;
 
   31 class ExcelSpecialFormatExport 
implements ExcelExport.ExcelSheetExport {
 
   37     static class ItemDimensions {
 
   39         private final int rowStart;
 
   40         private final int rowEnd;
 
   41         private final int colStart;
 
   42         private final int colEnd;
 
   52         ItemDimensions(
int rowStart, 
int colStart, 
int rowEnd, 
int colEnd) {
 
   53             this.rowStart = rowStart;
 
   54             this.colStart = colStart;
 
   91     interface ExcelItemExportable {
 
  103         ItemDimensions write(Sheet sheet, 
int rowStart, 
int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException;
 
  109     static class SingleCellExportable implements ExcelItemExportable {
 
  111         private final CellModel item;
 
  118         SingleCellExportable(String key) {
 
  119             this(
new DefaultCellModel<>(key));
 
  127         SingleCellExportable(CellModel item) {
 
  132         public ItemDimensions write(Sheet sheet, 
int rowStart, 
int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
 
  133             Row row = sheet.createRow(rowStart);
 
  134             ExcelExport.createCell(env, row, colStart, item, Optional.empty());
 
  135             return new ItemDimensions(rowStart, colStart, rowStart, colStart);
 
  143     static class KeyValueItemExportable 
implements ExcelItemExportable {
 
  145         private final CellModel key;
 
  146         private final CellModel value;
 
  154         KeyValueItemExportable(String key, CellModel value) {
 
  155             this(
new DefaultCellModel<>(key), value);
 
  164         KeyValueItemExportable(CellModel key, CellModel value) {
 
  170         public ItemDimensions write(Sheet sheet, 
int rowStart, 
int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
 
  171             Row row = sheet.createRow(rowStart);
 
  172             ExcelExport.createCell(env, row, colStart, key, Optional.of(env.getHeaderStyle()));
 
  173             ExcelExport.createCell(env, row, colStart + 1, value, Optional.empty());
 
  174             return new ItemDimensions(rowStart, colStart, rowStart, colStart + 1);
 
  189     static class TitledExportable 
implements ExcelItemExportable {
 
  191         private static final int DEFAULT_INDENT = 1;
 
  193         private final String title;
 
  194         private final List<? extends ExcelItemExportable> children;
 
  202         TitledExportable(String title, List<? extends ExcelItemExportable> children) {
 
  204             this.children = children;
 
  208         public ItemDimensions write(Sheet sheet, 
int rowStart, 
int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
 
  209             ExcelExport.createCell(env, sheet.createRow(rowStart), colStart, 
new DefaultCellModel<>(title), Optional.of(env.getHeaderStyle()));
 
  210             int curRow = rowStart + 1;
 
  211             int maxCol = colStart;
 
  212             for (ExcelItemExportable export : children) {
 
  213                 if (export == null) {
 
  217                 ItemDimensions thisItemDim = export.write(sheet, curRow, colStart + DEFAULT_INDENT, env);
 
  218                 curRow = thisItemDim.getRowEnd() + 1;
 
  219                 maxCol = Math.max(thisItemDim.getColEnd(), maxCol);
 
  222             return new ItemDimensions(rowStart, colStart, curRow - 1, maxCol);
 
  226     private final String sheetName;
 
  227     private final List<ExcelItemExportable> exports;
 
  235     ExcelSpecialFormatExport(String sheetName, List<ExcelItemExportable> exports) {
 
  236         this.sheetName = sheetName;
 
  237         this.exports = exports == null ? Collections.emptyList() : exports;
 
  241     public String getSheetName() {
 
  246     public void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv env) throws ExcelExportException {
 
  249         for (ExcelItemExportable export : exports) {
 
  250             if (export == null) {
 
  254             ItemDimensions dimensions = export.write(sheet, rowStart, 0, env);
 
  255             rowStart = dimensions.getRowEnd() + 1;
 
  256             maxCol = Math.max(maxCol, dimensions.getColEnd());
 
  260         for (
int i = 0; i <= maxCol; i++) {
 
  261             sheet.autoSizeColumn(i);