| | |
| | | import java.util.Enumeration; |
| | | import java.util.Vector; |
| | | |
| | | import org.opends.server.util.cli.ConsoleApplication; |
| | | |
| | | |
| | | |
| | | /** |
| | | * Utility class for printing aligned collumns of text. |
| | | * Utility class for printing aligned columns of text. |
| | | * <P> |
| | | * This class allows you to specify: |
| | | * <UL> |
| | | * <LI>The number of collumns in the output. This will determine the |
| | | * <LI>The number of columns in the output. This will determine the |
| | | * dimension of the string arrays passed to add(String[]) or |
| | | * addTitle(String[]). |
| | | * <LI>spacing/gap between columns |
| | |
| | | * </UL> |
| | | * <P> |
| | | * Example usage: |
| | | * |
| | | * |
| | | * <PRE> |
| | | * MyPrinter mp = new MyPrinter(3, 2, "-"); |
| | | * String oneRow[] = new String[3]; |
| | |
| | | * <P> |
| | | * The above would print: |
| | | * <P> |
| | | * |
| | | * |
| | | * <PRE> |
| | | * -------------------------------------- |
| | | * User Name Email Address Phone Number |
| | |
| | | * <P> |
| | | * This class also supports multi-row titles and having title strings |
| | | * spanning multiple collumns. Example usage: |
| | | * |
| | | * |
| | | * <PRE> |
| | | * TestPrinter tp = new TestPrinter(4, 2, "-"); |
| | | * String oneRow[] = new String[4]; |
| | |
| | | * <P> |
| | | * The above would print: |
| | | * <P> |
| | | * |
| | | * |
| | | * <PRE> |
| | | * ------------------------------------ |
| | | * Name Contact |
| | |
| | | { |
| | | |
| | | final public static int LEFT = 0; |
| | | |
| | | final public static int CENTER = 1; |
| | | |
| | | final public static int RIGHT = 2; |
| | | |
| | | private int numCol = 2; |
| | | |
| | | private int gap = 4; |
| | | |
| | | private int align = CENTER; |
| | | |
| | | private int titleAlign = CENTER; |
| | | |
| | | private String border = null; |
| | | |
| | | private Vector<String[]> titleTable = null; |
| | | |
| | | private Vector<int[]> titleSpanTable = null; |
| | | |
| | | private int curLength[]; |
| | | |
| | | private final ConsoleApplication app; |
| | |
| | | |
| | | /** |
| | | * Creates a new MultiColumnPrinter class. |
| | | * |
| | | * |
| | | * @param numCol |
| | | * number of columns |
| | | * @param gap |
| | |
| | | * @param align |
| | | * type of alignment within columns |
| | | */ |
| | | public MultiColumnPrinter(int numCol, int gap, String border, |
| | | int align, ConsoleApplication app) |
| | | MultiColumnPrinter(int numCol, int gap, String border, int align, |
| | | ConsoleApplication app) |
| | | { |
| | | |
| | | titleTable = new Vector<String[]>(); |
| | |
| | | |
| | | /** |
| | | * Creates a sorted new MultiColumnPrinter class using LEFT alignment. |
| | | * |
| | | * |
| | | * @param numCol |
| | | * number of columns |
| | | * @param gap |
| | |
| | | * @param border |
| | | * character used to frame the titles |
| | | */ |
| | | public MultiColumnPrinter(int numCol, int gap, String border, |
| | | MultiColumnPrinter(int numCol, int gap, String border, |
| | | ConsoleApplication app) |
| | | { |
| | | this(numCol, gap, border, LEFT, app); |
| | |
| | | /** |
| | | * Creates a sorted new MultiColumnPrinter class using LEFT alignment |
| | | * and with no title border. |
| | | * |
| | | * |
| | | * @param numCol |
| | | * number of columns |
| | | * @param gap |
| | | * gap between each column |
| | | */ |
| | | public MultiColumnPrinter(int numCol, int gap, ConsoleApplication app) |
| | | MultiColumnPrinter(int numCol, int gap, ConsoleApplication app) |
| | | { |
| | | this(numCol, gap, null, LEFT, app); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Adds to the row of strings to be used as the title for the table. |
| | | * |
| | | * |
| | | * @param row |
| | | * Array of strings to print in one row of title. |
| | | */ |
| | | public void addTitle(String[] row) |
| | | void addTitle(String[] row) |
| | | { |
| | | if (row == null) |
| | | return; |
| | | if (row == null) return; |
| | | |
| | | int[] span = new int[row.length]; |
| | | for (int i = 0; i < row.length; i++) |
| | |
| | | * is 4 collumns wide, it is possible to have some title strings in a |
| | | * row to 'span' multiple collumns: |
| | | * <P> |
| | | * |
| | | * |
| | | * <PRE> |
| | | * ------------------------------------ |
| | | * Name Contact |
| | |
| | | * Bob Jones bob@foo.com 123-4567 |
| | | * John Doe john@foo.com 456-7890 |
| | | * </PRE> |
| | | * |
| | | * |
| | | * In the example above, the title row has a string 'Name' that spans |
| | | * 2 collumns. The string 'Contact' also spans 2 collumns. The above |
| | | * is done by passing in to addTitle() an array that contains: |
| | | * |
| | | * |
| | | * <PRE> |
| | | * span[0] = 2; // spans 2 collumns |
| | | * span[1] = 0; // spans 0 collumns, ignore |
| | |
| | | * <P> |
| | | * A span value of 1 is the default. The method addTitle(String[] row) |
| | | * basically does: |
| | | * |
| | | * |
| | | * <PRE> |
| | | * int[] span = new int[row.length]; |
| | | * for (int i = 0; i < row.length; i++) |
| | |
| | | * } |
| | | * addTitle(row, span); |
| | | * </PRE> |
| | | * |
| | | * |
| | | * @param row |
| | | * Array of strings to print in one row of title. |
| | | * @param span |
| | | * Array of integers that reflect the number of collumns the |
| | | * corresponding title string will occupy. |
| | | */ |
| | | public void addTitle(String[] row, int span[]) |
| | | void addTitle(String[] row, int span[]) |
| | | { |
| | | // Need to create a new instance of it, otherwise the new values |
| | | // will always overwrite the old values. |
| | |
| | | |
| | | /** |
| | | * Set alignment for title strings |
| | | * |
| | | * |
| | | * @param titleAlign |
| | | */ |
| | | public void setTitleAlign(int titleAlign) |
| | | void setTitleAlign(int titleAlign) |
| | | { |
| | | this.titleAlign = titleAlign; |
| | | } |
| | |
| | | /** |
| | | * Clears title strings. |
| | | */ |
| | | public void clearTitle() |
| | | void clearTitle() |
| | | { |
| | | titleTable.clear(); |
| | | titleSpanTable.clear(); |
| | |
| | | /** |
| | | * Prints the table title |
| | | */ |
| | | public void printTitle() |
| | | void printTitle() |
| | | { |
| | | // Get the longest string for each column and store in curLength[] |
| | | |
| | |
| | | Enumeration<int[]> spanEnum = titleSpanTable.elements(); |
| | | while (elm.hasMoreElements()) |
| | | { |
| | | String[] row = (String[]) elm.nextElement(); |
| | | int[] curSpan = (int[]) spanEnum.nextElement(); |
| | | String[] row = elm.nextElement(); |
| | | int[] curSpan = spanEnum.nextElement(); |
| | | |
| | | for (int i = 0; i < numCol; i++) |
| | | { |
| | | // None of the fields should be null, but if it |
| | | // happens to be so, replace it with "-". |
| | | if (row[i] == null) |
| | | row[i] = "-"; |
| | | if (row[i] == null) row[i] = "-"; |
| | | |
| | | int len = row[i].length(); |
| | | |
| | |
| | | |
| | | while (elm.hasMoreElements()) |
| | | { |
| | | String[] row = (String[]) elm.nextElement(); |
| | | int[] curSpan = (int[]) spanEnum.nextElement(); |
| | | String[] row = elm.nextElement(); |
| | | int[] curSpan = spanEnum.nextElement(); |
| | | |
| | | for (int i = 0; i < numCol; i++) |
| | | { |
| | | int availableSpace = 0, span = curSpan[i]; |
| | | |
| | | if (span == 0) |
| | | continue; |
| | | if (span == 0) continue; |
| | | |
| | | availableSpace = curLength[i]; |
| | | |
| | |
| | | int space_before = availableSpace - row[i].length(); |
| | | printSpaces(space_before); |
| | | app.getOutputStream().print(row[i]); |
| | | if (i < numCol - 1) |
| | | printSpaces(gap); |
| | | if (i < numCol - 1) printSpaces(gap); |
| | | } |
| | | else if (titleAlign == CENTER) |
| | | { |
| | |
| | | printSpaces(space_before); |
| | | app.getOutputStream().print(row[i]); |
| | | printSpaces(space_after); |
| | | if (i < numCol - 1) |
| | | printSpaces(gap); |
| | | if (i < numCol - 1) printSpaces(gap); |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | /** |
| | | * Adds one row of text to output. |
| | | * |
| | | * |
| | | * @param row |
| | | * Array of strings to print in one row. |
| | | */ |
| | | public void printRow(String... row) |
| | | void printRow(String... row) |
| | | { |
| | | for (int i = 0; i < numCol; i++) |
| | | { |
| | |
| | | int space_before = curLength[i] - row[i].length(); |
| | | printSpaces(space_before); |
| | | app.getOutputStream().print(row[i]); |
| | | if (i < numCol - 1) |
| | | printSpaces(gap); |
| | | if (i < numCol - 1) printSpaces(gap); |
| | | } |
| | | else if (align == CENTER) |
| | | { |
| | |
| | | printSpaces(space1); |
| | | app.getOutputStream().print(row[i]); |
| | | printSpaces(space2); |
| | | if (i < numCol - 1) |
| | | printSpaces(gap); |
| | | if (i < numCol - 1) printSpaces(gap); |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | private void printBorder() |
| | | { |
| | | if (border == null) |
| | | return; |
| | | if (border == null) return; |
| | | |
| | | // For the value in each column |
| | | for (int i = 0; i < numCol; i++) |