KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > cmdui > util > Table


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.cmdui.util;
17
18 import org.jmanage.util.StringUtils;
19
20 import java.util.List JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * Utility class to print formatted table to command line.
26  *
27  * date: Feb 25, 2005
28  * @author Rakesh Kalra
29  */

30 public class Table {
31
32     private static final int COLUMN_SPACING = 5;
33
34     private Object JavaDoc[] header;
35     private final int columns;
36     private final List JavaDoc rows;
37     /* contains the max column sizes */
38     private final int[] columnSize;
39
40     public Table(int columns){
41         this.columns = columns;
42         this.rows = new LinkedList JavaDoc();
43         this.columnSize = new int[columns];
44     }
45
46     public void add(Object JavaDoc obj1, Object JavaDoc obj2){
47         add(new Object JavaDoc[]{obj1, obj2});
48     }
49
50     public void add(Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3){
51         add(new Object JavaDoc[]{obj1, obj2, obj3});
52     }
53
54     public void add(Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3, Object JavaDoc obj4){
55         add(new Object JavaDoc[]{obj1, obj2, obj3, obj4});
56     }
57
58     public void add(Object JavaDoc obj1, Object JavaDoc obj2, Object JavaDoc obj3, Object JavaDoc obj4, Object JavaDoc obj5){
59         add(new Object JavaDoc[]{obj1, obj2, obj3, obj4, obj5});
60     }
61
62     public void add(Object JavaDoc[] objects){
63         assert columns == objects.length;
64         rows.add(objects);
65         setColumnSize(objects);
66     }
67
68     public void setHeader(Object JavaDoc[] header) {
69         this.header = header;
70         setColumnSize(header);
71     }
72
73     private void setColumnSize(Object JavaDoc[] objects){
74         for(int i=0; i<objects.length; i++){
75             if(objects[i].toString().length() > columnSize[i])
76                 columnSize[i] = objects[i].toString().length();
77         }
78     }
79
80     public void print(){
81         Out.println();
82         if(header != null){
83             printRow(header);
84             printUnderline(header);
85         }
86         for(Iterator JavaDoc it=rows.iterator(); it.hasNext();){
87             Object JavaDoc[] cols = (Object JavaDoc[])it.next();
88             printRow(cols);
89         }
90     }
91
92     private void printRow(Object JavaDoc[] cols){
93         for(int i=0; i < columns; i++){
94             String JavaDoc columnValue = StringUtils.padRight(cols[i].toString(),
95                     columnSize[i] + COLUMN_SPACING);
96             Out.print(columnValue);
97         }
98         Out.println();
99     }
100
101     private void printUnderline(Object JavaDoc[] cols){
102         for(int i=0; i < columns; i++){
103             String JavaDoc underline =
104                     StringUtils.getCharSeries('-', cols[i].toString().length());
105             underline = StringUtils.padRight(underline,
106                     columnSize[i] + COLUMN_SPACING);
107             Out.print(underline);
108         }
109         Out.println();
110     }
111 }
112
Popular Tags