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