aeb68ad499b01649734eb894edb824f3aecfb6ce
[debian/openrocket] / src / net / sf / openrocket / util / SaveCSVWorker.java
1 package net.sf.openrocket.util;
2
3 import java.awt.Window;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.concurrent.ExecutionException;
9
10 import javax.swing.JOptionPane;
11 import javax.swing.SwingWorker;
12
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;
20
21
22 public class SaveCSVWorker extends SwingWorker<Void, Void> {
23         
24         private static final int BYTES_PER_FIELD_PER_POINT = 7;
25
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;
36         
37         
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) {
41                 this.file = file;
42                 this.simulation = simulation;
43                 this.branch = branch;
44                 this.fields = fields;
45                 this.units = units;
46                 this.fieldSeparator = fieldSeparator;
47                 this.commentStarter = commentStarter;
48                 this.simulationComments = simulationComments;
49                 this.fieldComments = fieldComments;
50                 this.eventComments = eventComments;
51         }
52
53
54         @Override
55         protected Void doInBackground() throws Exception {
56                 
57                 int estimate = BYTES_PER_FIELD_PER_POINT * fields.length * branch.getLength();
58                 estimate = Math.max(estimate, 1000);
59                 
60                 // Create the ProgressOutputStream that provides progress estimates
61                 ProgressOutputStream os = new ProgressOutputStream(
62                                 new BufferedOutputStream(new FileOutputStream(file)), 
63                                 estimate, this) {
64                         
65                         @Override
66                         protected void setProgress(int progress) {
67                                 SaveCSVWorker.this.setProgress(progress);
68                         }
69                         
70                 };
71                 
72                 try {
73                         CSVExport.exportCSV(os, simulation, branch, fields, units, fieldSeparator, 
74                                         commentStarter, simulationComments, fieldComments, eventComments);
75                 } finally {
76                         try {
77                                 os.close();
78                         } catch (Exception e) {
79                                 ExceptionHandler.handleErrorCondition("Error closing file", e);
80                         }
81                 }
82                 return null;
83         }
84         
85         
86         
87         /**
88          * Exports a CSV file using a progress dialog if necessary.
89          *
90          * @return      <code>true</code> if the save was successful, <code>false</code> otherwise.
91          */
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,
95                         Window parent) {
96                 
97
98                 SaveCSVWorker worker = new SaveCSVWorker(file, simulation, branch, fields, units,
99                                 fieldSeparator, commentStarter, simulationComments, fieldComments, 
100                                 eventComments);
101                 
102             if (!SwingWorkerDialog.runWorker(parent, "Exporting flight data", 
103                         "Writing " + file.getName() + "...", worker)) {
104                 
105                 // User cancelled the save
106                 file.delete();
107                 return false;
108             }
109             
110             try {
111                         worker.get();
112                 } catch (ExecutionException e) {
113                         Throwable cause = e.getCause();
114                         
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);
119                         return false;
120                         } else {
121                                 throw new BugException("Unknown error when saving file", e);
122                         }
123                         
124                 } catch (InterruptedException e) {
125                         throw new BugException("EDT was interrupted", e);
126                 }
127                 
128                 return true;
129         }
130 }