1 16 package org.mortbay.html; 17 import java.io.IOException ; 18 import java.io.Writer ; 19 import java.util.Enumeration ; 20 21 28 public class TableForm extends Form 29 { 30 31 32 private Table table = null; 33 private Table column = null; 34 private int columns = 1; 35 private Composite hidden = new Composite(); 36 private Composite buttons = null; 37 private Composite bottomButtons = null; 38 private String fieldAttributes = null; 39 private boolean extendRow = false; 40 41 42 45 public TableForm(String target) 46 { 47 super(target); 48 newTable(); 49 super.add(hidden); 50 } 51 52 53 55 public void addText(String label, 56 String value) 57 { 58 Composite c = new Composite(); 59 c.add(value); 60 addField(label,c); 61 } 62 63 64 68 public Input addTextField(String tag, 69 String label, 70 int length, 71 String value) 72 { 73 Input i = new Input(Input.Text,tag,value); 74 i.setSize(length); 75 addField(label,i); 76 return i; 77 } 78 79 80 84 public TextArea addTextArea(String tag, 85 String label, 86 int width, 87 int height, 88 String value) 89 { 90 TextArea ta = new TextArea(tag,value); 91 ta.setSize(width,height); 92 addField(label,ta); 93 return ta; 94 } 95 96 97 101 public Input addFileField(String tag, 102 String label) 103 { 104 Input i = new Input(Input.File,tag); 105 addField(label,i); 106 return i; 107 } 108 109 110 114 public void addInfoField(String tag, 115 String label, 116 String value) 117 { 118 addText(label,value); 119 addHiddenField(tag,value); 120 } 121 122 123 126 public void addHiddenField(String tag, 127 String value) 128 { 129 Element e = new Input(Input.Hidden,tag,value); 130 hidden.add(e); 131 } 132 133 134 138 public void addPassword(String tag, 139 String label, 140 int length) 141 { 142 Input i = new Input(Input.Password,tag); 143 i.setSize(length); 144 addField(label,i); 145 } 146 147 148 152 public void addCheckbox(String tag, 153 String label, 154 boolean checked) 155 { 156 Input cb = new Input(Input.Checkbox,tag); 157 addField(label,cb); 158 if (checked) 159 cb.check(); 160 } 161 162 163 167 public Select addSelect(String tag, 168 String label, 169 boolean multiple, 170 int size) 171 { 172 Select s = new Select(tag,multiple); 173 s.setSize(size); 174 addField(label,s); 175 return s; 176 } 177 178 179 183 public Select addSelect(String tag, 184 String label, 185 boolean multiple, 186 int size, 187 Enumeration values) 188 { 189 Select s = addSelect(tag,label,multiple,size); 190 s.setSize(size); 191 while (values.hasMoreElements()) 192 s.add(values.nextElement().toString()); 193 return s; 194 } 195 196 197 202 public void addButtonArea(String label) 203 { 204 buttons=new Composite(); 205 addField(label,buttons); 206 } 207 208 209 214 public void addButtonArea() 215 { 216 buttons=new Composite(); 217 addField(null,buttons); 218 } 219 220 221 227 public void addButtonRow() 228 { 229 buttons=new Composite(); 230 231 if (!extendRow) 232 { 233 column.newRow(); 234 column.addCell(buttons).left().middle(); 235 column.cell().attribute("colspan","2"); 236 } 237 extendRow=false; 238 } 239 240 241 247 public void buttonsAtBottom() 248 { 249 if (bottomButtons!=null) 250 buttons=bottomButtons; 251 else 252 { 253 buttons=new Composite(); 254 bottomButtons=buttons; 255 } 256 } 257 258 259 263 public Input addButton(String tag, 264 String label) 265 { 266 if (buttons==null) 267 buttonsAtBottom(); 268 Input e = new Input(Input.Submit,tag,label); 269 270 if (extendRow) 271 addField(null,e); 272 else 273 buttons.add(e); 274 return e; 275 } 276 277 278 281 public void addReset(String label) 282 { 283 if (buttons==null) 284 buttonsAtBottom(); 285 Element e = new Input(Input.Reset,"Reset",label); 286 if (extendRow) 287 addField(null,e); 288 else 289 buttons.add(e); 290 } 291 292 294 public void useAttributes(String attr){ 295 fieldAttributes = attr; 296 } 297 298 300 public Table table(){ 301 return column; 302 } 303 304 306 public Table outerTable(){ 307 return table; 308 } 309 310 311 317 public TableForm extendRow() 318 { 319 extendRow=true; 320 return this; 321 } 322 323 324 327 public void addField(String label,Element field) 328 { 329 if (label==null) 330 label=" "; 331 else 332 label="<b>"+label+":</b>"; 333 334 if (extendRow) 335 { 336 column.add(field); 337 extendRow=false; 338 } 339 else 340 { 341 column.newRow(); 342 column.addCell(label); 343 column.cell().right(); 344 345 if (fieldAttributes != null) 346 { 347 column.addCell(field,fieldAttributes); 348 fieldAttributes = null; 349 } 350 else 351 column.addCell(field); 352 } 353 } 354 355 356 358 public void addColumn() 359 { 360 column = new Table(0); 361 table.addCell(column).top(); 362 columns++; 363 } 364 365 366 368 public void addColumn(int spacing) 369 { 370 table.addCell(" ","width="+spacing); 371 column = new Table(0); 372 table.addCell(column); 373 table.cell().top(); 374 columns++; 375 } 376 377 378 380 public void newColumns() 381 { 382 column = new Table(0); 383 columns = 1; 384 table.newRow(); 385 table.addCell(column); 386 table.cell().top(); 387 } 388 389 390 396 public void setColumnSpan(int span) 397 { 398 table.cell().attribute("colspan",""+span); 399 } 400 401 402 406 public void newTable() 407 { 408 table = new Table(0); 409 column = new Table(0); 410 columns = 1; 411 super.add(table); 412 table.newRow(); 413 table.addCell(column).top(); 414 } 415 416 417 public void write(Writer out) 418 throws IOException 419 { 420 if (bottomButtons!=null) 421 { 422 table.newRow(); 423 table.addCell(bottomButtons).attribute("colspan",columns); 424 } 425 super.write(out); 426 } 427 } 428 | Popular Tags |