001: // HashtableAttributeEditor.java
002: // $Id: HashtableAttributeEditor.java,v 1.15 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.GridBagConstraints;
013: import java.awt.GridBagLayout;
014: import java.awt.Insets;
015: import java.awt.Label;
016: import java.awt.Panel;
017: import java.awt.TextComponent;
018: import java.awt.TextField;
019: import java.awt.Window;
020:
021: import java.awt.List;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.ItemEvent;
026: import java.awt.event.ItemListener;
027:
028: import java.util.Enumeration;
029: import java.util.EventObject;
030: import java.util.Properties;
031:
032: import org.w3c.tools.resources.Attribute;
033:
034: import org.w3c.jigsaw.admin.RemoteAccessException;
035:
036: import org.w3c.jigadm.RemoteResourceWrapper;
037:
038: import org.w3c.tools.widgets.ClosableFrame;
039: import org.w3c.tools.widgets.ListEditor;
040:
041: import org.w3c.util.ArrayDictionary;
042:
043: /**
044: * HashtableAttributeEditor :
045: * @author Benoit Mahe <bmahe@sophia.inria.fr>
046: */
047:
048: public class HashtableAttributeEditor extends AttributeEditor {
049:
050: class HashtableAttributePopup extends ClosableFrame implements
051: ActionListener, ItemListener {
052:
053: protected HashtableAttributeComponent parent = null;
054: protected ArrayDictionary table = null;
055: protected List keys = null;
056: protected TextField tkey = null;
057: protected TextField tvalue = null;
058: protected boolean modified = false;
059: protected String selectedKey = null;
060:
061: public void itemStateChanged(ItemEvent e) {
062: if (e.getStateChange() == ItemEvent.SELECTED) {
063: Integer idx = (Integer) e.getItem();
064: String key = keys.getItem(idx.intValue());
065: if (key != null) {
066: String value = (String) table.get(key);
067: tkey.setText(key);
068: selectedKey = key;
069: tvalue.setText(value);
070: tkey.requestFocus();
071: }
072: }
073: }
074:
075: public void actionPerformed(ActionEvent evt) {
076: String command = evt.getActionCommand();
077: if (command.equals("add")) {
078: String key = tkey.getText();
079: String value = tvalue.getText();
080: if (key.length() > 0) {
081: table.put(key, value);
082: updateKeys();
083: modified = true;
084: }
085: } else if (command.equals("replace")) {
086: if (selectedKey != null)
087: table.remove(selectedKey);
088: String key = tkey.getText();
089: String value = tvalue.getText();
090: if (key.length() > 0) {
091: table.put(key, value);
092: updateKeys();
093: modified = true;
094: }
095: } else if (command.equals("del")) {
096: int isels[] = keys.getSelectedIndexes();
097: if ((isels != null) && (isels.length > 0)) {
098: modified = true;
099: for (int i = 0; i < isels.length; i++) {
100: String key = keys.getItem(isels[i]);
101: table.remove(key);
102: keys.deselect(isels[i]);
103: }
104: for (int i = 0; i < isels.length; i++)
105: keys.delItem(isels[i] - i);
106: }
107: tkey.setText("");
108: tvalue.setText("");
109: } else if (command.equals("update")) {
110: if (modified) {
111: parent.setTable(table);
112: parent.setModified();
113: modified = false;
114: }
115: tkey.setText("");
116: tvalue.setText("");
117: setVisible(false);
118: } else if (command.equals("cancel")) {
119: close();
120: } else if (evt.getSource() == tkey) {
121: tvalue.requestFocus();
122: } else if (evt.getSource() == tvalue) {
123: String key = tkey.getText();
124: String value = tvalue.getText();
125: if (key.length() > 0) {
126: table.put(key, value);
127: updateKeys();
128: modified = true;
129: }
130: tkey.requestFocus();
131: }
132: }
133:
134: protected void close() {
135: modified = false;
136: tkey.setText("");
137: tvalue.setText("");
138: setVisible(false);
139: }
140:
141: protected void updateKeys() {
142: tkey.setText("");
143: tvalue.setText("");
144: keys.removeAll();
145: Enumeration e = table.keys();
146: while (e.hasMoreElements())
147: keys.addItem((String) e.nextElement());
148: }
149:
150: protected void updateSize() {
151: setSize(350, 230);
152: }
153:
154: public void start(ArrayDictionary table) {
155: this .table = table;
156: updateKeys();
157: selectedKey = null;
158: modified = false;
159: }
160:
161: HashtableAttributePopup(HashtableAttributeComponent parent,
162: ArrayDictionary table, String title) {
163: super (title);
164: this .parent = parent;
165: this .table = table;
166:
167: GridBagLayout layout = new GridBagLayout();
168: GridBagConstraints c = new GridBagConstraints();
169: c.fill = GridBagConstraints.NONE;
170: c.insets = new Insets(5, 5, 5, 5);
171: setLayout(layout);
172:
173: Label lkey = new Label("Key");
174: Label lvalue = new Label("Value");
175:
176: tkey = new TextField(15);
177: tkey.addActionListener(this );
178: tvalue = new TextField(15);
179: tvalue.addActionListener(this );
180:
181: keys = new List(5, true);
182: keys.addItemListener(this );
183:
184: Button addB = new Button("add");
185: addB.setActionCommand("add");
186: addB.addActionListener(this );
187:
188: Button removeB = new Button("Remove");
189: removeB.setActionCommand("del");
190: removeB.addActionListener(this );
191:
192: Button replaceB = new Button("Replace");
193: replaceB.setActionCommand("replace");
194: replaceB.addActionListener(this );
195:
196: Button okB = new Button("Ok");
197: okB.setActionCommand("update");
198: okB.addActionListener(this );
199:
200: Button cancelB = new Button("Cancel");
201: cancelB.setActionCommand("cancel");
202: cancelB.addActionListener(this );
203:
204: c.fill = GridBagConstraints.NONE;
205: c.gridwidth = GridBagConstraints.RELATIVE;
206: c.anchor = GridBagConstraints.CENTER;
207: layout.setConstraints(lkey, c);
208: add(lkey);
209:
210: c.gridwidth = GridBagConstraints.REMAINDER;
211: c.anchor = GridBagConstraints.CENTER;
212: layout.setConstraints(lvalue, c);
213: add(lvalue);
214:
215: c.fill = GridBagConstraints.BOTH;
216: c.gridwidth = GridBagConstraints.RELATIVE;
217: layout.setConstraints(tkey, c);
218: add(tkey);
219:
220: c.gridwidth = GridBagConstraints.REMAINDER;
221: layout.setConstraints(tvalue, c);
222: add(tvalue);
223:
224: c.gridwidth = GridBagConstraints.RELATIVE;
225: // c.gridheight = 2;
226: layout.setConstraints(keys, c);
227: add(keys);
228:
229: c.fill = GridBagConstraints.NONE;
230:
231: Panel control = new Panel(new BorderLayout());
232: control.add(addB, "North");
233: control.add(replaceB, "Center");
234: control.add(removeB, "South");
235: c.gridwidth = GridBagConstraints.REMAINDER;
236: layout.setConstraints(control, c);
237: add(control);
238:
239: c.fill = GridBagConstraints.BOTH;
240:
241: c.gridwidth = GridBagConstraints.RELATIVE;
242: layout.setConstraints(okB, c);
243: add(okB);
244:
245: c.gridwidth = GridBagConstraints.REMAINDER;
246: layout.setConstraints(cancelB, c);
247: add(cancelB);
248:
249: updateSize();
250: updateKeys();
251: }
252:
253: }
254:
255: class HashtableAttributeComponent extends ListEditor {
256:
257: protected HashtableAttributeEditor editor = null;
258: protected HashtableAttributePopup popup = null;
259: protected ArrayDictionary hashtable = null;
260:
261: protected void edit() {
262: if (popup == null)
263: popup = new HashtableAttributePopup(this ,
264: (ArrayDictionary) hashtable.clone(), "Edit");
265: else
266: popup.start((ArrayDictionary) hashtable.clone());
267: popup.show();
268: popup.toFront();
269: }
270:
271: protected void setModified() {
272: editor.setModified();
273: }
274:
275: protected void setTable(ArrayDictionary table) {
276: if (table == null) {
277: hashtable = new ArrayDictionary(5);
278: return;
279: }
280: hashtable = table;
281: Enumeration keys = table.keys();
282: list.removeAll();
283: while (keys.hasMoreElements())
284: list.addItem((String) keys.nextElement());
285: }
286:
287: protected ArrayDictionary getTable() {
288: return hashtable;
289: }
290:
291: HashtableAttributeComponent(HashtableAttributeEditor editor,
292: ArrayDictionary table) {
293: super (3, false);
294: this .editor = editor;
295: setTable(table);
296: }
297:
298: }
299:
300: // The HashtableAttributeEditor itself
301:
302: protected boolean hasChanged = false;
303: protected HashtableAttributeComponent comp = null;
304: protected ArrayDictionary oldValue = null;
305:
306: /**
307: * get the Component created by the editor.
308: * @return a Component
309: */
310: public Component getComponent() {
311: return comp;
312: }
313:
314: protected void createComponent(ArrayDictionary table) {
315: if (comp == null)
316: comp = new HashtableAttributeComponent(this , table);
317: }
318:
319: protected void setModified() {
320: hasChanged = true;
321: }
322:
323: /**
324: * Tells if the edited value has changed
325: * @return true if the value changed.
326: */
327: public boolean hasChanged() {
328: return hasChanged;
329: }
330:
331: /**
332: * set the current value to be the original value, ie: changed
333: * must return <strong>false</strong> after a reset.
334: */
335: public void clearChanged() {
336: hasChanged = false;
337: }
338:
339: /**
340: * reset the changes (if any)
341: */
342: public void resetChanges() {
343: hasChanged = false;
344: comp.setTable(oldValue);
345: }
346:
347: /**
348: * Get the current value of the edited value
349: * @return an object or <strong>null</strong> if the object was not
350: * initialized
351: */
352: public Object getValue() {
353: return comp.getTable();
354: }
355:
356: /**
357: * Set the value of the edited value
358: * @param o the new value.
359: */
360: public void setValue(Object o) {
361: this .oldValue = (ArrayDictionary) o;
362: comp.setTable(oldValue);
363: }
364:
365: /**
366: * Initialize the editor
367: * @param w the ResourceWrapper father of the attribute
368: * @param a the Attribute we are editing
369: * @param o the value of the above attribute
370: * @param p some Properties, used to fine-tune the editor
371: * @exception RemoteAccessException if a remote access error occurs.
372: */
373: public void initialize(RemoteResourceWrapper w, Attribute a,
374: Object o, Properties p) throws RemoteAccessException {
375: createComponent((ArrayDictionary) o);
376: oldValue = (ArrayDictionary) o;
377: }
378:
379: public HashtableAttributeEditor() {
380: super();
381: }
382:
383: }
|