1 16 package org.mortbay.html; 17 import java.util.Hashtable ; 18 19 20 31 public class Table extends Block 32 { 33 34 private Block row = null; 35 private Block cell = null; 36 private static Hashtable threadNestingMap = null; 37 private CompositeFactory cellNestFactory = null; 38 private Block _defaultHead=null; 39 private Block _defaultCell=null; 40 private Block _defaultRow=null; 41 42 43 45 public Table() 46 { 47 super("table"); 48 if (threadNestingMap!=null) 49 cellNestFactory = (CompositeFactory) 50 threadNestingMap.get(Thread.currentThread()); 51 } 52 53 54 56 public Table(int border) 57 { 58 super("table"); 59 attribute("border",border); 60 if (threadNestingMap!=null) 61 cellNestFactory = (CompositeFactory) 62 threadNestingMap.get(Thread.currentThread()); 63 } 64 65 66 68 public Table(int border, String attributes) 69 { 70 this(border); 71 attribute(attributes); 72 } 73 74 75 79 public Table newRow() 80 { 81 unnest(); 82 nest(row = new Block("tr")); 83 if (_defaultRow!=null) 84 { 85 row.setAttributesFrom(_defaultRow); 86 if (_defaultRow.size()>0) 87 row.add(_defaultRow.contents()); 88 } 89 cell=null; 90 return this; 91 } 92 93 94 98 public Table newRow(String attributes) 99 { 100 newRow(); 101 row.attribute(attributes); 102 return this; 103 } 104 105 106 110 private void newBlock(String m) 111 { 112 if (row==null) 113 newRow(); 114 else 115 row.unnest(); 116 row.nest(cell=new Block(m)); 117 118 if (cellNestFactory!=null) 119 cell.nest(cellNestFactory.newComposite()); 120 } 121 122 123 127 public Table newCell() 128 { 129 newBlock("td"); 130 if (_defaultCell!=null) 131 { 132 cell.setAttributesFrom(_defaultCell); 133 if (_defaultCell.size()>0) 134 cell.add(_defaultCell.contents()); 135 } 136 return this; 137 } 138 139 140 145 public Table newCell(String attributes) 146 { 147 newCell(); 148 cell.attribute(attributes); 149 return this; 150 } 151 152 153 158 public Table addCell(Object o) 159 { 160 newCell(); 161 cell.add(o); 162 return this; 163 } 164 165 166 171 public Table addCell(Object o, String attributes) 172 { 173 addCell(o); 174 cell.attribute(attributes); 175 return this; 176 } 177 178 179 183 public Table newHeading() 184 { 185 newBlock("th"); 186 if (_defaultHead!=null) 187 { 188 cell.setAttributesFrom(_defaultHead); 189 if (_defaultHead.size()>0) 190 cell.add(_defaultHead.contents()); 191 } 192 return this; 193 } 194 195 196 201 public Table addHeading(Object o) 202 { 203 newHeading(); 204 cell.add(o); 205 return this; 206 } 207 208 209 214 public Table addHeading(Object o,String attributes) 215 { 216 addHeading(o); 217 cell.attribute(attributes); 218 return this; 219 } 220 221 222 226 public Table cellSpacing(int s) 227 { 228 attribute("cellspacing",s); 229 return this; 230 } 231 232 233 234 238 public Table cellPadding(int padding) 239 { 240 attribute("cellpadding",padding); 241 return this; 242 } 243 244 245 250 public Table spacing(int h, int v) 251 { 252 if (h>=0) 253 attribute("hspace",h); 254 if (v>=0) 255 attribute("vspace",v); 256 return this; 257 } 258 259 260 265 public Block row() 266 { 267 return row; 268 } 269 270 271 276 public Block cell() 277 { 278 return cell; 279 } 280 281 282 290 public static void setCellNestingFactory(CompositeFactory factory) 291 { 292 if (threadNestingMap==null) 293 threadNestingMap= new Hashtable (); 294 295 if (factory == null) 296 threadNestingMap.remove(Thread.currentThread()); 297 else 298 threadNestingMap.put(Thread.currentThread(),factory); 299 } 300 301 302 309 public static void setThreadNestingFactory(CompositeFactory factory) 310 { 311 if (threadNestingMap==null) 312 threadNestingMap= new Hashtable (); 313 314 if (factory == null) 315 threadNestingMap.remove(Thread.currentThread()); 316 else 317 threadNestingMap.put(Thread.currentThread(),factory); 318 } 319 320 321 328 public void setNestingFactory(CompositeFactory factory) 329 { 330 cellNestFactory = factory; 331 } 332 333 334 335 341 public Block defaultRow() 342 { 343 if (_defaultRow==null) 344 _defaultRow=new Block("tr"); 345 return _defaultRow; 346 } 347 348 349 355 public Block defaultHead() 356 { 357 if (_defaultHead==null) 358 _defaultHead=new Block("th"); 359 return _defaultHead; 360 } 361 362 363 369 public Block defaultCell() 370 { 371 if (_defaultCell==null) 372 _defaultCell=new Block("td"); 373 return _defaultCell; 374 } 375 } 376 377 378 379 380 381 382 383 384 | Popular Tags |