001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015
016 package lapisx.progress;
017
018 import java.io.*;
019 import lapisx.util.Str;
020
021 public class StreamProgressBar implements ProgressListener {
022 PrintStream out; // output stream
023 int labelWidth; // width of label field in characters
024 int barWidth; // number of dots in progress bar
025 int dotsPrinted; // number of dots already printed
026
027 int maxLabelWidth; // label field will grow until it reaches this size
028
029 public StreamProgressBar () {
030 this (System.out);
031 }
032
033 public StreamProgressBar (OutputStream out) {
034 this (new PrintStream (out), 20, 20);
035 }
036
037 public StreamProgressBar (OutputStream out, int labelWidth, int barWidth) {
038 this.out = new PrintStream (out);
039 this.labelWidth = labelWidth;
040 this.barWidth = barWidth;
041 dotsPrinted = 0;
042 maxLabelWidth = 75 - barWidth;
043 }
044
045 public void startProgress (ProgressEvent event) {
046 String label = event.getLabel ();
047
048 int len = label.length();
049 if (len > labelWidth)
050 labelWidth = Math.min (len, maxLabelWidth);
051 if (len > labelWidth)
052 label = label.substring (0, labelWidth-3) + "...";
053 else
054 label = label + Str.repeat (" ", labelWidth - len);
055
056 out.print (label);
057 out.print (" [");
058
059 dotsPrinted = 0;
060 printDots (event);
061 }
062
063 public void makeProgress (ProgressEvent event) {
064 printDots (event);
065 }
066
067 public void doneProgress (ProgressEvent event) {
068 printDots (event);
069 out.println ("]");
070 }
071
072 void printDots (ProgressEvent event) {
073 // out.print (" {min=" + event.getMinimum ()
074 // + ",max=" + event.getMaximum ()
075 // + ",val=" + event.getValue ()
076 // + ",pct=" + event.getPercentComplete ()
077 // + "}");
078
079 double percent = event.getPercentComplete ();
080 int dots = (int) (percent * barWidth);
081 if (dots > dotsPrinted) {
082 out.print (Str.repeat (".", dots - dotsPrinted));
083 dotsPrinted = dots;
084 }
085 }
086 }