// This code prints the column title for all columns.
// It also prints the values that appear in all columns.
Main {
traversal allColumns (ColumnTitleVisitor) {
// columns and sub-parts
bypassing Expr
to *;
}
traversal allColumns (ColumnValueVisitor) {
// just columns
to Column;
}
void printColumnTitle() =
allColumns(ColumnTitleVisitor);
void printColumnValue(String names) =
allColumns(ColumnValueVisitor);
}
Column {
{{
static int min_width = 7;
static int max_width = 30;
int real_width; // the actual width that will be used
}}
}
// This visitor prints the titles of the columns and sets the
// real width that will be used to print the values.
ColumnTitleVisitor {
// Column = "column"
Title [Width] "is" Expr.
{{
String theTitle;
String line1;
String line2;
String dashes = "===================================================";
Column col;
}}
init {{
line1 = "";
line2 = "";
}}
before Column {{
col = host;
col.real_width = 0;
}}
after Main {{
System.out.println (line1 + "Events");
System.out.println (line2 + "======");
}}
after Column {{
if (col.real_width == 0) {
col.real_width = theTitle.length();
}
if (col.real_width < Column.min_width) {
col.real_width = Column.min_width;
} else if (host.real_width > Column.max_width) {
col.real_width = Column.max_width;
}
String paddedString = " " + theTitle;
int paddedLen = paddedString.length();
line1 = line1 + paddedString.substring(paddedLen - col.real_width) + " ";
int dashLen = dashes.length();
line2 = line2 + dashes.substring(dashLen - col.real_width) + " ";
}}
before Title {{
theTitle = host.get_string();
}}
before Width {{
col.real_width = host.get_width().intValue();
}}
}
// This visitor prints the values of the columns.
ColumnValueVisitor {
{{
String line = "";
}}
after Main {{
System.out.println (line + names);
}}
after Column {{
Double value = host.get_expr().evaluate();
Long longValue = new Long(value.longValue()); // don't print decimal point
String paddedString = " " + longValue;
int paddedLen = paddedString.length();
line = line + paddedString.substring(paddedLen - host.real_width) + " ";
}}
}