001: // SelectEditor.java
002: // $Id: SelectEditor.java,v 1.7 2000/08/16 21:37:27 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.BorderLayout;
009: import java.awt.Button;
010: import java.awt.Component;
011: import java.awt.Container;
012: import java.awt.Panel;
013: import java.awt.TextComponent;
014: import java.awt.TextField;
015: import java.awt.Window;
016:
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.awt.event.ItemEvent;
020: import java.awt.event.ItemListener;
021: import java.awt.event.TextEvent;
022: import java.awt.event.TextListener;
023:
024: import java.util.Hashtable;
025: import java.util.Properties;
026:
027: import org.w3c.tools.resources.Attribute;
028:
029: import org.w3c.tools.widgets.ClosableFrame;
030:
031: import org.w3c.jigadm.RemoteResourceWrapper;
032:
033: import org.w3c.jigsaw.admin.RemoteAccessException;
034:
035: /**
036: * SelectEditor :
037: * @author Benoit Mahe <bmahe@sophia.inria.fr>
038: */
039:
040: public class SelectEditor extends AttributeEditor {
041:
042: class SelectPopup extends ClosableFrame implements ItemListener {
043:
044: SelectComponent parent = null;
045: java.awt.List list = null;
046: EditorFeeder feeder = null;
047: EditorModifier modifier = null;
048:
049: // ItemListener
050: public void itemStateChanged(ItemEvent e) {
051: if (e.getStateChange() == ItemEvent.SELECTED) {
052: String selected = (String) list.getItem(((Integer) e
053: .getItem()).intValue());
054: if (modifier != null)
055: selected = modifier.modify(selected);
056: parent.setText(selected);
057: setVisible(false);
058: }
059: }
060:
061: protected void setDefaultItems() {
062: list.removeAll();
063: String items[] = feeder.getDefaultItems();
064: if (items != null) {
065: for (int i = 0; i < items.length; i++)
066: if (items[i] != null)
067: list.addItem(items[i]);
068: }
069: }
070:
071: protected void close() {
072: setVisible(false);
073: }
074:
075: SelectPopup(SelectComponent parent, EditorFeeder feeder,
076: EditorModifier modifier) {
077: super ("Select");
078: this .parent = parent;
079: this .feeder = feeder;
080: this .modifier = modifier;
081: setLayout(new BorderLayout());
082: list = new java.awt.List(20);
083: list.addItemListener(this );
084: setDefaultItems();
085: add(list);
086: setSize(170, 300);
087: }
088: }
089:
090: class SelectComponent extends Panel implements ActionListener,
091: TextListener {
092:
093: protected TextField selected = null;
094: protected SelectPopup popup = null;
095: protected SelectEditor editor = null;
096: EditorFeeder feeder = null;
097: EditorModifier modifier = null;
098:
099: public void textValueChanged(TextEvent e) {
100: setModified();
101: }
102:
103: public void actionPerformed(ActionEvent e) {
104: String command = e.getActionCommand();
105: if (command != null) {
106: if (command.equals("edit")) {
107: if (popup == null)
108: popup = new SelectPopup(this , feeder, modifier);
109: popup.show();
110: }
111: }
112: }
113:
114: public String getText() {
115: return selected.getText();
116: }
117:
118: public void setText(String text) {
119: selected.setText(text);
120: editor.setModified();
121: }
122:
123: SelectComponent(SelectEditor editor, EditorFeeder feeder,
124: EditorModifier modifier, String selected) {
125: super ();
126: this .feeder = feeder;
127: this .modifier = modifier;
128: this .editor = editor;
129: this .selected = new TextField(20);
130: this .selected.setText(selected);
131: this .selected.addTextListener(this );
132: Button editB = new Button("Change");
133: editB.setActionCommand("edit");
134: editB.addActionListener(this );
135: setLayout(new BorderLayout());
136: add(this .selected, "West");
137: add(editB, "Center");
138: }
139:
140: }
141:
142: // The SelectEditor itself
143:
144: /**
145: * Properties - The feeder's class name.
146: */
147: public static final String FEEDER_CLASS_P = "feeder.class";
148: public static final String MODIFIER_CLASS_P = "modifier.class";
149:
150: protected SelectComponent comp = null;
151: protected boolean hasChanged = false;
152: protected String oldvalue = null;
153:
154: protected void createComponent(EditorFeeder feeder,
155: EditorModifier modifier, String selected) {
156: if (comp == null)
157: comp = new SelectComponent(this , feeder, modifier, selected);
158: }
159:
160: protected void setModified() {
161: hasChanged = true;
162: }
163:
164: /**
165: * Tells if the edited value has changed
166: * @return true if the value changed.
167: */
168: public boolean hasChanged() {
169: return hasChanged;
170: }
171:
172: /**
173: * set the current value to be the original value, ie: changed
174: * must return <strong>false</strong> after a reset.
175: */
176: public void clearChanged() {
177: hasChanged = false;
178: }
179:
180: /**
181: * reset the changes (if any)
182: */
183: public void resetChanges() {
184: hasChanged = false;
185: comp.setText(oldvalue);
186: }
187:
188: /**
189: * Get the current value of the edited value
190: * @return an object or <strong>null</strong> if the object was not
191: * initialized
192: */
193: public Object getValue() {
194: try {
195: return comp.getText();
196: } catch (Exception ex) {
197: ex.printStackTrace();
198: }
199: return null;
200: }
201:
202: /**
203: * Set the value of the edited value
204: * @param o the new value.
205: */
206: public void setValue(Object o) {
207: this .oldvalue = (String) o;
208: comp.setText(oldvalue);
209: }
210:
211: /**
212: * get the Component created by the editor.
213: * @return a Component
214: */
215: public Component getComponent() {
216: return comp;
217: }
218:
219: /**
220: * Initialize the editor
221: * @param w the ResourceWrapper father of the attribute
222: * @param a the Attribute we are editing
223: * @param o the value of the above attribute
224: * @param p some Properties, used to fine-tune the editor
225: * @exception RemoteAccessException if a remote access error occurs.
226: */
227: public void initialize(RemoteResourceWrapper w, Attribute a,
228: Object o, Properties p) throws RemoteAccessException {
229: // Get the feeder class fromproperties:
230: EditorFeeder feeder = null;
231: EditorModifier modifier = null;
232: String feederClass = null;
233:
234: feederClass = (String) p.get(FEEDER_CLASS_P);
235: if (feederClass == null)
236: throw new RuntimeException(
237: "SelectEditor mis-configuration: " + FEEDER_CLASS_P
238: + " property undefined.");
239: try {
240: Class c = Class.forName(feederClass);
241: feeder = (EditorFeeder) c.newInstance();
242: feeder.initialize(w, p);
243: } catch (Exception ex) {
244: ex.printStackTrace();
245: throw new RuntimeException("SelectEditor mis-configured: "
246: + " unable to instantiate " + feederClass + ".");
247: }
248:
249: String modifierClass = (String) p.get(MODIFIER_CLASS_P);
250: if (modifierClass != null) {
251: try {
252: Class cm = Class.forName(modifierClass);
253: modifier = (EditorModifier) cm.newInstance();
254: } catch (Exception ex) {
255: ex.printStackTrace();
256: throw new RuntimeException(
257: "SelectEditor mis-configured: "
258: + " unable to instantiate "
259: + modifierClass + ".");
260: }
261: }
262:
263: String selected = (String) o;
264: createComponent(feeder, modifier, selected);
265: if (selected != null)
266: oldvalue = new String(selected);
267: }
268:
269: }
|