Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 ira 1
/*******************************************************************************
2
 * GanttChart.java
3
 *
4
 * Produces a beautiful Gantt Chart from a Vector of LogEntrys. This is
5
 * commonly produced by the Scheduler class and its derivatives.
6
 *
7
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
 * IN THE SOFTWARE.
26
 ******************************************************************************/
27
 
28
import java.io.PrintStream;
29
import java.io.UnsupportedEncodingException;
30
import java.util.Vector;
31
 
32
/**
33
 * Produces a Gantt Chart from a Vector of LogEntrys. This is the output
34
 * of a Scheduler run.
35
 *
36
 * @class CS431 Fall 2006
37
 * @author Ira W. Snyder (devel@irasnyder.com)
38
 */
39
public class GanttChart
40
{
41
    /* Upper Left, Middle, and Right */
42
    private final String ul = "\u250c";
43
    private final String um = "\u252c";
44
    private final String ur = "\u2510";
45
 
46
    /* Lower Left, Middle, and Right */
47
    private final String ll = "\u251c";
48
    private final String lm = "\u253c";
49
    private final String lr = "\u2524";
50
 
51
    /* Horizonal and Vertical Bars */
52
    private final String hb = "\u2500";
53
    private final String vb = "\u2502";
54
    private final String ds = "   "; // 3 Spaces
55
 
56
    /* The log from the Scheduler class */
57
    private final Vector<LogEntry> log;
58
 
59
    /**
60
     * Constructor for the GanttChart class.
61
     *
62
     * @param log a log produced by the Scheduler class
63
     */
64
    public GanttChart (Vector<LogEntry> log)
65
    {
66
        this.log = log;
67
    }
68
 
69
    /**
70
     * Prints a beautiful Gantt Chart representation of the log
71
     * from a run of the Scheduler class.
72
     */
73
    public void printGanttChart ()
74
    {
75
        /* Each box will be constructed here */
76
        String top_temp  = new String();
77
        String name_temp = new String();
78
        String bot_temp  = new String();
79
        String labl_temp = new String();
80
 
81
        /* Each line will be constructed here */
82
        String top_line  = new String();
83
        String name_line = new String();
84
        String bot_line  = new String();
85
        String labl_line = new String();
86
 
87
        int exptime = 0;
88
        int templen;
89
 
90
        PrintStream out;
91
 
92
        /* Create the UTF-8 Capable PrintStream */
93
        try {
94
            out = new PrintStream (System.out, true, "UTF-8");
95
        } catch (UnsupportedEncodingException e) {
96
            System.err.println ("Unsupported Terminal, sorry...");
97
            System.err.println ("No pretty Gantt Charts... :(");
98
            return;
99
        }
100
 
101
        /* Run through every log entry in the order they were created, and add a
102
         * box to the output for each relevant one. */
103
        for (LogEntry e : log)
104
        {
105
            if (e.msg == LogEntry.MsgType.ADDED)
106
                continue;
107
 
108
            if (e.msg == LogEntry.MsgType.EXPIRE || e.msg == LogEntry.MsgType.COMPLETE)
109
            {
110
                exptime = e.time;
111
                continue;
112
            }
113
 
114
            /* Generate the "name" for this box */
115
            name_temp = vb + ds + e.proc.name + ds;
116
 
117
            /* Generate the "top" for this box */
118
            if (top_line.length() == 0)
119
                top_temp = ul;
120
            else
121
                top_temp = um;
122
 
123
            for (int i=0; top_temp.length() < name_temp.length(); i++)
124
                top_temp += hb;
125
 
126
            /* Generate the "bot" for this box */
127
            if (bot_line.length() == 0)
128
                bot_temp = ll;
129
            else
130
                bot_temp = lm;
131
 
132
            for (int i=0; bot_temp.length() < name_temp.length(); i++)
133
                bot_temp += hb;
134
 
135
            /* Generate the "labl" for this box */
136
            labl_temp = "" + e.time;
137
 
138
            for (int i=0; labl_temp.length() < name_temp.length(); i++)
139
                labl_temp += " ";
140
 
141
            /* Check if we need to wrap */
142
            if (name_line.length() + name_temp.length() > 79)
143
            {
144
                top_line  += ur;
145
                name_line += vb;
146
                bot_line  += lr;
147
 
148
                templen = labl_line.length() - String.valueOf(exptime).length();
149
                labl_line = labl_line.substring (0, templen+1) + String.valueOf(exptime);
150
 
151
                /* Print this line, since we're finished generating it */
152
                out.println (top_line);
153
                out.println (name_line);
154
                out.println (bot_line);
155
                out.println (labl_line);
156
                out.println ();
157
 
158
                /* Clear Lines */
159
                top_line  = new String();
160
                name_line = new String();
161
                bot_line  = new String();
162
                labl_line = new String(); 
163
 
164
                /* "Fix" the current box */
165
                top_temp = ul + top_temp.substring (1, top_temp.length());
166
                bot_temp = ll + bot_temp.substring (1, bot_temp.length());
167
            }
168
 
169
            top_line  += top_temp;
170
            name_line += name_temp;
171
            bot_line  += bot_temp;
172
            labl_line += labl_temp;
173
        }
174
 
175
        /* We're done with entries, so cap it off */
176
        top_line  += ur;
177
        name_line += vb;
178
        bot_line  += lr;
179
 
180
        templen = labl_line.length() - String.valueOf(exptime).length();
181
        labl_line = labl_line.substring (0, templen+1) + String.valueOf(exptime);
182
 
183
        /* Print this line, since we've run out of data */
184
        out.println (top_line);
185
        out.println (name_line);
186
        out.println (bot_line);
187
        out.println (labl_line);
188
        out.println ();
189
    }
190
}
191
 
192
/* vim: set ts=4 sts=4 sw=4 expandtab: */
193