001: // FormManager.java
002: // $Id: FormManager.java,v 1.4 2000/08/16 21:37:49 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.forms;
007:
008: import java.awt.Component;
009: import java.awt.Container;
010: import java.awt.Dimension;
011: import java.awt.Frame;
012: import java.awt.Panel;
013: import java.awt.Window;
014:
015: import java.util.Vector;
016:
017: public class FormManager {
018: /**
019: * Our list of field, at description time.
020: */
021: protected Vector vfields = null;
022: /**
023: * Our list of fields, at runtime.
024: */
025: protected FormField fields[] = null;
026: /**
027: * The current field being edited, as an index in our fields.
028: */
029: protected int cursor = 0;
030: /**
031: * Is this form description completed ?
032: */
033: protected boolean finished = false;
034: /**
035: * The form's title.
036: */
037: protected String title = null;
038: /**
039: * The form grphical UI.
040: */
041: protected FormPanel panel = null;
042:
043: /**
044: * Callback for field value's change.
045: * @param field The field that changed.
046: */
047:
048: public void notifyChange(FormField field) {
049: return;
050: }
051:
052: /**
053: * Construct the Panel to edit the form.
054: * @return A Panel instance, layed out for this form edition.
055: */
056:
057: protected FormPanel createPanel() {
058: panel = new FormPanel(this );
059: for (int i = 0; i < fields.length; i++)
060: panel.addField(fields[i].getTitle(), fields[i].getEditor());
061: return panel;
062: }
063:
064: /**
065: * Move to the field whose index is given.
066: * @param n The field to move to.
067: */
068:
069: public void gotoField(int idx) {
070: if ((idx < 0) || (idx >= fields.length))
071: throw new RuntimeException("invalid form cursor:" + cursor);
072: cursor = idx;
073: fields[cursor].getEditor().requestFocus();
074: }
075:
076: /**
077: * Move the focus to the next editable field.
078: */
079:
080: public void nextField() {
081: gotoField((cursor + 1) % fields.length);
082: }
083:
084: /**
085: * Some of our field got the focus, update our cursor.
086: * @param field The field that now has the focus.
087: */
088:
089: protected void gotFocus(FormField field) {
090: for (int i = 0; i < fields.length; i++) {
091: if (fields[i] == field) {
092: cursor = i;
093: break;
094: }
095: }
096: }
097:
098: /**
099: * Add a field to the form.
100: * @param name The field name (the key by wich this field will be
101: * accessible.)
102: * @param field The field to be created.
103: */
104:
105: public void addField(FormField field) {
106: if (finished)
107: throw new RuntimeException("This form has been finished.");
108: vfields.addElement(field);
109: }
110:
111: /**
112: * Mark the description of the form as completed.
113: * Once this method is called, no more fields can be added to the form.
114: * This method will perform any required compilation of the form.
115: */
116:
117: public void finish() {
118: if (finished)
119: return;
120: finished = true;
121: // Compile the vfields into fields, for fast access and no casts:
122: fields = new FormField[vfields.size()];
123: vfields.copyInto(fields);
124: vfields = null;
125: }
126:
127: /**
128: * Get the graphical object for editing the form.
129: */
130:
131: public Panel getPanel() {
132: if (!finished)
133: finish();
134: if (panel == null)
135: panel = createPanel();
136: return panel;
137: }
138:
139: /**
140: * Create a new, empty form.
141: * @param title The form's title.
142: */
143:
144: public FormManager(String title) {
145: this .title = title;
146: this .vfields = new Vector();
147: }
148:
149: /**
150: * Test.
151: * @exception IllegalFieldValueException test
152: */
153:
154: public static void main(String args[])
155: throws IllegalFieldValueException {
156: FormManager manager = new FormManager("test");
157: FormField field = null;
158: // Create the first field:
159: field = new StringField(manager, "field-1", "title-1",
160: "value-1");
161: manager.addField(field);
162: // Create the second field:
163: field = new StringField(manager, "field-2", "title-2",
164: "value-2");
165: manager.addField(field);
166: // Create an Integer field;
167: field = new IntegerField(manager, "field-3", "title-3", 10);
168: manager.addField(field);
169: // Create an option field:
170: String opts[] = { "option-1", "option-2", "foo", "bar", "etc" };
171: field = new OptionField(manager, "field-4", "title-4", opts, 0);
172: manager.addField(field);
173: // Create a boolean field.
174: field = new BooleanField(manager, "field-5", "title-5", true);
175: manager.addField(field);
176: // Create a RangedInteger
177: field = new RangedIntegerField(manager, "field-6", "title-6",
178: 0, 10000, 5000);
179: manager.addField(field);
180: // Display the resulting GUI:
181: Panel p = manager.getPanel();
182: Frame toplevel = new Frame("form test");
183: toplevel.add("Center", p);
184: toplevel.pack();
185: toplevel.resize(toplevel.preferredSize());
186: toplevel.show();
187:
188: }
189:
190: }
|