001: // DoubleField.java
002: // $Id: DoubleField.java,v 1.4 2000/08/16 21:37:49 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
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 DoubleFieldEditor extends TextField {
014: DoubleField field = null;
015: boolean donext = false;
016:
017: public boolean action(Event evt, Object arg) {
018: try {
019: Double dval = Double.valueOf(getText());
020: if (field.acceptChange(dval)) {
021: donext = true;
022: return true;
023: }
024: } catch (NumberFormatException ex) {
025: }
026: // The new proposed value was rejected, retreiev the old value:
027: Double oldval = (Double) field.getValue();
028: if (oldval != null)
029: setText(oldval.toString());
030: else
031: setText("");
032: return true;
033: }
034:
035: /**
036: * Notify the form manager that we did get the focus.
037: */
038:
039: public boolean gotFocus(Event evt, Object what) {
040: field.gotFocus();
041: return false;
042: }
043:
044: public void setValue(Double dval) {
045: setText(dval.toString());
046: }
047:
048: /**
049: * Handle event: manage fields walking.
050: * @param evt The event to handle.
051: */
052:
053: public boolean keyDown(Event evt, int key) {
054: switch (key) {
055: case 9:
056: case 10:
057: action(evt, evt.arg);
058: if (donext) {
059: donext = false;
060: field.manager.nextField();
061: }
062: return true;
063: default:
064: return super .keyDown(evt, key);
065: }
066: }
067:
068: DoubleFieldEditor(DoubleField field, Double dval) {
069: super ((dval != null) ? dval.toString() : "");
070: this .field = field;
071: }
072:
073: }
074:
075: public class DoubleField extends FormField {
076: /**
077: * Our editor for the field value.
078: */
079: DoubleFieldEditor editor = null;
080: /**
081: * Our current value.
082: */
083: Double value = null;
084:
085: /**
086: * Do we want to accept this new value ?
087: * @return A boolean, <strong>true</strong> if we accept this new
088: * value.
089: */
090:
091: public boolean acceptChange(Double dval) {
092: try {
093: setValue(dval, true, false);
094: } catch (IllegalFieldValueException ex) {
095: throw new RuntimeException("implementation bug.");
096: }
097: return true;
098: }
099:
100: /**
101: * Get this field's value in its native format (ie Double).
102: * @return An instance of Double.
103: */
104:
105: public Object getValue() {
106: return value;
107: }
108:
109: /**
110: * Get this field's value as a Double.
111: * @return An instance of Double.
112: */
113:
114: public Double getDoubleValue() {
115: return value;
116: }
117:
118: /**
119: * Set this field's value to thegiven object.
120: * @param value The new value for the field.
121: * @param update Should the editor updates its view ?
122: * @exception IllegalFieldValueException If the provided value is not
123: * a Double object.
124: */
125:
126: public void setValue(Object value, boolean notify, boolean update)
127: throws IllegalFieldValueException {
128: if (!(value instanceof Double))
129: throw new IllegalFieldValueException(value);
130: setValue((Double) value, notify, update);
131: }
132:
133: /**
134: * Set this field's value to the given Double value.
135: * @param value The double value to set the field to.
136: * @param update Should the editor updates its view ?
137: * @exception IllegalFieldValueException If the value couldn't be set.
138: */
139:
140: public void setValue(Double value, boolean notify, boolean update)
141: throws IllegalFieldValueException {
142: this .value = value;
143: if (update && (editor != null))
144: editor.setValue(value);
145: if (notify)
146: manager.notifyChange(this );
147: }
148:
149: /**
150: * Get an editor for this field.
151: */
152:
153: public Component getEditor() {
154: if (editor == null)
155: editor = new DoubleFieldEditor(this , value);
156: return editor;
157: }
158:
159: /**
160: * Create a new double field.
161: * @param manager The associated form manager.
162: * @param name The name for this field.
163: * @param title This field's title.
164: * @param value The initial value for the field.
165: * @exception IllegalFieldValueException If the default value isn't
166: * accepted by the field.
167: */
168:
169: public DoubleField(FormManager manager, String name, String title,
170: Double value) throws IllegalFieldValueException {
171: super (manager, name, title);
172: setValue(value, false);
173: }
174:
175: /**
176: * Create a new uninitialized double field.
177: * @param manager The asociated form manager.
178: * @param name The field's name.
179: * @param title The field's title.
180: */
181:
182: public DoubleField(FormManager manager, String name, String title) {
183: super(manager, name, title);
184: this.value = null;
185: }
186:
187: }
|