001: // OptionField.java
002: // $Id: OptionField.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.Choice;
009: import java.awt.Component;
010: import java.awt.Event;
011:
012: class OptionFieldEditor extends Choice {
013: OptionField field = null;
014:
015: public boolean action(Event evt, Object arg) {
016: if (!field.acceptChange(getSelectedIndex()))
017: select(field.getIntValue());
018: return true;
019: }
020:
021: public boolean keyDown(Event evt, int key) {
022: switch (key) {
023: case 9:
024: case 10:
025: action(evt, evt.arg);
026: field.manager.nextField();
027: return true;
028: default:
029: return super .keyDown(evt, key);
030: }
031: }
032:
033: public void setValue(int idx) {
034: select(idx);
035: }
036:
037: OptionFieldEditor(OptionField field, String options[], int cursor) {
038: super ();
039: this .field = field;
040: for (int i = 0; i < options.length; i++)
041: addItem(options[i]);
042: select(cursor);
043: }
044: }
045:
046: public class OptionField extends FormField {
047: /**
048: * List of allwed options.
049: */
050: String options[] = null;
051: /**
052: * Our value.
053: */
054: int cursor = 0;
055: /**
056: * Our editor.
057: */
058: OptionFieldEditor editor = null;
059:
060: /**
061: * Do we accept to change to the given (valid) index ?
062: */
063:
064: public boolean acceptChange(int idx) {
065: try {
066: setValue(idx, true, false);
067: } catch (IllegalFieldValueException ex) {
068: throw new RuntimeException("implementation bug.");
069: }
070: return true;
071: }
072:
073: /**
074: * Get this field's value in its native type.
075: * @return The currently selected option as a String.
076: */
077:
078: public Object getValue() {
079: return options[cursor];
080: }
081:
082: /**
083: * Get the selected option as its index in our array of options.
084: */
085:
086: public int getIntValue() {
087: return cursor;
088: }
089:
090: /**
091: * Get the selected option as a String.
092: */
093:
094: public String getStringValue() {
095: return options[cursor];
096: }
097:
098: /**
099: * Set this option's value.
100: * @param value The new value.
101: * @param update Should we update the editor view.
102: * @exception IllegalFieldValueException if the value isn't accepted
103: */
104:
105: public void setValue(Object value, boolean notify, boolean update)
106: throws IllegalFieldValueException {
107: if (!(value instanceof String))
108: throw new IllegalFieldValueException(value);
109: setValue((String) value, notify, update);
110: }
111:
112: /**
113: * Set this option's value.
114: * @param idx The index of the option to set.
115: * @param update Should we update our editor's view.
116: * @exception IllegalFieldValueException if the value isn't accepted
117: */
118:
119: public void setValue(int idx, boolean notify, boolean update)
120: throws IllegalFieldValueException {
121: if ((idx < 0) || (idx >= options.length))
122: throw new IllegalFieldValueException(new Integer(idx));
123: this .cursor = idx;
124: if (update && (editor != null))
125: editor.setValue(cursor);
126: if (notify)
127: manager.notifyChange(this );
128: }
129:
130: /**
131: * Set this option's value.
132: * @exception IllegalFieldValueException if the value isn't accepted
133: */
134:
135: public void setValue(String value, boolean notify, boolean update)
136: throws IllegalFieldValueException {
137: for (int i = 0; i < options.length; i++) {
138: if (options[i].equals(value))
139: setValue(i, notify, update);
140: }
141: throw new IllegalFieldValueException(value);
142: }
143:
144: /**
145: * Get an editor to edit this option's value.
146: */
147:
148: public Component getEditor() {
149: if (editor == null)
150: editor = new OptionFieldEditor(this , options, cursor);
151: return editor;
152: }
153:
154: /**
155: * Create an option field.
156: * @exception IllegalFieldValueException if the value isn't accepted
157: */
158:
159: public OptionField(FormManager manager, String name, String title,
160: String options[], int value)
161: throws IllegalFieldValueException {
162: super (manager, name, title);
163: this .options = options;
164: setValue(value, false, false);
165: }
166:
167: }
|