001: // StringField.java
002: // $Id: StringField.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.Event;
010: import java.awt.TextComponent;
011: import java.awt.TextField;
012:
013: class StringFieldEditor extends TextField {
014: StringField field = null;
015:
016: /**
017: * Handle the action: the field edition is finished.
018: */
019:
020: public boolean action(Event evt, Object arg) {
021: if (!field.acceptChange(getText())) {
022: String oldtxt = field.getStringValue();
023: setText((oldtxt != null) ? oldtxt : "");
024: }
025: return true;
026: }
027:
028: /**
029: * Set the editor's value.
030: */
031:
032: public void setValue(String value) {
033: setText((value == null) ? "" : value);
034: }
035:
036: /**
037: * This editor got the focus, notify the form manager.
038: */
039:
040: public boolean gotFocus(Event evt, Object what) {
041: field.gotFocus();
042: return false;
043: }
044:
045: /**
046: * Handle event: manage fields walking.
047: * @param evt The event to handle.
048: */
049:
050: public boolean keyDown(Event evt, int key) {
051: switch (key) {
052: case 9:
053: case 10:
054: action(evt, evt.arg);
055: field.manager.nextField();
056: return true;
057: default:
058: return super .keyDown(evt, key);
059: }
060: }
061:
062: StringFieldEditor(StringField field, String defvalue) {
063: super ((defvalue == null) ? "" : defvalue, 32);
064: this .field = field;
065: }
066:
067: }
068:
069: /**
070: * An editor for string fields.
071: */
072:
073: public class StringField extends FormField {
074: /**
075: * Our current value.
076: */
077: String value = null;
078: /**
079: * Our GUI editor.
080: */
081: StringFieldEditor editor = null;
082:
083: /**
084: * Do we want to accept this value as our new value.
085: */
086:
087: public boolean acceptChange(String value) {
088: try {
089: setValue(value, true, false);
090: } catch (IllegalFieldValueException ex) {
091: throw new RuntimeException("implementation bug.");
092: }
093: return true;
094: }
095:
096: /**
097: * Get this field's value according to its native type.
098: */
099:
100: public Object getValue() {
101: return value;
102: }
103:
104: /**
105: * Get this field value as a String.
106: */
107:
108: public String getStringValue() {
109: return value;
110: }
111:
112: /**
113: * Set this field value.
114: * @exception IllegalFieldValueException if the value isn't accepted
115: */
116:
117: public void setValue(Object value, boolean notify, boolean update)
118: throws IllegalFieldValueException {
119: if (!(value instanceof String))
120: throw new IllegalFieldValueException(value);
121: setValue((String) value, notify, update);
122: }
123:
124: /**
125: * Set this field value.
126: * @exception IllegalFieldValueException if the value isn't accepted
127: */
128: public void setValue(String value, boolean notify, boolean update)
129: throws IllegalFieldValueException {
130: this .value = value;
131: if (update && (editor != null))
132: editor.setValue(value);
133: if (notify)
134: manager.notifyChange(this );
135: }
136:
137: /**
138: * FormField implementation - Get the editor for the field.
139: */
140:
141: public Component getEditor() {
142: if (editor == null)
143: editor = new StringFieldEditor(this , value);
144: return editor;
145: }
146:
147: /**
148: * Create a new field for string edition.
149: * @param manager The form manager.
150: * @param value The initial value for the field.
151: */
152:
153: public StringField(FormManager manager, String name, String title,
154: String value) {
155: super (manager, name, title);
156: this .value = value;
157: }
158:
159: /**
160: * Create a new field for string edition, with no initial value.
161: * @param manager The form manager.
162: * @param name The field's name.
163: * @param title The field's title.
164: */
165:
166: public StringField(FormManager manager, String name, String title) {
167: this(manager, name, title, null);
168: }
169:
170: }
|