001    package net.sf.cpsolver.ifs.util;
002    
003    import java.io.PrintStream;
004    
005    /**
006     * Prints current progres to {@link PrintStream}. <br>
007     * <br>
008     * Example usage:
009     * <ul>
010     * <code>
011     * Progress.getInstance().addProgressListener(new ProgressWriter(System.out));<br>
012     * </code>
013     * </ul>
014     * <br>
015     * Example output:
016     * <ul>
017     * <code>
018     * Reading course.pl ...       :<br>
019     * Reading altcourse.pl ...    :<br>
020     * Reading room.pl ...         :<br>
021     * Creating rooms ...          : ................................................<br>
022     * Creating variables ...      : ................................................<br>
023     * Reading students.pl ...     :<br>
024     * Reading jenr.pl ...         :<br>
025     * Creating jenrl constraints .: ................................................<br>
026     * Reading add.pl ...          :<br>
027     * Creating group constraints .: ................................................<br>
028     * Creating initial assignment : ................................................<br>
029     * Creating dept. spread constr: ................................................<br>
030     * Input data loaded           : ................................................<br>
031     * Initializing solver         :<br>
032     * Searching for initial soluti: ................................................<br>
033     * Improving found solution ...: ................................................<br>
034     * Improving found solution ...: ................................................<br>
035     * Improving found solution ...: ...................................
036     * </code>
037     * </ul>
038     * 
039     * 
040     * @version IFS 1.2 (Iterative Forward Search)<br>
041     *          Copyright (C) 2006 - 2010 Tomas Muller<br>
042     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
043     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
044     * <br>
045     *          This library is free software; you can redistribute it and/or modify
046     *          it under the terms of the GNU Lesser General Public License as
047     *          published by the Free Software Foundation; either version 3 of the
048     *          License, or (at your option) any later version. <br>
049     * <br>
050     *          This library is distributed in the hope that it will be useful, but
051     *          WITHOUT ANY WARRANTY; without even the implied warranty of
052     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
053     *          Lesser General Public License for more details. <br>
054     * <br>
055     *          You should have received a copy of the GNU Lesser General Public
056     *          License along with this library; if not see
057     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
058     */
059    public class ProgressWriter implements ProgressListener {
060        private PrintStream iTextOut = null;
061        private static int TEXT_LENGTH = 28;
062        private static int DOTS_LENGTH = 48;
063        private int iPrintedDots = -1;
064    
065        public ProgressWriter(PrintStream out) {
066            iTextOut = out;
067        }
068    
069        @Override
070        public void statusChanged(String status) {
071            // iTextOut.println("Status: "+status);
072        }
073    
074        @Override
075        public void phaseChanged(String phase) {
076            if (iPrintedDots > 0) {
077                while (iPrintedDots < DOTS_LENGTH) {
078                    iTextOut.print(".");
079                    iPrintedDots++;
080                }
081            }
082            iTextOut.println();
083            iTextOut.print(expand(phase, TEXT_LENGTH, ' ', false) + ": ");
084            iPrintedDots = 0;
085            iTextOut.flush();
086        }
087    
088        @Override
089        public void progressChanged(long currentProgress, long maxProgress) {
090            int dotsToPrint = (maxProgress == 0 ? 0 : (int) ((DOTS_LENGTH * currentProgress) / maxProgress));
091            while (iPrintedDots < dotsToPrint) {
092                iTextOut.print(".");
093                iPrintedDots++;
094            }
095            iTextOut.flush();
096        }
097    
098        @Override
099        public void progressSaved() {
100        }
101    
102        @Override
103        public void progressRestored() {
104        }
105    
106        @Override
107        public void progressMessagePrinted(Progress.Message msg) {
108        }
109    
110        private static String expand(String source, int length, char ch, boolean beg) {
111            StringBuffer sb = new StringBuffer(source == null ? "" : source.length() > length ? (beg ? source
112                    .substring(source.length() - length) : source.substring(0, length)) : source);
113            while (sb.length() < length) {
114                if (beg)
115                    sb.insert(0, ch);
116                else
117                    sb.append(ch);
118            }
119            return sb.toString();
120        }
121    }