01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.x.impl.swing;
14:
15: import java.awt.event.ItemEvent;
16: import java.awt.event.ItemListener;
17:
18: import javax.swing.ComboBoxModel;
19: import javax.swing.DefaultComboBoxModel;
20: import javax.swing.JComboBox;
21:
22: import com.eviware.x.form.XFormOptionsField;
23:
24: public class JComboBoxFormField extends
25: AbstractSwingXFormField<JComboBox> implements ItemListener,
26: XFormOptionsField {
27: public JComboBoxFormField(Object[] values) {
28: super (new JComboBox());
29:
30: setOptions(values);
31:
32: getComponent().addItemListener(this );
33: }
34:
35: public void setValue(String value) {
36: getComponent().setSelectedItem(value);
37: }
38:
39: public String getValue() {
40: Object selectedItem = getComponent().getSelectedItem();
41: return selectedItem == null ? null : selectedItem.toString();
42: }
43:
44: public void itemStateChanged(ItemEvent e) {
45: Object selectedItem = getComponent().getSelectedItem();
46: fireValueChanged(selectedItem == null ? null : selectedItem
47: .toString(), null);
48: }
49:
50: public void addItem(String value) {
51: getComponent().addItem(value);
52: }
53:
54: public void setOptions(Object[] values) {
55: String selectedItem = getValue();
56: DefaultComboBoxModel model = new DefaultComboBoxModel(values);
57:
58: if (values.length > 0 && values[0] == null) {
59: model.removeElementAt(0);
60: getComponent().setEditable(true);
61: } else {
62: getComponent().setEditable(false);
63: }
64:
65: getComponent().setModel(model);
66:
67: if (selectedItem != null)
68: getComponent().setSelectedItem(selectedItem);
69: else if (getComponent().isEditable())
70: getComponent().setSelectedItem("");
71: }
72:
73: public String[] getOptions() {
74: ComboBoxModel model = getComponent().getModel();
75:
76: String[] result = new String[model.getSize()];
77: for (int c = 0; c < result.length; c++)
78: result[c] = model.getElementAt(c).toString();
79:
80: return result;
81: }
82:
83: public String[] getSelectedOptions() {
84: return new String[] { getValue() };
85: }
86:
87: public void setSelectedOptions(String[] options) {
88: // TODO Auto-generated method stub
89:
90: }
91: }
|