KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > client > monitoring > MonitorDisplayFormatter


1 /*
2  * MonitorDisplayFormatter.java
3  *
4  * Created on December 24, 2002, 12:23 PM
5  */

6
7 package com.quikj.application.web.talk.client.monitoring;
8
9 import java.awt.*;
10
11 /**
12  *
13  * @author amit
14  */

15 public class MonitorDisplayFormatter
16 {
17     private static final int MAX_COLUMN_LENGTH = 255;
18     private TextArea textArea;
19     
20     /** Creates a new instance of MonitorDisplayFormatter */
21     public MonitorDisplayFormatter(TextArea text_area)
22     {
23         textArea = text_area;
24         
25         // cannot used "Monospaced" because the stupid Microsoft JVM for Windows 98
26
// does not support it.
27
textArea.setFont(new Font("Courier", Font.PLAIN, 12));
28     }
29     
30     public void clear()
31     {
32         textArea.setText("");
33     }
34     
35     public void appendText(String JavaDoc text)
36     {
37         textArea.append(text);
38     }
39     
40     public TextArea getTextArea()
41     {
42         return textArea;
43     }
44     
45     public void appendTable(String JavaDoc[] table_header, String JavaDoc[][] data)
46     {
47         // print the result
48
int cols = table_header.length;
49         int width = 0;
50         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
51         StringBuffer JavaDoc bar = new StringBuffer JavaDoc();
52         int i;
53         
54         // top bar
55
for (i = 0; i < cols; i++)
56         {
57             int col_width = table_header[i].length();
58             if (col_width > MAX_COLUMN_LENGTH)
59             {
60                 col_width = MAX_COLUMN_LENGTH;
61             }
62             width += col_width;
63         }
64         width += 1 + cols;
65         
66         for (i = 0; i < width; i++)
67         {
68             bar.append('-');
69         }
70         bar.append('\n');
71         buffer.append(bar + "|");
72         
73         // now print the columns
74
for (i = 0; i < cols; i++)
75         {
76             StringBuffer JavaDoc filler = new StringBuffer JavaDoc();
77             String JavaDoc label = table_header[i];
78             int size = table_header[i].length();
79             if (size > MAX_COLUMN_LENGTH)
80             {
81                 size = MAX_COLUMN_LENGTH;
82             }
83             
84             int x;
85             if (label.length() > size)
86             {
87                 label = label.substring(0, size);
88             }
89             
90             if (label.length() < size)
91             {
92                 x = size - label.length();
93                 int j;
94                 for (j = 0; j < x; j++)
95                 {
96                     filler.append(' ');
97                 }
98                 
99                 label = label + filler;
100             }
101             
102             buffer.append(label + "|");
103         }
104         
105         // lower bar
106
buffer.append("\n" + bar);
107         
108         // now, add the rows
109
for (i = 0; i < data.length; i++)
110         {
111             buffer.append("|");
112             
113             // format each column of the row
114
for (int j = 0; j < cols; j++)
115             {
116                 String JavaDoc value = data[i][j];
117                 
118                 int size = table_header[j].length();
119                 if (size > MAX_COLUMN_LENGTH)
120                 {
121                     size = MAX_COLUMN_LENGTH;
122                 }
123                 
124                 String JavaDoc str;
125                 if (value == null)
126                 {
127                     str = "";
128                 }
129                 else
130                 {
131                     str = value;
132                 }
133                 
134                 if (str.length() > size)
135                 {
136                     str = str.substring(0, size);
137                 }
138                 else if (str.length() < size)
139                 {
140                     int x = size - str.length();
141                     StringBuffer JavaDoc filler = new StringBuffer JavaDoc();
142                     for (int k = 0; k < x; k++)
143                     {
144                         filler.append(' ');
145                     }
146                     str += filler;//toString()
147
}
148                 
149                 // replace any new lines with the space characters
150
str = str.replace('\n', ' ');
151                 
152                 buffer.append(str + "|");
153             }
154             buffer.append("\n");
155         }
156         
157         buffer.append(bar.toString() + "\n");
158         textArea.append(buffer.toString());
159     }
160     
161 }
162
Popular Tags