create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / SaveFileWorker.java
1 package net.sf.openrocket.gui.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 import net.sf.openrocket.startup.Application;
12
13 public class SaveFileWorker extends SwingWorker<Void, Void> {
14
15         private final OpenRocketDocument document;
16         private final File file;
17         private final RocketSaver saver;
18         
19         public SaveFileWorker(OpenRocketDocument document, File file, RocketSaver saver) {
20                 this.document = document;
21                 this.file = file;
22                 this.saver = saver;
23         }
24         
25         
26         @Override
27         protected Void doInBackground() throws Exception {
28                 
29                 int estimate = (int)saver.estimateFileSize(document, 
30                                 document.getDefaultStorageOptions());
31                 
32                 // Create the ProgressOutputStream that provides progress estimates
33                 ProgressOutputStream os = new ProgressOutputStream(
34                                 new BufferedOutputStream(new FileOutputStream(file)), 
35                                 estimate, this) {
36                         
37                         @Override
38                         protected void setProgress(int progress) {
39                                 SaveFileWorker.this.setProgress(progress);
40                         }
41                         
42                 };
43                 
44                 try {
45                         saver.save(os, document);
46                 } finally {
47                         try {
48                                 os.close();
49                         } catch (Exception e) {
50                                 Application.getExceptionHandler().handleErrorCondition("Error closing file", e);
51                         }
52                 }
53                 return null;
54         }
55         
56 }