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.form.support;
14:
15: import java.util.HashMap;
16: import java.util.Map;
17:
18: import javax.swing.BoxLayout;
19: import javax.swing.ButtonGroup;
20: import javax.swing.ButtonModel;
21: import javax.swing.JPanel;
22: import javax.swing.JRadioButton;
23:
24: import com.eviware.soapui.support.types.StringList;
25: import com.eviware.x.form.XFormOptionsField;
26: import com.eviware.x.impl.swing.AbstractSwingXFormField;
27:
28: /**
29: * Swing-specific RadioGroup
30: *
31: * @author ole.matzura
32: */
33:
34: public class XFormRadioGroup extends AbstractSwingXFormField<JPanel>
35: implements XFormOptionsField {
36: private ButtonGroup buttonGroup;
37: private Map<String, ButtonModel> models = new HashMap<String, ButtonModel>();
38: private StringList items = new StringList();
39:
40: public XFormRadioGroup(String[] values) {
41: super (new JPanel());
42:
43: buttonGroup = new ButtonGroup();
44: getComponent().setLayout(
45: new BoxLayout(getComponent(), BoxLayout.Y_AXIS));
46:
47: for (String value : values) {
48: addItem(value);
49: }
50: }
51:
52: public String getValue() {
53: ButtonModel selection = buttonGroup.getSelection();
54: return selection == null ? null : selection.getActionCommand();
55: }
56:
57: public void setValue(String value) {
58: buttonGroup.setSelected(models.get(value), true);
59: }
60:
61: public void addItem(String value) {
62: JRadioButton button = new JRadioButton(value);
63: button.setActionCommand(value);
64: getComponent().add(button);
65: buttonGroup.add(button);
66: models.put(value, button.getModel());
67: items.add(value);
68: }
69:
70: public String[] getOptions() {
71: return items.toStringArray();
72: }
73:
74: public String[] getSelectedOptions() {
75: return new String[] { getValue() };
76: }
77:
78: public void setOptions(Object[] values) {
79: while (buttonGroup.getButtonCount() > 0)
80: buttonGroup.remove(buttonGroup.getElements().nextElement());
81:
82: models.clear();
83: items.clear();
84: getComponent().removeAll();
85:
86: for (Object value : values) {
87: addItem(value.toString());
88: }
89: }
90:
91: public void setSelectedOptions(String[] options) {
92: // TODO Auto-generated method stub
93:
94: }
95: }
|