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