Component scaling support
[debian/openrocket] / src / net / sf / openrocket / util / SaveFileWorker.java
index 1a4ab534033b6f407180f987884b98385d31a9e7..fe5f70cfb51f23c4992b18d71d5278436e98af48 100644 (file)
@@ -3,15 +3,12 @@ package net.sf.openrocket.util;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.FilterOutputStream;
-import java.io.IOException;
-import java.io.InterruptedIOException;
-import java.io.OutputStream;
 
 import javax.swing.SwingWorker;
 
 import net.sf.openrocket.document.OpenRocketDocument;
 import net.sf.openrocket.file.RocketSaver;
+import net.sf.openrocket.gui.main.ExceptionHandler;
 
 public class SaveFileWorker extends SwingWorker<Void, Void> {
 
@@ -28,9 +25,21 @@ public class SaveFileWorker extends SwingWorker<Void, Void> {
        
        @Override
        protected Void doInBackground() throws Exception {
+               
+               int estimate = (int)saver.estimateFileSize(document, 
+                               document.getDefaultStorageOptions());
+               
+               // Create the ProgressOutputStream that provides progress estimates
                ProgressOutputStream os = new ProgressOutputStream(
                                new BufferedOutputStream(new FileOutputStream(file)), 
-                               (int)saver.estimateFileSize(document, document.getDefaultStorageOptions()));
+                               estimate, this) {
+                       
+                       @Override
+                       protected void setProgress(int progress) {
+                               SaveFileWorker.this.setProgress(progress);
+                       }
+                       
+               };
                
                try {
                        saver.save(os, document);
@@ -38,62 +47,10 @@ public class SaveFileWorker extends SwingWorker<Void, Void> {
                        try {
                                os.close();
                        } catch (Exception e) {
-                               System.err.println("Error closing file: ");
-                               e.printStackTrace();
+                               ExceptionHandler.handleErrorCondition("Error closing file", e);
                        }
                }
                return null;
        }
        
-       
-       private class ProgressOutputStream extends FilterOutputStream {
-
-               private final int totalBytes;
-               private int writtenBytes = 0;
-               private int progress = -1;
-               
-               public ProgressOutputStream(OutputStream out, int estimate) {
-                       super(out);
-                       this.totalBytes = estimate;
-               }
-
-               @Override
-               public void write(byte[] b, int off, int len) throws IOException {
-                       out.write(b, off, len);
-                       writtenBytes += len;
-                       setProgress();
-                       if (isCancelled()) {
-                               throw new InterruptedIOException("SaveFileWorker was cancelled");
-                       }
-               }
-
-               @Override
-               public void write(byte[] b) throws IOException {
-                       out.write(b);
-                       writtenBytes += b.length;
-                       setProgress();
-                       if (isCancelled()) {
-                               throw new InterruptedIOException("SaveFileWorker was cancelled");
-                       }
-               }
-
-               @Override
-               public void write(int b) throws IOException {
-                       out.write(b);
-                       writtenBytes++;
-                       setProgress();
-                       if (isCancelled()) {
-                               throw new InterruptedIOException("SaveFileWorker was cancelled");
-                       }
-               }
-               
-               
-               private void setProgress() {
-                       int p = MathUtil.clamp(writtenBytes * 100 / totalBytes, 0, 100);
-                       if (progress != p) {
-                               progress = p;
-                               SaveFileWorker.this.setProgress(progress);
-                       }
-               }
-       }
 }