View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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( Object value )
67  	{
68  		JRadioButton button = new JRadioButton( String.valueOf( value ));
69  		button.setActionCommand( String.valueOf( value ) );
70  		button.addActionListener( new ActionListener()
71  		{
72  
73  			public void actionPerformed( ActionEvent e )
74  			{
75  				fireValueChanged( e.getActionCommand(), null );
76  			}
77  		} );
78  
79  		getComponent().add( button );
80  		buttonGroup.add( button );
81  		models.put( String.valueOf( value ), button.getModel() );
82  		items.add( String.valueOf( value ) );
83  	}
84  
85  	public Object[] getOptions()
86  	{
87  		return items.toStringArray();
88  	}
89  
90  	public Object[] getSelectedOptions()
91  	{
92  		return new String[] { getValue() };
93  	}
94  
95  	public void setOptions( Object[] values )
96  	{
97  		while( buttonGroup.getButtonCount() > 0 )
98  			buttonGroup.remove( buttonGroup.getElements().nextElement() );
99  
100 		models.clear();
101 		items.clear();
102 		getComponent().removeAll();
103 
104 		for( Object value : values )
105 		{
106 			addItem( value.toString() );
107 		}
108 	}
109 
110 	public void setSelectedOptions( Object[] options )
111 	{
112 
113 	}
114 
115 	public int[] getSelectedIndexes()
116 	{
117 		return new int[] { items.indexOf( getValue() ) };
118 	}
119 }