001: // ========================================================================
002: // $Id: TableForm.java,v 1.6 2004/05/09 20:31:28 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.io.IOException;
019: import java.io.Writer;
020: import java.util.Enumeration;
021:
022: // =======================================================================
023: /** A form laid out in a Table.
024: * <p> This class creates a form and lays out all the elements within a
025: * table. Each element added has a label part and a element part. The label
026: * is displayed in the form beside the element. All buttons are shown at the
027: * bottom.
028: */
029: public class TableForm extends Form {
030:
031: /* ----------------------------------------------------------- */
032: private Table table = null;
033: private Table column = null;
034: private int columns = 1;
035: private Composite hidden = new Composite();
036: private Composite buttons = null;
037: private Composite bottomButtons = null;
038: private String fieldAttributes = null;
039: private boolean extendRow = false;
040:
041: /* ----------------------------------------------------------- */
042: /** Create a new TableForm.
043: * @param target The target url to send the form contents to
044: */
045: public TableForm(String target) {
046: super (target);
047: newTable();
048: super .add(hidden);
049: }
050:
051: /* ----------------------------------------------------------- */
052: /** Add an informational section.
053: */
054: public void addText(String label, String value) {
055: Composite c = new Composite();
056: c.add(value);
057: addField(label, c);
058: }
059:
060: /* ----------------------------------------------------------- */
061: /** Add a Text Entry Field.
062: * @param tag The form name of the element
063: * @param label The label for the element in the table.
064: */
065: public Input addTextField(String tag, String label, int length,
066: String value) {
067: Input i = new Input(Input.Text, tag, value);
068: i.setSize(length);
069: addField(label, i);
070: return i;
071: }
072:
073: /* ----------------------------------------------------------- */
074: /** Add a Text Area.
075: * @param tag The form name of the element
076: * @param label The label for the element in the table.
077: */
078: public TextArea addTextArea(String tag, String label, int width,
079: int height, String value) {
080: TextArea ta = new TextArea(tag, value);
081: ta.setSize(width, height);
082: addField(label, ta);
083: return ta;
084: }
085:
086: /* ----------------------------------------------------------- */
087: /** Add a File Entry Field.
088: * @param tag The form name of the element
089: * @param label The label for the element in the table.
090: */
091: public Input addFileField(String tag, String label) {
092: Input i = new Input(Input.File, tag);
093: addField(label, i);
094: return i;
095: }
096:
097: /* ----------------------------------------------------------- */
098: /** Add an informational field which also passes the data as hidden.
099: * @param tag The form name of the element
100: * @param label The label for the element in the table.
101: */
102: public void addInfoField(String tag, String label, String value) {
103: addText(label, value);
104: addHiddenField(tag, value);
105: }
106:
107: /* ----------------------------------------------------------- */
108: /** Add a hidden field.
109: * @param tag The form name of the element
110: */
111: public void addHiddenField(String tag, String value) {
112: Element e = new Input(Input.Hidden, tag, value);
113: hidden.add(e);
114: }
115:
116: /* ----------------------------------------------------------- */
117: /** Add a password field.
118: * @param tag The form name of the element
119: * @param label The label for the element in the table.
120: */
121: public void addPassword(String tag, String label, int length) {
122: Input i = new Input(Input.Password, tag);
123: i.setSize(length);
124: addField(label, i);
125: }
126:
127: /* ----------------------------------------------------------- */
128: /**
129: * @param tag The form name of the element
130: * @param label The label for the element in the table.
131: */
132: public void addCheckbox(String tag, String label, boolean checked) {
133: Input cb = new Input(Input.Checkbox, tag);
134: addField(label, cb);
135: if (checked)
136: cb.check();
137: }
138:
139: /* ----------------------------------------------------------- */
140: /** Add a Select field.
141: * @param tag The form name of the element
142: * @param label The label for the element in the table.
143: */
144: public Select addSelect(String tag, String label, boolean multiple,
145: int size) {
146: Select s = new Select(tag, multiple);
147: s.setSize(size);
148: addField(label, s);
149: return s;
150: }
151:
152: /* ----------------------------------------------------------- */
153: /** Add a Select field initialised with fields.
154: * @param tag The form name of the element
155: * @param label The label for the element in the table.
156: */
157: public Select addSelect(String tag, String label, boolean multiple,
158: int size, Enumeration values) {
159: Select s = addSelect(tag, label, multiple, size);
160: s.setSize(size);
161: while (values.hasMoreElements())
162: s.add(values.nextElement().toString());
163: return s;
164: }
165:
166: /* ----------------------------------------------------------- */
167: /* add a new button area.
168: * A button area is a line of a column in a table form where multiple
169: * buttons can be placed. Subsequent calls to addButton will
170: * add buttons to this area.
171: */
172: public void addButtonArea(String label) {
173: buttons = new Composite();
174: addField(label, buttons);
175: }
176:
177: /* ----------------------------------------------------------- */
178: /* add a new button area.
179: * A button area is a line of a column in a table form where multiple
180: * buttons can be placed. Subsequent calls to addButton will
181: * add buttons to this area.
182: */
183: public void addButtonArea() {
184: buttons = new Composite();
185: addField(null, buttons);
186: }
187:
188: /* ----------------------------------------------------------- */
189: /* add a new button row.
190: * A button row is a line of a column in a table form where multiple
191: * buttons can be placed, that is aligned with the left hand side of the
192: * TableForm Subsequent calls to addButton will
193: * add buttons to this area.
194: */
195: public void addButtonRow() {
196: buttons = new Composite();
197:
198: if (!extendRow) {
199: column.newRow();
200: column.addCell(buttons).left().middle();
201: column.cell().attribute("colspan", "2");
202: }
203: extendRow = false;
204: }
205:
206: /* ----------------------------------------------------------- */
207: /* add a new button area to bottom of multicolumn form.
208: * A button area is a line of a table form where multiple
209: * buttons can be placed. Subsequent calls to addButton will
210: * add buttons to this area.
211: * This is the default if no call is made to newButtonArea.
212: */
213: public void buttonsAtBottom() {
214: if (bottomButtons != null)
215: buttons = bottomButtons;
216: else {
217: buttons = new Composite();
218: bottomButtons = buttons;
219: }
220: }
221:
222: /* ----------------------------------------------------------- */
223: /** Add a Submit Button.
224: * @param tag The form name of the element
225: * @param label The label for the Button
226: */
227: public Input addButton(String tag, String label) {
228: if (buttons == null)
229: buttonsAtBottom();
230: Input e = new Input(Input.Submit, tag, label);
231:
232: if (extendRow)
233: addField(null, e);
234: else
235: buttons.add(e);
236: return e;
237: }
238:
239: /* ----------------------------------------------------------- */
240: /** Add a reset button.
241: * @param label The label for the element in the table.
242: */
243: public void addReset(String label) {
244: if (buttons == null)
245: buttonsAtBottom();
246: Element e = new Input(Input.Reset, "Reset", label);
247: if (extendRow)
248: addField(null, e);
249: else
250: buttons.add(e);
251: }
252:
253: // ------------------------------------------------------------
254: /** Use the given attributes on the next addXXX */
255: public void useAttributes(String attr) {
256: fieldAttributes = attr;
257: }
258:
259: // ------------------------------------------------------------
260: /** Get the internal table */
261: public Table table() {
262: return column;
263: }
264:
265: // ------------------------------------------------------------
266: /** Get the internal table */
267: public Table outerTable() {
268: return table;
269: }
270:
271: /* ----------------------------------------------------------- */
272: /** Extend the usage of the current row in the form. The next
273: * element added will be added to the same row as the form and
274: * not have a label of it's own.
275: * @return TableForm, the this pointer so that users can write:<pre>
276: * tableForm.extendRow().addField(...)</pre>
277: */
278: public TableForm extendRow() {
279: extendRow = true;
280: return this ;
281: }
282:
283: /* ----------------------------------------------------------- */
284: /** Add an arbitrary element to the table.
285: * @param label The label for the element in the table.
286: */
287: public void addField(String label, Element field) {
288: if (label == null)
289: label = " ";
290: else
291: label = "<b>" + label + ":</b>";
292:
293: if (extendRow) {
294: column.add(field);
295: extendRow = false;
296: } else {
297: column.newRow();
298: column.addCell(label);
299: column.cell().right();
300:
301: if (fieldAttributes != null) {
302: column.addCell(field, fieldAttributes);
303: fieldAttributes = null;
304: } else
305: column.addCell(field);
306: }
307: }
308:
309: /* ----------------------------------------------------------- */
310: /** Create a new column in the form.
311: */
312: public void addColumn() {
313: column = new Table(0);
314: table.addCell(column).top();
315: columns++;
316: }
317:
318: /* ----------------------------------------------------------- */
319: /** Create a new column in the form.
320: */
321: public void addColumn(int spacing) {
322: table.addCell(" ", "width=" + spacing);
323: column = new Table(0);
324: table.addCell(column);
325: table.cell().top();
326: columns++;
327: }
328:
329: /* ------------------------------------------------------------ */
330: /** Add a new sections of columns.
331: */
332: public void newColumns() {
333: column = new Table(0);
334: columns = 1;
335: table.newRow();
336: table.addCell(column);
337: table.cell().top();
338: }
339:
340: /* ------------------------------------------------------------ */
341: /** Set the column span of the current column.
342: * This call is needed for forms that have varying numbers
343: * of columns in different sections. NB. and column spacing
344: * counts as a column.
345: * @param span
346: */
347: public void setColumnSpan(int span) {
348: table.cell().attribute("colspan", "" + span);
349: }
350:
351: /* ----------------------------------------------------------- */
352: /** Start using a new Table.
353: * Anything added to the Composite parent of
354: * this object before this is called will be added between the two
355: * tables. */
356: public void newTable() {
357: table = new Table(0);
358: column = new Table(0);
359: columns = 1;
360: super .add(table);
361: table.newRow();
362: table.addCell(column).top();
363: }
364:
365: /* ----------------------------------------------------------- */
366: public void write(Writer out) throws IOException {
367: if (bottomButtons != null) {
368: table.newRow();
369: table.addCell(bottomButtons).attribute("colspan", columns);
370: }
371: super.write(out);
372: }
373: }
|