X-Git-Url: https://git.gag.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fnet%2Fsf%2Fopenrocket%2Futil%2FSaveCSVWorker.java;fp=src%2Fnet%2Fsf%2Fopenrocket%2Futil%2FSaveCSVWorker.java;h=444c6926fd4d60212adeeb1c109f8b436777ffb8;hb=6afc62224f6f7e581b1d321e125ed97a6ec77dc1;hp=0000000000000000000000000000000000000000;hpb=800e211417d977c55c8fddcc175ec8c74793f51b;p=debian%2Fopenrocket diff --git a/src/net/sf/openrocket/util/SaveCSVWorker.java b/src/net/sf/openrocket/util/SaveCSVWorker.java new file mode 100644 index 00000000..444c6926 --- /dev/null +++ b/src/net/sf/openrocket/util/SaveCSVWorker.java @@ -0,0 +1,130 @@ +package net.sf.openrocket.util; + +import java.awt.Window; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +import javax.swing.JOptionPane; +import javax.swing.SwingWorker; + +import net.sf.openrocket.document.Simulation; +import net.sf.openrocket.file.CSVExport; +import net.sf.openrocket.gui.dialogs.SwingWorkerDialog; +import net.sf.openrocket.simulation.FlightDataBranch; +import net.sf.openrocket.simulation.FlightDataBranch.Type; +import net.sf.openrocket.unit.Unit; + + +public class SaveCSVWorker extends SwingWorker { + + private static final int BYTES_PER_FIELD_PER_POINT = 7; + + private final File file; + private final Simulation simulation; + private final FlightDataBranch branch; + private final FlightDataBranch.Type[] fields; + private final Unit[] units; + private final String fieldSeparator; + private final String commentStarter; + private final boolean simulationComments; + private final boolean fieldComments; + private final boolean eventComments; + + + public SaveCSVWorker(File file, Simulation simulation, FlightDataBranch branch, + Type[] fields, Unit[] units, String fieldSeparator, String commentStarter, + boolean simulationComments, boolean fieldComments, boolean eventComments) { + this.file = file; + this.simulation = simulation; + this.branch = branch; + this.fields = fields; + this.units = units; + this.fieldSeparator = fieldSeparator; + this.commentStarter = commentStarter; + this.simulationComments = simulationComments; + this.fieldComments = fieldComments; + this.eventComments = eventComments; + } + + + @Override + protected Void doInBackground() throws Exception { + + int estimate = BYTES_PER_FIELD_PER_POINT * fields.length * branch.getLength(); + estimate = Math.max(estimate, 1000); + + // Create the ProgressOutputStream that provides progress estimates + ProgressOutputStream os = new ProgressOutputStream( + new BufferedOutputStream(new FileOutputStream(file)), + estimate, this) { + + @Override + protected void setProgress(int progress) { + SaveCSVWorker.this.setProgress(progress); + } + + }; + + try { + CSVExport.exportCSV(os, simulation, branch, fields, units, fieldSeparator, + commentStarter, simulationComments, fieldComments, eventComments); + } finally { + try { + os.close(); + } catch (Exception e) { + System.err.println("Error closing file: "); + e.printStackTrace(); + } + } + return null; + } + + + + /** + * Exports a CSV file using a progress dialog if necessary. + * + * @return true if the save was successful, false otherwise. + */ + public static boolean export(File file, Simulation simulation, FlightDataBranch branch, + Type[] fields, Unit[] units, String fieldSeparator, String commentStarter, + boolean simulationComments, boolean fieldComments, boolean eventComments, + Window parent) { + + + SaveCSVWorker worker = new SaveCSVWorker(file, simulation, branch, fields, units, + fieldSeparator, commentStarter, simulationComments, fieldComments, + eventComments); + + if (!SwingWorkerDialog.runWorker(parent, "Exporting flight data", + "Writing " + file.getName() + "...", worker)) { + + // User cancelled the save + file.delete(); + return false; + } + + try { + worker.get(); + } catch (ExecutionException e) { + Throwable cause = e.getCause(); + + if (cause instanceof IOException) { + JOptionPane.showMessageDialog(parent, new String[] { + "An I/O error occurred while saving:", + e.getMessage() }, "Saving failed", JOptionPane.ERROR_MESSAGE); + return false; + } else { + throw new RuntimeException("Unknown error when saving file", e); + } + + } catch (InterruptedException e) { + throw new RuntimeException("EDT was interrupted", e); + } + + return true; + } +}