package com.onaro.sanscreen.client.view.changes.export; import java.io.PrintWriter; import com.onaro.sanscreen.client.view.changes.ChangeTime; import com.onaro.sanscreen.client.view.changes.ChangesTableModel; import com.onaro.util.jfc.tables.FilterTable; public class Exporter { private FilterTable filterTable; private ChangesTableModel changesModel; private ExportFormatting formatting; private String title; private String subTitle; private String timeFilter; private String typeFilter; public Exporter(FilterTable filterTable, ChangesTableModel changesModel, String title, String subtitle, String timeFilter, String typeFilter, ExportFormatting formatting) { this.filterTable = filterTable; this.changesModel = changesModel; this.title = title; this.subTitle = subtitle; this.formatting = formatting; this.timeFilter = timeFilter; this.typeFilter = typeFilter; } /** * Exports the changes to an output writer using hte specified formatting. The expande/collapse of the * exported changes is the same as in the table. * @param out the writer to which the changes will be exported */ public void export(PrintWriter out) { out.print(formatting.getBodyHead()); out.print(formatting.getHeading(title, subTitle, timeFilter, typeFilter)); out.print(formatting.getTableHead()); long prevRowTime = 0; int filteredRowCount = filterTable.getFilteredRowCount(); for (int filterRowIndex = 0; filterRowIndex < filteredRowCount; filterRowIndex++) { int row = filterTable.getRowInEncapsulatedModel(filterRowIndex); ChangesTableModel.Row changeRow = changesModel.getChangeRow(row); out.print(formatting.getRowHead()); /** * If the time is not-empty, count the empty rows that follows so we could * use a single table cell that spans on multiple rows. */ ChangeTime changeTime = changeRow.getChangeTime(); long time = changeTime.getTime(); if (time != prevRowTime) { int span = 1; for (int emptyFilteredTimeRow = filterRowIndex + 1; emptyFilteredTimeRow < filteredRowCount; ++emptyFilteredTimeRow) { int emptyTimeRow = filterTable.getRowInEncapsulatedModel(emptyFilteredTimeRow); ChangesTableModel.Row emptyTimeChangeRow = changesModel.getChangeRow(emptyTimeRow); if (emptyTimeChangeRow.getTime() != time) { break; } ++span; } out.print(formatting.getTimeCell(changeTime.toString(), span)); } /** * export the description */ String html = changeRow.getDescriptionHtml(); out.print(formatting.getDescriptionCell(html)); out.print(formatting.getRowTail()); prevRowTime = time; } out.print(formatting.getTableTail()); out.print(formatting.getBodyTail()); } }