001: // StringArrayEditor.java
002: // $Id: StringArrayEditor.java,v 1.6 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.BorderLayout;
010: import java.awt.GridLayout;
011: import java.awt.Dimension;
012: import java.awt.Image;
013: import java.awt.Insets;
014: import java.awt.event.ActionListener;
015: import java.awt.event.ActionEvent;
016:
017: import javax.swing.BorderFactory;
018: import javax.swing.JLabel;
019: import javax.swing.JFrame;
020: import javax.swing.JScrollPane;
021: import javax.swing.JList;
022: import javax.swing.ListSelectionModel;
023: import javax.swing.JPanel;
024: import javax.swing.JTextField;
025: import javax.swing.JButton;
026:
027: import java.util.Properties;
028: import java.util.Vector;
029:
030: import org.w3c.jigadmin.widgets.ListEditor;
031: import org.w3c.jigadmin.widgets.ClosableDialog;
032: import org.w3c.jigadmin.widgets.Icons;
033:
034: import org.w3c.jigadm.RemoteResourceWrapper;
035: import org.w3c.jigadm.editors.AttributeEditor;
036: import org.w3c.jigadm.editors.EditorFeeder;
037:
038: import org.w3c.jigsaw.admin.RemoteResource;
039: import org.w3c.jigsaw.admin.RemoteAccessException;
040:
041: import org.w3c.tools.resources.Attribute;
042: import org.w3c.tools.widgets.TextEditable;
043:
044: /**
045: * An editor for StringArray attributes.
046: * @author Benoit Mahe <bmahe@sophia.inria.fr>
047: */
048:
049: public class StringArrayEditor extends AttributeEditor {
050:
051: protected JFrame frame = null;
052:
053: class EditStringArrayPopup extends ClosableDialog implements
054: ActionListener {
055:
056: protected StringArrayComponent parent = null;
057: protected EditorFeeder feeder = null;
058: protected Vector selected = null;
059: protected Vector vitems = null;
060: protected JList witems = null;
061: protected JPanel items = null;
062: protected JPanel pitems = null;
063: protected JButton waddItem = null;
064: protected JButton wdelItem = null;
065: protected JList wselected = null;
066: protected JPanel pselected = null;
067: protected TextEditable newItem = null;
068: protected boolean modified = false;
069:
070: /**
071: * ActionListsner implementation - One of our button was fired.
072: * @param evt The ActionEvent.
073: */
074:
075: public void actionPerformed(ActionEvent evt) {
076: String command = evt.getActionCommand();
077: if (command.equals("add")) {
078: if (newItem.updated()) {
079: modified = true;
080: selected.addElement(newItem.getText());
081: }
082: newItem.setDefault();
083: // Add witems selection to wselected:
084: Object sels[] = witems.getSelectedValues();
085: if ((sels != null) && (sels.length > 0)) {
086: // Wait until processing done to remove items...
087: modified = true;
088: for (int i = 0; i < sels.length; i++) {
089: selected.addElement(sels[i]);
090: vitems.removeElement(sels[i]);
091: }
092: }
093: wselected.setListData(selected);
094: witems.setListData(vitems);
095: } else if (command.equals("del")) {
096: Object sels[] = wselected.getSelectedValues();
097: if ((sels != null) && (sels.length > 0)) {
098: // Wait until processing done to remove items...
099: modified = true;
100: for (int i = 0; i < sels.length; i++) {
101: vitems.addElement(sels[i]);
102: selected.removeElement(sels[i]);
103: }
104: wselected.setListData(selected);
105: witems.setListData(vitems);
106: }
107: } else if (command.equals("update")) {
108: if (modified) {
109: String items[] = new String[selected.size()];
110: selected.copyInto(items);
111: parent.setSelectedItems(items);
112: parent.setModified();
113: }
114: close();
115: } else if (command.equals("cancel")) {
116: close();
117: } else if (evt.getSource().equals(newItem)) {
118: if (newItem.updated()) {
119: modified = true;
120: selected.addElement(newItem.getText());
121: }
122: newItem.setDefault();
123: wselected.setListData(selected);
124: ((Component) newItem).requestFocus();
125: }
126: }
127:
128: protected void close() {
129: modified = false;
130: setVisible(false);
131: dispose();
132: }
133:
134: /**
135: * Create the list of possible items, querying the feeder:
136: * @param feeder The one that knows about default items.
137: */
138:
139: protected void createDefaultItems(EditorFeeder feeder) {
140: this .witems = new JList();
141: witems
142: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
143: // Feed that list:
144: String items[] = feeder.getDefaultItems();
145:
146: if (items != null) {
147: vitems = new Vector(items.length);
148: for (int i = 0; i < items.length; i++)
149: vitems.addElement(items[i]);
150: witems.setListData(vitems);
151: } else {
152: vitems = new Vector();
153: }
154: }
155:
156: protected void createSelectedItems(String sel[]) {
157: this .wselected = new JList();
158: wselected
159: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
160:
161: if (sel != null) {
162: selected = new Vector(sel.length);
163: for (int i = 0; i < sel.length; i++)
164: selected.addElement(sel[i]);
165: wselected.setListData(selected);
166: } else {
167: selected = new Vector();
168: }
169: }
170:
171: protected void updateSize() {
172: setSize(parent.editor.getPopupSize());
173: }
174:
175: public EditStringArrayPopup(StringArrayComponent parent,
176: EditorFeeder feeder, String selected[], String title) {
177: super (StringArrayEditor.this .frame, title, false);
178:
179: this .parent = parent;
180: this .feeder = feeder;
181: this .newItem = parent.editor.getTextEditor();
182: createDefaultItems(feeder);
183: createSelectedItems(selected);
184:
185: waddItem = new JButton(Icons.copyLIcon);
186: waddItem.setActionCommand("add");
187: waddItem.addActionListener(this );
188:
189: wdelItem = new JButton(Icons.copyRIcon);
190: wdelItem.setActionCommand("del");
191: wdelItem.addActionListener(this );
192:
193: JPanel arrows = new JPanel(new GridLayout(1, 2, 5, 5));
194: arrows.add(wdelItem);
195: arrows.add(waddItem);
196:
197: JButton Ok = new JButton("Ok");
198: Ok.setActionCommand("update");
199: Ok.addActionListener(this );
200:
201: JButton Cancel = new JButton("Cancel");
202: Cancel.setActionCommand("cancel");
203: Cancel.addActionListener(this );
204:
205: JPanel buttons = new JPanel(new GridLayout(1, 2, 5, 5));
206: buttons.add(Ok);
207: buttons.add(Cancel);
208:
209: JPanel pselected = new JPanel(new BorderLayout(3, 3));
210: pselected.setBorder(BorderFactory
211: .createTitledBorder("Selection"));
212: pselected.add(new JScrollPane(wselected), "Center");
213:
214: JPanel items = new JPanel(new BorderLayout(3, 3));
215: items.setBorder(BorderFactory.createTitledBorder("Choice"));
216: newItem.addActionListener(this );
217: items.add((Component) newItem, "North");
218: items.add(new JScrollPane(witems), "Center");
219:
220: JPanel lists = new JPanel(new GridLayout(1, 2, 5, 5));
221: lists.add(pselected);
222: lists.add(items);
223:
224: JPanel mainp = new JPanel(new BorderLayout(0, 5));
225: mainp.add(arrows, BorderLayout.NORTH);
226: mainp.add(lists, BorderLayout.CENTER);
227: mainp.add(buttons, BorderLayout.SOUTH);
228: mainp.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
229: 10));
230:
231: getContentPane().setLayout(new BorderLayout());
232: getContentPane().add(mainp);
233: updateSize();
234: }
235: }
236:
237: class StringArrayComponent extends ListEditor {
238:
239: protected StringArrayEditor editor = null;
240: protected String selected[] = null;
241: protected EditorFeeder feeder = null;
242:
243: protected void edit() {
244: EditStringArrayPopup popup = new EditStringArrayPopup(this ,
245: feeder, getSelectedItems(), "Edit");
246: popup.setLocationRelativeTo(this );
247: popup.show();
248: popup.toFront();
249:
250: }
251:
252: public void setModified() {
253: editor.setModified();
254: }
255:
256: protected void setSelectedItems(String selected[]) {
257: this .selected = selected;
258: // Remove any prev set items:
259: list.removeAll();
260: // Refill the list:
261: if (selected != null)
262: list.setListData(selected);
263: }
264:
265: protected String[] getSelectedItems() {
266: return selected;
267: }
268:
269: StringArrayComponent(StringArrayEditor editor,
270: String selected[], EditorFeeder feeder) {
271: super (5, true);
272: this .editor = editor;
273: this .feeder = feeder;
274: setSelectedItems(selected);
275: }
276:
277: }
278:
279: class TextEditor extends JTextField implements TextEditable {
280:
281: public boolean updated() {
282: return ((getText().length() > 0) && (!getText().equals("")));
283: }
284:
285: public void setDefault() {
286: setText("");
287: }
288:
289: TextEditor(int nb) {
290: super (nb);
291: setBorder(BorderFactory.createLoweredBevelBorder());
292: }
293:
294: }
295:
296: // The StringArrayEditor itself
297:
298: /**
299: * Properties - The feeder's class name.
300: */
301: public static final String FEEDER_CLASS_P = "feeder.class";
302:
303: protected boolean hasChanged = false;
304: protected String oldvalue[] = null;
305: protected StringArrayComponent comp = null;
306:
307: protected TextEditable getTextEditor() {
308: return new TextEditor(15);
309: }
310:
311: protected Dimension getPopupSize() {
312: return new Dimension(350, 250);
313: }
314:
315: protected void createComponent(EditorFeeder feeder,
316: String selected[]) {
317: if (comp == null)
318: comp = new StringArrayComponent(this , selected, feeder);
319:
320: }
321:
322: protected void setModified() {
323: hasChanged = true;
324: }
325:
326: /**
327: * Tells if the edited value has changed
328: * @return true if the value changed.
329: */
330: public boolean hasChanged() {
331: return hasChanged;
332: }
333:
334: /**
335: * set the current value to be the original value, ie: changed
336: * must return <strong>false</strong> after a reset.
337: */
338: public void clearChanged() {
339: hasChanged = false;
340: }
341:
342: /**
343: * reset the changes (if any)
344: */
345: public void resetChanges() {
346: hasChanged = false;
347: comp.setSelectedItems(oldvalue);
348: }
349:
350: /**
351: * Get the current value of the edited value
352: * @return an object or <strong>null</strong> if the object was not
353: * initialized
354: */
355: public Object getValue() {
356: String[] val = comp.getSelectedItems();
357: if ((val != null) && (val.length > 0)) {
358: return val;
359: }
360: return null;
361: }
362:
363: /**
364: * Set the value of the edited value
365: * @param o the new value.
366: */
367: public void setValue(Object o) {
368: this .oldvalue = (String[]) o;
369: comp.setSelectedItems(oldvalue);
370: }
371:
372: /**
373: * get the Component created by the editor.
374: * @return a Component
375: */
376: public Component getComponent() {
377: return comp;
378: }
379:
380: public static String[] toStringArray(Object array)
381: throws ClassCastException {
382: if (array == null)
383: return null;
384: if (array instanceof String[])
385: return (String[]) array;
386: if (array instanceof Object[]) {
387: Object objects[] = (Object[]) array;
388: String strArray[] = new String[objects.length];
389: for (int i = 0; i < objects.length; i++)
390: strArray[i] = objects[i].toString();
391: return strArray;
392: }
393: throw new ClassCastException("Object array required");
394: }
395:
396: /**
397: * Initialize the editor
398: * @param w the ResourceWrapper father of the attribute
399: * @param a the Attribute we are editing
400: * @param o the value of the above attribute
401: * @param p some Properties, used to fine-tune the editor
402: * @exception RemoteAccessException if a remote access error occurs.
403: */
404: public void initialize(RemoteResourceWrapper w, Attribute a,
405: Object o, Properties p) throws RemoteAccessException {
406: // Get the feeder class fromproperties:
407: EditorFeeder feeder = null;
408: String feederClass = null;
409:
410: feederClass = (String) p.get(FEEDER_CLASS_P);
411: if (feederClass == null)
412: throw new RuntimeException(
413: "StringArrayEditor mis-configuration: "
414: + FEEDER_CLASS_P + " property undefined.");
415: try {
416: Class c = Class.forName(feederClass);
417: feeder = (EditorFeeder) c.newInstance();
418: feeder.initialize(w, p);
419: } catch (Exception ex) {
420: ex.printStackTrace();
421: throw new RuntimeException(
422: "StringArrayEditor mis-configured: "
423: + " unable to instantiate " + feederClass
424: + ".");
425: }
426:
427: this .frame = ((org.w3c.jigadmin.RemoteResourceWrapper) w)
428: .getServerBrowser().getFrame();
429:
430: String s[] = toStringArray(o);
431: createComponent(feeder, s);
432: oldvalue = s;
433: }
434:
435: public StringArrayEditor() {
436: super();
437: }
438:
439: }
|