19 package org.sleuthkit.autopsy.datasourcesummary.ui;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
24 import java.io.IOException;
25 import java.nio.file.Paths;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Date;
31 import java.util.List;
32 import java.util.concurrent.CancellationException;
33 import java.util.concurrent.ExecutionException;
34 import java.util.function.Consumer;
35 import java.util.logging.Level;
36 import javax.swing.JOptionPane;
37 import javax.swing.SwingUtilities;
38 import javax.swing.SwingWorker;
39 import org.openide.util.NbBundle;
40 import org.openide.util.NbBundle.Messages;
41 import org.openide.windows.WindowManager;
58 "ExcelExportAction_moduleName=Data Source Summary",})
59 class ExcelExportAction implements Consumer<DataSource> {
61 private static final Logger logger = Logger.getLogger(ExcelExportAction.class.getName());
66 interface ExportableTab {
81 List<ExcelSheetExport> getExcelExports(DataSource dataSource);
84 private final ExcelExport excelExport = ExcelExport.getInstance();
85 private final List<? extends ExportableTab> tabExports;
92 ExcelExportAction(List<? extends ExportableTab> tabExports) {
93 this.tabExports = Collections.unmodifiableList(
new ArrayList<>(tabExports));
103 public void accept(DataSource ds) {
108 File outputLoc = getXLSXPath(ds.getName());
109 if (outputLoc == null) {
113 runXLSXExport(ds, outputLoc);
124 "ExcelExportAction_getXLSXPath_directory=DataSourceSummary",})
125 private File getXLSXPath(String dataSourceName) {
128 DateFormat dateFormat =
new SimpleDateFormat(
"MM-dd-yyyy-HH-mm-ss");
129 String fileName = String.format(
"%s-%s.xlsx", dataSourceName == null ?
"" : FileUtil.escapeFileName(dataSourceName), dateFormat.format(
new Date()));
131 String reportsDir = Case.getCurrentCaseThrows().getReportDirectory();
132 File reportsDirFile = Paths.get(reportsDir, Bundle.ExcelExportAction_getXLSXPath_directory()).toFile();
133 if (!reportsDirFile.exists()) {
134 reportsDirFile.mkdirs();
137 return Paths.get(reportsDirFile.getAbsolutePath(), fileName).toFile();
138 }
catch (NoCurrentCaseException ex) {
139 logger.log(Level.WARNING,
"Unable to find reports directory.", ex);
150 private SwingWorker<Boolean, Void> worker = null;
154 if (worker != null && !worker.isCancelled() && !worker.isDone()) {
164 SwingWorker<Boolean, Void> getWorker() {
173 void setWorker(SwingWorker<Boolean, Void> worker) {
174 this.worker = worker;
186 "# {0} - dataSource",
187 "ExcelExportAction_runXLSXExport_progressTitle=Exporting {0} to XLSX",
188 "ExcelExportAction_runXLSXExport_progressCancelTitle=Cancel",
189 "ExcelExportAction_runXLSXExport_progressCancelActionTitle=Cancelling...",
190 "ExcelExportAction_runXLSXExport_errorTitle=Error While Exporting",
191 "ExcelExportAction_runXLSXExport_errorMessage=There was an error while exporting.",
193 private void runXLSXExport(DataSource dataSource, File path) {
195 CancelExportListener cancelButtonListener =
new CancelExportListener();
197 ProgressIndicator progressIndicator =
new ModalDialogProgressIndicator(
198 WindowManager.getDefault().getMainWindow(),
199 Bundle.ExcelExportAction_runXLSXExport_progressTitle(dataSource.getName()),
200 new String[]{Bundle.ExcelExportAction_runXLSXExport_progressCancelTitle()},
201 Bundle.ExcelExportAction_runXLSXExport_progressCancelTitle(),
205 SwingWorker<Boolean, Void> worker =
new SwingWorker<Boolean, Void>() {
207 protected Boolean doInBackground() throws Exception {
208 exportToXLSX(progressIndicator, dataSource, path);
213 protected void done() {
216 }
catch (ExecutionException ex) {
217 logger.log(Level.WARNING,
"Error while trying to export data source summary to xlsx.", ex);
218 JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
219 Bundle.ExcelExportAction_runXLSXExport_errorMessage(),
220 Bundle.ExcelExportAction_runXLSXExport_errorTitle(),
221 JOptionPane.ERROR_MESSAGE);
222 }
catch (InterruptedException | CancellationException ex) {
225 progressIndicator.finish();
230 cancelButtonListener.setWorker(worker);
245 "ExcelExportAction_exportToXLSX_beginExport=Beginning Export...",
247 "ExcelExportAction_exportToXLSX_gatheringTabData=Fetching Data for {0} Tab...",
248 "ExcelExportAction_exportToXLSX_writingToFile=Writing to File...",})
250 private void exportToXLSX(ProgressIndicator progressIndicator, DataSource dataSource, File path)
251 throws InterruptedException, IOException, ExcelExport.ExcelExportException {
253 int exportWeight = 3;
254 int totalWeight = tabExports.size() + exportWeight;
255 progressIndicator.start(Bundle.ExcelExportAction_exportToXLSX_beginExport(), totalWeight);
256 List<ExcelExport.ExcelSheetExport> sheetExports =
new ArrayList<>();
257 for (
int i = 0; i < tabExports.size(); i++) {
258 if (Thread.interrupted()) {
259 throw new InterruptedException(
"Export has been cancelled.");
262 ExportableTab tab = tabExports.get(i);
263 progressIndicator.progress(Bundle.ExcelExportAction_exportToXLSX_gatheringTabData(tab == null ?
"" : tab.getTabTitle()), i);
265 List<ExcelExport.ExcelSheetExport> exports = tab.getExcelExports(dataSource);
266 if (exports != null) {
267 sheetExports.addAll(exports);
271 if (Thread.interrupted()) {
272 throw new InterruptedException(
"Export has been cancelled.");
275 progressIndicator.progress(Bundle.ExcelExportAction_exportToXLSX_writingToFile(), tabExports.size());
276 excelExport.writeExcel(sheetExports, path);
278 progressIndicator.finish();
282 Case curCase = Case.getCurrentCaseThrows();
283 curCase.addReport(path.getParent(),
284 Bundle.ExcelExportAction_moduleName(),
289 SwingUtilities.invokeLater(() -> {
290 ExcelExportDialog dialog =
new ExcelExportDialog(WindowManager.getDefault().getMainWindow(), path);
291 dialog.setResizable(
false);
292 dialog.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
293 dialog.setVisible(
true);
297 }
catch (NoCurrentCaseException | TskCoreException ex) {
298 logger.log(Level.WARNING,
"There was an error attaching report to case.", ex);
void actionPerformed(ActionEvent e)