1 19 20 package de.gulden.util.text; 21 22 import de.gulden.util.Toolbox; 23 import java.util.*; 24 import java.util.Collection ; 25 26 32 public class TextTable { 33 34 38 41 protected int columnCount; 42 43 46 protected Collection rows; 47 48 51 protected double[] columnWeight; 52 53 56 protected boolean border; 57 58 61 protected char borderchar; 62 63 66 protected int maxWidth; 67 68 69 73 76 public TextTable(int columnCount, int width) { 77 this(columnCount,width,initArray(columnCount),'+'); 78 } 79 80 83 public TextTable(int columnCount, int maxWidth, double[] columnWeights, char borderchar) { 84 this.columnCount=columnCount; 85 this.maxWidth=maxWidth; 86 this.columnWeight=columnWeights; 87 this.borderchar=borderchar; 88 rows=new ArrayList(); 89 } 90 91 92 96 99 public void addRow(String [] row) { 100 rows.add(row); 101 } 102 103 public String toString() { 104 String [][][] tableWords=new String [rows.size()][columnCount][]; 106 int row=0; 107 for (Iterator it=rows.iterator();it.hasNext();) { 108 String [] cols=(String [])it.next(); 109 for (int col=0;col<columnCount;col++) { 110 String text=cols[col]; 111 String [] words=splitWords(text); 112 tableWords[row][col]=words; 113 } 114 row++; 115 } 116 int[] width=new int[columnCount]; 118 int minW=0; 119 for (int col=0;col<columnCount;col++) { 120 width[col]=minimumWidth(tableWords,col); 121 minW+=width[col]; 122 } 123 minW+=(columnCount+1); int totalWidth; 126 if (minW<maxWidth) { 127 int rest=maxWidth-minW; 128 normalizeWeights(); 129 totalWidth=0; 130 for (int i=0;i<columnCount;i++) { 131 width[i]+=Math.round(((double)rest)*columnWeight[i]); 132 totalWidth+=width[i]; 133 } 134 totalWidth+=(columnCount+1); 135 } else { 136 totalWidth=minW; 137 } 138 139 String [][][] text=new String [rows.size()][columnCount][]; 141 row=0; 142 for (Iterator it=rows.iterator();it.hasNext();) { 143 String [] r=(String [])it.next(); 144 for (int col=0;col<columnCount;col++) { 145 String s=r[col]; 146 text[row][col]=textBox(tableWords[row][col],width[col]); 147 } 148 row++; 149 } 150 StringBuffer out=new StringBuffer (); 152 for (row=0;row<text.length;row++) { 153 out.append(Toolbox.repeat(borderchar,totalWidth)+Toolbox.NL); int maxHeight=0; 155 for (int col=0;col<columnCount;col++) { 156 if (text[row][col].length>maxHeight) { 157 maxHeight=text[row][col].length; 158 } 159 } 160 for (int i=0;i<maxHeight;i++) { 161 out.append(borderchar); for (int col=0;col<columnCount;col++) { 163 String t; 164 if (i<text[row][col].length) { 165 t=text[row][col][i]; 166 } else { 167 t=Toolbox.repeat(" ",width[col]); 168 } 169 out.append(t+borderchar); } 171 out.append(Toolbox.NL); 172 } 173 } 174 out.append(Toolbox.repeat(borderchar,totalWidth)+Toolbox.NL); return out.toString(); 176 } 177 178 private void normalizeWeights() { 179 double sum=0.0; 180 for (int i=0;i<columnCount;i++) { 181 sum+=columnWeight[i]; 182 } 183 if (sum!=0.0) { 184 for (int i=0;i<columnCount;i++) { 185 columnWeight[i]/=sum; 186 } 187 } 188 } 190 191 192 196 199 private static double[] initArray(int size) { 200 double[] d=new double[size]; 201 for (int i=0;i<size;i++) { 202 d[i]=1.0; 203 } 204 return d; 205 } 206 207 private static int minimumWidth(String [][][] words, int column) { 208 int min=0; 209 for (int row=0;row<words.length;row++) { 210 String [] w=words[row][column]; 211 for (int i=0;i<w.length;i++) { 212 String s=w[i]; 213 if (s.length()>min) { 214 min=s.length(); 215 } 216 } 217 } 218 return min; 219 } 220 221 private static String [] splitWords(String text) { 222 Collection words=new ArrayList(); 224 StringTokenizer st=new StringTokenizer(text," \n", true); 225 while (st.hasMoreTokens()) { 226 String tok=st.nextToken(); 227 if (!tok.equals(" ")) { 228 words.add(tok); 229 } 230 } 231 String [] r=new String [words.size()]; 232 return (String [])words.toArray(r); 233 } 234 235 private static String [] textBox(String [] words, int width) { 236 Collection c=new ArrayList(); 237 String line=""; 238 int i=0; 239 boolean newline = false; 240 while (i<words.length) { 241 String word=words[i]; 242 if (word.equals("\n")) { 243 newline = true; 244 i++; 245 } else { 246 if (word.length()>width) { 248 word=word.substring(0,width); 249 } 250 boolean addSpace=(!line.equals(""))&&(!line.endsWith("-")); 251 if (addSpace) { 252 word=" "+word; 253 } 254 if ((line.length()+word.length())<=width) { 255 line+=word; 256 i++; 257 } else { 258 newline = true; 259 } 260 } 261 if (newline) { 262 line=Toolbox.padRight(line," ",width); 263 c.add(line); line=""; 265 newline = false; 266 } 267 } 268 if (!line.equals("")) { 269 line=Toolbox.padRight(line," ",width); 270 c.add(line); 271 } 272 String [] s=new String [c.size()]; 273 return (String [])c.toArray(s); 274 } 275 276 } | Popular Tags |