[Japi-cvs] SF.net SVN: japi: [340] libs/swing-misc/trunk/src/net/sf/japi/swing/misc/ ConsoleProgres
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-05-15 11:25:58
|
Revision: 340 http://svn.sourceforge.net/japi/?rev=340&view=rev Author: christianhujer Date: 2007-05-15 04:25:57 -0700 (Tue, 15 May 2007) Log Message: ----------- Added progress implementation for the console. Added Paths: ----------- libs/swing-misc/trunk/src/net/sf/japi/swing/misc/ConsoleProgress.java Added: libs/swing-misc/trunk/src/net/sf/japi/swing/misc/ConsoleProgress.java =================================================================== --- libs/swing-misc/trunk/src/net/sf/japi/swing/misc/ConsoleProgress.java (rev 0) +++ libs/swing-misc/trunk/src/net/sf/japi/swing/misc/ConsoleProgress.java 2007-05-15 11:25:57 UTC (rev 340) @@ -0,0 +1,109 @@ +package net.sf.japi.swing.misc; + +import java.awt.Component; +import java.io.Closeable; +import java.io.IOException; +import java.io.Flushable; +import org.jetbrains.annotations.Nullable; + +/** + * ConsoleProgress is a {@link Progress} implementation for headless systems. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class ConsoleProgress implements Progress { + + /** The destination for messages. */ + private final Appendable out; + + /** The flushable for out. */ + private final Flushable flushOut; + + /** The closeable for out. */ + private final Closeable closeOut; + + /** Current maximum. */ + private int max; + + /** Create a ConsoleProgress that uses System.out. */ + public ConsoleProgress() { + this(System.out, true, false); + } + + /** Create a ConsoleProgress that uses the specified Appendable. + * @param out Appendable to use + * @param flush Whether to flush the Appendable + * @param close Whether to close the Appendable + */ + public ConsoleProgress(final Appendable out, final boolean flush, final boolean close) { + this.out = out; + flushOut = flush && out instanceof Flushable ? (Flushable) out : null; + closeOut = close && out instanceof Closeable ? (Closeable) out : null; + } + + /** Close the closeable. */ + private void close() { + if (closeOut != null) { + try { + closeOut.close(); + } catch (final IOException e) { + // ignore + } + } + } + + /** Flush the flushable. */ + private void flush() { + if (flushOut != null) { + try { + flushOut.flush(); + } catch (final IOException e) { + // ignore + } + } + } + + /** {@inheritDoc} */ + public void finished() { + close(); + } + + /** {@inheritDoc} */ + @Nullable + public Component getParentComponent() { + return null; + } + + /** {@inheritDoc} */ + public void setLabel(final String msg, final int max) { + this.max = max; + try { + out.append(" 0%: "); + out.append(msg); + flush(); + for (int i = 0; i < msg.length(); i++) { + out.append('\b'); + } + for (int i = 0; i < " 0%: ".length(); i++) { + out.append('\b'); + } + } catch (final IOException e) { + // ignore + } + } + + /** {@inheritDoc} */ + public void setValue(final int value) { + if (max == 0) { + return; + } + try { + final int prog = value * 100 / max; + out.append(String.format("%3d", prog)); + flush(); + out.append("\b\b\b"); + } catch (final IOException e) { + // ignore + } + } + +} // class ConsoleProgress Property changes on: libs/swing-misc/trunk/src/net/sf/japi/swing/misc/ConsoleProgress.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |