    001    package ifs.util;
    002
    003    import java.io.PrintStream;
    004
    005    /** Prints current progres to {@link PrintStream}.
    006     * <br><br>
    007     * Example usage:<ul><code>
    008     * Progress.getInstance().addProgressListener(new ProgressWriter(System.out));<br>
    009     * </code></ul>
    010     * <br>
    011     * Example output:<ul><code>
    012     * Reading course.pl ...       :<br>
    013     * Reading altcourse.pl ...    :<br>
    014     * Reading room.pl ...         :<br>
    015     * Creating rooms ...          : ................................................<br>
    016     * Creating variables ...      : ................................................<br>
    017     * Reading students.pl ...     :<br>
    018     * Reading jenr.pl ...         :<br>
    019     * Creating jenrl constraints .: ................................................<br>
    020     * Reading add.pl ...          :<br>
    021     * Creating group constraints .: ................................................<br>
    022     * Creating initial assignment : ................................................<br>
    023     * Creating dept. spread constr: ................................................<br>
    024     * Input data loaded           : ................................................<br>
    025     * Initializing solver         :<br>
    026     * Searching for initial soluti: ................................................<br>
    027     * Improving found solution ...: ................................................<br>
    028     * Improving found solution ...: ................................................<br>
    029     * Improving found solution ...: ...................................
    030     * </code></ul>
    031     *
    032     *
    033     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    034     * @version 1.0
    035    */
    036    public class ProgressWriter implements ProgressListener {
    037        private PrintStream iTextOut = null;
    038        private static int TEXT_LENGTH = 28;
    039        private static int DOTS_LENGTH = 48;
    040        private int iPrintedDots = -1;
    041
    042        public ProgressWriter(PrintStream out) {
    043            iTextOut = out;
    044        }
    045
    046        public void statusChanged(String status) {
    047            //iTextOut.println("Status: "+status);
    048        }
    049
    050        public void phaseChanged(String phase) {
    051            if (iPrintedDots>0) {
    052                while (iPrintedDots<DOTS_LENGTH) { iTextOut.print("."); iPrintedDots++; }
    053            }
    054            iTextOut.println();
    055            iTextOut.print(expand(phase,TEXT_LENGTH,' ',false)+": ");
    056            iPrintedDots = 0;
    057            iTextOut.flush();
    058        }
    059
    060        public void progressChanged(long currentProgress, long maxProgress) {
    061            int dotsToPrint = (maxProgress==0?0:(int)((DOTS_LENGTH*currentProgress)/maxProgress));
    062            while (iPrintedDots<dotsToPrint) {
    063                iTextOut.print(".");
    064                iPrintedDots++;
    065            }
    066            iTextOut.flush();
    067        }
    068
    069        public void progressSaved() {}
    070        public void progressRestored() {}
    071
    072        private static String expand(String source, int length, char ch, boolean beg) {
    073            StringBuffer sb = new StringBuffer(source==null?"":source.length()>length?(beg?source.substring(source.length()-length):source.substring(0,length)):source);
    074            while (sb.length()<length) {
    075                if (beg) sb.insert(0,ch); else sb.append(ch);
    076            }
    077            return sb.toString();
    078        }
    079    }