From: kruland2607 Date: Thu, 22 Dec 2011 02:33:43 +0000 (+0000) Subject: Repackage ProgressOutputStream to net.sf.openrocket.gui.util X-Git-Tag: upstream/12.03~1^2~228 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=9fd7dbf36fcf805b0dace37b9077cce3e973e725;p=debian%2Fopenrocket Repackage ProgressOutputStream to net.sf.openrocket.gui.util git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@242 180e2498-e6e9-4542-8430-84ac67f01cd8 --- diff --git a/src/net/sf/openrocket/gui/util/ProgressOutputStream.java b/src/net/sf/openrocket/gui/util/ProgressOutputStream.java new file mode 100644 index 00000000..22935736 --- /dev/null +++ b/src/net/sf/openrocket/gui/util/ProgressOutputStream.java @@ -0,0 +1,73 @@ +package net.sf.openrocket.gui.util; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.InterruptedIOException; +import java.io.OutputStream; + +import javax.swing.SwingWorker; + +import net.sf.openrocket.util.MathUtil; + + +public abstract class ProgressOutputStream extends FilterOutputStream { + + private final int totalBytes; + private final SwingWorker worker; + private int writtenBytes = 0; + private int progress = -1; + + public ProgressOutputStream(OutputStream out, int estimate, SwingWorker worker) { + super(out); + this.totalBytes = estimate; + this.worker = worker; + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + writtenBytes += len; + setProgress(); + if (worker.isCancelled()) { + throw new InterruptedIOException("SaveFileWorker was cancelled"); + } + } + + @Override + public void write(byte[] b) throws IOException { + out.write(b); + writtenBytes += b.length; + setProgress(); + if (worker.isCancelled()) { + throw new InterruptedIOException("SaveFileWorker was cancelled"); + } + } + + @Override + public void write(int b) throws IOException { + out.write(b); + writtenBytes++; + setProgress(); + if (worker.isCancelled()) { + throw new InterruptedIOException("SaveFileWorker was cancelled"); + } + } + + + private void setProgress() { + int p = MathUtil.clamp(writtenBytes * 100 / totalBytes, 0, 100); + if (progress != p) { + progress = p; + setProgress(progress); + } + } + + /** + * Set the current progress. The value of progress is guaranteed + * to be between 0 and 100, inclusive. + * + * @param progress the current progress in the range 0-100. + */ + protected abstract void setProgress(int progress); + +} \ No newline at end of file diff --git a/src/net/sf/openrocket/gui/util/SaveCSVWorker.java b/src/net/sf/openrocket/gui/util/SaveCSVWorker.java index 55ef4da7..efa7d13d 100644 --- a/src/net/sf/openrocket/gui/util/SaveCSVWorker.java +++ b/src/net/sf/openrocket/gui/util/SaveCSVWorker.java @@ -18,7 +18,6 @@ import net.sf.openrocket.simulation.FlightDataType; import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.Unit; import net.sf.openrocket.util.BugException; -import net.sf.openrocket.util.ProgressOutputStream; public class SaveCSVWorker extends SwingWorker { diff --git a/src/net/sf/openrocket/gui/util/SaveFileWorker.java b/src/net/sf/openrocket/gui/util/SaveFileWorker.java index 9e5f4117..1ccc3145 100644 --- a/src/net/sf/openrocket/gui/util/SaveFileWorker.java +++ b/src/net/sf/openrocket/gui/util/SaveFileWorker.java @@ -9,7 +9,6 @@ import javax.swing.SwingWorker; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.file.RocketSaver; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.util.ProgressOutputStream; public class SaveFileWorker extends SwingWorker { diff --git a/src/net/sf/openrocket/util/ProgressOutputStream.java b/src/net/sf/openrocket/util/ProgressOutputStream.java deleted file mode 100644 index c52a2126..00000000 --- a/src/net/sf/openrocket/util/ProgressOutputStream.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.sf.openrocket.util; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.io.OutputStream; - -import javax.swing.SwingWorker; - - -public abstract class ProgressOutputStream extends FilterOutputStream { - - private final int totalBytes; - private final SwingWorker worker; - private int writtenBytes = 0; - private int progress = -1; - - public ProgressOutputStream(OutputStream out, int estimate, SwingWorker worker) { - super(out); - this.totalBytes = estimate; - this.worker = worker; - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - out.write(b, off, len); - writtenBytes += len; - setProgress(); - if (worker.isCancelled()) { - throw new InterruptedIOException("SaveFileWorker was cancelled"); - } - } - - @Override - public void write(byte[] b) throws IOException { - out.write(b); - writtenBytes += b.length; - setProgress(); - if (worker.isCancelled()) { - throw new InterruptedIOException("SaveFileWorker was cancelled"); - } - } - - @Override - public void write(int b) throws IOException { - out.write(b); - writtenBytes++; - setProgress(); - if (worker.isCancelled()) { - throw new InterruptedIOException("SaveFileWorker was cancelled"); - } - } - - - private void setProgress() { - int p = MathUtil.clamp(writtenBytes * 100 / totalBytes, 0, 100); - if (progress != p) { - progress = p; - setProgress(progress); - } - } - - /** - * Set the current progress. The value of progress is guaranteed - * to be between 0 and 100, inclusive. - * - * @param progress the current progress in the range 0-100. - */ - protected abstract void setProgress(int progress); - -} \ No newline at end of file