1 package org.sapia.console.table; 2 3 4 9 public class Line { 10 StringBuffer _content = new StringBuffer (); 11 Line _next; 12 13 Line() { 14 } 15 16 Line append(String 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 render(int width, int cellSpacing) { 27 insertSpaces(_content, cellSpacing); 28 appendSpaces(_content, width, cellSpacing); 29 30 StringBuffer toReturn = new StringBuffer (); 31 toReturn.append(_content); 32 33 return toReturn.toString(); 34 } 35 36 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 buf, int cellSpacing) { 93 for (int i = 0; i < cellSpacing; i++) { 94 buf.insert(i, ' '); 95 } 96 } 97 98 private void appendSpaces(StringBuffer 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 |