KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > util > display > TextTable


1 /**
2 * Copyright (c) 2004-2005 jManage.org
3 *
4 * This is a free software; you can redistribute it and/or
5 * modify it under the terms of the license at
6 * http://www.jmanage.org.
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */

14 package org.jmanage.util.display;
15
16 import org.jmanage.util.StringUtils;
17
18 import java.io.File JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  *
24  * <p>
25  * Date: Sep 29, 2005
26  * @author Rakesh Kalra
27  */

28 public class TextTable extends AbstractTable {
29
30     private static final int COLUMN_SPACING = 5;
31
32     /* contains the max column sizes */
33     private int[] columnSize;
34
35     public String JavaDoc draw(){
36
37         String JavaDoc[] header = getHeader();
38         List JavaDoc rows = getRows();
39
40         /* compute column sizes */
41         columnSize = new int[header.length];
42         setColumnSize(header);
43         for(Iterator JavaDoc it=rows.iterator(); it.hasNext();){
44             setColumnSize((String JavaDoc[])it.next());
45         }
46
47         /* draw */
48         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
49
50         /* print header */
51         printRow(header, buff);
52         printUnderline(header, buff);
53
54         for(Iterator JavaDoc it=rows.iterator(); it.hasNext();){
55             printRow((String JavaDoc[])it.next(), buff);
56         }
57         return buff.toString();
58     }
59
60     private void setColumnSize(String JavaDoc[] row){
61         for(int i=0; i<row.length; i++){
62             if(row[i].length() > columnSize[i])
63                 columnSize[i] = row[i].length();
64         }
65     }
66
67     private void printRow(String JavaDoc[] rows, StringBuffer JavaDoc buff){
68         for(int i=0; i < rows.length; i++){
69             String JavaDoc columnValue = StringUtils.padRight(rows[i],
70                     columnSize[i] + COLUMN_SPACING);
71             buff.append(columnValue);
72         }
73         buff.append(System.getProperty("line.separator"));
74     }
75
76     private void printUnderline(String JavaDoc[] rows, StringBuffer JavaDoc buff){
77         for(int i=0; i < rows.length; i++){
78             String JavaDoc underline =
79                     StringUtils.getCharSeries('-', rows[i].toString().length());
80             underline = StringUtils.padRight(underline,
81                     columnSize[i] + COLUMN_SPACING);
82             buff.append(underline);
83         }
84         buff.append(System.getProperty("line.separator"));
85     }
86 }
87
Popular Tags