KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > console > table > Line


1 package org.sapia.console.table;
2
3
4 /**
5  * @author Yanick Duchesne
6  * 2002-03-05
7  *
8  */

9 public class Line {
10   StringBuffer JavaDoc _content = new StringBuffer JavaDoc();
11   Line _next;
12
13   Line() {
14   }
15
16   Line append(String JavaDoc content) {
17     _content.append(content);
18
19     return this;
20   }
21
22   void format(int cellWidth) {
23     Utils.formatLine(this, cellWidth);
24   }
25
26   String JavaDoc render(int width, int cellSpacing) {
27     insertSpaces(_content, cellSpacing);
28     appendSpaces(_content, width, cellSpacing);
29
30     StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc();
31     toReturn.append(_content);
32
33     return toReturn.toString();
34   }
35
36   // String render(int width, int cellSpacing){
37
// insertSpaces(_content, cellSpacing);
38
// appendSpaces(_content, width, cellSpacing);
39
// StringBuffer toReturn = new StringBuffer();
40
// toReturn.append(_content);
41
//
42
// if(_next != null){
43
// toReturn.append(System.getProperty("line.separator"));
44
// toReturn.append(_next.render(width, cellSpacing));
45
// }
46
//
47
// return toReturn.toString();
48
// }
49
int getLineCount() {
50     Line current = this;
51     int count = 1;
52
53     while (current._next != null) {
54       current = current._next;
55       count++;
56     }
57
58     return count;
59   }
60
61   Line getLineAt(int height) {
62     int count = 0;
63     Line current = this;
64
65     while (count < height) {
66       current = current._next;
67       count++;
68     }
69
70     return current;
71   }
72
73   void addEmptyLines(int numToAdd, int width, int cellSpacing) {
74     for (int i = 0; i < numToAdd; i++) {
75       addEmptyLine(width, cellSpacing);
76     }
77   }
78
79   void addEmptyLine(int width, int cellSpacing) {
80     Line current = this;
81     int count = 1;
82
83     while (current._next != null) {
84       current = current._next;
85       count++;
86     }
87
88     Line line = new Line();
89     current._next = line;
90   }
91
92   private void insertSpaces(StringBuffer JavaDoc buf, int cellSpacing) {
93     for (int i = 0; i < cellSpacing; i++) {
94       buf.insert(i, ' ');
95     }
96   }
97
98   private void appendSpaces(StringBuffer JavaDoc buf, int width, int cellSpacing) {
99     int j = (width + (2 * cellSpacing)) - buf.length();
100
101     for (int i = 0; i < j; i++) {
102       buf.append(' ');
103     }
104   }
105 }
106
Popular Tags