1 package net.sf.openrocket.util;
3 import java.awt.Window;
4 import java.io.BufferedOutputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.concurrent.ExecutionException;
10 import javax.swing.JOptionPane;
11 import javax.swing.SwingWorker;
13 import net.sf.openrocket.document.Simulation;
14 import net.sf.openrocket.file.CSVExport;
15 import net.sf.openrocket.gui.dialogs.SwingWorkerDialog;
16 import net.sf.openrocket.gui.main.ExceptionHandler;
17 import net.sf.openrocket.simulation.FlightDataBranch;
18 import net.sf.openrocket.simulation.FlightDataBranch.Type;
19 import net.sf.openrocket.unit.Unit;
22 public class SaveCSVWorker extends SwingWorker<Void, Void> {
24 private static final int BYTES_PER_FIELD_PER_POINT = 7;
26 private final File file;
27 private final Simulation simulation;
28 private final FlightDataBranch branch;
29 private final FlightDataBranch.Type[] fields;
30 private final Unit[] units;
31 private final String fieldSeparator;
32 private final String commentStarter;
33 private final boolean simulationComments;
34 private final boolean fieldComments;
35 private final boolean eventComments;
38 public SaveCSVWorker(File file, Simulation simulation, FlightDataBranch branch,
39 Type[] fields, Unit[] units, String fieldSeparator, String commentStarter,
40 boolean simulationComments, boolean fieldComments, boolean eventComments) {
42 this.simulation = simulation;
46 this.fieldSeparator = fieldSeparator;
47 this.commentStarter = commentStarter;
48 this.simulationComments = simulationComments;
49 this.fieldComments = fieldComments;
50 this.eventComments = eventComments;
55 protected Void doInBackground() throws Exception {
57 int estimate = BYTES_PER_FIELD_PER_POINT * fields.length * branch.getLength();
58 estimate = Math.max(estimate, 1000);
60 // Create the ProgressOutputStream that provides progress estimates
61 ProgressOutputStream os = new ProgressOutputStream(
62 new BufferedOutputStream(new FileOutputStream(file)),
66 protected void setProgress(int progress) {
67 SaveCSVWorker.this.setProgress(progress);
73 CSVExport.exportCSV(os, simulation, branch, fields, units, fieldSeparator,
74 commentStarter, simulationComments, fieldComments, eventComments);
78 } catch (Exception e) {
79 ExceptionHandler.handleErrorCondition("Error closing file", e);
88 * Exports a CSV file using a progress dialog if necessary.
90 * @return <code>true</code> if the save was successful, <code>false</code> otherwise.
92 public static boolean export(File file, Simulation simulation, FlightDataBranch branch,
93 Type[] fields, Unit[] units, String fieldSeparator, String commentStarter,
94 boolean simulationComments, boolean fieldComments, boolean eventComments,
98 SaveCSVWorker worker = new SaveCSVWorker(file, simulation, branch, fields, units,
99 fieldSeparator, commentStarter, simulationComments, fieldComments,
102 if (!SwingWorkerDialog.runWorker(parent, "Exporting flight data",
103 "Writing " + file.getName() + "...", worker)) {
105 // User cancelled the save
112 } catch (ExecutionException e) {
113 Throwable cause = e.getCause();
115 if (cause instanceof IOException) {
116 JOptionPane.showMessageDialog(parent, new String[] {
117 "An I/O error occurred while saving:",
118 e.getMessage() }, "Saving failed", JOptionPane.ERROR_MESSAGE);
121 throw new BugException("Unknown error when saving file", e);
124 } catch (InterruptedException e) {
125 throw new BugException("EDT was interrupted", e);