001: // StringChoiceEditor.java
002: // $Id: StringChoiceEditor.java,v 1.6 2000/08/16 21:37:28 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.jigadm.editors;
007:
008: import java.awt.Component;
009: import java.awt.Dimension;
010:
011: import java.awt.event.TextEvent;
012: import java.awt.event.TextListener;
013:
014: import java.util.Hashtable;
015: import java.util.Properties;
016:
017: import org.w3c.tools.resources.Attribute;
018:
019: import org.w3c.jigadm.RemoteResourceWrapper;
020:
021: import org.w3c.jigsaw.admin.RemoteAccessException;
022:
023: import org.w3c.tools.widgets.StringChoice;
024:
025: /**
026: * An editor for StringChoice attributes.
027: * @author Benoit Mahe <bmahe@sophia.inria.fr>
028: */
029:
030: public class StringChoiceEditor extends AttributeEditor {
031:
032: class StringChoiceComponent extends StringChoice implements
033: TextListener {
034:
035: EditorFeeder feeder = null;
036: StringChoiceEditor editor = null;
037:
038: //TextListener
039: public void textValueChanged(TextEvent e) {
040: editor.setModified();
041: }
042:
043: StringChoiceComponent(StringChoiceEditor editor,
044: String selected, EditorFeeder feeder) {
045: super ();
046: addTextListener(this );
047: this .editor = editor;
048: this .feeder = feeder;
049: setText(selected);
050: removeAll();
051: String items[] = feeder.getDefaultItems();
052: addItems(items);
053: }
054: }
055:
056: /**
057: * Properties - The feeder's class name.
058: */
059: public static final String FEEDER_CLASS_P = "feeder.class";
060:
061: protected boolean hasChanged = false;
062: protected String oldvalue = null;
063: protected StringChoiceComponent comp = null;
064:
065: protected Dimension getPopupSize() {
066: return new Dimension(400, 160);
067: }
068:
069: protected void createComponent(EditorFeeder feeder, String selected) {
070: if (comp == null)
071: comp = new StringChoiceComponent(this , selected, feeder);
072: }
073:
074: protected void setModified() {
075: hasChanged = true;
076: }
077:
078: /**
079: * Tells if the edited value has changed
080: * @return true if the value changed.
081: */
082: public boolean hasChanged() {
083: return hasChanged;
084: }
085:
086: /**
087: * set the current value to be the original value, ie: changed
088: * must return <strong>false</strong> after a reset.
089: */
090: public void clearChanged() {
091: hasChanged = false;
092: }
093:
094: /**
095: * reset the changes (if any)
096: */
097: public void resetChanges() {
098: hasChanged = false;
099: comp.setText(oldvalue);
100: }
101:
102: /**
103: * Get the current value of the edited value
104: * @return an object or <strong>null</strong> if the object was not
105: * initialized
106: */
107: public Object getValue() {
108: return comp.getText();
109: }
110:
111: /**
112: * Set the value of the edited value
113: * @param o the new value.
114: */
115: public void setValue(Object o) {
116: this .oldvalue = (String) o;
117: comp.setText(oldvalue);
118: }
119:
120: /**
121: * get the Component created by the editor.
122: * @return a Component
123: */
124: public Component getComponent() {
125: return comp;
126: }
127:
128: /**
129: * Initialize the editor
130: * @param w the ResourceWrapper father of the attribute
131: * @param a the Attribute we are editing
132: * @param o the value of the above attribute
133: * @param p some Properties, used to fine-tune the editor
134: * @exception RemoteAccessException if a remote access error occurs.
135: */
136: public void initialize(RemoteResourceWrapper w, Attribute a,
137: Object o, Properties p) throws RemoteAccessException {
138: // Get the feeder class fromproperties:
139: EditorFeeder feeder = null;
140: String feederClass = null;
141:
142: feederClass = (String) p.get(FEEDER_CLASS_P);
143: if (feederClass == null)
144: throw new RuntimeException(
145: "StringChoiceEditor mis-configuration:"
146: + FEEDER_CLASS_P + " property undefined.");
147: try {
148: Class c = Class.forName(feederClass);
149: feeder = (EditorFeeder) c.newInstance();
150: feeder.initialize(w, p);
151: } catch (Exception ex) {
152: ex.printStackTrace();
153: throw new RuntimeException(
154: "StringChoiceEditor mis-configured: "
155: + " unable to instantiate " + feederClass
156: + ".");
157: }
158: createComponent(feeder, (String) o);
159: oldvalue = (String) o;
160: }
161:
162: public StringChoiceEditor() {
163: super();
164: }
165: }
|