1 // ======================================================================== 2 // $Id: TableForm.java,v 1.6 2004/05/09 20:31:28 gregwilkins Exp $ 3 // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // ======================================================================== 15 16 package org.mortbay.html; 17 import java.io.IOException; 18 import java.io.Writer; 19 import java.util.Enumeration; 20 21 // ======================================================================= 22 /** A form laid out in a Table. 23 * <p> This class creates a form and lays out all the elements within a 24 * table. Each element added has a label part and a element part. The label 25 * is displayed in the form beside the element. All buttons are shown at the 26 * bottom. 27 */ 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 /** Create a new TableForm. 43 * @param target The target url to send the form contents to 44 */ 45 public TableForm(String target) 46 { 47 super(target); 48 newTable(); 49 super.add(hidden); 50 } 51 52 /* ----------------------------------------------------------- */ 53 /** Add an informational section. 54 */ 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 /** Add a Text Entry Field. 65 * @param tag The form name of the element 66 * @param label The label for the element in the table. 67 */ 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 /** Add a Text Area. 81 * @param tag The form name of the element 82 * @param label The label for the element in the table. 83 */ 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 /** Add a File Entry Field. 98 * @param tag The form name of the element 99 * @param label The label for the element in the table. 100 */ 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 /** Add an informational field which also passes the data as hidden. 111 * @param tag The form name of the element 112 * @param label The label for the element in the table. 113 */ 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 /** Add a hidden field. 124 * @param tag The form name of the element 125 */ 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 /** Add a password field. 135 * @param tag The form name of the element 136 * @param label The label for the element in the table. 137 */ 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 /** 149 * @param tag The form name of the element 150 * @param label The label for the element in the table. 151 */ 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 /** Add a Select field. 164 * @param tag The form name of the element 165 * @param label The label for the element in the table. 166 */ 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 /** Add a Select field initialised with fields. 180 * @param tag The form name of the element 181 * @param label The label for the element in the table. 182 */ 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 /* add a new button area. 198 * A button area is a line of a column in a table form where multiple 199 * buttons can be placed. Subsequent calls to addButton will 200 * add buttons to this area. 201 */ 202 public void addButtonArea(String label) 203 { 204 buttons=new Composite(); 205 addField(label,buttons); 206 } 207 208 /* ----------------------------------------------------------- */ 209 /* add a new button area. 210 * A button area is a line of a column in a table form where multiple 211 * buttons can be placed. Subsequent calls to addButton will 212 * add buttons to this area. 213 */ 214 public void addButtonArea() 215 { 216 buttons=new Composite(); 217 addField(null,buttons); 218 } 219 220 /* ----------------------------------------------------------- */ 221 /* add a new button row. 222 * A button row is a line of a column in a table form where multiple 223 * buttons can be placed, that is aligned with the left hand side of the 224 * TableForm Subsequent calls to addButton will 225 * add buttons to this area. 226 */ 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 /* add a new button area to bottom of multicolumn form. 242 * A button area is a line of a table form where multiple 243 * buttons can be placed. Subsequent calls to addButton will 244 * add buttons to this area. 245 * This is the default if no call is made to newButtonArea. 246 */ 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 /** Add a Submit Button. 260 * @param tag The form name of the element 261 * @param label The label for the Button 262 */ 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 /** Add a reset button. 279 * @param label The label for the element in the table. 280 */ 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 // ------------------------------------------------------------ 293 /** Use the given attributes on the next addXXX */ 294 public void useAttributes(String attr){ 295 fieldAttributes = attr; 296 } 297 298 // ------------------------------------------------------------ 299 /** Get the internal table */ 300 public Table table(){ 301 return column; 302 } 303 304 // ------------------------------------------------------------ 305 /** Get the internal table */ 306 public Table outerTable(){ 307 return table; 308 } 309 310 /* ----------------------------------------------------------- */ 311 /** Extend the usage of the current row in the form. The next 312 * element added will be added to the same row as the form and 313 * not have a label of it's own. 314 * @return TableForm, the this pointer so that users can write:<pre> 315 * tableForm.extendRow().addField(...)</pre> 316 */ 317 public TableForm extendRow() 318 { 319 extendRow=true; 320 return this; 321 } 322 323 /* ----------------------------------------------------------- */ 324 /** Add an arbitrary element to the table. 325 * @param label The label for the element in the table. 326 */ 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 /** Create a new column in the form. 357 */ 358 public void addColumn() 359 { 360 column = new Table(0); 361 table.addCell(column).top(); 362 columns++; 363 } 364 365 /* ----------------------------------------------------------- */ 366 /** Create a new column in the form. 367 */ 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 /** Add a new sections of columns. 379 */ 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 /** Set the column span of the current column. 391 * This call is needed for forms that have varying numbers 392 * of columns in different sections. NB. and column spacing 393 * counts as a column. 394 * @param span 395 */ 396 public void setColumnSpan(int span) 397 { 398 table.cell().attribute("colspan",""+span); 399 } 400 401 /* ----------------------------------------------------------- */ 402 /** Start using a new Table. 403 * Anything added to the Composite parent of 404 * this object before this is called will be added between the two 405 * tables. */ 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 }