View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.awt.event.ActionEvent;
16  import java.awt.event.ActionListener;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import javax.swing.BoxLayout;
21  import javax.swing.ButtonGroup;
22  import javax.swing.ButtonModel;
23  import javax.swing.JPanel;
24  import javax.swing.JRadioButton;
25  
26  import com.eviware.soapui.support.types.StringList;
27  import com.eviware.x.form.XFormOptionsField;
28  import com.eviware.x.impl.swing.AbstractSwingXFormField;
29  
30  /***
31   * Swing-specific RadioGroup
32   * 
33   * @author ole.matzura
34   */
35  
36  public class XFormRadioGroup extends AbstractSwingXFormField<JPanel> implements XFormOptionsField
37  {
38  	private ButtonGroup buttonGroup;
39  	private Map<String,ButtonModel> models = new HashMap<String,ButtonModel>();
40  	private StringList items = new StringList();
41  
42  	public XFormRadioGroup( String [] values )
43  	{
44  		super( new JPanel() );
45  		
46  		buttonGroup = new ButtonGroup();
47  		getComponent().setLayout( new BoxLayout( getComponent(), BoxLayout.Y_AXIS ));
48  		
49  		for( String value : values )
50  		{
51  			addItem( value );
52  		}
53  	}
54  
55  	public String getValue()
56  	{
57  		ButtonModel selection = buttonGroup.getSelection();
58  		return selection == null ? null : selection.getActionCommand();
59  	}
60  
61  	public void setValue( String value )
62  	{
63  		buttonGroup.setSelected( models.get( value ), true ); 
64  	}
65  
66  	public void addItem( String value )
67  	{
68  		JRadioButton button = new JRadioButton( value );
69  		button.setActionCommand( value );
70  		button.addActionListener( new ActionListener() {
71  
72  			public void actionPerformed( ActionEvent e )
73  			{
74  				fireValueChanged( e.getActionCommand(), null );
75  			}} );
76  		
77  		getComponent().add( button );
78  		buttonGroup.add( button );
79  		models.put( value, button.getModel() );
80  		items.add( value );
81  	}
82  
83  	public String[] getOptions()
84  	{
85  		return items.toStringArray();
86  	}
87  
88  	public String[] getSelectedOptions()
89  	{
90  		return new String[] {getValue()};
91  	}
92  
93  	public void setOptions( Object[] values )
94  	{
95  		while( buttonGroup.getButtonCount() > 0 )
96  			buttonGroup.remove( buttonGroup.getElements().nextElement() );
97  		
98  		models.clear();
99  		items.clear();
100 		getComponent().removeAll();
101 		
102 		for( Object value : values )
103 		{
104 			addItem( value.toString() );
105 		}
106 	}
107 
108 	public void setSelectedOptions( String[] options )
109 	{
110 		
111 	}
112 
113 	public int[] getSelectedIndexes()
114 	{
115 		return new int[] { items.indexOf( getValue() )};
116 	}
117 }