View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  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> implements XFormOptionsField
35  {
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  	{
42  		super( new JPanel() );
43  		
44  		buttonGroup = new ButtonGroup();
45  		getComponent().setLayout( new BoxLayout( getComponent(), BoxLayout.Y_AXIS ));
46  		
47  		for( String value : values )
48  		{
49  			addItem( value );
50  		}
51  	}
52  
53  	public String getValue()
54  	{
55  		ButtonModel selection = buttonGroup.getSelection();
56  		return selection == null ? null : selection.getActionCommand();
57  	}
58  
59  	public void setValue( String value )
60  	{
61  		buttonGroup.setSelected( models.get( value ), true ); 
62  	}
63  
64  	public void addItem( String value )
65  	{
66  		JRadioButton button = new JRadioButton( value );
67  		button.setActionCommand( value );
68  		getComponent().add( button );
69  		buttonGroup.add( button );
70  		models.put( value, button.getModel() );
71  		items.add( value );
72  	}
73  
74  	public String[] getOptions()
75  	{
76  		return items.toStringArray();
77  	}
78  
79  	public String[] getSelectedOptions()
80  	{
81  		return new String[] {getValue()};
82  	}
83  
84  	public void setOptions( Object[] values )
85  	{
86  		while( buttonGroup.getButtonCount() > 0 )
87  			buttonGroup.remove( buttonGroup.getElements().nextElement() );
88  		
89  		models.clear();
90  		items.clear();
91  		getComponent().removeAll();
92  		
93  		for( Object value : values )
94  		{
95  			addItem( value.toString() );
96  		}
97  	}
98  
99  	public void setSelectedOptions( String[] options )
100 	{
101 		// TODO Auto-generated method stub
102 		
103 	}
104 }