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