--- /dev/null
+// StringField.java\r
+// $Id: StringField.java,v 1.1 2010/06/15 12:27:21 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.tools.forms ;\r
+\r
+import java.awt.Component;\r
+import java.awt.Event;\r
+import java.awt.TextComponent;\r
+import java.awt.TextField;\r
+\r
+class StringFieldEditor extends TextField {\r
+ StringField field = null ;\r
+\r
+ /**\r
+ * Handle the action: the field edition is finished.\r
+ */\r
+\r
+ public boolean action (Event evt, Object arg) {\r
+ if ( ! field.acceptChange(getText()) ) {\r
+ String oldtxt = field.getStringValue() ;\r
+ setText((oldtxt != null) ? oldtxt : "") ;\r
+ }\r
+ return true ;\r
+ }\r
+\r
+ /**\r
+ * Set the editor's value.\r
+ */\r
+\r
+ public void setValue(String value) {\r
+ setText((value == null) ? "" : value) ;\r
+ }\r
+\r
+ /**\r
+ * This editor got the focus, notify the form manager.\r
+ */\r
+\r
+ public boolean gotFocus(Event evt, Object what) {\r
+ field.gotFocus() ;\r
+ return false ;\r
+ }\r
+\r
+ /**\r
+ * Handle event: manage fields walking.\r
+ * @param evt The event to handle.\r
+ */\r
+\r
+ public boolean keyDown(Event evt, int key) {\r
+ switch (key) {\r
+ case 9:\r
+ case 10:\r
+ action(evt, evt.arg) ;\r
+ field.manager.nextField() ;\r
+ return true ;\r
+ default:\r
+ return super.keyDown(evt, key) ;\r
+ }\r
+ }\r
+\r
+ StringFieldEditor(StringField field, String defvalue) {\r
+ super((defvalue == null) ? "" : defvalue, 32) ;\r
+ this.field = field ;\r
+ }\r
+\r
+}\r
+\r
+/**\r
+ * An editor for string fields.\r
+ */\r
+\r
+public class StringField extends FormField {\r
+ /**\r
+ * Our current value.\r
+ */\r
+ String value = null ;\r
+ /**\r
+ * Our GUI editor.\r
+ */\r
+ StringFieldEditor editor = null ;\r
+\r
+ /**\r
+ * Do we want to accept this value as our new value.\r
+ */\r
+\r
+ public boolean acceptChange(String value) {\r
+ try {\r
+ setValue(value, true, false) ;\r
+ } catch (IllegalFieldValueException ex) {\r
+ throw new RuntimeException ("implementation bug.") ;\r
+ }\r
+ return true ;\r
+ }\r
+\r
+ /**\r
+ * Get this field's value according to its native type.\r
+ */\r
+\r
+ public Object getValue() {\r
+ return value ;\r
+ }\r
+\r
+ /**\r
+ * Get this field value as a String.\r
+ */\r
+\r
+ public String getStringValue() {\r
+ return value ;\r
+ }\r
+\r
+ /**\r
+ * Set this field value.\r
+ * @exception IllegalFieldValueException if the value isn't accepted\r
+ */\r
+\r
+ public void setValue(Object value, boolean notify, boolean update)\r
+ throws IllegalFieldValueException\r
+ {\r
+ if ( ! (value instanceof String) )\r
+ throw new IllegalFieldValueException (value) ;\r
+ setValue((String) value, notify, update) ;\r
+ }\r
+\r
+ /**\r
+ * Set this field value.\r
+ * @exception IllegalFieldValueException if the value isn't accepted\r
+ */\r
+ public void setValue(String value, boolean notify, boolean update) \r
+ throws IllegalFieldValueException\r
+ {\r
+ this.value = value;\r
+ if ( update && (editor != null) )\r
+ editor.setValue(value) ;\r
+ if ( notify )\r
+ manager.notifyChange(this) ;\r
+ }\r
+\r
+ /**\r
+ * FormField implementation - Get the editor for the field.\r
+ */\r
+\r
+ public Component getEditor() {\r
+ if ( editor == null ) \r
+ editor = new StringFieldEditor(this, value) ;\r
+ return editor ;\r
+ }\r
+\r
+ /**\r
+ * Create a new field for string edition.\r
+ * @param manager The form manager.\r
+ * @param value The initial value for the field.\r
+ */\r
+\r
+ public StringField (FormManager manager\r
+ , String name, String title\r
+ , String value) {\r
+ super(manager, name, title) ;\r
+ this.value = value ;\r
+ }\r
+\r
+ /**\r
+ * Create a new field for string edition, with no initial value.\r
+ * @param manager The form manager.\r
+ * @param name The field's name.\r
+ * @param title The field's title.\r
+ */\r
+\r
+ public StringField (FormManager manager, String name, String title) {\r
+ this(manager, name, title, null) ;\r
+ }\r
+\r
+}\r