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.simulation.FlightDataBranch;
17 import net.sf.openrocket.simulation.FlightDataBranch.Type;
18 import net.sf.openrocket.unit.Unit;
21 public class SaveCSVWorker extends SwingWorker<Void, Void> {
23 private static final int BYTES_PER_FIELD_PER_POINT = 7;
25 private final File file;
26 private final Simulation simulation;
27 private final FlightDataBranch branch;
28 private final FlightDataBranch.Type[] fields;
29 private final Unit[] units;
30 private final String fieldSeparator;
31 private final String commentStarter;
32 private final boolean simulationComments;
33 private final boolean fieldComments;
34 private final boolean eventComments;
37 public SaveCSVWorker(File file, Simulation simulation, FlightDataBranch branch,
38 Type[] fields, Unit[] units, String fieldSeparator, String commentStarter,
39 boolean simulationComments, boolean fieldComments, boolean eventComments) {
41 this.simulation = simulation;
45 this.fieldSeparator = fieldSeparator;
46 this.commentStarter = commentStarter;
47 this.simulationComments = simulationComments;
48 this.fieldComments = fieldComments;
49 this.eventComments = eventComments;
54 protected Void doInBackground() throws Exception {
56 int estimate = BYTES_PER_FIELD_PER_POINT * fields.length * branch.getLength();
57 estimate = Math.max(estimate, 1000);
59 // Create the ProgressOutputStream that provides progress estimates
60 ProgressOutputStream os = new ProgressOutputStream(
61 new BufferedOutputStream(new FileOutputStream(file)),
65 protected void setProgress(int progress) {
66 SaveCSVWorker.this.setProgress(progress);
72 CSVExport.exportCSV(os, simulation, branch, fields, units, fieldSeparator,
73 commentStarter, simulationComments, fieldComments, eventComments);
77 } catch (Exception e) {
78 System.err.println("Error closing file: ");
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 RuntimeException("Unknown error when saving file", e);
124 } catch (InterruptedException e) {
125 throw new RuntimeException("EDT was interrupted", e);