9f94d5105b91a8a4a2c5ef543ad2b4c6306f6ebb
[debian/openrocket] / src / net / sf / openrocket / util / SaveFileWorker.java
1 package net.sf.openrocket.util;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6
7 import javax.swing.SwingWorker;
8
9 import net.sf.openrocket.document.OpenRocketDocument;
10 import net.sf.openrocket.file.RocketSaver;
11
12 public class SaveFileWorker extends SwingWorker<Void, Void> {
13
14         private final OpenRocketDocument document;
15         private final File file;
16         private final RocketSaver saver;
17         
18         public SaveFileWorker(OpenRocketDocument document, File file, RocketSaver saver) {
19                 this.document = document;
20                 this.file = file;
21                 this.saver = saver;
22         }
23         
24         
25         @Override
26         protected Void doInBackground() throws Exception {
27                 
28                 int estimate = (int)saver.estimateFileSize(document, 
29                                 document.getDefaultStorageOptions());
30                 
31                 // Create the ProgressOutputStream that provides progress estimates
32                 ProgressOutputStream os = new ProgressOutputStream(
33                                 new BufferedOutputStream(new FileOutputStream(file)), 
34                                 estimate, this) {
35                         
36                         @Override
37                         protected void setProgress(int progress) {
38                                 SaveFileWorker.this.setProgress(progress);
39                         }
40                         
41                 };
42                 
43                 try {
44                         saver.save(os, document);
45                 } finally {
46                         try {
47                                 os.close();
48                         } catch (Exception e) {
49                                 System.err.println("Error closing file: ");
50                                 e.printStackTrace();
51                         }
52                 }
53                 return null;
54         }
55         
56 }