Subversion Repositories programming

Rev

Rev 419 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * GanttChart.java
 *
 * Produces a beautiful Gantt Chart from a Vector of LogEntrys. This is
 * commonly produced by the Scheduler class and its derivatives.
 *
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 ******************************************************************************/

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

/**
 * Produces a Gantt Chart from a Vector of LogEntrys. This is the output
 * of a Scheduler run.
 *
 * @class CS431 Fall 2006
 * @author Ira W. Snyder (devel@irasnyder.com)
 */
public class GanttChart
{
    /* Upper Left, Middle, and Right */
    private final String ul = "\u250c";
    private final String um = "\u252c";
    private final String ur = "\u2510";

    /* Lower Left, Middle, and Right */
    private final String ll = "\u251c";
    private final String lm = "\u253c";
    private final String lr = "\u2524";

    /* Horizonal and Vertical Bars */
    private final String hb = "\u2500";
    private final String vb = "\u2502";
    private final String ds = "   "; // 3 Spaces

    /* The log from the Scheduler class */
    private final Vector<LogEntry> log;

    /**
     * Constructor for the GanttChart class.
     *
     * @param log a log produced by the Scheduler class
     */
    public GanttChart (Vector<LogEntry> log)
    {
        this.log = log;
    }

    /**
     * Prints a beautiful Gantt Chart representation of the log
     * from a run of the Scheduler class.
     */
    public void printGanttChart ()
    {
        /* Each box will be constructed here */
        String top_temp  = new String();
        String name_temp = new String();
        String bot_temp  = new String();
        String labl_temp = new String();

        /* Each line will be constructed here */
        String top_line  = new String();
        String name_line = new String();
        String bot_line  = new String();
        String labl_line = new String();

        int exptime = 0;
        int templen;

        PrintStream out;

        /* Create the UTF-8 Capable PrintStream */
        try {
            out = new PrintStream (System.out, true, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.err.println ("Unsupported Terminal, sorry...");
            System.err.println ("No pretty Gantt Charts... :(");
            return;
        }

        /* Run through every log entry in the order they were created, and add a
         * box to the output for each relevant one. */
        for (LogEntry e : log)
        {
            if (e.msg == LogEntry.MsgType.ADDED)
                continue;

            if (e.msg == LogEntry.MsgType.EXPIRE || e.msg == LogEntry.MsgType.COMPLETE)
            {
                exptime = e.time;
                continue;
            }

            /* Generate the "name" for this box */
            name_temp = vb + ds + e.proc.name + ds;

            /* Generate the "top" for this box */
            if (top_line.length() == 0)
                top_temp = ul;
            else
                top_temp = um;

            /* Pad out top_temp with horiz bars */
            for ( ; top_temp.length() < name_temp.length(); top_temp += hb);

            /* Generate the "bot" for this box */
            if (bot_line.length() == 0)
                bot_temp = ll;
            else
                bot_temp = lm;

            /* Pad out bot_temp with horiz bars */
            for ( ; bot_temp.length() < name_temp.length(); bot_temp += hb);

            /* Generate the "labl" for this box */
            labl_temp = "" + e.time;

            /* Pad out labl_temp with spaces */
            for ( ; labl_temp.length() < name_temp.length(); labl_temp += " ");

            /* Check if we need to wrap */
            if (name_line.length() + name_temp.length() > 79)
            {
                top_line  += ur;
                name_line += vb;
                bot_line  += lr;

                templen = labl_line.length() - String.valueOf(exptime).length();
                labl_line = labl_line.substring (0, templen+1) + String.valueOf(exptime);

                /* Print this line, since we're finished generating it */
                out.println (top_line);
                out.println (name_line);
                out.println (bot_line);
                out.println (labl_line);
                out.println ();

                /* Clear Lines */
                top_line  = new String();
                name_line = new String();
                bot_line  = new String();
                labl_line = new String(); 

                /* "Fix" the current box */
                top_temp = ul + top_temp.substring (1, top_temp.length());
                bot_temp = ll + bot_temp.substring (1, bot_temp.length());
            }

            top_line  += top_temp;
            name_line += name_temp;
            bot_line  += bot_temp;
            labl_line += labl_temp;
        }

        /* We're done with entries, so cap it off */
        top_line  += ur;
        name_line += vb;
        bot_line  += lr;

        templen = labl_line.length() - String.valueOf(exptime).length();
        labl_line = labl_line.substring (0, templen+1) + String.valueOf(exptime);

        /* Print this line, since we've run out of data */
        out.println (top_line);
        out.println (name_line);
        out.println (bot_line);
        out.println (labl_line);
        out.println ();
    }
}

/* vim: set ts=4 sts=4 sw=4 expandtab: */